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

// This is the base class used by all webserver message handlers.
class WebserverPostHandler : public WebserverMessageHandler, public IWebserverPost {
protected:
    UWebserverPost * user;

    void SendResult() override { user->WebserverPostSendResult(this); }
    void * RecvBuffer(size_t size)  override { return user->WebserverPostRecvBuffer(size); }
    void RecvResult(byte * buffer, size_t len) override { user->WebserverPostRecvResult(this, buffer, len); }
    void RecvCanceled(byte * buffer) override { user->WebserverPostRecvCanceled(this, buffer); }

    void CloseComplete() override
    {
        ASSERT(user, "WebserverPostHandler::CloseComplete() called, but user is NULL. Forgot do call \"delete webserverPost\" in WebserverPostCloseComplete()?");
        UWebserverPost * tmp = user;
        user = NULL;
        tmp->WebserverPostCloseComplete(this);
    }

public:
    WebserverPostHandler(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 ~WebserverPostHandler();

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

    ulong64 GetDataSize()  override { return WebserverMessageHandler::GetDataSize(); }
    bool DataIsChunkEncoded() override  { return WebserverMessageHandler::DataIsChunkEncoded(); }

    // IWebserverGet
	dword GetConnectionID() override { return WebserverMessageHandler::GetConnectionID(); }
	const char * GetResourceName() override { return WebserverMessageHandler::GetResourceName(); }
	const char * GetRegisteredPathForRequest() override { return WebserverMessageHandler::GetRegisteredPathForRequest(); }
	const char * GetHeaderFieldValue(const char * fieldName) override { return WebserverMessageHandler::GetHeaderFieldValue(fieldName); }
    virtual const char * GetRemoteAddress() { return WebserverMessageHandler::GetRemoteAddress(); }
    virtual word GetRemotePort() { return WebserverMessageHandler::GetRemotePort(); }
    class IDigestAuthenticator * GetAuthenticator() override { return WebserverMessageHandler::GetAuthenticator(); }
    bool IsSysClientAuthenticated() override { return connFlags.sysClient; }

    void SetTransferInfo(wsr_type_t resourceType, ulong64 dataSize, wsr_flags_t flags = WSP_FLAG_NONE, const char * etag = nullptr) { WebserverMessageHandler::SetTransferInfo(resourceType, dataSize, flags, etag); };
    void ForceDownloadResponse(const char * fileName = nullptr) override;
    void SetHeaderField(const char * fname, const char * fvalue) override { WebserverMessageHandler::SetHeaderField(fname, fvalue); }
	void SetStatusCode(dword code, const char * description = nullptr) override { WebserverMessageHandler::SetStatusCode(code, description); }
    void Cancel(wsr_cancel_type_t reason) override { WebserverMessageHandler::Cancel(reason); }
    void Send(const void * buffer, size_t len) override { WebserverMessageHandler::Send(buffer, len); }
    void Recv(void * buffer = nullptr, size_t len = 0) override { WebserverMessageHandler::Recv(static_cast<byte *>(buffer), len); }
    void Close() override { WebserverMessageHandler::Close(); }
    void AcceptComplete() override { user->WebserverPostRequestAcceptComplete(this); }
};
