/
Request.C
423 lines (364 loc) · 9.84 KB
/
Request.C
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* Copyright (C) 2009 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include <sstream>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include "Wt/Http/Request"
#include "Wt/Utils"
#include "Wt/WEnvironment"
#include "Wt/WSslInfo"
#include "WebUtils.h"
#include "WebRequest.h"
#include "Message"
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/tokenizer.hpp>
namespace {
std::stringstream emptyStream;
inline std::string str(const char *s) {
return s ? std::string(s) : std::string();
}
}
namespace Wt {
namespace Http {
UploadedFile::UploadedFile()
{}
UploadedFile::UploadedFile(const std::string& spoolName,
const std::string& clientFileName,
const std::string& contentType)
{
fileInfo_.reset(new Impl());
fileInfo_->spoolFileName = spoolName;
fileInfo_->clientFileName = clientFileName;
fileInfo_->contentType = contentType;
fileInfo_->isStolen = false;
}
void UploadedFile::Impl::cleanup()
{
if (!isStolen)
unlink(spoolFileName.c_str());
}
UploadedFile::Impl::~Impl()
{
cleanup();
}
const std::string& UploadedFile::spoolFileName() const
{
return fileInfo_->spoolFileName;
}
const std::string& UploadedFile::clientFileName() const
{
return fileInfo_->clientFileName;
}
const std::string& UploadedFile::contentType() const
{
return fileInfo_->contentType;
}
void UploadedFile::stealSpoolFile() const
{
fileInfo_->isStolen = true;
}
Request::ByteRange::ByteRange()
: firstByte_(0),
lastByte_(0)
{
}
Request::ByteRange::ByteRange(::uint64_t first, ::uint64_t last)
: firstByte_(first),
lastByte_(last)
{ }
Request::ByteRangeSpecifier::ByteRangeSpecifier()
: satisfiable_(true)
{ }
const std::string *get(const ParameterMap& map, const std::string& name)
{
ParameterMap::const_iterator i = map.find(name);
if (i != map.end())
return &i->second[0];
else
return 0;
}
const ParameterValues& Request::getParameterValues(const std::string& name)
const
{
ParameterMap::const_iterator i = parameters_.find(name);
if (i != parameters_.end())
return i->second;
else
return WebRequest::emptyValues_;
}
const std::string *Request::getParameter(const std::string& name) const
{
const ParameterValues& v = getParameterValues(name);
if (!Utils::isEmpty(v))
return &v[0];
else
return 0;
}
const UploadedFile *Request::getUploadedFile(const std::string& name) const
{
UploadedFileMap::const_iterator i = files_.find(name);
if (i != files_.end())
return &i->second;
else
return 0;
}
std::string Request::method() const
{
return request_ ? str(request_->requestMethod()) : str("GET");
}
std::string Request::serverName() const
{
return request_ ? request_->serverName() : std::string();
}
std::string Request::serverPort() const
{
return request_ ? request_->serverPort() : std::string();
}
std::string Request::path() const
{
return request_ ? request_->scriptName() : std::string();
}
std::string Request::pathInfo() const
{
return request_ ? request_->pathInfo() : std::string();
}
std::string Request::queryString() const
{
return request_ ? request_->queryString() : std::string();
}
std::string Request::urlScheme() const
{
return request_ ? request_->urlScheme() : std::string();
}
std::string Request::headerValue(const std::string& field) const
{
return request_ ? str(request_->headerValue(field.c_str())) : std::string();
}
#ifndef WT_TARGET_JAVA
std::vector<Message::Header> Request::headers() const
{
return request_->headers();
}
#endif
::int64_t Request::tooLarge() const
{
return request_ ? request_->postDataExceeded() : 0;
}
std::istream& Request::in() const
{
if (request_) {
WebRequest *web = const_cast<WebRequest *>(request_);
return web->in();
} else {
return emptyStream;
}
}
std::string Request::contentType() const
{
return request_ ? str(request_->contentType()) : std::string();
}
int Request::contentLength() const
{
return request_ ? request_->contentLength() : 0;
}
std::string Request::userAgent() const
{
return request_ ? str(request_->userAgent()) : std::string();
}
std::string Request::clientAddress() const
{
return request_ ? request_->remoteAddr() : std::string();
}
WSslInfo *Request::sslInfo() const
{
if (sslInfo_)
return sslInfo_;
if (request_)
sslInfo_ = request_->sslInfo();
return sslInfo_;
}
Request::ByteRangeSpecifier Request::getRanges(::int64_t filesize) const
{
return getRanges(headerValue("Range"), filesize);
}
Request::ByteRangeSpecifier Request::getRanges(const std::string &rangeHdr,
::int64_t filesize)
{
Request::ByteRangeSpecifier retval;
if (filesize == 0) {
if (rangeHdr.empty()) {
retval.setSatisfiable(true);
} else {
// Don't waste CPU time and simplify code below.
retval.setSatisfiable(false);
}
return retval;
}
bool syntaxError = false;
bool satisfiable = filesize == -1;
std::vector<std::string> rangeSpecifier;
boost::split(rangeSpecifier, rangeHdr, boost::is_any_of("="));
if (rangeSpecifier.size() == 2) {
boost::trim(rangeSpecifier[0]);
if (boost::iequals(rangeSpecifier[0], "bytes")) {
std::vector<std::string> ranges;
boost::split(ranges, rangeSpecifier[1], boost::is_any_of(","));
for (std::size_t i = 0; i < ranges.size(); ++i) {
std::vector<std::string> range;
boost::split(range, ranges[i], boost::is_any_of("-"));
if (range.size() == 2) {
std::string start = range[0];
std::string end = range[1];
boost::trim(start);
boost::trim(end);
uint64_t startInt=0, endInt=0;
try {
if (start != "")
startInt = boost::lexical_cast<uint64_t>(start);
if (end != "")
endInt = boost::lexical_cast<uint64_t>(end);
} catch (boost::bad_lexical_cast &) {
// syntactically invalid
syntaxError = true;
}
if (start == "") {
// notation -599: return last 599 bytes
if (filesize != -1 && end != "") {
if (endInt >= (uint64_t)filesize) {
endInt = (std::size_t)filesize;
}
if (endInt > 0) {
satisfiable = true;
retval.push_back
(ByteRange
(uint64_t(filesize - endInt), std::size_t(filesize - 1)));
}
else {
// Not really specified as syntax error. The paragraph about
// 'satisfiability' seems to imply that we should simply
// ignore it.
}
} else {
// syntactically invalid
syntaxError = true;
}
} else {
if (filesize == -1 || startInt < (uint64_t)filesize) {
if (end == "") {
satisfiable = true;
// notation 599-: returns from byte 599 to eof
if (filesize == -1)
retval.push_back
(ByteRange(startInt, std::numeric_limits<uint64_t>::max()));
else
retval.push_back
(ByteRange
(startInt, uint64_t(filesize - 1)));
} else {
if (startInt <= endInt) {
satisfiable = true;
if (filesize >= 0 && endInt > (uint64_t)filesize)
endInt = uint64_t(filesize - 1);
retval.push_back(ByteRange(startInt, endInt));
} else {
// syntactically invalid
syntaxError = true;
}
}
} else {
// Not-satisfiable: just skip this range
}
}
} else {
syntaxError = true;
}
}
} else {
// only understand 'bytes'
syntaxError = true;
}
} else {
// Too many equals
syntaxError = true;
}
if (syntaxError) {
return ByteRangeSpecifier();
} else {
retval.setSatisfiable(satisfiable);
return retval;
}
}
Request::Request(const WebRequest& request, ResponseContinuation *continuation)
: request_(&request),
parameters_(request.getParameterMap()),
files_(request.uploadedFiles()),
continuation_(continuation),
sslInfo_(0)
{
if (!continuation) {
const char *cookie = request_->headerValue("Cookie");
if (cookie)
parseCookies(cookie, cookies_);
}
}
Request::Request(const ParameterMap& parameters, const UploadedFileMap& files)
: request_(0),
parameters_(parameters),
files_(files),
continuation_(0),
sslInfo_(0)
{ }
Request::~Request()
{
#ifdef WT_WITH_SSL
delete sslInfo_;
#endif
}
#ifndef WT_TARGET_JAVA
void Request::parseFormUrlEncoded(const std::string& s,
ParameterMap& parameters)
{
for (std::size_t pos = 0; pos < s.length();) {
std::size_t next = s.find_first_of("&=", pos);
if (next == std::string::npos || s[next] == '&') {
if (next == std::string::npos)
next = s.length();
std::string key = s.substr(pos, next - pos);
Utils::inplaceUrlDecode(key);
parameters[key].push_back(std::string());
pos = next + 1;
} else {
std::size_t amp = s.find('&', next + 1);
if (amp == std::string::npos)
amp = s.length();
std::string key = s.substr(pos, next - pos);
Utils::inplaceUrlDecode(key);
std::string value = s.substr(next + 1, amp - (next + 1));
Utils::inplaceUrlDecode(value);
parameters[key].push_back(value);
pos = amp + 1;
}
}
}
#endif // WT_TARGET_JAVA
void Request::parseCookies(const std::string& cookie,
std::map<std::string, std::string>& result)
{
// in WEnvironment for oink
WEnvironment::parseCookies(cookie, result);
}
const std::string *Request::getCookieValue(const std::string& cookieName) const
{
CookieMap::const_iterator i = cookies_.find(cookieName);
if (i == cookies_.end())
return 0;
else
return &i->second;
}
}
}