From 3f778eecc705990598f1033e6245522f42e2fcb5 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 12 Apr 2020 14:01:40 +0200 Subject: Refactor path concept --- plugins/webbox/webbox.cpp | 134 +++++++++++++++++----------------------------- 1 file changed, 50 insertions(+), 84 deletions(-) (limited to 'plugins/webbox/webbox.cpp') 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 + #include +#include +#include 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 status_map { + { "400", "Bad Request"}, + { "403", "Forbidden" }, + { "404", "Not Found" }, + { "505", "Internal Server Error" }, + }; -std::string webbox_plugin::generate_page( - std::function& GetServerParam, - std::function& GetRequestParam, // request including body (POST...) - std::function& 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& SetResponseHeader) { - return "Webbox"; -} + SetResponseHeader("status", status); + SetResponseHeader("content_type", "text/html"); -#if 0 -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#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 "

"s + status + " "s + description + "

"s + message + "

"; } -// 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

%1 %2

%3

\r\n").arg(httpStatusCode).arg(description).arg(message); -} struct CommandParameters { + std::function& GetServerParam; + std::function& GetRequestParam; // request including body (POST...) + std::function& 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& GetServerParam, + std::function& GetRequestParam, // request including body (POST...) + std::function& SetResponseHeader // to be added to result string +) +{ + return "Webbox"; +} -- cgit v1.2.3