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

class WebserverProppatchHandler : public WebserverWebdavMessageHandler, public IWebserverProppatch {
protected:
    UWebserverProppatch * user;

    void SendResult()  override { user->WebserverProppatchSendResult(this); }
    void * RecvBuffer(size_t size) { return user->WebserverProppatchRecvBuffer(size); }
    void RecvResult(byte * buffer, size_t len) { user->WebserverProppatchRecvResult(this, buffer, len); }
    void RecvCanceled(byte * buffer) { user->WebserverProppatchRecvCanceled(this, buffer); }

    virtual void CloseComplete()
    {
        ASSERT(user, "WebserverProppatchHandler::CloseComplete() called, but user is NULL. Forgot do call \"delete webserverProppatch\" in WebserverProppatchCloseComplete()?");
        UWebserverProppatch * tmp = user;
        user = NULL;
        tmp->WebserverProppatchCloseComplete(this);
    }

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

    void SetUser(UWebserverProppatch * 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(); }
    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(); }

    const char * GetHeaderFieldValue(const char * fieldName) override { return WebserverWebdavMessageHandler::GetHeaderFieldValue(fieldName); }
    void SetResultCode(ws_webdav_result_t result, ulong64 dataSize = 0) override { WebserverWebdavMessageHandler::SetResultCode(result, dataSize); }
    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 Cancel(wsr_cancel_type_t reason) override { WebserverWebdavMessageHandler::Cancel(reason); }
    void Send(const void * buffer, size_t len) override { WebserverWebdavMessageHandler::Send(buffer, len); }
    void Recv(void * buffer = nullptr, size_t len = 0) override { WebserverWebdavMessageHandler::Recv(static_cast<byte *>(buffer), len); }
    void Close() override { WebserverWebdavMessageHandler::Close(); }
    void AcceptComplete() override { user->WebserverProppatchRequestAcceptComplete(this); }
};
