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

class WebserverGetHandler : public WebserverMessageHandler, public IWebserverGet {
protected:
    UWebserverGet * user;
    char * etag;
    size_t rangeCount;
    byte * ranges;
    dword transferInfosGP;
    class HTTPRange * httpRange;
    IWebserverGetRange range;

    void SendResult() override { user->WebserverGetSendResult(this); }
    void * RecvBuffer(size_t size) override { return NULL; } // Unused
    void RecvResult(byte * buffer, size_t len) override { } // Unused

    void CloseComplete() override
    {
        ASSERT(user, "WebserverGetHandler::CloseComplete() called, but user is NULL. Forgot do call \"delete webserverGet\" in WebserverGetCloseComplete()?");
        UWebserverGet * tmp = user;
        user = NULL;
        tmp->WebserverGetCloseComplete(this);
    }

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

    virtual ~WebserverGetHandler();

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

    // 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); }
	class IDigestAuthenticator * GetAuthenticator() override { return WebserverMessageHandler::GetAuthenticator(); }
    bool IsSysClientAuthenticated() override { return connFlags.sysClient; }
    const char * GetETag() override { return etag; }
    size_t GetRangeCount() override { return rangeCount; }
    IWebserverGetRange * GetRange(size_t idx) override;
    virtual const char * GetRemoteAddress() { return WebserverMessageHandler::GetRemoteAddress(); }
    virtual word GetRemotePort() { return WebserverMessageHandler::GetRemotePort(); }
    bool IsEncryptedConnection() override { return this->connFlags.connectionIsTLS; }

    void Cancel(wsr_cancel_type_t reason) override { WebserverMessageHandler::Cancel(reason); }
    void SetTransferInfo(wsr_type_t resourceType, ulong64 dataSize, wsr_flags_t flags = WSP_FLAG_NONE, const char * etag = NULL) { WebserverMessageHandler::SetTransferInfo(resourceType, dataSize, flags, etag); };
    void SetTransferRange(ulong64 start, ulong64 end) override;
	void SetStatusCode(dword code, const char * description = nullptr) override { WebserverMessageHandler::SetStatusCode(code, description); }
    void SetHeaderField(const char * fname, const char * fvalue) override { WebserverMessageHandler::SetHeaderField(fname, fvalue); }
    void ForceDownloadResponse(const char * fileName = NULL) override;

    void Send(const void * buffer, size_t len) override { WebserverMessageHandler::Send(buffer, len); }
    void Close() override { WebserverMessageHandler::Close(); }
    void AcceptComplete() override { user->WebserverGetRequestAcceptComplete(this); }
};
