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/Makefile | 1 + plugins/webbox/file.cpp | 46 ++++++++++++++++ plugins/webbox/file.h | 15 ++++++ plugins/webbox/webbox.cpp | 134 +++++++++++++++++----------------------------- 4 files changed, 112 insertions(+), 84 deletions(-) create mode 100644 plugins/webbox/file.cpp create mode 100644 plugins/webbox/file.h (limited to 'plugins') diff --git a/plugins/webbox/Makefile b/plugins/webbox/Makefile index ed66b50..e16171d 100644 --- a/plugins/webbox/Makefile +++ b/plugins/webbox/Makefile @@ -57,6 +57,7 @@ LIBS+= \ endif PROGSRC=\ + file.cpp \ webbox.cpp TESTSRC=\ diff --git a/plugins/webbox/file.cpp b/plugins/webbox/file.cpp new file mode 100644 index 0000000..47ab8be --- /dev/null +++ b/plugins/webbox/file.cpp @@ -0,0 +1,46 @@ +#include "file.h" + +#include + +namespace fs = std::filesystem; + +using namespace std::string_literals; + +std::string File::getFile(const fs::path& filename) +{ + std::ifstream file(filename.string(), std::ios::in | std::ios::binary | std::ios::ate); + + if (file.is_open()) { + std::ifstream::pos_type fileSize = file.tellg(); + file.seekg(0, std::ios::beg); + + std::string bytes(fileSize, ' '); + file.read(reinterpret_cast(bytes.data()), fileSize); + + return bytes; + + } else { + throw std::runtime_error("Opening "s + filename.string() + " for reading"); + } +} + +void File::setFile(const fs::path& filename, const std::string& s) +{ + File::setFile(filename, s.data(), s.size()); +} + +void File::setFile(const fs::path& filename, const char* data, size_t size) +{ + std::ofstream file(filename.string(), std::ios::out | std::ios::binary); + if (file.is_open()) { + file.write(data, size); + } else { + throw std::runtime_error("Opening "s + filename.string() + " for writing"); + } +} + +void File::setFile(const fs::path& filename, const std::vector& data) +{ + File::setFile(filename, reinterpret_cast(data.data()), data.size()); +} + diff --git a/plugins/webbox/file.h b/plugins/webbox/file.h new file mode 100644 index 0000000..e7e4cf6 --- /dev/null +++ b/plugins/webbox/file.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +#include +#include + +namespace File { + +std::string getFile(const std::filesystem::path& filename); +void setFile(const std::filesystem::path& filename, const std::string& s); +void setFile(const std::filesystem::path& filename, const char* data, size_t size); +void setFile(const std::filesystem::path& filename, const std::vector& data); + +} 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