summaryrefslogtreecommitdiffhomepage
path: root/plugins/webbox/webbox.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-04-12 14:01:40 +0200
committerRoland Reichwein <mail@reichwein.it>2020-04-12 14:01:40 +0200
commit3f778eecc705990598f1033e6245522f42e2fcb5 (patch)
treedfa2af27ef4e6b6a299ecb014a684c272db77992 /plugins/webbox/webbox.cpp
parent77a68fbe16246245937c5d692bb8c89dc14d7800 (diff)
Refactor path concept
Diffstat (limited to 'plugins/webbox/webbox.cpp')
-rw-r--r--plugins/webbox/webbox.cpp134
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:
-// < : &lt;
-// > : &gt;
-// & : &amp;
-// " : &quot;
-// ' : &apos;
-//
-// here:replace &
-QString escapeXML(QString s) {
- s.replace("&", "&amp;");
- 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("&amp;", "&");
- 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";
+}