5 // Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #include "file_handler.hpp"
15 #include <boost/lexical_cast.hpp>
16 #include "mime_types.hpp"
18 #include "request.hpp"
23 file_handler::file_handler(const std::string& doc_root)
28 void file_handler::operator()(const request& req, reply& rep)
30 // Decode url to path.
31 std::string request_path;
32 if (!url_decode(req.uri, request_path))
34 rep = reply::stock_reply(reply::bad_request);
38 // Request path must be absolute and not contain "..".
39 if (request_path.empty() || request_path[0] != '/'
40 || request_path.find("..") != std::string::npos)
42 rep = reply::stock_reply(reply::bad_request);
46 // If path ends in slash (i.e. is a directory) then add "index.html".
47 if (request_path[request_path.size() - 1] == '/')
49 request_path += "index.html";
52 // Determine the file extension.
53 std::size_t last_slash_pos = request_path.find_last_of("/");
54 std::size_t last_dot_pos = request_path.find_last_of(".");
55 std::string extension;
56 if (last_dot_pos != std::string::npos && last_dot_pos > last_slash_pos)
58 extension = request_path.substr(last_dot_pos + 1);
61 // Open the file to send back.
62 std::string full_path = doc_root_ + request_path;
63 std::ifstream is(full_path.c_str(), std::ios::in | std::ios::binary);
66 rep = reply::stock_reply(reply::not_found);
70 // Fill out the reply to be sent to the client.
71 rep.status = reply::ok;
73 while (is.read(buf, sizeof(buf)).gcount() > 0)
74 rep.content.append(buf, is.gcount());
75 rep.headers.resize(2);
76 rep.headers[0].name = "Content-Length";
77 rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size());
78 rep.headers[1].name = "Content-Type";
79 rep.headers[1].value = mime_types::extension_to_type(extension);
82 bool file_handler::url_decode(const std::string& in, std::string& out)
85 out.reserve(in.size());
86 for (std::size_t i = 0; i < in.size(); ++i)
90 if (i + 3 <= in.size())
93 std::istringstream is(in.substr(i + 1, 2));
94 if (is >> std::hex >> value)
96 out += static_cast<char>(value);
109 else if (in[i] == '+')
121 } // namespace server4