
/*---------------------------------------------------------------------------*/
/* set_certificate_handler.cpp                                               */
/* copyright (c) innovaphone 2016                                            */
/*                                                                           */
/*---------------------------------------------------------------------------*/

#include "platform/platform.h"

#include "common/os/iomux.h"

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

#include "common/service/webserver_shared/webserver_internal.h"
#include "common/lib/appservice.h"
#include "common/lib/httplib.h"

#include "apparguments.h"
#include "http_method_handler.h"
#include "http_connection.h"
#include "http_datasource.h"
#include "types.h"
#include "webserver.h"
#include "set_certificate_handler.h"

SetCertificateHandler::SetCertificateHandler(class Webserver * webserver, IWebserverMessage * webserverMessageSend, IInstanceLog * const log) :
log(log),
webserver(webserver)
{
    this->webserverMessageSend = webserverMessageSend;
    this->hostName = nullptr;
    this->password = nullptr;
    this->certBuffer = nullptr;
    this->certSize = 0;
    this->curBufPos = nullptr;
    this->dataToRead = 0;
}


SetCertificateHandler::~SetCertificateHandler()
{
    free(this->hostName);
    free(this->password);
    free(this->certBuffer);
}


void SetCertificateHandler::HandleMessage(dword msgState, byte * dataBuffer, size_t len)
{
    if (msgState == GP_SEND_CERT_HOST_NAME) { // new cert arriving...
        ExtendedHeader extHeader(this->log, 0); // Protocoll-Version nut used in this case...
        extHeader.Decode(0, dataBuffer, len);
        certificate_header_t certHeader = extHeader.CertificateHeader();

        this->hostName = _strdup(certHeader.certHostName);
        this->password = _strdup(certHeader.certPassword);
        this->dataToRead = this->certSize = certHeader.certSize;
        this->curBufPos = this->certBuffer = static_cast<byte *>(malloc(this->certSize));
        this->log->Log(LOG_WEBSERVER, "SetCertificateHandler::HandleMessage new cert arriving, password:%s hostname:%s certSize:%u", this->password, this->hostName, this->certSize);
    }
    else {
        this->dataToRead -= len;
        this->curBufPos += len;
        if (this->dataToRead == 0) {
            // Make something usefull with the cert......
            this->log->Log(LOG_WEBSERVER, "SetCertificateHandler::HandleMessage new cert ready, certLen:%u", this->certSize);
            webserver->SetServerCertificate(this->password, this->certBuffer, this->certSize, this->hostName);
            free(this->hostName);
            free(this->password);
            free(this->certBuffer);
            this->hostName = this->password = nullptr;
            this->certBuffer = this->curBufPos = nullptr;
        }
    }
    this->webserverMessageSend->Send("SetCertificateHandler", CONNECTION_ID_SET_CERT, WSM_SEND_RESULT);
}
