diff options
Diffstat (limited to 'plugins/redirect/redirect.cpp')
-rw-r--r-- | plugins/redirect/redirect.cpp | 64 |
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); + } +} + |