
/*---------------------------------------------------------------------------*/
/* webserver_options_handler.h                                               */
/* copyright (c) innovaphone 2016                                            */
/*                                                                           */
/*---------------------------------------------------------------------------*/

// This is the base class used by all webserver message handlers.
class WebserverOptionsHandler : public WebserverWebdavMessageHandler, public IWebserverOptions {
protected:
    UWebserverOptions * user;

    void SendResult() override { user->WebserverOptionsSendResult(this); }

    void CloseComplete() override
    {
        ASSERT(user, "WebserverOptionsHandler::CloseComplete() called, but user is NULL. Forgot do call \"delete webserverOptions\" in WebserverOptionsCloseComplete()?");
        UWebserverOptions * tmp = user;
        user = NULL;
        tmp->WebserverOptionsCloseComplete(this);
    }

public:
    WebserverOptionsHandler(UWebserverMessageHandler * baseUser,
                            byte * extHeaderBuffer,
                            dword connectionID,
                            ulong64 dataSize,
                            char * resourceName,
                            char * resReqPath,
                            class ExtendedHeader & extHeader,
                            IWebserverMessage * const webserverMessageSend,
                            class IInstanceLog * const log, 
                            word protocolVersion);
    virtual ~WebserverOptionsHandler();

    void SetUser(UWebserverOptions * user)
    {
        this->userSet = true;
        this->user = user;
    }

	dword GetConnectionID() override { return WebserverWebdavMessageHandler::GetConnectionID(); }
	const char * GetResourceName() override { return WebserverWebdavMessageHandler::GetResourceName(); }
	const char * GetRegisteredPathForRequest() override { return WebserverWebdavMessageHandler::GetRegisteredPathForRequest(); }
	const char * GetHeaderFieldValue(const char * fieldName) override { return WebserverWebdavMessageHandler::GetHeaderFieldValue(fieldName); }
    virtual const char * GetRemoteAddress() { return WebserverMessageHandler::GetRemoteAddress(); }
    virtual word GetRemotePort() { return WebserverMessageHandler::GetRemotePort(); }
    bool IsEncryptedConnection() override { return this->connFlags.connectionIsTLS; }
    virtual class IDigestAuthenticator * GetAuthenticator() override { return WebserverWebdavMessageHandler::GetAuthenticator(); }

    void SetSupportedRequests(dword requestList) override;
    void SetTransferInfo(wsr_type_t resourceType, ulong64 dataSize, wsr_flags_t flags = WSP_FLAG_NONE, const char* etag = nullptr) { WebserverWebdavMessageHandler::SetTransferInfo(resourceType, dataSize, flags, etag); };
	void SetStatusCode(dword code, const char * description = nullptr) override { WebserverWebdavMessageHandler::SetStatusCode(code, description); }
    void SetHeaderField(const char * fname, const char * fvalue) override { WebserverWebdavMessageHandler::SetHeaderField(fname, fvalue); }
    void Send(const void* buffer, size_t len) override { WebserverWebdavMessageHandler::Send(buffer, len); }
    void Close() override { WebserverWebdavMessageHandler::Close(); }
    void AcceptComplete() override { user->WebserverOptionsRequestAcceptComplete(this); }
};
