
/*---------------------------------------------------------------------------*/
/* webserver_post_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_post_handler.h"


WebserverPostHandler::WebserverPostHandler(UWebserverMessageHandler * baseUser,
                                           byte * extHeaderBuffer,
                                           dword connectionID,
                                           ulong64 dataSize,
                                           char * resourceName,
                                           char * resReqPath,
                                           class ExtendedHeader & extHeader,
                                           IWebserverMessage * const webserverMessageSend,
                                           IInstanceLog * const log,
                                           word protocolVersion)
    : WebserverMessageHandler("IWebserverPost",
                              MSG_HANDLER_POST,
                              baseUser,
                              extHeaderBuffer,
                              connectionID,
                              dataSize,
                              webserverMessageSend,
                              resourceName,
                              resReqPath,
                              log,
                              extHeader,
                              protocolVersion)
{
    this->user = NULL;
}


WebserverPostHandler::~WebserverPostHandler()
{
}


void WebserverPostHandler::ForceDownloadResponse(const char * fileName)
{
    this->log->Log(LOG_WEBSERVER_PLUGIN, "WebserverPostHandler(connection id: %u)::ForceDownloadResponse() fileName: '%s'", this->connectionID, fileName);

    char buffer[2048];
    char format[] = "attachment; filename=\"%s\"";
    if (fileName && (sizeof(format) - 3 + strlen(fileName) < sizeof(buffer))) {
        _sprintf(buffer, format, fileName);
        WebserverMessageHandler::SetHeaderField("Content-Disposition", buffer);
    }
    else WebserverMessageHandler::SetHeaderField("Content-Disposition", "attachment");
}
