diff options
Diffstat (limited to 'plugins/webbox/webbox.cpp')
-rw-r--r-- | plugins/webbox/webbox.cpp | 134 |
1 files changed, 50 insertions, 84 deletions
diff --git a/plugins/webbox/webbox.cpp b/plugins/webbox/webbox.cpp index 6166895..5d3f64c 100644 --- a/plugins/webbox/webbox.cpp +++ b/plugins/webbox/webbox.cpp @@ -1,101 +1,42 @@ #include "webbox.h" +#include <boost/algorithm/string/replace.hpp> + #include <iostream> +#include <string> +#include <unordered_map> using namespace std::string_literals; -std::string webbox_plugin::name() -{ - return "webbox"; -} +namespace { -webbox_plugin::webbox_plugin() -{ - //std::cout << "Plugin constructor" << std::endl; -} -webbox_plugin::~webbox_plugin() -{ - //std::cout << "Plugin destructor" << std::endl; -} + unordered_map<std::string> status_map { + { "400", "Bad Request"}, + { "403", "Forbidden" }, + { "404", "Not Found" }, + { "505", "Internal Server Error" }, + }; -std::string webbox_plugin::generate_page( - std::function<std::string(const std::string& key)>& GetServerParam, - std::function<std::string(const std::string& key)>& GetRequestParam, // request including body (POST...) - std::function<void(const std::string& key, const std::string& value)>& SetResponseHeader // to be added to result string -) +// Used to return errors by generating response page and HTTP status code +std::string HttpStatus(std::string status, std::string message, std::function<plugin_interface_setter_type>& SetResponseHeader) { - return "Webbox"; -} + SetResponseHeader("status", status); + SetResponseHeader("content_type", "text/html"); -#if 0 -#include <fcgiapp.h> - -#include <QString> -#include <QStringList> -#include <QHash> -#include <QDir> -#include <QFileInfo> -#include <QXmlStreamReader> -#include <QXmlStreamWriter> -#include <QDateTime> -#include <QProcess> -#include <QTemporaryFile> -#include <QUrlQuery> -#include <QPair> - -#define BUFSIZE 1000000 - -// XML special characters: -// < : < -// > : > -// & : & -// " : " -// ' : ' -// -// here:replace & -QString escapeXML(QString s) { - s.replace("&", "&"); - return s; -} + auto it{status_map.find(status)}; + std::string description{"(Unknown)"}; + if (it != status_map.end()) + description = it->second; -// revert escapeXML(); -QString unescapeXML(QString s) { - s.replace("&", "&"); - return s; + return "<html><body><h1>"s + status + " "s + description + "</h1><p>"s + message + "</p></body></html>"; } -// supported httpStatusCode: -// 400 Bad Request -// 403 Forbidden -// 404 Not Found -// 500 Internal Server Error -// message: additional message -QString httpError(int httpStatusCode, QString message) { - QString description; - - switch(httpStatusCode) { - case 400: - description = "Bad Request"; - break; - case 403: - description = "Forbidden"; - break; - case 404: - description = "Not Found"; - break; - case 500: - description = "Internal Server Error"; - break; - default: - message = QString("Bad error code: %1, message: %2").arg(httpStatusCode).arg(message); - httpStatusCode = 500; - description = "Internal Server Error"; - } - return QString("Status: %1 %2\r\nContent-Type: text/html\r\n\r\n<html><body><h1>%1 %2</h1><p>%3</p></body></html>\r\n").arg(httpStatusCode).arg(description).arg(message); -} struct CommandParameters { + std::function<std::string(const std::string& key)>& GetServerParam; + std::function<std::string(const std::string& key)>& GetRequestParam; // request including body (POST...) + std::function<void(const std::string& key, const std::string& value)>& SetResponseHeader; // to be added to result string FCGX_Request request; // the request QUrlQuery urlQuery; // derived from request @@ -721,7 +662,7 @@ class DownloadCommand: public GetCommand { FCGX_PutS("Content-Type: application/octet-stream\r\n\r\n", p.request.out); while (!file.atEnd()) { - QByteArray ba = file.read(BUFSIZE); + QByteArray ba = File::getFile(); FCGX_PutStr(ba.data(), ba.size(), p.request.out); } } else { @@ -834,4 +775,29 @@ int main(int argc, char* argv[]) { return 0; } -#endif + +} // anonymous namespace + +std::string webbox_plugin::name() +{ + return "webbox"; +} + +webbox_plugin::webbox_plugin() +{ + //std::cout << "Plugin constructor" << std::endl; +} + +webbox_plugin::~webbox_plugin() +{ + //std::cout << "Plugin destructor" << std::endl; +} + +std::string webbox_plugin::generate_page( + std::function<std::string(const std::string& key)>& GetServerParam, + std::function<std::string(const std::string& key)>& GetRequestParam, // request including body (POST...) + std::function<void(const std::string& key, const std::string& value)>& SetResponseHeader // to be added to result string +) +{ + return "Webbox"; +} |