summaryrefslogtreecommitdiffhomepage
path: root/plugins/websocket/websocket.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/websocket/websocket.cpp')
-rw-r--r--plugins/websocket/websocket.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/plugins/websocket/websocket.cpp b/plugins/websocket/websocket.cpp
new file mode 100644
index 0000000..884f691
--- /dev/null
+++ b/plugins/websocket/websocket.cpp
@@ -0,0 +1,80 @@
+#include "websocket.h"
+
+#include <boost/algorithm/string/predicate.hpp>
+#include <boost/array.hpp>
+#include <boost/endian/conversion.hpp>
+#include <boost/coroutine2/coroutine.hpp>
+#include <boost/process.hpp>
+
+#include <algorithm>
+#include <filesystem>
+#include <fstream>
+#include <iostream>
+#include <string>
+#include <unordered_map>
+
+using namespace std::string_literals;
+namespace bp = boost::process;
+namespace fs = std::filesystem;
+
+namespace {
+
+ // 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)
+ {
+ SetResponseHeader("status", status);
+ SetResponseHeader("content_type", "text/html");
+ return status + " " + message;
+ }
+
+} // anonymous namespace
+
+std::string websocket_plugin::name()
+{
+ return "websocket";
+}
+
+websocket_plugin::websocket_plugin()
+{
+ //std::cout << "Plugin constructor" << std::endl;
+}
+
+websocket_plugin::~websocket_plugin()
+{
+ //std::cout << "Plugin destructor" << std::endl;
+}
+
+std::string websocket_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
+)
+{
+ try {
+ // Request path must not contain "..".
+ std::string rel_target{GetRequestParam("rel_target")};
+ size_t query_pos{rel_target.find("?")};
+ if (query_pos != rel_target.npos)
+ rel_target = rel_target.substr(0, query_pos);
+
+ std::string target{GetRequestParam("target")};
+ if (rel_target.find("..") != std::string::npos) {
+ return HttpStatus("400", "Illegal request: "s + target, SetResponseHeader);
+ }
+
+ try {
+ return "<html>Dummy</html>";
+ } catch (const std::exception& ex) {
+ return HttpStatus("500", "Internal Server Error: "s + ex.what(), SetResponseHeader);
+ }
+
+ } catch (const std::exception& ex) {
+ return HttpStatus("500", "Unknown Error: "s + ex.what(), SetResponseHeader);
+ }
+}
+
+bool websocket_plugin::has_own_authentication()
+{
+ return false;
+}
+