lib/Plack/Handler/picohttpparser/picohttpparser.c

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