summaryrefslogtreecommitdiffhomepage
path: root/plugins/redirect/redirect.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-05-03 12:01:13 +0200
committerRoland Reichwein <mail@reichwein.it>2020-05-03 12:01:13 +0200
commit21a066b4b972bd055b424a946ff1f80a939443c3 (patch)
tree177ece2787518f3e68bd073ebd887548f9672d79 /plugins/redirect/redirect.cpp
parent4e699f552db264b9d988873f2189440280e821a7 (diff)
Added redirect plugin, documentation
Diffstat (limited to 'plugins/redirect/redirect.cpp')
-rw-r--r--plugins/redirect/redirect.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/plugins/redirect/redirect.cpp b/plugins/redirect/redirect.cpp
new file mode 100644
index 0000000..91d5f64
--- /dev/null
+++ b/plugins/redirect/redirect.cpp
@@ -0,0 +1,64 @@
+#include "redirect.h"
+
+#include <boost/algorithm/string/predicate.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 redirect_plugin::name()
+{
+ return "redirect";
+}
+
+redirect_plugin::redirect_plugin()
+{
+ //std::cout << "Plugin constructor" << std::endl;
+}
+
+redirect_plugin::~redirect_plugin()
+{
+ //std::cout << "Plugin destructor" << std::endl;
+}
+
+std::string redirect_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 {
+ std::string new_location{GetRequestParam("doc_root")};
+
+ std::string status_code{GetRequestParam("STATUS_CODE")};
+ std::string message{GetRequestParam("MESSAGE")};
+
+ SetResponseHeader("location", new_location);
+ return HttpStatus(status_code, message, SetResponseHeader);
+
+ } catch (const std::exception& ex) {
+ return HttpStatus("500", "Unknown Error: "s + ex.what(), SetResponseHeader);
+ }
+}
+