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

#include <platform.h>

#include "sdk/common/interface/webserver_plugin.h"

#include "sdk/common/service/webserver_shared/webserver_internal.h"

#include "webserver_message_handler.h"
#include "webserver_webdav_message_handler.h"
#include "webserver_options_handler.h"


WebserverOptionsHandler::WebserverOptionsHandler(UWebserverMessageHandler * baseUser,
                                                 byte * extHeaderBuffer,
                                                 dword connectionID,
                                                 ulong64 dataSize,
                                                 char * resourceName,
                                                 char * resReqPath,
                                                 class ExtendedHeader & extHeader,
                                                 IWebserverMessage * const webserverMessageSend,
                                                 IInstanceLog * const log,
                                                 word protocolVersion)
    : WebserverWebdavMessageHandler("IWebserverOptions",
                                     MSG_HANDLER_OPTIONS,
                                     baseUser,
                                     extHeaderBuffer,
                                     connectionID,
                                     dataSize,
                                     webserverMessageSend,
                                     resourceName,
                                     resReqPath,
                                     extHeader,
                                     log,
                                     protocolVersion)
{
    this->user = nullptr;
}


WebserverOptionsHandler::~WebserverOptionsHandler()
{
}


void WebserverOptionsHandler::SetSupportedRequests(dword requestList)
{
    char buf[1024]; // Will be more than enough...
    char * tmpBuf = buf;

    if (requestList & WS_REQUEST_GET) tmpBuf += _sprintf(tmpBuf, ",GET");
    if (requestList & WS_REQUEST_POST) tmpBuf += _sprintf(tmpBuf, ",POST");
    if (requestList & WS_REQUEST_PUT) tmpBuf += _sprintf(tmpBuf, ",PUT");
    if (requestList & WS_REQUEST_PROPFIND) tmpBuf += _sprintf(tmpBuf, ",PROPFIND");
    if (requestList & WS_REQUEST_MOVE) tmpBuf += _sprintf(tmpBuf, ",MOVE");
    if (requestList & WS_REQUEST_COPY) tmpBuf += _sprintf(tmpBuf, ",COPY");
    if (requestList & WS_REQUEST_MKCOL) tmpBuf += _sprintf(tmpBuf, ",MKCOL");
    if (requestList & WS_REQUEST_DELETE) tmpBuf += _sprintf(tmpBuf, ",DELETE");
    if (requestList & WS_REQUEST_OPTIONS) tmpBuf += _sprintf(tmpBuf, ",OPTIONS");
    if (requestList & WS_REQUEST_LOCK) tmpBuf += _sprintf(tmpBuf, ",LOCK");
    if (requestList & WS_REQUEST_UNLOCK) tmpBuf += _sprintf(tmpBuf, ",UNLOCK");
    if (requestList & WS_REQUEST_PROPPATCH) tmpBuf += _sprintf(tmpBuf, ",PROPPATCH");

    if (tmpBuf == buf) {
        tmpBuf++;
        *tmpBuf = '\0';
    }

    tmpBuf = buf + 1; // Ignore the leading ','

    this->log->Log(LOG_WEBSERVER_PLUGIN, "WebserverOptionsHandler(connection id: %u)::SetSupportedRequests() requestList: %u, supported requests: %s", this->connectionID, requestList, tmpBuf);
    this->SetHeaderField("Allow", tmpBuf);
}
