summaryrefslogtreecommitdiffhomepage
path: root/plugins/redirect/redirect.cpp
blob: 796926df43a1becd2fbac15a66c88561240bb4c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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);
 }
}

bool redirect_plugin::has_own_authentication()
{
 return false;
}