|
4 | 4 |
|
5 | 5 | #include <assert.h>
|
6 | 6 |
|
7 |
| -void HttpRequest::GetFormDataFromString ( const std::string & irsString ///< string to parse for form data |
8 |
| - ) |
| 7 | +void HttpRequest::ParseRequestURI(std::string_view uri) |
9 | 8 | {
|
| 9 | + if (uri.empty()) |
| 10 | + return; |
10 | 11 |
|
11 |
| - PME oNameValueRegex ( "[?]?([^?=]*)=([^&]*)&?", "g" ); |
12 |
| - while ( oNameValueRegex.match ( irsString ) ) { |
| 12 | + // See: https://www.rfc-editor.org/rfc/rfc3986#section-3.4 |
| 13 | + if (size_t queryDelimiter = uri.find('?'); queryDelimiter != std::string_view::npos) |
| 14 | + { |
| 15 | + // Skip repeating '?' (question mark) characters after the first one. |
| 16 | + if (queryDelimiter = uri.find_first_not_of('?', queryDelimiter + 1); queryDelimiter == std::string_view::npos) |
| 17 | + return; |
13 | 18 |
|
14 |
| - ContentDisposition oContentDisposition; |
15 |
| - std::string sName = oNameValueRegex [ 1 ]; |
16 |
| - std::string sValue = oNameValueRegex [ 2 ]; |
| 19 | + std::string_view parameters = uri.substr(queryDelimiter); |
17 | 20 |
|
18 |
| -#ifdef EHS_DEBUG |
19 |
| - fprintf ( stderr, "[EHS_DEBUG] Info: Got form data: '%s' => '%s'\n", |
20 |
| - sName.c_str ( ), |
21 |
| - sValue.c_str ( ) ); |
22 |
| -#endif |
| 21 | + // Discard any trailing fragment. |
| 22 | + if (size_t fragmentDelimiter = parameters.find('#'); fragmentDelimiter != std::string_view::npos) |
| 23 | + { |
| 24 | + parameters = parameters.substr(0, fragmentDelimiter); |
| 25 | + } |
23 | 26 |
|
24 |
| - oFormValueMap [ sName ] = |
25 |
| - FormValue ( sValue, oContentDisposition ); |
| 27 | + // Limit request uri parameters to 4096 bytes. |
| 28 | + if (parameters.length() > 4096) |
| 29 | + { |
| 30 | + parameters = parameters.substr(0, 4096); |
| 31 | + } |
26 | 32 |
|
| 33 | + // According to the RFC, query can be anything, but this web server implementation only supports "key=value" pairs, |
| 34 | + // which are parsable as form data. |
| 35 | + ParseFormData(parameters); |
27 | 36 | }
|
28 |
| - |
29 | 37 | }
|
30 | 38 |
|
| 39 | +void HttpRequest::ParseFormData(std::string_view formData) |
| 40 | +{ |
| 41 | + size_t counter = 0; |
| 42 | + |
| 43 | + // Limit the maximum acceptable form data fields to 256. |
| 44 | + while (!formData.empty() && counter < 256) |
| 45 | + { |
| 46 | + std::string_view parameter; |
| 47 | + |
| 48 | + if (size_t delimiter = formData.find('&'); delimiter != std::string_view::npos) |
| 49 | + { |
| 50 | + parameter = formData.substr(0, delimiter); |
| 51 | + |
| 52 | + // Skip repeating '&' (ampersand) characters after the first one. |
| 53 | + if (delimiter = formData.find_first_not_of('&', delimiter + 1); delimiter != std::string_view::npos) |
| 54 | + { |
| 55 | + formData = formData.substr(delimiter); |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + formData = {}; |
| 60 | + } |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + parameter = formData; |
| 65 | + formData = {}; |
| 66 | + } |
| 67 | + |
| 68 | + if (!parameter.empty()) |
| 69 | + { |
| 70 | + std::string_view key, value; |
| 71 | + |
| 72 | + if (size_t delimiter = parameter.find('='); delimiter != std::string_view::npos) |
| 73 | + { |
| 74 | + key = parameter.substr(0, delimiter); |
31 | 75 |
|
| 76 | + // Skip repeating '=' (equals) characters after the first one. |
| 77 | + if (delimiter = parameter.find_first_not_of('=', delimiter + 1); delimiter != std::string_view::npos) |
| 78 | + { |
| 79 | + value = parameter.substr(delimiter); |
| 80 | + } |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + key = parameter; |
| 85 | + // NOTE: value is empty. |
| 86 | + } |
32 | 87 |
|
| 88 | + if (!key.empty()) |
| 89 | + { |
| 90 | + oFormValueMap[std::string{key}] = FormValue{value}; |
| 91 | + } |
| 92 | + } |
33 | 93 |
|
| 94 | + counter += 1; |
| 95 | + } |
| 96 | +} |
34 | 97 |
|
35 | 98 | // this parses a single piece of a multipart form body
|
36 | 99 | // here are two examples -- first is an uploaded file, second is a
|
@@ -334,7 +397,7 @@ HttpRequest::HttpParseStates HttpRequest::ParseData ( std::string & irsData ///<
|
334 | 397 |
|
335 | 398 |
|
336 | 399 | // check to see if the uri appeared to have form data in it
|
337 |
| - GetFormDataFromString ( sUri ); |
| 400 | + ParseRequestURI(sUri); |
338 | 401 |
|
339 | 402 | // on to the headers
|
340 | 403 | nCurrentHttpParseState = HTTPPARSESTATE_HEADERS;
|
@@ -491,7 +554,7 @@ HttpRequest::HttpParseStates HttpRequest::ParseData ( std::string & irsData ///<
|
491 | 554 | // else the body is just one piece
|
492 | 555 | else {
|
493 | 556 | // check for any form data
|
494 |
| - GetFormDataFromString ( sBody ); |
| 557 | + ParseFormData(sBody); |
495 | 558 |
|
496 | 559 | #ifdef EHS_DEBUG
|
497 | 560 | fprintf ( stderr, "Done with body, done with entire request\n" );
|
|
0 commit comments