diff options
author | Roland Reichwein <mail@reichwein.it> | 2023-01-10 14:22:47 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2023-01-10 14:22:47 +0100 |
commit | d02a29f0ff33279268e675aae0856f3f8cf9d939 (patch) | |
tree | bbb22aeb9c14488ef0871b34f0400259658d46f0 /plugins/websocket/websocket.cpp | |
parent | 1191f07767583a9b19280a4f29cb1b0bd6799785 (diff) |
Configurable Websocket für HTTPS
Diffstat (limited to 'plugins/websocket/websocket.cpp')
-rw-r--r-- | plugins/websocket/websocket.cpp | 80 |
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; +} + |