picohttpparser/picohttpparser.c

filter: Statementstmt 45.9% 123 / 268 Branchbran 40.9% 108 / 264 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 43.4% 231 / 532 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/HTTP/Parser/XS/PP.pm ↑ Summary blib/lib/HTTP/Parser/XS.pm »
1 /*
2 * Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
3 * Shigeo Mitsunari
4 *
5 * The software is licensed under either the MIT License (below) or the Perl
6 * license.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to
10 * deal in the Software without restriction, including without limitation the
11 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 * sell copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * IN THE SOFTWARE.
25 */
26  
27 #include <assert.h>
28 #include <stddef.h>
29 #include <string.h>
30 #ifdef __SSE4_2__
31 # include <x86intrin.h>
32 #endif
33 #include "picohttpparser.h"
34  
35 /* $Id: ca67b79ef774daaee2a144c32203c0a43a93f877 $ */
36  
37 #if __GNUC__ >= 3
38 # define likely(x) __builtin_expect(!!(x), 1)
39 # define unlikely(x) __builtin_expect(!!(x), 0)
40 #else
41 # define likely(x) (x)
42 # define unlikely(x) (x)
43 #endif
44  
45 #define IS_PRINTABLE_ASCII(c) ((unsigned char)(c) - 040u < 0137u)
46  
47 #define CHECK_EOF() \
48 if (buf == buf_end) { \
49 *ret = -2; \
50 return NULL; \
51 }
52  
53 #define EXPECT_CHAR(ch) \
54 CHECK_EOF(); \
55 if (*buf++ != ch) { \
56 *ret = -1; \
57 return NULL; \
58 }
59  
60 #define ADVANCE_TOKEN(tok, toklen) do { \
61 const char* tok_start = buf; \
62 static const char ranges2[] __attribute__((aligned(16))) = "\000\040\177\177"; \
63 int found2; \
64 buf = findchar_fast(buf, buf_end, ranges2, sizeof(ranges2) - 1, &found2); \
65 if (! found2) { \
66 CHECK_EOF(); \
67 } \
68 while (1) { \
69 if (*buf == ' ') { \
70 break; \
71 } else if (unlikely(! IS_PRINTABLE_ASCII(*buf))) { \
72 if ((unsigned char)*buf < '\040' || *buf == '\177') { \
73 *ret = -1; \
74 return NULL; \
75 } \
76 } \
77 ++buf; \
78 CHECK_EOF(); \
79 } \
80 tok = tok_start; \
81 toklen = buf - tok_start; \
82 } while (0)
83  
84 static const char* token_char_map =
85 "\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"
86 "\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"
87 "\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"
88 "\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"
89 "\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"
90 "\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"
91 "\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"
92 "\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";
93  
94 344 static const char* findchar_fast(const char* buf, const char* buf_end, const char *ranges, size_t ranges_size, int* found)
95 {
96 344 *found = 0;
97 #if __SSE4_2__
98 if (likely(buf_end - buf >= 16)) {
99 __m128i ranges16 = _mm_loadu_si128((const __m128i*)ranges);
100  
101 size_t left = (buf_end - buf) & ~15;
102 do {
103 __m128i b16 = _mm_loadu_si128((void*)buf);
104 int r = _mm_cmpestri(ranges16, ranges_size, b16, 16, _SIDD_LEAST_SIGNIFICANT | _SIDD_CMP_RANGES | _SIDD_UBYTE_OPS);
105 if (unlikely(r != 16)) {
106 buf += r;
107 *found = 1;
108 break;
109 }
110 buf += 16;
111 left -= 16;
112 } while (likely(left != 0));
113 }
114 #endif
115 344 return buf;
116 }
117  
118 364 static const char* get_token_to_eol(const char* buf, const char* buf_end,
119 const char** token, size_t* token_len,
120 int* ret)
121 42 {
122 364 const char* token_start = buf;
123
124 #ifdef __SSE4_2__
125 static const char ranges1[] =
126 "\0\010"
127 /* allow HT */
128 "\012\037"
129 /* allow SP and up to but not including DEL */
130 "\177\177"
131 /* allow chars w. MSB set */
132 ;
133 int found;
134 buf = findchar_fast(buf, buf_end, ranges1, sizeof(ranges1) - 1, &found);
135 if (found)
136 goto FOUND_CTL;
137 #else
138 /* find non-printable char within the next 8 bytes, this is the hottest code; manually inlined */
139 406 ▶ while (likely(buf_end - buf >= 8)) {
Branch: 1 branch
BranchTrueFalseTotal
while (likely(buf_end - buf >= 8)) { 379 27 406
140 #define DOIT() if (unlikely(! IS_PRINTABLE_ASCII(*buf))) goto NonPrintable; ++buf
141 379 ▶ DOIT(); DOIT(); DOIT(); DOIT();
Branch: 4 branches
BranchTrueFalseTotal
DOIT(); DOIT(); DOIT(); DOIT(); 2 377 379
DOIT(); DOIT(); DOIT(); DOIT(); 57 320 377
DOIT(); DOIT(); DOIT(); DOIT(); 218 102 320
DOIT(); DOIT(); DOIT(); DOIT(); 58 44 102
142 44 ▶ DOIT(); DOIT(); DOIT(); DOIT();
Branch: 4 branches
BranchTrueFalseTotal
DOIT(); DOIT(); DOIT(); DOIT(); 1 43 44
DOIT(); DOIT(); DOIT(); DOIT(); 1 42 43
DOIT(); DOIT(); DOIT(); DOIT(); 0 42 42
DOIT(); DOIT(); DOIT(); DOIT(); 0 42 42
143 #undef DOIT
144 42 continue;
145 337 NonPrintable:
146 337 ▶ if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) {
Branch: 3 branches
BranchTrueFalseTotal
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) { 337 0 337
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) { 0 337 337
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) { 0 0 0
147 337 goto FOUND_CTL;
148 }
149 0 ++buf;
150 }
151 #endif
152 47 for (; ; ++buf) {
153 74 ▶ CHECK_EOF();
Branch: 1 branch
BranchTrueFalseTotal
CHECK_EOF(); 0 74 74
154 74 ▶ if (unlikely(! IS_PRINTABLE_ASCII(*buf))) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(! IS_PRINTABLE_ASCII(*buf))) { 27 47 74
155 27 ▶ if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) {
Branch: 3 branches
BranchTrueFalseTotal
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) { 27 0 27
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) { 0 27 27
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) { 0 0 0
156 27 goto FOUND_CTL;
157 }
158 }
159 }
160 364 FOUND_CTL:
161 364 ▶ if (likely(*buf == '\015')) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(*buf == '\015')) { 273 91 364
162 273 ++buf;
163 273 ▶ EXPECT_CHAR('\012');
Branch: 2 branches
BranchTrueFalseTotal
EXPECT_CHAR('\012'); 0 273 273
EXPECT_CHAR('\012'); 0 273 273
164 273 *token_len = buf - 2 - token_start;
165 91 ▶ } else if (*buf == '\012') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*buf == '\012') { 91 0 91
166 91 *token_len = buf - token_start;
167 91 ++buf;
168 } else {
169 0 *ret = -1;
170 0 return NULL;
171 }
172 364 *token = token_start;
173
174 364 return buf;
175 }
176
177 0 static const char* is_complete(const char* buf, const char* buf_end,
178 size_t last_len, int* ret)
179 {
180 0 int ret_cnt = 0;
181 0 ▶ buf = last_len < 3 ? buf : buf + last_len - 3;
Branch: 1 branch
BranchTrueFalseTotal
buf = last_len < 3 ? buf : buf + last_len - 3; 0 0 0
182
183 while (1) {
184 0 ▶ CHECK_EOF();
Branch: 1 branch
BranchTrueFalseTotal
CHECK_EOF(); 0 0 0
185 0 ▶ if (*buf == '\015') {
Branch: 1 branch
BranchTrueFalseTotal
if (*buf == '\015') { 0 0 0
186 0 ++buf;
187 0 ▶ CHECK_EOF();
Branch: 1 branch
BranchTrueFalseTotal
CHECK_EOF(); 0 0 0
188 0 ▶ EXPECT_CHAR('\012');
Branch: 2 branches
BranchTrueFalseTotal
EXPECT_CHAR('\012'); 0 0 0
EXPECT_CHAR('\012'); 0 0 0
189 0 ++ret_cnt;
190 0 ▶ } else if (*buf == '\012') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*buf == '\012') { 0 0 0
191 0 ++buf;
192 0 ++ret_cnt;
193 } else {
194 0 ++buf;
195 0 ret_cnt = 0;
196 }
197 0 ▶ if (ret_cnt == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret_cnt == 2) { 0 0 0
198 0 return buf;
199 }
200 }
201
202 *ret = -2;
203 return NULL;
204 }
205  
206 /* *_buf is always within [buf, buf_end) upon success */
207 91 static const char* parse_int(const char* buf, const char* buf_end, int* value,
208 int* ret)
209 {
210 int v;
211 91 ▶ CHECK_EOF();
Branch: 1 branch
BranchTrueFalseTotal
CHECK_EOF(); 0 91 91
212 91 ▶ if (! ('0' <= *buf && *buf <= '9')) {
Branch: 2 branches
BranchTrueFalseTotal
if (! ('0' <= *buf && *buf <= '9')) { 91 0 91
if (! ('0' <= *buf && *buf <= '9')) { 0 91 91
213 0 *ret = -1;
214 0 return NULL;
215 }
216 91 v = 0;
217 169 for (; ; ++buf) {
218 260 ▶ CHECK_EOF();
Branch: 1 branch
BranchTrueFalseTotal
CHECK_EOF(); 0 260 260
219 260 ▶ if ('0' <= *buf && *buf <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if ('0' <= *buf && *buf <= '9') { 169 91 260
if ('0' <= *buf && *buf <= '9') { 169 0 169
220 169 v = v * 10 + *buf - '0';
221 } else {
222 break;
223 }
224 }
225
226 91 *value = v;
227 91 return buf;
228 }
229  
230 /* returned pointer is always within [buf, buf_end), or null */
231 53 static const char* parse_http_version(const char* buf, const char* buf_end,
232 int* minor_version, int* ret)
233 {
234 53 ▶ EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P');
Branch: 8 branches
BranchTrueFalseTotal
EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); 0 53 53
EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); 0 53 53
EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); 0 53 53
EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); 1 52 53
EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); 0 52 52
EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); 0 52 52
EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); 0 52 52
EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); 0 52 52
235 52 ▶ EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.');
Branch: 6 branches
BranchTrueFalseTotal
EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); 0 52 52
EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); 0 52 52
EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); 0 52 52
EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); 0 52 52
EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); 0 52 52
EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); 0 52 52
236 52 return parse_int(buf, buf_end, minor_version, ret);
237 }
238  
239 52 static const char* parse_headers(const char* buf, const char* buf_end,
240 struct phr_header* headers,
241 size_t* num_headers, size_t max_headers,
242 int* ret)
243 {
244 325 for (; ; ++*num_headers) {
245 377 ▶ CHECK_EOF();
Branch: 1 branch
BranchTrueFalseTotal
CHECK_EOF(); 2 375 377
246 375 ▶ if (*buf == '\015') {
Branch: 1 branch
BranchTrueFalseTotal
if (*buf == '\015') { 14 361 375
247 14 ++buf;
248 14 ▶ EXPECT_CHAR('\012');
Branch: 2 branches
BranchTrueFalseTotal
EXPECT_CHAR('\012'); 0 14 14
EXPECT_CHAR('\012'); 0 14 14
249 14 break;
250 361 ▶ } else if (*buf == '\012') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*buf == '\012') { 34 327 361
251 34 ++buf;
252 34 break;
253 }
254 327 ▶ if (*num_headers == max_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (*num_headers == max_headers) { 2 325 327
255 2 *ret = -1;
256 2 return NULL;
257 }
258 643 ▶ if (! (*num_headers != 0 && (*buf == ' ' || *buf == '\t'))) {
Branch: 3 branches
BranchTrueFalseTotal
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
259 318 ▶ if (! token_char_map[(unsigned char)*buf]) {
Branch: 1 branch
BranchTrueFalseTotal
if (! token_char_map[(unsigned char)*buf]) { 0 318 318
260 0 *ret = -1;
261 0 return NULL;
262 }
263 /* parsing name, but do not discard SP before colon, see
264 * http://www.mozilla.org/security/announce/2006/mfsa2006-33.html */
265 318 headers[*num_headers].name = buf;
266 static const char ranges1[] __attribute__((aligned(16))) = "::\x00\037";
267 int found;
268 318 buf = findchar_fast(buf, buf_end, ranges1, sizeof(ranges1) - 1, &found);
269 318 ▶ if (! found) {
Branch: 1 branch
BranchTrueFalseTotal
if (! found) { 0 318 318
270 318 ▶ CHECK_EOF();
Branch: 1 branch
BranchTrueFalseTotal
CHECK_EOF(); 318 0 318
271 }
272 while (1) {
273 5762 ▶ if (*buf == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (*buf == ':') { 318 5444 5762
274 318 break;
275 5444 ▶ } else if (*buf < ' ') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*buf < ' ') { 0 5444 5444
276 0 *ret = -1;
277 0 return NULL;
278 }
279 5444 ++buf;
280 5444 ▶ CHECK_EOF();
Branch: 1 branch
BranchTrueFalseTotal
CHECK_EOF(); 0 5444 5444
281 }
282 318 headers[*num_headers].name_len = buf - headers[*num_headers].name;
283 318 ++buf;
284 318 for (; ; ++buf) {
285 636 ▶ CHECK_EOF();
Branch: 1 branch
BranchTrueFalseTotal
CHECK_EOF(); 0 636 636
286 636 ▶ if (! (*buf == ' ' || *buf == '\t')) {
Branch: 2 branches
BranchTrueFalseTotal
if (! (*buf == ' ' || *buf == '\t')) { 318 318 636
if (! (*buf == ' ' || *buf == '\t')) { 318 0 318
287 318 break;
288 }
289 }
290 } else {
291 7 headers[*num_headers].name = NULL;
292 7 headers[*num_headers].name_len = 0;
293 }
294 325 ▶ if ((buf = get_token_to_eol(buf, buf_end, &headers[*num_headers].value,
Branch: 1 branch
BranchTrueFalseTotal
if ((buf = get_token_to_eol(buf, buf_end, &headers[*num_headers].value, 0 325 325
295 325 &headers[*num_headers].value_len, ret))
296 == NULL) {
297 0 return NULL;
298 }
299 }
300 48 return buf;
301 }
302  
303 13 const char* parse_request(const char* buf, const char* buf_end,
304 const char** method, size_t* method_len,
305 const char** path, size_t* path_len,
306 int* minor_version, struct phr_header* headers,
307 size_t* num_headers, size_t max_headers, int* ret)
308 {
309 /* skip first empty line (some clients add CRLF after POST content) */
310 13 ▶ CHECK_EOF();
Branch: 1 branch
BranchTrueFalseTotal
CHECK_EOF(); 0 13 13
311 13 ▶ if (*buf == '\015') {
Branch: 1 branch
BranchTrueFalseTotal
if (*buf == '\015') { 0 13 13
312 0 ++buf;
313 0 ▶ EXPECT_CHAR('\012');
Branch: 2 branches
BranchTrueFalseTotal
EXPECT_CHAR('\012'); 0 0 0
EXPECT_CHAR('\012'); 0 0 0
314 13 ▶ } else if (*buf == '\012') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*buf == '\012') { 0 13 13
315 0 ++buf;
316 }
317
318 /* parse request line */
319 66 ▶ ADVANCE_TOKEN(*method, *method_len);
Branch: 7 branches
BranchTrueFalseTotal
ADVANCE_TOKEN(*method, *method_len); 0 13 13
ADVANCE_TOKEN(*method, *method_len); 13 0 13
ADVANCE_TOKEN(*method, *method_len); 13 40 53
ADVANCE_TOKEN(*method, *method_len); 0 40 40
ADVANCE_TOKEN(*method, *method_len); 0 0 0
ADVANCE_TOKEN(*method, *method_len); 0 0 0
ADVANCE_TOKEN(*method, *method_len); 0 40 40
320 13 ++buf;
321 86 ▶ ADVANCE_TOKEN(*path, *path_len);
Branch: 7 branches
BranchTrueFalseTotal
ADVANCE_TOKEN(*path, *path_len); 0 13 13
ADVANCE_TOKEN(*path, *path_len); 13 0 13
ADVANCE_TOKEN(*path, *path_len); 13 60 73
ADVANCE_TOKEN(*path, *path_len); 0 60 60
ADVANCE_TOKEN(*path, *path_len); 0 0 0
ADVANCE_TOKEN(*path, *path_len); 0 0 0
ADVANCE_TOKEN(*path, *path_len); 0 60 60
322 13 ++buf;
323 13 ▶ if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) { 0 13 13
324 0 return NULL;
325 }
326 13 ▶ if (*buf == '\015') {
Branch: 1 branch
BranchTrueFalseTotal
if (*buf == '\015') { 13 0 13
327 13 ++buf;
328 13 ▶ EXPECT_CHAR('\012');
Branch: 2 branches
BranchTrueFalseTotal
EXPECT_CHAR('\012'); 0 13 13
EXPECT_CHAR('\012'); 0 13 13
329 0 ▶ } else if (*buf == '\012') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*buf == '\012') { 0 0 0
330 0 ++buf;
331 } else {
332 0 *ret = -1;
333 0 return NULL;
334 }
335
336 13 return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret);
337 }
338  
339 13 int phr_parse_request(const char* buf_start, size_t len, const char** method,
340 size_t* method_len, const char** path, size_t* path_len,
341 int* minor_version, struct phr_header* headers,
342 size_t* num_headers, size_t last_len)
343 {
344 13 const char * buf = buf_start, * buf_end = buf_start + len;
345 13 size_t max_headers = *num_headers;
346 int r;
347
348 13 *method = NULL;
349 13 *method_len = 0;
350 13 *path = NULL;
351 13 *path_len = 0;
352 13 *minor_version = -1;
353 13 *num_headers = 0;
354
355 /* if last_len != 0, check if the request is complete (a fast countermeasure
356 againt slowloris */
357 13 ▶ if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { 0 13 13
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { 0 0 0
358 0 return r;
359 }
360
361 13 ▶ if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len,
Branch: 1 branch
BranchTrueFalseTotal
if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len, 1 12 13
362 minor_version, headers, num_headers, max_headers,
363 &r))
364 == NULL) {
365 1 return r;
366 }
367
368 12 return (int)(buf - buf_start);
369 }
370  
371 40 static const char* parse_response(const char* buf, const char* buf_end,
372 int* minor_version, int* status,
373 const char** msg, size_t* msg_len,
374 struct phr_header* headers,
375 size_t* num_headers, size_t max_headers,
376 int* ret)
377 {
378 /* parse "HTTP/1.x" */
379 40 ▶ if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) { 1 39 40
380 1 return NULL;
381 }
382 /* skip space */
383 39 ▶ if (*buf++ != ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if (*buf++ != ' ') { 0 39 39
384 0 *ret = -1;
385 0 return NULL;
386 }
387 /* parse status code */
388 39 ▶ if ((buf = parse_int(buf, buf_end, status, ret)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((buf = parse_int(buf, buf_end, status, ret)) == NULL) { 0 39 39
389 0 return NULL;
390 }
391 /* skip space */
392 39 ▶ if (*buf++ != ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if (*buf++ != ' ') { 0 39 39
393 0 *ret = -1;
394 0 return NULL;
395 }
396 /* get message */
397 39 ▶ if ((buf = get_token_to_eol(buf, buf_end, msg, msg_len, ret)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((buf = get_token_to_eol(buf, buf_end, msg, msg_len, ret)) == NULL) { 0 39 39
398 0 return NULL;
399 }
400
401 39 return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret);
402 }
403  
404 40 int phr_parse_response(const char* buf_start, size_t len, int* minor_version,
405 int* status, const char** msg, size_t* msg_len,
406 struct phr_header* headers, size_t* num_headers,
407 size_t last_len)
408 {
409 40 const char * buf = buf_start, * buf_end = buf + len;
410 40 size_t max_headers = *num_headers;
411 int r;
412
413 40 *minor_version = -1;
414 40 *status = 0;
415 40 *msg = NULL;
416 40 *msg_len = 0;
417 40 *num_headers = 0;
418
419 /* if last_len != 0, check if the response is complete (a fast countermeasure
420 against slowloris */
421 40 ▶ if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
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
422 0 return r;
423 }
424
425 40 ▶ if ((buf = parse_response(buf, buf_end, minor_version, status, msg, msg_len,
Branch: 1 branch
BranchTrueFalseTotal
if ((buf = parse_response(buf, buf_end, minor_version, status, msg, msg_len, 4 36 40
426 headers, num_headers, max_headers, &r))
427 == NULL) {
428 4 return r;
429 }
430
431 36 return (int)(buf - buf_start);
432 }
433  
434 0 int phr_parse_headers(const char* buf_start, size_t len,
435 struct phr_header* headers, size_t* num_headers,
436 size_t last_len)
437 {
438 0 const char* buf = buf_start, * buf_end = buf + len;
439 0 size_t max_headers = *num_headers;
440 int r;
441  
442 0 *num_headers = 0;
443  
444 /* if last_len != 0, check if the response is complete (a fast countermeasure
445 against slowloris */
446 0 ▶ if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { 0 0 0
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { 0 0 0
447 0 return r;
448 }
449  
450 0 ▶ if ((buf = parse_headers(buf, buf_end, headers, num_headers, max_headers, &r))
Branch: 1 branch
BranchTrueFalseTotal
if ((buf = parse_headers(buf, buf_end, headers, num_headers, max_headers, &r)) 0 0 0
451 == NULL) {
452 0 return r;
453 }
454  
455 0 return (int)(buf - buf_start);
456 }
457  
458 enum {
459 CHUNKED_IN_CHUNK_SIZE,
460 CHUNKED_IN_CHUNK_EXT,
461 CHUNKED_IN_CHUNK_DATA,
462 CHUNKED_IN_CHUNK_CRLF,
463 CHUNKED_IN_TRAILERS_LINE_HEAD,
464 CHUNKED_IN_TRAILERS_LINE_MIDDLE
465 };
466  
467 0 static int decode_hex(int ch)
468 {
469 0 ▶ if ('0' <= ch && ch <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if ('0' <= ch && ch <= '9') { 0 0 0
if ('0' <= ch && ch <= '9') { 0 0 0
470 0 return ch - '0';
471 0 ▶ } else if ('A' <= ch && ch <= 'F') {
Branch: 2 branches
BranchTrueFalseTotal
} else if ('A' <= ch && ch <= 'F') { 0 0 0
} else if ('A' <= ch && ch <= 'F') { 0 0 0
472 0 return ch - 'A' + 0xa;
473 0 ▶ } else if ('a' <= ch && ch <= 'f') {
Branch: 2 branches
BranchTrueFalseTotal
} else if ('a' <= ch && ch <= 'f') { 0 0 0
} else if ('a' <= ch && ch <= 'f') { 0 0 0
474 0 return ch - 'a' + 0xa;
475 } else {
476 0 return -1;
477 }
478 }
479  
480 0 ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf,
481 size_t *_bufsz)
482 {
483 0 size_t dst = 0, src = 0, bufsz = *_bufsz;
484 0 ssize_t ret = -2; /* incomplete */
485  
486 while (1) {
487 0 switch (decoder->_state) {
488 0 case CHUNKED_IN_CHUNK_SIZE:
489 0 for (; ; ++src) {
490 int v;
491 0 ▶ if (src == bufsz)
Branch: 1 branch
BranchTrueFalseTotal
if (src == bufsz) 0 0 0
492 0 goto Exit;
493 0 ▶ if ((v = decode_hex(buf[src])) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = decode_hex(buf[src])) == -1) { 0 0 0
494 0 ▶ if (decoder->_hex_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (decoder->_hex_count == 0) { 0 0 0
495 0 ret = -1;
496 0 goto Exit;
497 }
498 0 break;
499 }
500 0 ▶ if (decoder->_hex_count == sizeof(size_t) * 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (decoder->_hex_count == sizeof(size_t) * 2) { 0 0 0
501 0 ret = -1;
502 0 goto Exit;
503 }
504 0 decoder->bytes_left_in_chunk = decoder->bytes_left_in_chunk * 16 + v;
505 0 ++decoder->_hex_count;
506 }
507 0 decoder->_hex_count = 0;
508 0 decoder->_state = CHUNKED_IN_CHUNK_EXT;
509 /* fallthru */
510 0 case CHUNKED_IN_CHUNK_EXT:
511 /* RFC 7230 A.2 "Line folding in chunk extensions is disallowed" */
512 0 for (; ; ++src) {
513 0 ▶ if (src == bufsz)
Branch: 1 branch
BranchTrueFalseTotal
if (src == bufsz) 0 0 0
514 0 goto Exit;
515 0 ▶ if (buf[src] == '\012')
Branch: 1 branch
BranchTrueFalseTotal
if (buf[src] == '\012') 0 0 0
516 0 break;
517 }
518 0 ++src;
519 0 ▶ if (decoder->bytes_left_in_chunk == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (decoder->bytes_left_in_chunk == 0) { 0 0 0
520 0 ▶ if (decoder->consume_trailer) {
Branch: 1 branch
BranchTrueFalseTotal
if (decoder->consume_trailer) { 0 0 0
521 0 decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD;
522 0 break;
523 } else {
524 0 goto Complete;
525 }
526 }
527 0 decoder->_state = CHUNKED_IN_CHUNK_DATA;
528 /* fallthru */
529 0 case CHUNKED_IN_CHUNK_DATA:
530 {
531 0 size_t avail = bufsz - src;
532 0 ▶ if (avail < decoder->bytes_left_in_chunk) {
Branch: 1 branch
BranchTrueFalseTotal
if (avail < decoder->bytes_left_in_chunk) { 0 0 0
533 0 ▶ if (dst != src)
Branch: 1 branch
BranchTrueFalseTotal
if (dst != src) 0 0 0
534 0 memmove(buf + dst, buf + src, avail);
535 0 src += avail;
536 0 dst += avail;
537 0 decoder->bytes_left_in_chunk -= avail;
538 0 goto Exit;
539 }
540 0 ▶ if (dst != src)
Branch: 1 branch
BranchTrueFalseTotal
if (dst != src) 0 0 0
541 0 memmove(buf + dst, buf + src, decoder->bytes_left_in_chunk);
542 0 src += decoder->bytes_left_in_chunk;
543 0 dst += decoder->bytes_left_in_chunk;
544 0 decoder->bytes_left_in_chunk = 0;
545 0 decoder->_state = CHUNKED_IN_CHUNK_CRLF;
546 }
547 /* fallthru */
548 0 case CHUNKED_IN_CHUNK_CRLF:
549 0 for (; ; ++src) {
550 0 ▶ if (src == bufsz)
Branch: 1 branch
BranchTrueFalseTotal
if (src == bufsz) 0 0 0
551 0 goto Exit;
552 0 ▶ if (buf[src] != '\015')
Branch: 1 branch
BranchTrueFalseTotal
if (buf[src] != '\015') 0 0 0
553 0 break;
554 }
555 0 ▶ if (buf[src] != '\012') {
Branch: 1 branch
BranchTrueFalseTotal
if (buf[src] != '\012') { 0 0 0
556 0 ret = -1;
557 0 goto Exit;
558 }
559 0 ++src;
560 0 decoder->_state = CHUNKED_IN_CHUNK_SIZE;
561 0 break;
562 0 case CHUNKED_IN_TRAILERS_LINE_HEAD:
563 0 for (; ; ++src) {
564 0 ▶ if (src == bufsz)
Branch: 1 branch
BranchTrueFalseTotal
if (src == bufsz) 0 0 0
565 0 goto Exit;
566 0 ▶ if (buf[src] != '\015')
Branch: 1 branch
BranchTrueFalseTotal
if (buf[src] != '\015') 0 0 0
567 0 break;
568 }
569 0 ▶ if (buf[src++] == '\012')
Branch: 1 branch
BranchTrueFalseTotal
if (buf[src++] == '\012') 0 0 0
570 0 goto Complete;
571 0 decoder->_state = CHUNKED_IN_TRAILERS_LINE_MIDDLE;
572 /* fallthru */
573 0 case CHUNKED_IN_TRAILERS_LINE_MIDDLE:
574 0 for (; ; ++src) {
575 0 ▶ if (src == bufsz)
Branch: 1 branch
BranchTrueFalseTotal
if (src == bufsz) 0 0 0
576 0 goto Exit;
577 0 ▶ if (buf[src] == '\012')
Branch: 1 branch
BranchTrueFalseTotal
if (buf[src] == '\012') 0 0 0
578 0 break;
579 }
580 0 ++src;
581 0 decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD;
582 0 break;
583 0 default:
584 assert(!"decoder is corrupt");
585 }
586 }
587  
588 0 Complete:
589 0 ret = bufsz - src;
590 0 Exit:
591 0 ▶ if (dst != src)
Branch: 1 branch
BranchTrueFalseTotal
if (dst != src) 0 0 0
592 0 memmove(buf + dst, buf + src, bufsz - src);
593 0 *_bufsz = dst;
594 0 return ret;
595 }
596  
597 #undef CHECK_EOF
598 #undef EXPECT_CHAR
599 #undef ADVANCE_TOKEN
« blib/lib/HTTP/Parser/XS/PP.pm ↑ Summary blib/lib/HTTP/Parser/XS.pm »