First alpha version

master
denes 6 years ago
parent 5c5e49c03e
commit dacda254ee

@ -0,0 +1,112 @@
/*
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

@ -20,6 +20,20 @@ https://www.gnu.org/licenses/gpl-3.0.html.
#include <iostream> #include <iostream>
#include <cpp_utils/ArgParse.hpp> #include <cpp_utils/ArgParse.hpp>
#include <cpp_utils/TcpConnection.hpp>
#include <cpp_utils/Logger.hpp>
#include <cpp_utils/Common.hpp>
#include <cpp_utils/TcpConnection.hpp>
#include <cpp_utils/SocketServer.hpp>
#include "WebhookMessage.hpp"
#include <iostream>
#include <string>
#include <unistd.h> // sleep
#include <sys/stat.h>
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
@ -34,6 +48,8 @@ int main(int argc, char* argv[])
ArgParse::Required::REQUIRED, ArgParse::Required::REQUIRED,
ArgParse::Required::REQUIRED); ArgParse::Required::REQUIRED);
/// @todo Add log verbosity as option
try { try {
a.parseArgs(argc, argv); a.parseArgs(argc, argv);
} catch(...) { } catch(...) {
@ -44,5 +60,50 @@ int main(int argc, char* argv[])
if (a.foundArg("-h")) if (a.foundArg("-h"))
std::cout << a.usage() << std::endl; 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<void*>(
const_cast<char*>(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; return EXIT_SUCCESS;
} }

Loading…
Cancel
Save