| 1 |
|
| #include <stddef.h> |
| 2 |
|
| #include "picohttpparser.h" |
| 3 |
|
| |
| 4 |
|
| /* $Id: f88e09222873f8bd56c7836037b3fd0047024023 $ */ |
| 5 |
|
| |
| 6 |
|
| #if __GNUC__ >= 3 |
| 7 |
|
| # define likely(x) __builtin_expect(!!(x), 1) |
| 8 |
|
| # define unlikely(x) __builtin_expect(!!(x), 0) |
| 9 |
|
| #else |
| 10 |
|
| # define likely(x) (x) |
| 11 |
|
| # define unlikely(x) (x) |
| 12 |
|
| #endif |
| 13 |
|
| |
| 14 |
|
| #define CHECK_EOF() \ |
| 15 |
|
| if (buf == buf_end) { \ |
| 16 |
|
| *ret = -2; \ |
| 17 |
|
| return NULL; \ |
| 18 |
|
| } |
| 19 |
|
| |
| 20 |
|
| #define EXPECT_CHAR(ch) \ |
| 21 |
|
| CHECK_EOF(); \ |
| 22 |
|
| if (*buf++ != ch) { \ |
| 23 |
|
| *ret = -1; \ |
| 24 |
|
| return NULL; \ |
| 25 |
|
| } |
| 26 |
|
| |
| 27 |
|
| #define ADVANCE_TOKEN(tok, toklen) do { \ |
| 28 |
|
| const char* tok_start = buf; \ |
| 29 |
|
| for (; ; ++buf) { \ |
| 30 |
|
| CHECK_EOF(); \ |
| 31 |
|
| if (*buf == ' ') { \ |
| 32 |
|
| break; \ |
| 33 |
|
| } else if (*buf == '\015' || *buf == '\012') { \ |
| 34 |
|
| *ret = -1; \ |
| 35 |
|
| return NULL; \ |
| 36 |
|
| } \ |
| 37 |
|
| } \ |
| 38 |
|
| tok = tok_start; \ |
| 39 |
|
| toklen = buf - tok_start; \ |
| 40 |
|
| } while (0) |
| 41 |
|
| |
| 42 |
|
| static const char* token_char_map = |
| 43 |
|
| "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" |
| 44 |
|
| "\0\1\1\1\1\1\1\1\0\0\1\1\0\1\1\0\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0" |
| 45 |
|
| "\0\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\1\1" |
| 46 |
|
| "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\1\0\1\0" |
| 47 |
|
| "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" |
| 48 |
|
| "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" |
| 49 |
|
| "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" |
| 50 |
|
| "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; |
| 51 |
|
| |
| 52 |
364 |
| static const char* get_token_to_eol(const char* buf, const char* buf_end, |
| 53 |
|
| const char** token, size_t* token_len, |
| 54 |
|
| int* ret) |
| 55 |
|
| { |
| 56 |
364 |
| const char* token_start = buf; |
| 57 |
|
| |
| 58 |
|
| while (1) { |
| 59 |
364 |
▶ | if (likely(buf_end - buf >= 16)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (likely(buf_end - buf >= 16)) { |
331 |
33 |
364 |
|
| 60 |
|
| unsigned i; |
| 61 |
1230 |
▶ | for (i = 0; i < 16; i++, ++buf) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 16; i++, ++buf) { |
1230 |
0 |
1230 |
|
| 62 |
1230 |
▶ | if (unlikely((unsigned char)*buf <= '\015') |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely((unsigned char)*buf <= '\015') |
331 |
899 |
1230 |
|
| 63 |
331 |
▶ | && (*buf == '\015' || *buf == '\012')) { |
Branch: 2 branches
| Branch | True | False | Total |
| && (*buf == '\015' || *buf == '\012')) { |
64 |
267 |
331 |
| && (*buf == '\015' || *buf == '\012')) { |
64 |
0 |
64 |
|
| 64 |
331 |
| goto EOL_FOUND; |
| 65 |
|
| } |
| 66 |
|
| } |
| 67 |
|
| } else { |
| 68 |
160 |
| for (; ; ++buf) { |
| 69 |
193 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
0 |
193 |
193 |
|
| 70 |
193 |
▶ | if (unlikely((unsigned char)*buf <= '\015') |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely((unsigned char)*buf <= '\015') |
33 |
160 |
193 |
|
| 71 |
33 |
▶ | && (*buf == '\015' || *buf == '\012')) { |
Branch: 2 branches
| Branch | True | False | Total |
| && (*buf == '\015' || *buf == '\012')) { |
27 |
6 |
33 |
| && (*buf == '\015' || *buf == '\012')) { |
27 |
0 |
27 |
|
| 72 |
33 |
| goto EOL_FOUND; |
| 73 |
|
| } |
| 74 |
|
| } |
| 75 |
|
| } |
| 76 |
|
| } |
| 77 |
364 |
| EOL_FOUND: |
| 78 |
364 |
▶ | if (*buf == '\015') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*buf == '\015') { |
273 |
91 |
364 |
|
| 79 |
273 |
| ++buf; |
| 80 |
273 |
▶ | EXPECT_CHAR('\012'); |
Branch: 2 branches
| Branch | True | False | Total |
| EXPECT_CHAR('\012'); |
0 |
273 |
273 |
| EXPECT_CHAR('\012'); |
0 |
273 |
273 |
|
| 81 |
273 |
| *token_len = buf - 2 - token_start; |
| 82 |
|
| } else { /* should be: *buf == '\012' */ |
| 83 |
91 |
| *token_len = buf - token_start; |
| 84 |
91 |
| ++buf; |
| 85 |
|
| } |
| 86 |
364 |
| *token = token_start; |
| 87 |
|
| |
| 88 |
364 |
| return buf; |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
0 |
| static const char* is_complete(const char* buf, const char* buf_end, |
| 92 |
|
| size_t last_len, int* ret) |
| 93 |
|
| { |
| 94 |
0 |
| int ret_cnt = 0; |
| 95 |
0 |
▶ | buf = last_len < 3 ? buf : buf + last_len - 3; |
Branch: 1 branch
| Branch | True | False | Total |
| buf = last_len < 3 ? buf : buf + last_len - 3; |
0 |
0 |
0 |
|
| 96 |
|
| |
| 97 |
|
| while (1) { |
| 98 |
0 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
0 |
0 |
0 |
|
| 99 |
0 |
▶ | if (*buf == '\015') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*buf == '\015') { |
0 |
0 |
0 |
|
| 100 |
0 |
| ++buf; |
| 101 |
0 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
0 |
0 |
0 |
|
| 102 |
0 |
▶ | EXPECT_CHAR('\012'); |
Branch: 2 branches
| Branch | True | False | Total |
| EXPECT_CHAR('\012'); |
0 |
0 |
0 |
| EXPECT_CHAR('\012'); |
0 |
0 |
0 |
|
| 103 |
0 |
| ++ret_cnt; |
| 104 |
0 |
▶ | } else if (*buf == '\012') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*buf == '\012') { |
0 |
0 |
0 |
|
| 105 |
0 |
| ++buf; |
| 106 |
0 |
| ++ret_cnt; |
| 107 |
|
| } else { |
| 108 |
0 |
| ++buf; |
| 109 |
0 |
| ret_cnt = 0; |
| 110 |
|
| } |
| 111 |
0 |
▶ | if (ret_cnt == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret_cnt == 2) { |
0 |
0 |
0 |
|
| 112 |
0 |
| return buf; |
| 113 |
|
| } |
| 114 |
|
| } |
| 115 |
|
| |
| 116 |
|
| *ret = -2; |
| 117 |
|
| return NULL; |
| 118 |
|
| } |
| 119 |
|
| |
| 120 |
|
| /* *_buf is always within [buf, buf_end) upon success */ |
| 121 |
97 |
| static const char* parse_int(const char* buf, const char* buf_end, int* value, |
| 122 |
|
| int* ret) |
| 123 |
|
| { |
| 124 |
|
| int v; |
| 125 |
97 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
0 |
97 |
97 |
|
| 126 |
97 |
▶ | if (! ('0' <= *buf && *buf <= '9')) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (! ('0' <= *buf && *buf <= '9')) { |
97 |
0 |
97 |
| if (! ('0' <= *buf && *buf <= '9')) { |
0 |
97 |
97 |
|
| 127 |
0 |
| *ret = -1; |
| 128 |
0 |
| return NULL; |
| 129 |
|
| } |
| 130 |
97 |
| v = 0; |
| 131 |
175 |
| for (; ; ++buf) { |
| 132 |
272 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
0 |
272 |
272 |
|
| 133 |
272 |
▶ | if ('0' <= *buf && *buf <= '9') { |
Branch: 2 branches
| Branch | True | False | Total |
| if ('0' <= *buf && *buf <= '9') { |
175 |
97 |
272 |
| if ('0' <= *buf && *buf <= '9') { |
175 |
0 |
175 |
|
| 134 |
175 |
| v = v * 10 + *buf - '0'; |
| 135 |
|
| } else { |
| 136 |
|
| break; |
| 137 |
|
| } |
| 138 |
|
| } |
| 139 |
|
| |
| 140 |
97 |
| *value = v; |
| 141 |
97 |
| return buf; |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
|
| /* returned pointer is always within [buf, buf_end), or null */ |
| 145 |
59 |
| static const char* parse_http_version(const char* buf, const char* buf_end, |
| 146 |
|
| int* minor_version, int* ret) |
| 147 |
|
| { |
| 148 |
59 |
▶ | EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
Branch: 8 branches
| Branch | True | False | Total |
| EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
0 |
59 |
59 |
| EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
0 |
59 |
59 |
| EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
0 |
59 |
59 |
| EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
1 |
58 |
59 |
| EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
0 |
58 |
58 |
| EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
0 |
58 |
58 |
| EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
0 |
58 |
58 |
| EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
0 |
58 |
58 |
|
| 149 |
58 |
▶ | EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); |
Branch: 6 branches
| Branch | True | False | Total |
| EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); |
0 |
58 |
58 |
| EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); |
0 |
58 |
58 |
| EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); |
0 |
58 |
58 |
| EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); |
0 |
58 |
58 |
| EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); |
0 |
58 |
58 |
| EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); |
0 |
58 |
58 |
|
| 150 |
58 |
| return parse_int(buf, buf_end, minor_version, ret); |
| 151 |
|
| } |
| 152 |
|
| |
| 153 |
58 |
| static const char* parse_headers(const char* buf, const char* buf_end, |
| 154 |
|
| struct phr_header* headers, |
| 155 |
|
| size_t* num_headers, size_t max_headers, |
| 156 |
|
| int* ret) |
| 157 |
|
| { |
| 158 |
325 |
| for (; ; ++*num_headers) { |
| 159 |
383 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
2 |
381 |
383 |
|
| 160 |
381 |
▶ | if (*buf == '\015') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*buf == '\015') { |
20 |
361 |
381 |
|
| 161 |
20 |
| ++buf; |
| 162 |
20 |
▶ | EXPECT_CHAR('\012'); |
Branch: 2 branches
| Branch | True | False | Total |
| EXPECT_CHAR('\012'); |
0 |
20 |
20 |
| EXPECT_CHAR('\012'); |
0 |
20 |
20 |
|
| 163 |
20 |
| break; |
| 164 |
361 |
▶ | } else if (*buf == '\012') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*buf == '\012') { |
34 |
327 |
361 |
|
| 165 |
34 |
| ++buf; |
| 166 |
34 |
| break; |
| 167 |
|
| } |
| 168 |
327 |
▶ | if (*num_headers == max_headers) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*num_headers == max_headers) { |
2 |
325 |
327 |
|
| 169 |
2 |
| *ret = -1; |
| 170 |
2 |
| return NULL; |
| 171 |
|
| } |
| 172 |
325 |
▶ | if (! (*num_headers != 0 && (*buf == ' ' || *buf == '\t'))) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (! (*num_headers != 0 && (*buf == ' ' || *buf == '\t'))) { |
286 |
39 |
325 |
| if (! (*num_headers != 0 && (*buf == ' ' || *buf == '\t'))) { |
279 |
7 |
286 |
| if (! (*num_headers != 0 && (*buf == ' ' || *buf == '\t'))) { |
279 |
0 |
279 |
|
| 173 |
318 |
▶ | if (! token_char_map[(unsigned char)*buf]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! token_char_map[(unsigned char)*buf]) { |
0 |
318 |
318 |
|
| 174 |
0 |
| *ret = -1; |
| 175 |
0 |
| return NULL; |
| 176 |
|
| } |
| 177 |
|
| /* parsing name, but do not discard SP before colon, see |
| 178 |
|
| * http://www.mozilla.org/security/announce/2006/mfsa2006-33.html */ |
| 179 |
318 |
| headers[*num_headers].name = buf; |
| 180 |
5444 |
| for (; ; ++buf) { |
| 181 |
5762 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
0 |
5762 |
5762 |
|
| 182 |
5762 |
▶ | if (*buf == ':') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*buf == ':') { |
318 |
5444 |
5762 |
|
| 183 |
318 |
| break; |
| 184 |
5444 |
▶ | } else if (*buf < ' ') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*buf < ' ') { |
0 |
5444 |
5444 |
|
| 185 |
0 |
| *ret = -1; |
| 186 |
0 |
| return NULL; |
| 187 |
|
| } |
| 188 |
|
| } |
| 189 |
318 |
| headers[*num_headers].name_len = buf - headers[*num_headers].name; |
| 190 |
318 |
| ++buf; |
| 191 |
318 |
| for (; ; ++buf) { |
| 192 |
636 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
0 |
636 |
636 |
|
| 193 |
636 |
▶ | if (! (*buf == ' ' || *buf == '\t')) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (! (*buf == ' ' || *buf == '\t')) { |
318 |
318 |
636 |
| if (! (*buf == ' ' || *buf == '\t')) { |
318 |
0 |
318 |
|
| 194 |
318 |
| break; |
| 195 |
|
| } |
| 196 |
|
| } |
| 197 |
|
| } else { |
| 198 |
7 |
| headers[*num_headers].name = NULL; |
| 199 |
7 |
| headers[*num_headers].name_len = 0; |
| 200 |
|
| } |
| 201 |
325 |
▶ | if ((buf = get_token_to_eol(buf, buf_end, &headers[*num_headers].value, |
Branch: 1 branch
| Branch | True | False | Total |
| if ((buf = get_token_to_eol(buf, buf_end, &headers[*num_headers].value, |
0 |
325 |
325 |
|
| 202 |
325 |
| &headers[*num_headers].value_len, ret)) |
| 203 |
|
| == NULL) { |
| 204 |
0 |
| return NULL; |
| 205 |
|
| } |
| 206 |
|
| } |
| 207 |
54 |
| return buf; |
| 208 |
|
| } |
| 209 |
|
| |
| 210 |
19 |
| const char* parse_request(const char* buf, const char* buf_end, |
| 211 |
|
| const char** method, size_t* method_len, |
| 212 |
|
| const char** path, size_t* path_len, |
| 213 |
|
| int* minor_version, struct phr_header* headers, |
| 214 |
|
| size_t* num_headers, size_t max_headers, int* ret) |
| 215 |
|
| { |
| 216 |
|
| /* skip first empty line (some clients add CRLF after POST content) */ |
| 217 |
19 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
0 |
19 |
19 |
|
| 218 |
19 |
▶ | if (*buf == '\015') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*buf == '\015') { |
0 |
19 |
19 |
|
| 219 |
0 |
| ++buf; |
| 220 |
0 |
▶ | EXPECT_CHAR('\012'); |
Branch: 2 branches
| Branch | True | False | Total |
| EXPECT_CHAR('\012'); |
0 |
0 |
0 |
| EXPECT_CHAR('\012'); |
0 |
0 |
0 |
|
| 221 |
19 |
▶ | } else if (*buf == '\012') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*buf == '\012') { |
0 |
19 |
19 |
|
| 222 |
0 |
| ++buf; |
| 223 |
|
| } |
| 224 |
|
| |
| 225 |
|
| /* parse request line */ |
| 226 |
77 |
▶ | ADVANCE_TOKEN(*method, *method_len); |
Branch: 4 branches
| Branch | True | False | Total |
| ADVANCE_TOKEN(*method, *method_len); |
0 |
77 |
77 |
| ADVANCE_TOKEN(*method, *method_len); |
19 |
58 |
77 |
| ADVANCE_TOKEN(*method, *method_len); |
58 |
0 |
58 |
| ADVANCE_TOKEN(*method, *method_len); |
0 |
58 |
58 |
|
| 227 |
19 |
| ++buf; |
| 228 |
140 |
▶ | ADVANCE_TOKEN(*path, *path_len); |
Branch: 4 branches
| Branch | True | False | Total |
| ADVANCE_TOKEN(*path, *path_len); |
0 |
140 |
140 |
| ADVANCE_TOKEN(*path, *path_len); |
19 |
121 |
140 |
| ADVANCE_TOKEN(*path, *path_len); |
121 |
0 |
121 |
| ADVANCE_TOKEN(*path, *path_len); |
0 |
121 |
121 |
|
| 229 |
19 |
| ++buf; |
| 230 |
19 |
▶ | if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) { |
0 |
19 |
19 |
|
| 231 |
0 |
| return NULL; |
| 232 |
|
| } |
| 233 |
19 |
▶ | if (*buf == '\015') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*buf == '\015') { |
19 |
0 |
19 |
|
| 234 |
19 |
| ++buf; |
| 235 |
19 |
▶ | EXPECT_CHAR('\012'); |
Branch: 2 branches
| Branch | True | False | Total |
| EXPECT_CHAR('\012'); |
0 |
19 |
19 |
| EXPECT_CHAR('\012'); |
0 |
19 |
19 |
|
| 236 |
0 |
▶ | } else if (*buf == '\012') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*buf == '\012') { |
0 |
0 |
0 |
|
| 237 |
0 |
| ++buf; |
| 238 |
|
| } else { |
| 239 |
0 |
| *ret = -1; |
| 240 |
0 |
| return NULL; |
| 241 |
|
| } |
| 242 |
|
| |
| 243 |
19 |
| return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret); |
| 244 |
|
| } |
| 245 |
|
| |
| 246 |
19 |
| int phr_parse_request(const char* buf_start, size_t len, const char** method, |
| 247 |
|
| size_t* method_len, const char** path, size_t* path_len, |
| 248 |
|
| int* minor_version, struct phr_header* headers, |
| 249 |
|
| size_t* num_headers, size_t last_len) |
| 250 |
|
| { |
| 251 |
19 |
| const char * buf = buf_start, * buf_end = buf_start + len; |
| 252 |
19 |
| size_t max_headers = *num_headers; |
| 253 |
|
| int r; |
| 254 |
|
| |
| 255 |
19 |
| *method = NULL; |
| 256 |
19 |
| *method_len = 0; |
| 257 |
19 |
| *path = NULL; |
| 258 |
19 |
| *path_len = 0; |
| 259 |
19 |
| *minor_version = -1; |
| 260 |
19 |
| *num_headers = 0; |
| 261 |
|
| |
| 262 |
|
| /* if last_len != 0, check if the request is complete (a fast countermeasure |
| 263 |
|
| againt slowloris */ |
| 264 |
19 |
▶ | if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { |
0 |
19 |
19 |
| if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { |
0 |
0 |
0 |
|
| 265 |
0 |
| return r; |
| 266 |
|
| } |
| 267 |
|
| |
| 268 |
19 |
▶ | if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len, |
Branch: 1 branch
| Branch | True | False | Total |
| if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len, |
1 |
18 |
19 |
|
| 269 |
|
| minor_version, headers, num_headers, max_headers, |
| 270 |
|
| &r)) |
| 271 |
|
| == NULL) { |
| 272 |
1 |
| return r; |
| 273 |
|
| } |
| 274 |
|
| |
| 275 |
18 |
| return buf - buf_start; |
| 276 |
|
| } |
| 277 |
|
| |
| 278 |
40 |
| static const char* parse_response(const char* buf, const char* buf_end, |
| 279 |
|
| int* minor_version, int* status, |
| 280 |
|
| const char** msg, size_t* msg_len, |
| 281 |
|
| struct phr_header* headers, |
| 282 |
|
| size_t* num_headers, size_t max_headers, |
| 283 |
|
| int* ret) |
| 284 |
|
| { |
| 285 |
|
| /* parse "HTTP/1.x" */ |
| 286 |
40 |
▶ | if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) { |
1 |
39 |
40 |
|
| 287 |
1 |
| return NULL; |
| 288 |
|
| } |
| 289 |
|
| /* skip space */ |
| 290 |
39 |
▶ | if (*buf++ != ' ') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*buf++ != ' ') { |
0 |
39 |
39 |
|
| 291 |
0 |
| *ret = -1; |
| 292 |
0 |
| return NULL; |
| 293 |
|
| } |
| 294 |
|
| /* parse status code */ |
| 295 |
39 |
▶ | if ((buf = parse_int(buf, buf_end, status, ret)) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((buf = parse_int(buf, buf_end, status, ret)) == NULL) { |
0 |
39 |
39 |
|
| 296 |
0 |
| return NULL; |
| 297 |
|
| } |
| 298 |
|
| /* skip space */ |
| 299 |
39 |
▶ | if (*buf++ != ' ') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*buf++ != ' ') { |
0 |
39 |
39 |
|
| 300 |
0 |
| *ret = -1; |
| 301 |
0 |
| return NULL; |
| 302 |
|
| } |
| 303 |
|
| /* get message */ |
| 304 |
39 |
▶ | if ((buf = get_token_to_eol(buf, buf_end, msg, msg_len, ret)) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((buf = get_token_to_eol(buf, buf_end, msg, msg_len, ret)) == NULL) { |
0 |
39 |
39 |
|
| 305 |
0 |
| return NULL; |
| 306 |
|
| } |
| 307 |
|
| |
| 308 |
39 |
| return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret); |
| 309 |
|
| } |
| 310 |
|
| |
| 311 |
40 |
| int phr_parse_response(const char* buf_start, size_t len, int* minor_version, |
| 312 |
|
| int* status, const char** msg, size_t* msg_len, |
| 313 |
|
| struct phr_header* headers, size_t* num_headers, |
| 314 |
|
| size_t last_len) |
| 315 |
|
| { |
| 316 |
40 |
| const char * buf = buf_start, * buf_end = buf + len; |
| 317 |
40 |
| size_t max_headers = *num_headers; |
| 318 |
|
| int r; |
| 319 |
|
| |
| 320 |
40 |
| *minor_version = -1; |
| 321 |
40 |
| *status = 0; |
| 322 |
40 |
| *msg = NULL; |
| 323 |
40 |
| *msg_len = 0; |
| 324 |
40 |
| *num_headers = 0; |
| 325 |
|
| |
| 326 |
|
| /* if last_len != 0, check if the response is complete (a fast countermeasure |
| 327 |
|
| against slowloris */ |
| 328 |
40 |
▶ | if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { |
0 |
40 |
40 |
| if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { |
0 |
0 |
0 |
|
| 329 |
0 |
| return r; |
| 330 |
|
| } |
| 331 |
|
| |
| 332 |
40 |
▶ | if ((buf = parse_response(buf, buf_end, minor_version, status, msg, msg_len, |
Branch: 1 branch
| Branch | True | False | Total |
| if ((buf = parse_response(buf, buf_end, minor_version, status, msg, msg_len, |
4 |
36 |
40 |
|
| 333 |
|
| headers, num_headers, max_headers, &r)) |
| 334 |
|
| == NULL) { |
| 335 |
4 |
| return r; |
| 336 |
|
| } |
| 337 |
|
| |
| 338 |
36 |
| return buf - buf_start; |
| 339 |
|
| } |
| 340 |
|
| |
| 341 |
|
| #undef CHECK_EOF |
| 342 |
|
| #undef EXPECT_CHAR |
| 343 |
|
| #undef ADVANCE_TOKEN |