-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfile_handler.cpp
More file actions
183 lines (155 loc) · 4 KB
/
file_handler.cpp
File metadata and controls
183 lines (155 loc) · 4 KB
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
// Based Off http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp11/http/server/request_handler.cpp
#include "file_handler.hpp"
#include <fstream>
#include <sstream>
#include <boost/filesystem.hpp>
#include "cpp-markdown/markdown.h"
namespace http {
namespace server {
const char* FILE_HANDLER_ROOT_TOKEN = "root";
bool file_handler::url_decode(const std::string& in, std::string& out) {
out.clear();
out.reserve(in.size());
for (std::size_t i = 0; i < in.size(); ++i)
{
if (in[i] == '%')
{
if (i + 3 <= in.size())
{
int value = 0;
std::istringstream is(in.substr(i + 1, 2));
if (is >> std::hex >> value)
{
out += static_cast<char>(value);
i += 2;
}
else
{
return false;
}
}
else
{
return false;
}
}
else if (in[i] == '+')
{
out += ' ';
}
else
{
out += in[i];
}
}
return true;
}
RequestHandler::Status file_handler::HandleRequest(const Request &request, Response* response) {
std::string encoded_request_path = request.uri();
encoded_request_path.erase(encoded_request_path.begin(), encoded_request_path.begin() + uri_prefix_.length());
std::string request_path;
if (!url_decode(encoded_request_path, request_path))
{
*response = Response::stock_response(Response::bad_request);
return RequestHandler::bad_request;
}
if (request_path.empty() || request_path[0] != '/' || request_path.find("..") != std::string::npos)
{
*response = Response::stock_response(Response::bad_request);
return RequestHandler::bad_request;
}
if (request_path[request_path.size() - 1] == '/')
{
request_path += "index.html";
}
// Determine the file extension.
std::size_t last_slash_pos = request_path.find_last_of("/");
std::size_t last_dot_pos = request_path.find_last_of(".");
std::string extension;
if (last_dot_pos != std::string::npos && last_dot_pos > last_slash_pos)
{
extension = request_path.substr(last_dot_pos + 1);
}
std::string full_path = docroot + request_path;
std::ifstream is(full_path.c_str(), std::ios::in | std::ios::binary);
if (!is)
{
*response = Response::stock_response(Response::not_found);
return RequestHandler::not_found;
}
response->SetStatus(Response::ok);
std::string body;
char buf[512];
while (is.read(buf, sizeof(buf)).gcount() > 0)
body.append(buf, is.gcount());
if(extension == "gif")
{
extension = "image/gif";
}
else if (extension == "htm")
{
extension = "text/html";
}
else if (extension == "html")
{
extension = "text/html";
}
else if (extension == "jpg")
{
extension = "image/jpg";
}
else if (extension == "png")
{
extension = "image/png";
}
else if (extension == "md")
{
extension = "text/markdown";
}
else
{
extension = "text/plain";
}
if (extension == "text/markdown")
{
markdown::Document doc;
doc.read(body);
std::ostringstream stream;
doc.write(stream);
std::string markd = stream.str();
response->SetBody(markd);
response->AddHeader("Content-Length", std::to_string(markd.size()));
response->AddHeader("Content-Type", "text/html");
}
else
{
response->SetBody(body);
response->AddHeader("Content-Length", std::to_string(body.size()));
response->AddHeader("Content-Type", extension);
}
return RequestHandler::OK;
}
RequestHandler::Status file_handler::Init(const std::string& uri_prefix, const NginxConfig& config)
{
uri_prefix_ = uri_prefix;
for(const auto& statement : config.statements_)
{
if(statement && statement->tokens_.size() >= 1)
{
if (statement->tokens_[0].compare(FILE_HANDLER_ROOT_TOKEN) == 0)
{
docroot = statement->tokens_[1];
}
}
}
if(!docroot.empty())
{
return RequestHandler::OK;
}
else
{
return RequestHandler::docroot_missing;
}
}
}
}