summaryrefslogtreecommitdiffhomepage
path: root/plugins/webbox
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
parent77a68fbe16246245937c5d692bb8c89dc14d7800 (diff)
Refactor path concept
Diffstat (limited to 'plugins/webbox')
-rw-r--r--plugins/webbox/Makefile1
-rw-r--r--plugins/webbox/file.cpp46
-rw-r--r--plugins/webbox/file.h15
-rw-r--r--plugins/webbox/webbox.cpp134
4 files changed, 112 insertions, 84 deletions
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 <fstream>
+
+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<char*>(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<uint8_t>& data)
+{
+ File::setFile(filename, reinterpret_cast<const char*>(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 <cstdint>
+#include <filesystem>
+#include <string>
+#include <vector>
+
+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<uint8_t>& 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 <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";
+}