summaryrefslogtreecommitdiffhomepage
path: root/response.cpp
blob: 92390cd8b874636a5bb6adb57c9b812d36931428 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "response.h"

#include "auth.h"
#include "base64.h"
#include "os.h"

#include "libreichwein/mime.h"
#include "libreichwein/file.h"

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

#include <functional>
#include <iostream>
#include <string>
#include <unordered_map>

using namespace std::placeholders;
using namespace Reichwein::Mime;

namespace {

class RequestContext
{
private:
 request_type& m_req;
 std::string m_host;
 std::string m_target;
 Server& m_server;
 const Path& m_path;

public:
 RequestContext(request_type& req, Server& server)
  : m_req(req)
  , m_host(req["host"])
  , m_target(req.target())
  , m_server(server)
  , m_path(server.GetConfig().GetPath(server.GetSocket(), m_host, m_target))
 {
 }

 // GetTarget() == GetPluginPath() + GetRelativePath()
 
 const Path& GetPath() const {return m_path;}
 
 std::string GetPluginName() const {return m_path.params.at("plugin");} // can throw std::out_of_range
 
 std::string GetPluginPath() const {return m_path.requested;}
 
 std::string GetDocRoot() const {return m_path.params.at("target");} // can throw std::out_of_range
 
 std::string GetRelativePath() const { // can throw std::runtime_error
  if (!boost::starts_with(m_target, m_path.requested))
   throw std::runtime_error("Mismatch of target ("s + m_target + ") and plugin path(" + m_path.requested + ")"s);
  if (m_target.size() > m_path.requested.size() && m_target[m_path.requested.size()] == '/')
   return m_target.substr(m_path.requested.size() + 1);
  else
   return m_target.substr(m_path.requested.size());
 }
 
 std::string GetPluginParam(const std::string& key) const {return m_path.params.at(key);} // can throw std::out_of_range
 
 plugin_type GetPlugin() const {return m_server.GetPlugin(m_path.params.at("plugin"));}; // can throw std::out_of_range

 request_type& GetReq() const {return m_req;}

 std::string GetTarget() const {return m_target;}
 
 std::string GetHost() const {return m_host;}

 Server& GetServer() const {return m_server; }
 
 const Socket& GetSocket() const {return m_server.GetSocket(); }

 // Returns error message, empty on auth success
 std::string isAuthenticated()
 {
  auto& auth{m_path.auth};
  if (auth.size() != 0) {
   std::string authorization{m_req[http::field::authorization]};
   if (authorization.substr(0, 6) != "Basic "s) {
    return "Bad Authorization Type";
   }

   authorization = authorization.substr(6);
   authorization = decode64(authorization);

   size_t pos {authorization.find(':')};
   if (pos == authorization.npos)
    return "Bad Authorization Encoding";

   std::string login{authorization.substr(0, pos)};
   std::string password{authorization.substr(pos + 1)};

   auto it {auth.find(login)};
   // it.second contains crypted/hash
   // password is plain text to validate against the hash
   if (it == auth.end() || !Auth::validate(it->second, password)) {
    return "Bad Authorization";
   }
  }

  return "";
 }

}; // class RequestContext

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

bool is_ipv6_address(const std::string& addr)
{
 return addr.find(":") != addr.npos;
}

std::unordered_map<std::string, std::function<std::string(Server&)>> GetServerParamFunctions{
 // following are the supported fields:
 {"address", [](Server& server) { return server.GetSocket().address; }},
 {"ipv6", [](Server& server) { return is_ipv6_address(server.GetSocket().address) ? "yes" : "no"; }},
 {"port", [](Server& server) { return server.GetSocket().port; }},
 {"statistics", [](Server& server) { return server.GetStatistics().getValues(); }},
 {"uptime_host", [](Server& server) { return OS::uptime_host(); }},
 {"uptime_webserver", [](Server& server) { return OS::uptime_process(); }},
 {"version", [](Server& server) { return Server::VersionString; }},
};

std::string GetServerParam(const std::string& key, Server& server)
{
 auto it = GetServerParamFunctions.find(key);
 if (it != GetServerParamFunctions.end())
  return it->second(server);
 
 throw std::runtime_error("Unsupported server param: "s + key);
}

std::unordered_map<std::string, std::function<std::string(RequestContext&)>> GetRequestParamFunctions{
 // following are the supported fields:
 {"authorization", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::authorization]}; }},

 {"is_authenticated", [](RequestContext& req_ctx) { return req_ctx.isAuthenticated() == ""s ? "1"s : "0"s;}},
 
 {"body", [](RequestContext& req_ctx) { return req_ctx.GetReq().body(); }},

 {"content_length", [](RequestContext& req_ctx) { return std::to_string(req_ctx.GetReq().body().size()); }},

 {"content_type", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::content_type]}; }},

 {"doc_root", [](RequestContext& req_ctx) { return req_ctx.GetDocRoot();}},
 
 {"host", [](RequestContext& req_ctx) { return req_ctx.GetHost();}},

 {"http_accept", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::accept]};}},

 {"http_accept_charset", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::accept_charset]};}},

 {"http_accept_encoding", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::accept_encoding]};}},

 {"http_accept_language", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::accept_language]};}},

 {"http_connection", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::connection]};}},

 {"http_host", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::host]};}},

 {"http_user_agent", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::user_agent]};}},
 
 {"http_version", [](RequestContext& req_ctx) {
   unsigned version {req_ctx.GetReq().version()};
   unsigned major{version / 10};
   unsigned minor{version % 10};
   return "HTTP/"s + std::to_string(major) + "."s + std::to_string(minor);
 }},

 {"https", [](RequestContext& req_ctx) { return req_ctx.GetSocket().protocol == SocketProtocol::HTTPS ? "on" : "off"; }},
 
 {"location", [](RequestContext& req_ctx) { return req_ctx.GetTarget(); }},
 
 {"method", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq().method_string()};}},
 
 {"plugin_path", [](RequestContext& req_ctx) { return std::string{req_ctx.GetPluginPath()};}},
 
 {"rel_target", [](RequestContext& req_ctx) {return req_ctx.GetRelativePath();}},

 {"target", [](RequestContext& req_ctx) {return req_ctx.GetTarget();}}, // target == plugin_path / rel_target
};

std::string GetRequestParam(const std::string& key, RequestContext& req_ctx)
{
 // first, look up functions from GetRequestParamFunctions
 {
  auto it = GetRequestParamFunctions.find(key);
  if (it != GetRequestParamFunctions.end())
   return it->second(req_ctx);
 }

 // second, look up plugin parameters
 {
  try {
   return req_ctx.GetPluginParam(key);
  } catch(const std::out_of_range& ex) {
   // not found
  };
 }

 // third, look up req parameters
 // contains: host
 {
  try {
   return std::string{req_ctx.GetReq()[key]};
  } catch(...) {
   // not found
  }
 }

 // otherwise: error
 throw std::runtime_error("Unsupported request param: "s + key);
}

std::unordered_map<std::string, std::function<void(const std::string&, response_type&)>> SetResponseHeaderActions{
 { "cache_control",       [](const std::string& value, response_type& res){res.set(http::field::cache_control, value);} },
 { "content_disposition", [](const std::string& value, response_type& res){res.set(http::field::content_disposition, value);} },// e.g. attachment; ...
 { "content_type",        [](const std::string& value, response_type& res){res.set(http::field::content_type, value);} },// e.g. text/html
 { "location",            [](const std::string& value, response_type& res){res.set(http::field::location, value);} },// e.g. 301 Moved Permanently: new Location
 { "server",              [](const std::string& value, response_type& res){res.set(http::field::server, value);} }, // Server name/version string
 { "set_cookie",          [](const std::string& value, response_type& res){res.set(http::field::set_cookie, value);} },
 { "status",              [](const std::string& value, response_type& res){
                                                                           try {
                                                                            res.result(unsigned(stoul(value)));
                                                                           } catch (...) {
                                                                            std::cerr << "Error: Bad status value: " << value << std::endl;
                                                                            res.result(400);
                                                                           }
                                                                          } }, // HTTP Status, e.g. "200" (OK)
 { "www_authenticate",    [](const std::string& value, response_type& res){res.set(http::field::www_authenticate, value);} },
};

void SetResponseHeader(const std::string& key, const std::string& value, response_type& res)
{
 // following are the supported fields:
 
 auto it{SetResponseHeaderActions.find(key)};
 if (it == SetResponseHeaderActions.end())
  throw std::runtime_error("Unsupported response field: "s + key);

 it->second(value, res);
}

response_type HttpStatus(std::string status, std::string message, response_type& res)
{
 if (status != "200") { // already handled at res init
  try {
   res.result(unsigned(stoul(status)));
  } catch (...) {
   std::cerr << "Error: HttpStatus: Bad status value: " << status << std::endl;
   res.result(400);
  }
  res.set(http::field::content_type, "text/html");

  res.body() = "<html><body><h1>"s + Server::VersionString + " Error</h1><p>"s + status + " "s + message + "</p></body></html>"s;
  res.prepare_payload();
 }

 return res;
}

// Used to return errors by generating response page and HTTP status code
response_type HttpStatusAndStats(std::string status, std::string message, RequestContext& req_ctx, response_type& res)
{
 HttpStatus(status, message, res);

 req_ctx.GetServer().GetStatistics().count(
   req_ctx.GetReq().body().size(),
   res.body().size(),
   res.result_int() != 200,
   is_ipv6_address(req_ctx.GetServer().GetSocket().address),
   req_ctx.GetServer().GetSocket().protocol == SocketProtocol::HTTPS);

 return std::move(res);
}

response_type handleAuth(RequestContext& req_ctx, response_type& res)
{
 if (req_ctx.GetPlugin()->has_own_authentication() == false) {
  std::string message { req_ctx.isAuthenticated() };
  if (message != "") {
   res.set(http::field::www_authenticate, "Basic realm=\"Reichwein.IT Webserver Login\"");
   return HttpStatusAndStats("401", message, req_ctx, res);
  }
 }

 return std::move(res);
}

} // anonymous namespace

response_type generate_response(request_type& req, Server& server)
{
 response_type res{http::status::ok, req.version()};
 res.set(http::field::server, Server::VersionString);
 res.set(http::field::content_type, mime_type(extend_index_html(std::string(req.target()))));
 res.keep_alive(req.keep_alive());

 try {
  RequestContext req_ctx{req, server}; // can throw std::out_of_range

  res = handleAuth(req_ctx, res);
  if (res.result_int() / 100 == 4) // status 4xx
   return res;

  plugin_type plugin{req_ctx.GetPlugin()};

  auto GetServerParamFunction {std::function<std::string(const std::string& key)>(std::bind(GetServerParam, _1, std::ref(server)))};
  auto GetRequestParamFunction {std::function<std::string(const std::string& key)>(std::bind(GetRequestParam, _1, std::ref(req_ctx)))};
  auto SetResponseHeaderFunction{std::function<void(const std::string& key, const std::string& value)>(std::bind(SetResponseHeader, _1, _2, std::ref(res)))};
  
  std::string res_data { plugin->generate_page(GetServerParamFunction, GetRequestParamFunction, SetResponseHeaderFunction)};
  if (req.method() == http::verb::head) {
   res.content_length(res_data.size());
  } else {
   res.body() = res_data;
   res.prepare_payload();
  }
  
  return HttpStatusAndStats("200", "OK", req_ctx, res);
 } catch(const std::out_of_range& ex) {
  return HttpStatus("400", "Bad request: "s + std::string{req["host"]} + ":"s + std::string{req.target()} + " unknown"s, res);
 } catch(const std::exception& ex) {
  return HttpStatus("400", "Bad request: "s + std::string{ex.what()}, res);
 }

}