From dacda254eee676bcf13a978f539129dda7f943df Mon Sep 17 00:00:00 2001 From: denes Date: Fri, 8 Feb 2019 14:49:37 +0100 Subject: [PATCH] First alpha version --- WebhookMessage.hpp | 112 +++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 61 ++++++++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 WebhookMessage.hpp diff --git a/WebhookMessage.hpp b/WebhookMessage.hpp new file mode 100644 index 0000000..375422b --- /dev/null +++ b/WebhookMessage.hpp @@ -0,0 +1,112 @@ +/* +Copyright 2018 Denes Matetelki + +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 + +#include // put_time +#include +#include + +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 diff --git a/main.cpp b/main.cpp index 9886d1d..85e47a3 100644 --- a/main.cpp +++ b/main.cpp @@ -20,6 +20,20 @@ https://www.gnu.org/licenses/gpl-3.0.html. #include #include +#include + +#include +#include +#include +#include + +#include "WebhookMessage.hpp" + +#include +#include + +#include // sleep +#include int main(int argc, char* argv[]) { @@ -34,6 +48,8 @@ int main(int argc, char* argv[]) ArgParse::Required::REQUIRED, ArgParse::Required::REQUIRED); + /// @todo Add log verbosity as option + try { a.parseArgs(argc, argv); } catch(...) { @@ -44,5 +60,50 @@ int main(int argc, char* argv[]) if (a.foundArg("-h")) std::cout << a.usage() << std::endl; + Logger::createInstance(); + Logger::init(std::cout); + Logger::setLogLevel(Logger::DEBUG); + + std::string script_path; + a.argAsString("-f", script_path); +// std::filesystem fpath(script_path); + struct stat st; + if (stat(script_path.c_str(), &st) < 0) { + LOG_STATIC( Logger::ERR, "Parameter file not found."); + Logger::destroy(); + return EXIT_FAILURE; + } + if ((st.st_mode & S_IEXEC) == 0) { + LOG_STATIC( Logger::ERR, "Parameter file is not executable."); + Logger::destroy(); + return EXIT_FAILURE; + } + + + WebhookMessage msg(reinterpret_cast( + const_cast(script_path.c_str()))); + + const std::string host("127.0.0.1"); + std::string port; + if (a.foundArg("-p")) + a.argAsString("-p", port); + else + port = std::string("5050"); + + TcpConnection conn(host, port, &msg); + SocketServer socketServer(&conn); + + if ( !socketServer.start() ) { + LOG_STATIC( Logger::ERR, "Failed to start TCP server, exiting..."); + Logger::destroy(); + return EXIT_FAILURE; + } + + // never reached + sleep(1); + + socketServer.stop(); + Logger::destroy(); + return EXIT_SUCCESS; }