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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
// Copyright (C) 2017-2018 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <http/response_parser.h>
#include <boost/bind.hpp>
using namespace isc::util;
namespace isc {
namespace http {
const int HttpResponseParser::RECEIVE_START_ST;
const int HttpResponseParser::HTTP_VERSION_H_ST;
const int HttpResponseParser::HTTP_VERSION_T1_ST;
const int HttpResponseParser::HTTP_VERSION_T2_ST;
const int HttpResponseParser::HTTP_VERSION_P_ST;
const int HttpResponseParser::HTTP_VERSION_SLASH_ST;
const int HttpResponseParser::HTTP_VERSION_MAJOR_START_ST;
const int HttpResponseParser::HTTP_VERSION_MAJOR_ST;
const int HttpResponseParser::HTTP_VERSION_MINOR_START_ST;
const int HttpResponseParser::HTTP_VERSION_MINOR_ST;
const int HttpResponseParser::HTTP_STATUS_CODE_START_ST;
const int HttpResponseParser::HTTP_STATUS_CODE_ST;
const int HttpResponseParser::HTTP_PHRASE_START_ST;
const int HttpResponseParser::HTTP_PHRASE_ST;
const int HttpResponseParser::EXPECTING_NEW_LINE1_ST;
const int HttpResponseParser::HEADER_LINE_START_ST;
const int HttpResponseParser::HEADER_LWS_ST;
const int HttpResponseParser::HEADER_NAME_ST;
const int HttpResponseParser::SPACE_BEFORE_HEADER_VALUE_ST;
const int HttpResponseParser::HEADER_VALUE_ST;
const int HttpResponseParser::EXPECTING_NEW_LINE2_ST;
const int HttpResponseParser::EXPECTING_NEW_LINE3_ST;
const int HttpResponseParser::HTTP_BODY_ST;
HttpResponseParser::HttpResponseParser(HttpResponse& response)
: HttpMessageParserBase(response), response_(response),
context_(response.context()) {
}
void
HttpResponseParser::initModel() {
// Initialize dictionaries of events and states.
initDictionaries();
// Set the current state to starting state and enter the run loop.
setState(RECEIVE_START_ST);
// Parsing starts from here.
postNextEvent(START_EVT);
}
void
HttpResponseParser::defineStates() {
// Call parent class implementation first.
HttpMessageParserBase::defineStates();
// Define HTTP parser specific states.
defineState(RECEIVE_START_ST, "RECEIVE_START_ST",
boost::bind(&HttpResponseParser::receiveStartHandler, this));
defineState(HTTP_VERSION_T1_ST, "HTTP_VERSION_T1_ST",
boost::bind(&HttpResponseParser::versionHTTPHandler, this, 'T',
HTTP_VERSION_T2_ST));
defineState(HTTP_VERSION_T2_ST, "HTTP_VERSION_T2_ST",
boost::bind(&HttpResponseParser::versionHTTPHandler, this, 'T',
HTTP_VERSION_P_ST));
defineState(HTTP_VERSION_P_ST, "HTTP_VERSION_P_ST",
boost::bind(&HttpResponseParser::versionHTTPHandler, this, 'P',
HTTP_VERSION_SLASH_ST));
defineState(HTTP_VERSION_SLASH_ST, "HTTP_VERSION_SLASH_ST",
boost::bind(&HttpResponseParser::versionHTTPHandler, this, '/',
HTTP_VERSION_MAJOR_ST));
defineState(HTTP_VERSION_MAJOR_START_ST, "HTTP_VERSION_MAJOR_START_ST",
boost::bind(&HttpResponseParser::numberStartHandler, this,
HTTP_VERSION_MAJOR_ST,
"HTTP version",
&context_->http_version_major_));
defineState(HTTP_VERSION_MAJOR_ST, "HTTP_VERSION_MAJOR_ST",
boost::bind(&HttpResponseParser::numberHandler, this,
'.', HTTP_VERSION_MINOR_START_ST,
"HTTP version",
&context_->http_version_major_));
defineState(HTTP_VERSION_MINOR_START_ST, "HTTP_VERSION_MINOR_START_ST",
boost::bind(&HttpResponseParser::numberStartHandler, this,
HTTP_VERSION_MINOR_ST,
"HTTP version",
&context_->http_version_minor_));
defineState(HTTP_VERSION_MINOR_ST, "HTTP_VERSION_MINOR_ST",
boost::bind(&HttpResponseParser::numberHandler, this,
' ', HTTP_STATUS_CODE_START_ST,
"HTTP version",
&context_->http_version_minor_));
defineState(HTTP_STATUS_CODE_START_ST, "HTTP_STATUS_CODE_START_ST",
boost::bind(&HttpResponseParser::numberStartHandler, this,
HTTP_STATUS_CODE_ST,
"HTTP status code",
&context_->status_code_));
defineState(HTTP_STATUS_CODE_ST, "HTTP_STATUS_CODE_ST",
boost::bind(&HttpResponseParser::numberHandler, this,
' ', HTTP_PHRASE_START_ST,
"HTTP status code",
&context_->status_code_));
defineState(HTTP_PHRASE_START_ST, "HTTP_PHRASE_START_ST",
boost::bind(&HttpResponseParser::phraseStartHandler, this));
defineState(HTTP_PHRASE_ST, "HTTP_PHRASE_ST",
boost::bind(&HttpResponseParser::phraseHandler, this));
defineState(EXPECTING_NEW_LINE1_ST, "EXPECTING_NEW_LINE1_ST",
boost::bind(&HttpResponseParser::expectingNewLineHandler, this,
HEADER_LINE_START_ST));
defineState(HEADER_LINE_START_ST, "HEADER_LINE_START_ST",
boost::bind(&HttpResponseParser::headerLineStartHandler, this));
defineState(HEADER_LWS_ST, "HEADER_LWS_ST",
boost::bind(&HttpResponseParser::headerLwsHandler, this));
defineState(HEADER_NAME_ST, "HEADER_NAME_ST",
boost::bind(&HttpResponseParser::headerNameHandler, this));
defineState(SPACE_BEFORE_HEADER_VALUE_ST, "SPACE_BEFORE_HEADER_VALUE_ST",
boost::bind(&HttpResponseParser::spaceBeforeHeaderValueHandler, this));
defineState(HEADER_VALUE_ST, "HEADER_VALUE_ST",
boost::bind(&HttpResponseParser::headerValueHandler, this));
defineState(EXPECTING_NEW_LINE2_ST, "EXPECTING_NEW_LINE2",
boost::bind(&HttpResponseParser::expectingNewLineHandler, this,
HEADER_LINE_START_ST));
defineState(EXPECTING_NEW_LINE3_ST, "EXPECTING_NEW_LINE3_ST",
boost::bind(&HttpResponseParser::expectingNewLineHandler, this,
HTTP_PARSE_OK_ST));
defineState(HTTP_BODY_ST, "HTTP_BODY_ST",
boost::bind(&HttpResponseParser::bodyHandler, this));
}
void
HttpResponseParser::receiveStartHandler() {
char c = getNextFromBuffer();
if (getNextEvent() != NEED_MORE_DATA_EVT) {
switch(getNextEvent()) {
case START_EVT:
if (c == 'H') {
transition(HTTP_VERSION_T1_ST, DATA_READ_OK_EVT);
} else {
parseFailure("unexpected first character " + std::string(1, c) +
": expected \'H\'");
}
break;
default:
invalidEventError("receiveStartHandler", getNextEvent());
}
}
}
void
HttpResponseParser::versionHTTPHandler(const char expected_letter,
const unsigned int next_state) {
stateWithReadHandler("versionHTTPHandler",
[this, expected_letter, next_state](const char c) {
// We're handling one of the letters: 'H', 'T' or 'P'.
if (c == expected_letter) {
// The HTTP version is specified as "HTTP/X.Y". If the current
// character is a slash we're starting to parse major HTTP version
// number. Let's reset the version numbers.
if (c == '/') {
context_->http_version_major_ = 0;
context_->http_version_minor_ = 0;
}
// In all cases, let's transition to next specified state.
transition(next_state, DATA_READ_OK_EVT);
} else {
// Unexpected character found. Parsing fails.
parseFailure("unexpected character " + std::string(1, c) +
" in HTTP version string");
}
});
}
void
HttpResponseParser::numberStartHandler(const unsigned int next_state,
const std::string& number_name,
unsigned int* storage) {
stateWithReadHandler("numberStartHandler",
[this, next_state, number_name, storage](const char c) mutable {
// HTTP version number must be a digit.
if (isdigit(c)) {
// Update the version number using new digit being parsed.
*storage = *storage * 10 + c - '0';
transition(next_state, DATA_READ_OK_EVT);
} else {
parseFailure("expected digit in " + number_name + ", found " +
std::string(1, c));
}
});
}
void
HttpResponseParser::numberHandler(const char following_character,
const unsigned int next_state,
const std::string& number_name,
unsigned int* const storage) {
stateWithReadHandler("numberHandler",
[this, following_character, number_name, next_state, storage](const char c)
mutable {
// We're getting to the end of the version number, let's transition
// to next state.
if (c == following_character) {
transition(next_state, DATA_READ_OK_EVT);
} else if (isdigit(c)) {
// Current character is a digit, so update the version number.
*storage = *storage * 10 + c - '0';
} else {
parseFailure("expected digit in " + number_name + ", found " +
std::string(1, c));
}
});
}
void
HttpResponseParser::phraseStartHandler() {
stateWithReadHandler("phraseStartHandler", [this](const char c) {
if (!isChar(c) || isCtl(c)) {
parseFailure("invalid first character " + std::string(1, c) +
" in HTTP phrase");
} else {
context_->phrase_.push_back(c);
transition(HTTP_PHRASE_ST, DATA_READ_OK_EVT);
}
});
}
void
HttpResponseParser::phraseHandler() {
stateWithReadHandler("phraseHandler", [this](const char c) {
if (c == '\r') {
transition(EXPECTING_NEW_LINE1_ST, DATA_READ_OK_EVT);
} else if (!isChar(c) || isCtl(c)) {
parseFailure("invalid character " + std::string(1, c) +
" in HTTP phrase");
} else {
context_->phrase_.push_back(c);
transition(HTTP_PHRASE_ST, DATA_READ_OK_EVT);
}
});
}
void
HttpResponseParser::expectingNewLineHandler(const unsigned int next_state) {
stateWithReadHandler("expectingNewLineHandler", [this, next_state](const char c) {
// Only a new line character is allowed in this state.
if (c == '\n') {
// If next state is HTTP_PARSE_OK_ST it means that we're
// parsing 3rd new line in the HTTP request message. This
// terminates the HTTP request (if there is no body) or marks the
// beginning of the body.
if (next_state == HTTP_PARSE_OK_ST) {
// Whether there is a body in this message or not, we should
// parse the HTTP headers to validate it and to check if there
// is "Content-Length" specified. The "Content-Length" is
// required for parsing body.
response_.create();
try {
// This will throw exception if there is no Content-Length.
uint64_t content_length =
response_.getHeaderValueAsUint64("Content-Length");
if (content_length > 0) {
// There is body in this request, so let's parse it.
transition(HTTP_BODY_ST, DATA_READ_OK_EVT);
}
} catch (const std::exception& ex) {
// There is no body in this message. If the body is required
// parsing fails.
if (response_.requiresBody()) {
parseFailure("HTTP message lacks a body");
} else {
// Body not required so simply terminate parsing.
transition(HTTP_PARSE_OK_ST, HTTP_PARSE_OK_EVT);
}
}
} else {
// This is 1st or 2nd new line, so let's transition to the
// next state required by this handler.
transition(next_state, DATA_READ_OK_EVT);
}
} else {
parseFailure("expecting new line after CR, found " +
std::string(1, c));
}
});
}
void
HttpResponseParser::headerLineStartHandler() {
stateWithReadHandler("headerLineStartHandler", [this](const char c) {
// If we're parsing HTTP headers and we found CR it marks the
// end of headers section.
if (c == '\r') {
transition(EXPECTING_NEW_LINE3_ST, DATA_READ_OK_EVT);
} else if (!context_->headers_.empty() && ((c == ' ') || (c == '\t'))) {
// New line in headers section followed by space or tab is an LWS,
// a line break within header value.
transition(HEADER_LWS_ST, DATA_READ_OK_EVT);
} else if (!isChar(c) || isCtl(c) || isSpecial(c)) {
parseFailure("invalid character " + std::string(1, c) +
" in header name");
} else {
// Update header name with the parsed letter.
context_->headers_.push_back(HttpHeaderContext());
context_->headers_.back().name_.push_back(c);
transition(HEADER_NAME_ST, DATA_READ_OK_EVT);
}
});
}
void
HttpResponseParser::headerLwsHandler() {
stateWithReadHandler("headerLwsHandler", [this](const char c) {
if (c == '\r') {
// Found CR during parsing a header value. Next value
// should be new line.
transition(EXPECTING_NEW_LINE2_ST, DATA_READ_OK_EVT);
} else if ((c == ' ') || (c == '\t')) {
// Space and tab is used to mark LWS. Simply swallow
// this character.
transition(getCurrState(), DATA_READ_OK_EVT);
} else if (isCtl(c)) {
parseFailure("control character found in the HTTP header " +
context_->headers_.back().name_);
} else {
// We're parsing header value, so let's update it.
context_->headers_.back().value_.push_back(c);
transition(HEADER_VALUE_ST, DATA_READ_OK_EVT);
}
});
}
void
HttpResponseParser::headerNameHandler() {
stateWithReadHandler("headerNameHandler", [this](const char c) {
// Colon follows header name and it has its own state.
if (c == ':') {
transition(SPACE_BEFORE_HEADER_VALUE_ST, DATA_READ_OK_EVT);
} else if (!isChar(c) || isCtl(c) || isSpecial(c)) {
parseFailure("invalid character " + std::string(1, c) +
" found in the HTTP header name");
} else {
// Parsing a header name, so update it.
context_->headers_.back().name_.push_back(c);
transition(getCurrState(), DATA_READ_OK_EVT);
}
});
}
void
HttpResponseParser::spaceBeforeHeaderValueHandler() {
stateWithReadHandler("spaceBeforeHeaderValueHandler", [this](const char c) {
if (c == ' ') {
// Remove leading whitespace from the header value.
transition(getCurrState(), DATA_READ_OK_EVT);
} else if (c == '\r') {
// If CR found during parsing header value, it marks the end
// of this value.
transition(EXPECTING_NEW_LINE2_ST, DATA_READ_OK_EVT);
} else if (isCtl(c)) {
parseFailure("control character found in the HTTP header "
+ context_->headers_.back().name_);
} else {
// Still parsing the value, so let's update it.
context_->headers_.back().value_.push_back(c);
transition(HEADER_VALUE_ST, DATA_READ_OK_EVT);
}
});
}
void
HttpResponseParser::headerValueHandler() {
stateWithReadHandler("headerValueHandler", [this](const char c) {
// If CR found during parsing header value, it marks the end
// of this value.
if (c == '\r') {
transition(EXPECTING_NEW_LINE2_ST, DATA_READ_OK_EVT);
} else if (isCtl(c)) {
parseFailure("control character found in the HTTP header "
+ context_->headers_.back().name_);
} else {
// Still parsing the value, so let's update it.
context_->headers_.back().value_.push_back(c);
transition(HEADER_VALUE_ST, DATA_READ_OK_EVT);
}
});
}
void
HttpResponseParser::bodyHandler() {
stateWithReadHandler("bodyHandler", [this](const char c) {
// We don't validate the body at this stage. Simply record the
// number of characters specified within "Content-Length".
context_->body_.push_back(c);
if (context_->body_.length() <
response_.getHeaderValueAsUint64("Content-Length")) {
transition(HTTP_BODY_ST, DATA_READ_OK_EVT);
} else {
transition(HTTP_PARSE_OK_ST, HTTP_PARSE_OK_EVT);
}
});
}
} // end of namespace isc::http
} // end of namespace isc
|