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
|
// file : web/apache/request.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#include <web/apache/request>
#include <apr_tables.h> // apr_table_*, apr_array_header_t
#include <apr_strings.h> // apr_pstrdup()
#include <httpd.h> // request_rec, HTTP_*, OK
#include <http_protocol.h> // ap_*()
#include <strings.h> // strcasecmp()
#include <ctime> // strftime(), time_t
#include <chrono>
#include <memory> // unique_ptr
#include <string>
#include <cassert>
#include <sstream>
#include <ostream>
#include <istream>
#include <cstring> // str*(), size_t
#include <utility> // move()
#include <stdexcept> // invalid_argument
#include <exception> // current_exception()
#include <streambuf>
#include <butl/optional>
#include <web/mime-url-encoding>
using namespace std;
namespace web
{
namespace apache
{
void request::
state (request_state s)
{
assert (s != request_state::initial);
if (s == state_)
return; // Noop.
if (s < state_)
{
// Can't "unwind" irrevocable interaction with Apache API.
//
static const char* names[] = {
"initial", "reading", "headers", "writing"};
string str ("web::apache::request::set_state: ");
str += names[static_cast<size_t> (state_)];
str += " to ";
str += names[static_cast<size_t> (s)];
throw sequence_error (move (str));
}
if (s == request_state::reading)
{
// Prepare request content for reading.
//
int r (ap_setup_client_block (rec_, REQUEST_CHUNKED_DECHUNK));
if (r != OK)
throw invalid_request (r);
}
else if (s > request_state::reading && state_ <= request_state::reading)
{
// Read request content if any, discard whatever is received.
//
int r (ap_discard_request_body (rec_));
if (r != OK)
throw invalid_request (r);
}
state_ = s;
}
void request::
rewind ()
{
// @@ Request content buffering, and response cookies buffering are not
// supported yet. When done will be possible to rewind in broader
// range of cases.
//
if (state_ == request_state::initial ||
// Form data have been read. Lucky case, can rewind.
//
(state_ == request_state::reading &&
dynamic_cast<stringbuf*> (in_buf_.get ()) != nullptr))
{
out_.reset ();
out_buf_.reset ();
rec_->status = HTTP_OK;
ap_set_content_type (rec_, nullptr);
if (in_)
in_->seekg (0);
}
else
throw sequence_error ("web::apache::request::rewind");
}
istream& request::
content (bool buffer)
{
assert (!buffer); // Request content buffering is not implemented yet.
if (!in_)
{
unique_ptr<streambuf> in_buf (new istreambuf (rec_, *this));
in_.reset (new istream (in_buf.get ()));
in_buf_ = move (in_buf);
in_->exceptions (istream::failbit | istream::badbit);
// Save form data now otherwise will not be available to do later
// when data already read from stream.
//
form_data ();
}
return *in_;
}
const path& request::
path ()
{
if (path_.empty ())
{
path_ = path_type (rec_->uri);
// Module request handler can not be called if URI is empty.
//
assert (!path_.empty ());
}
return path_;
}
const name_values& request::
parameters ()
{
if (parameters_ == nullptr)
{
parameters_.reset (new name_values ());
try
{
parse_parameters (rec_->args);
parse_parameters (form_data ().c_str ());
}
catch (const invalid_argument& )
{
throw invalid_request ();
}
}
return *parameters_;
}
const name_values& request::
cookies ()
{
if (cookies_ == nullptr)
{
cookies_.reset (new name_values ());
const apr_array_header_t* ha (apr_table_elts (rec_->headers_in));
size_t n (ha->nelts);
for (auto h (reinterpret_cast<const apr_table_entry_t *> (ha->elts));
n--; ++h)
{
if (strcasecmp (h->key, "Cookie") == 0)
{
for (const char* n (h->val); n != nullptr; )
{
const char* v (strchr (n, '='));
const char* e (strchr (n, ';'));
if (e != nullptr && e < v)
v = nullptr;
string name (v != nullptr
? mime_url_decode (n, v, true)
: (e
? mime_url_decode (n, e, true)
: mime_url_decode (n, n + strlen (n), true)));
optional<string> value;
if (v++)
value = e
? mime_url_decode (v, e, true)
: mime_url_decode (v, v + strlen (v), true);
if (!name.empty () || value)
cookies_->emplace_back (move (name), move (value));
n = e ? e + 1 : nullptr;
}
}
}
}
return *cookies_;
}
ostream& request::
content (status_code status, const string& type, bool buffer)
{
if (out_ &&
// Same status code.
//
status == rec_->status &&
// Same buffering flag.
//
buffer ==
(dynamic_cast<stringbuf*> (out_buf_.get ()) != nullptr) &&
// Same content type.
//
strcasecmp (rec_->content_type ? rec_->content_type : "",
type.c_str ()) == 0)
{
// No change, return the existing stream.
//
return *out_;
}
if (state_ >= request_state::writing)
throw sequence_error ("web::apache::request::content");
if (!buffer)
// Request body will be discarded prior first byte of content is
// written. Save form data now to make it available for furture
// parameters () call.
//
form_data ();
unique_ptr<streambuf> out_buf (
buffer
? static_cast<streambuf*> (new stringbuf ())
: static_cast<streambuf*> (new ostreambuf (rec_, *this)));
out_.reset (new ostream (out_buf.get ()));
out_buf_ = move (out_buf);
out_->exceptions (ostream::eofbit | ostream::failbit | ostream::badbit);
rec_->status = status;
ap_set_content_type (
rec_,
type.empty () ? nullptr : apr_pstrdup (rec_->pool, type.c_str ()));
return *out_;
}
void request::
status (status_code status)
{
if (status != rec_->status)
{
// Setting status code in exception handler is a common usecase
// where no sense to throw but still need to signal apache a
// proper status code.
//
if (state_ >= request_state::writing && !current_exception ())
throw sequence_error ("web::apache::request::status");
rec_->status = status;
out_.reset ();
out_buf_.reset ();
ap_set_content_type (rec_, nullptr);
}
}
void request::
cookie (const char* name,
const char* value,
const chrono::seconds* max_age,
const char* path,
const char* domain,
bool secure,
bool buffer)
{
assert (!buffer); // Cookie buffering is not implemented yet.
ostringstream s;
mime_url_encode (name, s);
s << "=";
mime_url_encode (value, s);
if (max_age)
{
chrono::system_clock::time_point tp (
chrono::system_clock::now () + *max_age);
time_t t (chrono::system_clock::to_time_t (tp));
// Assume global locale is not changed and still "C".
//
char b[100];
strftime (b, sizeof (b), "%a, %d-%b-%Y %H:%M:%S GMT", gmtime (&t));
s << "; Expires=" << b;
}
if (path)
s << ";Path=" << path;
if (domain)
s << ";Domain=" << domain;
if (secure)
s << ";Secure";
state (request_state::headers);
apr_table_add (rec_->err_headers_out, "Set-Cookie", s.str ().c_str ());
}
void request::
parse_parameters (const char* args)
{
for (auto n (args); n != nullptr; )
{
const char* v (strchr (n, '='));
const char* e (strchr (n, '&'));
if (e != nullptr && e < v)
v = nullptr;
string name (v != nullptr
? mime_url_decode (n, v) :
(e
? mime_url_decode (n, e)
: mime_url_decode (n, n + strlen (n))));
optional<string> value;
if (v++)
value = e
? mime_url_decode (v, e)
: mime_url_decode (v, v + strlen (v));
if (!name.empty () || value)
parameters_->emplace_back (move (name), move (value));
n = e ? e + 1 : nullptr;
}
}
}
}
|