You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
2.4 KiB
113 lines
2.4 KiB
6 years ago
|
/*
|
||
|
Copyright 2018 Denes Matetelki <denes@matetelki.com>
|
||
|
|
||
|
This file is part of webfish.
|
||
|
|
||
|
webfish is free software: you can redistribute it and/or modify it
|
||
|
under the terms of the GNU General Public License v3 as published by the Free
|
||
|
Software Foundation.
|
||
|
|
||
|
webfish is distributed in the hope that it will be useful, but
|
||
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License v3 for
|
||
|
more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License v3 along
|
||
|
with webfish. If not, see
|
||
|
https://www.gnu.org/licenses/gpl-3.0.html.
|
||
|
*/
|
||
|
|
||
|
#ifndef WEBHOOK_MESSAGE_HPP
|
||
|
#define WEBHOOK_MESSAGE_HPP
|
||
|
|
||
|
#include <cpp_utils/Message.hpp>
|
||
|
|
||
|
#include <iomanip> // put_time
|
||
|
#include <ctime>
|
||
|
#include <sstream>
|
||
|
|
||
|
class WebhookMessage : public Message
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
WebhookMessage( void *msgParam = 0)
|
||
|
: Message(msgParam)
|
||
|
{
|
||
|
TRACE;
|
||
|
}
|
||
|
|
||
|
bool buildMessage( const void *msgPart,
|
||
|
const size_t msgLen )
|
||
|
{
|
||
|
TRACE;
|
||
|
m_buffer = std::string( (const char*) msgPart, msgLen );
|
||
|
|
||
|
/// @todo use it!
|
||
|
// not using getExpectedLength
|
||
|
onMessageReady();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void onMessageReady()
|
||
|
{
|
||
|
TRACE;
|
||
|
|
||
|
// LOG_BEGIN(Logger::INFO)
|
||
|
// LOG_PROP("buffer", m_buffer)
|
||
|
// LOG_PROP("host", m_connection->getHost())
|
||
|
// LOG_PROP("port", m_connection->getPort())
|
||
|
// LOG_END("Got message.");
|
||
|
|
||
|
/// @todo check the source and pass is any and reply with 400 or 404
|
||
|
|
||
|
int status = system((const char*)m_param);
|
||
|
|
||
|
std::string r = generateReply(status == 0);
|
||
|
m_connection->send(r.c_str(), r.length());
|
||
|
m_buffer.clear();
|
||
|
}
|
||
|
|
||
|
std::string generateReply(bool ok = true) const {
|
||
|
const auto t = std::time(nullptr);
|
||
|
const auto tm = *std::localtime(&t);
|
||
|
std::ostringstream oss;
|
||
|
oss << "HTTP/1.1 ";
|
||
|
oss << (ok ? "200 OK" : "500 Internal Server Error") << "\r\n";
|
||
|
|
||
|
// Fri, 08 Feb 2019 12:35:37 GMT
|
||
|
oss << "Date: " << std::put_time(&tm, "%a, %d %b %Y %T %Z") << "\r\n";
|
||
|
|
||
|
oss << "X-Content-Type-Options: nosniff\r\n" \
|
||
|
"Content-Type: text/plain;charset=utf-8\r\n" \
|
||
|
"Content-Length: 10\r\n" \
|
||
|
"Server: webfish(0.1)\r\n" \
|
||
|
"\r\n" \
|
||
|
"Processed\r\n";
|
||
|
|
||
|
return oss.str();
|
||
|
}
|
||
|
|
||
|
Message* clone()
|
||
|
{
|
||
|
TRACE;
|
||
|
return new WebhookMessage(m_param);
|
||
|
}
|
||
|
|
||
|
std::string getBuffer()
|
||
|
{
|
||
|
TRACE;
|
||
|
return m_buffer;
|
||
|
}
|
||
|
|
||
|
protected:
|
||
|
|
||
|
size_t getExpectedLength()
|
||
|
{
|
||
|
TRACE;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
};
|
||
|
|
||
|
#endif // WEBHOOK_MESSAGE_HPP
|