-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathhttp.cpp
315 lines (253 loc) · 8.69 KB
/
http.cpp
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
#include "cgimap/config.hpp"
#include "cgimap/http.hpp"
#include <vector>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
#include <iterator> // for distance
#include <cctype> // for toupper, isxdigit
#include <cstdlib>
#include <regex>
#include <sstream>
namespace al = boost::algorithm;
using std::string;
using std::vector;
using std::pair;
using std::shared_ptr;
namespace {
/**
* Functions hexToChar and form_urldecode were taken from GNU CGICC by
* Stephen F. Booth and Sebastien Diaz, which is also released under the
* GPL.
*/
char hexToChar(char first, char second) {
int digit;
digit = (first >= 'A' ? ((first & 0xDF) - 'A') + 10 : (first - '0'));
digit *= 16;
digit += (second >= 'A' ? ((second & 0xDF) - 'A') + 10 : (second - '0'));
return static_cast<char>(digit);
}
std::string form_urldecode(const std::string &src) {
std::string result;
std::string::const_iterator iter;
char c;
for (iter = src.begin(); iter != src.end(); ++iter) {
switch (*iter) {
case '+':
result.append(1, ' ');
break;
case '%':
// Don't assume well-formed input
if (std::distance(iter, src.end()) >= 2 && std::isxdigit(*(iter + 1)) &&
std::isxdigit(*(iter + 2))) {
c = *++iter;
result.append(1, hexToChar(c, *++iter));
}
// Just pass the % through untouched
else {
result.append(1, '%');
}
break;
default:
result.append(1, *iter);
break;
}
}
return result;
}
}
namespace http {
exception::exception(int c, const string &h, const string &m)
: code_(c), header_(h), message_(m) {}
exception::~exception() noexcept = default;
int exception::code() const { return code_; }
const string &exception::header() const { return header_; }
const char *exception::what() const noexcept { return message_.c_str(); }
server_error::server_error(const string &message)
: exception(500, "Internal Server Error", message) {}
bad_request::bad_request(const string &message)
: exception(400, "Bad Request", message) {}
forbidden::forbidden(const string &message)
: exception(403, "Forbidden", message) {}
not_found::not_found(const string &uri) : exception(404, "Not Found", uri) {}
not_acceptable::not_acceptable(const string &message)
: exception(406, "Not Acceptable", message) {}
conflict::conflict(const string &message)
: exception(409, "Conflict", message) {}
precondition_failed::precondition_failed(const string &message)
: exception(412, "Precondition Failed", message) {}
payload_too_large::payload_too_large(const string &message)
: exception(413, "Payload Too Large", message) {}
bandwidth_limit_exceeded::bandwidth_limit_exceeded(const string &message)
: exception(509, "Bandwidth Limit Exceeded", message) {}
gone::gone(const string &message)
: exception(410, "Gone", message) {}
unsupported_media_type::unsupported_media_type(const string &message)
: exception(415, "Unsupported Media Type", message) {}
unauthorized::unauthorized(const std::string &message)
: exception(401, "Unauthorized", message) {}
method_not_allowed::method_not_allowed(const http::method method)
: exception(405, "Method not allowed", http::list_methods(method)),
allowed_methods(method) {}
string urldecode(const string &s) { return form_urldecode(s); }
string urlencode(const string &s) {
static const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
std::ostringstream ostr;
for (char c : s) {
if (((c >= 'a') && (c <= 'z')) ||
((c >= 'A') && (c <= 'Z')) ||
((c >= '0') && (c <= '9')) ||
(c == '-') ||
(c == '.') ||
(c == '_') ||
(c == '~')) {
ostr << c;
} else {
auto idx = (unsigned char)(c);
ostr << "%" << hex[idx >> 4] << hex[idx & 0xf];
}
}
std::string rv(ostr.str());
return rv;
}
vector<pair<string, string> > parse_params(const string &p) {
// Split the query string into components
vector<pair<string, string> > queryKVPairs;
if (!p.empty()) {
vector<string> temp;
al::split(temp, p, al::is_any_of("&"));
for (const string &kvPair : temp) {
vector<string> kvTemp;
al::split(kvTemp, kvPair, al::is_any_of("="));
if (kvTemp.size() == 2) {
queryKVPairs.push_back(std::make_pair(kvTemp[0], kvTemp[1]));
} else if (kvTemp.size() == 1) {
queryKVPairs.push_back(std::make_pair(kvTemp[0], std::string()));
}
}
}
return queryKVPairs;
}
shared_ptr<encoding> choose_encoding(const string &accept_encoding) {
vector<string> encodings;
al::split(encodings, accept_encoding, al::is_any_of(","));
float identity_quality = 0.001;
float deflate_quality = 0.000;
float gzip_quality = 0.000;
for (const string &encoding : encodings) {
std::smatch what;
string name;
float quality;
if (std::regex_match(
encoding, what,
std::regex("\\s*([^()<>@,;:\\\\\"/[\\]\\\\?={} "
"\\t]+)\\s*;\\s*q\\s*=(\\d+(\\.\\d+)?)\\s*"))) {
name = what[1];
quality = std::atof(string(what[2]).c_str());
} else if (std::regex_match(
encoding, what,
std::regex(
R"(\s*([^()<>@,;:\\"/[\]\\?={} \t]+)\s*)"))) {
name = what[1];
quality = 1.0;
} else {
name = "";
quality = 0.0;
}
if (al::iequals(name, "identity")) {
identity_quality = quality;
} else if (al::iequals(name, "deflate")) {
deflate_quality = quality;
} else if (al::iequals(name, "gzip")) {
gzip_quality = quality;
} else if (al::iequals(name, "*")) {
if (identity_quality == 0.000)
identity_quality = quality;
if (deflate_quality == 0.000)
deflate_quality = quality;
if (gzip_quality == 0.001)
gzip_quality = quality;
}
}
#ifdef HAVE_LIBZ
#ifdef ENABLE_DEFLATE
if (deflate_quality > 0.0 && deflate_quality >= gzip_quality &&
deflate_quality >= identity_quality) {
return shared_ptr<deflate>(new deflate());
} else
#endif /* ENABLE_DEFLATE */
if (gzip_quality > 0.0 && gzip_quality >= identity_quality) {
return shared_ptr<gzip>(new gzip());
}
#endif /* HAVE_LIBZ */
else if (identity_quality > 0.0) {
return shared_ptr<identity>(new identity());
} else {
throw http::not_acceptable("No acceptable content encoding found. Only "
"identity and gzip are supported.");
}
}
shared_ptr<ZLibBaseDecompressor> get_content_encoding_handler(const std::string &content_encoding) {
if (content_encoding.empty())
return shared_ptr<IdentityDecompressor>(new IdentityDecompressor());
if (content_encoding == "identity")
return shared_ptr<IdentityDecompressor>(new IdentityDecompressor());
#ifdef HAVE_LIBZ
else if (content_encoding == "gzip")
return shared_ptr<GZipDecompressor>(new GZipDecompressor());
else if (content_encoding == "deflate")
return shared_ptr<ZLibDecompressor>(new ZLibDecompressor());
throw http::unsupported_media_type("Supported Content-Encodings include 'gzip' and 'deflate'");
#else
throw http::unsupported_media_type("Supported Content-Encodings are 'identity'");
#endif
}
namespace {
const std::map<method, std::string> METHODS = {
{method::GET, "GET"},
{method::POST, "POST"},
{method::PUT, "PUT"},
{method::HEAD, "HEAD"},
{method::OPTIONS, "OPTIONS"}
};
} // anonymous namespace
std::string list_methods(method m) {
std::ostringstream result;
bool first = true;
for (auto const &pair : METHODS) {
if ((m & pair.first) == pair.first) {
if (first) { first = false; } else { result << ", "; }
result << pair.second;
}
}
return result.str();
}
boost::optional<method> parse_method(const std::string &s) {
boost::optional<method> result;
for (auto const &pair : METHODS) {
if (pair.second == s) {
result = pair.first;
break;
}
}
return result;
}
std::ostream &operator<<(std::ostream &out, method m) {
std::string s = list_methods(m);
out << "methods{" << s << "}";
return out;
}
unsigned long parse_content_length(const std::string &content_length_str) {
char *end;
const long length = strtol(content_length_str.c_str(), &end, 10);
if (end == content_length_str) {
throw http::bad_request("CONTENT_LENGTH not a decimal number");
} else if ('\0' != *end) {
throw http::bad_request("CONTENT_LENGTH: extra characters at end of input");
} else if (length < 0) {
throw http::bad_request("CONTENT_LENGTH: invalid value");
} else if (length > STDIN_MAX)
throw http::payload_too_large((boost::format("CONTENT_LENGTH exceeds limit of %1% bytes") % STDIN_MAX).str());
return length;
}
} // namespace http