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

class WebserverPassthroughHandler : public WebserverMessageHandler, public IWebserverPassthrough {
protected:
    class UWebserverPassthrough * user;

    void SendResult() override { user->WebserverPassthroughSendResult(this); }
    void * RecvBuffer(size_t size) override { return user->WebserverPassthroughRecvBuffer(size); }
    void RecvResult(byte * buffer, size_t len) override { user->WebserverPassthroughRecvResult(this, buffer, len); }
    void RecvCanceled(byte * buffer) override { user->WebserverPassthroughRecvCanceled(this, buffer); }

    void CloseComplete() override
    {
        ASSERT(user, "WebserverPassthroughHandler::CloseComplete() called, but user is NULL. Forgot do call \"delete webserverPassthrough\" in WebserverPassthroughCloseComplete()?");
        UWebserverPassthrough * tmp = user;
        user = NULL;
        tmp->WebserverPassthroughCloseComplete(this);
    }

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

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

	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(); }
    virtual bool IsEncryptedConnection() override { return this->connFlags.connectionIsTLS; }

    virtual void Cancel(wsr_cancel_type_t reason) override { WebserverMessageHandler::Cancel(reason); }
    virtual void Send(const void * buffer, size_t size) override { WebserverMessageHandler::Send(buffer, size); }
    virtual void Recv(void * buffer = NULL, size_t len = 0) override { WebserverMessageHandler::Recv(static_cast<byte *>(buffer), len); }
    virtual void Close() override { WebserverMessageHandler::Close(); }
    virtual void AcceptComplete()  override { user->WebserverPassthroughRequestAcceptComplete(this); }
};
