summaryrefslogtreecommitdiffhomepage
path: root/plugins/static-files/static-files.cpp
blob: 3f1c63a6e3993d6d793c9eb001155d4210b9be96 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "static-files.h"

#include "libcommon/mime.h"

#include <boost/algorithm/string/predicate.hpp>

#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

using namespace std::string_literals;
namespace fs = std::filesystem;

namespace {

std::string getFile(const fs::path& filename)
{
 std::ifstream file(filename.string(), std::ios::in | std::ios::binary | std::ios::ate);

 if (file.is_open()) {
  std::ifstream::pos_type fileSize = file.tellg();
  file.seekg(0, std::ios::beg);

  std::string bytes(fileSize, ' ');
  file.read(reinterpret_cast<char*>(bytes.data()), fileSize);

  return bytes;

 } else {
  throw std::runtime_error("Opening "s + filename.string() + " for reading");
 }
}

fs::path extend_index_html(fs::path path)
{
 if (path.string().size() == 0 || path.string().back() == '/')
  return path / "index.html";
 return path;
}

// 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;
}

}

std::string static_files_plugin::name()
{
 return "static-files";
}

static_files_plugin::static_files_plugin()
{
 //std::cout << "Plugin constructor" << std::endl;
}

static_files_plugin::~static_files_plugin()
{
 //std::cout << "Plugin destructor" << std::endl;
}

std::string static_files_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 {
  // Make sure we can handle the method
  std::string method {GetRequestParam("method")};
  if (method != "GET" && method != "HEAD")
   return HttpStatus("400", "Unknown HTTP method", SetResponseHeader);

  // Request path must not contain "..".
  std::string rel_target{GetRequestParam("rel_target")};
  std::string target{GetRequestParam("target")};
  if (rel_target.find("..") != std::string::npos) {
   return HttpStatus("400", "Illegal request: "s + target, SetResponseHeader);
  }

  // Build the path to the requested file
  std::string doc_root{GetRequestParam("doc_root")};
  fs::path path {fs::path{doc_root} / rel_target};
  if (target.size() && target.back() != '/' && fs::is_directory(path)) {
   std::string location{GetRequestParam("location") + "/"s};
   SetResponseHeader("location", location);
   return HttpStatus("301", "Correcting directory path", SetResponseHeader);
  }
  path = {extend_index_html(path)};
  SetResponseHeader("content_type", mime_type(path.string()));

  try {
   return getFile(path);
  } catch (const std::runtime_error& ex) {
   return HttpStatus("404", "Not found: "s + GetRequestParam("target"), SetResponseHeader);
  } 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);
 }
}