• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // request_handler.cpp
3 // ~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #include "request_handler.hpp"
12 #include <fstream>
13 #include <sstream>
14 #include <string>
15 #include "mime_types.hpp"
16 #include "reply.hpp"
17 #include "request.hpp"
18 
19 namespace http {
20 namespace server {
21 
request_handler(const std::string & doc_root)22 request_handler::request_handler(const std::string& doc_root)
23   : doc_root_(doc_root)
24 {
25 }
26 
handle_request(const request & req,reply & rep)27 void request_handler::handle_request(const request& req, reply& rep)
28 {
29   // Decode url to path.
30   std::string request_path;
31   if (!url_decode(req.uri, request_path))
32   {
33     rep = reply::stock_reply(reply::bad_request);
34     return;
35   }
36 
37   // Request path must be absolute and not contain "..".
38   if (request_path.empty() || request_path[0] != '/'
39       || request_path.find("..") != std::string::npos)
40   {
41     rep = reply::stock_reply(reply::bad_request);
42     return;
43   }
44 
45   // If path ends in slash (i.e. is a directory) then add "index.html".
46   if (request_path[request_path.size() - 1] == '/')
47   {
48     request_path += "index.html";
49   }
50 
51   // Determine the file extension.
52   std::size_t last_slash_pos = request_path.find_last_of("/");
53   std::size_t last_dot_pos = request_path.find_last_of(".");
54   std::string extension;
55   if (last_dot_pos != std::string::npos && last_dot_pos > last_slash_pos)
56   {
57     extension = request_path.substr(last_dot_pos + 1);
58   }
59 
60   // Open the file to send back.
61   std::string full_path = doc_root_ + request_path;
62   std::ifstream is(full_path.c_str(), std::ios::in | std::ios::binary);
63   if (!is)
64   {
65     rep = reply::stock_reply(reply::not_found);
66     return;
67   }
68 
69   // Fill out the reply to be sent to the client.
70   rep.status = reply::ok;
71   char buf[512];
72   while (is.read(buf, sizeof(buf)).gcount() > 0)
73     rep.content.append(buf, is.gcount());
74   rep.headers.resize(2);
75   rep.headers[0].name = "Content-Length";
76   rep.headers[0].value = std::to_string(rep.content.size());
77   rep.headers[1].name = "Content-Type";
78   rep.headers[1].value = mime_types::extension_to_type(extension);
79 }
80 
url_decode(const std::string & in,std::string & out)81 bool request_handler::url_decode(const std::string& in, std::string& out)
82 {
83   out.clear();
84   out.reserve(in.size());
85   for (std::size_t i = 0; i < in.size(); ++i)
86   {
87     if (in[i] == '%')
88     {
89       if (i + 3 <= in.size())
90       {
91         int value = 0;
92         std::istringstream is(in.substr(i + 1, 2));
93         if (is >> std::hex >> value)
94         {
95           out += static_cast<char>(value);
96           i += 2;
97         }
98         else
99         {
100           return false;
101         }
102       }
103       else
104       {
105         return false;
106       }
107     }
108     else if (in[i] == '+')
109     {
110       out += ' ';
111     }
112     else
113     {
114       out += in[i];
115     }
116   }
117   return true;
118 }
119 
120 } // namespace server
121 } // namespace http
122