TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
http_parser.c
Go to the documentation of this file.
1 /* Based on src/http/ngx_http_parse.c from NGINX copyright Igor Sysoev
2  *
3  * Additional changes are licensed under the same terms as NGINX and
4  * copyright Joyent, Inc. and other Node contributors. All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 #include "http_parser.h"
25 #include <assert.h>
26 #include <stddef.h>
27 #include <ctype.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <limits.h>
31 
32 #ifndef ULLONG_MAX
33 # define ULLONG_MAX ((uint64_t) -1) /* 2^64-1 */
34 #endif
35 
36 #ifndef MIN
37 # define MIN(a,b) ((a) < (b) ? (a) : (b))
38 #endif
39 
40 #ifndef ARRAY_SIZE
41 # define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
42 #endif
43 
44 #ifndef BIT_AT
45 # define BIT_AT(a, i) \
46  (!!((unsigned int) (a)[(unsigned int) (i) >> 3] & \
47  (1 << ((unsigned int) (i) & 7))))
48 #endif
49 
50 #ifndef ELEM_AT
51 # define ELEM_AT(a, i, v) ((unsigned int) (i) < ARRAY_SIZE(a) ? (a)[(i)] : (v))
52 #endif
53 
54 #define SET_ERRNO(e) \
55 do { \
56  parser->http_errno = (e); \
57 } while(0)
58 
59 #define CURRENT_STATE() p_state
60 #define UPDATE_STATE(V) p_state = (enum state) (V);
61 #define RETURN(V) \
62 do { \
63  parser->state = CURRENT_STATE(); \
64  return (V); \
65 } while (0);
66 #define REEXECUTE() \
67  goto reexecute; \
68 
69 
70 #ifdef __GNUC__
71 # define LIKELY(X) __builtin_expect(!!(X), 1)
72 # define UNLIKELY(X) __builtin_expect(!!(X), 0)
73 #else
74 # define LIKELY(X) (X)
75 # define UNLIKELY(X) (X)
76 #endif
77 
78 
79 /* Run the notify callback FOR, returning ER if it fails */
80 #define CALLBACK_NOTIFY_(FOR, ER) \
81 do { \
82  assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \
83  \
84  if (LIKELY(settings->on_##FOR)) { \
85  parser->state = CURRENT_STATE(); \
86  if (UNLIKELY(0 != settings->on_##FOR(parser))) { \
87  SET_ERRNO(HPE_CB_##FOR); \
88  } \
89  UPDATE_STATE(parser->state); \
90  \
91  /* We either errored above or got paused; get out */ \
92  if (UNLIKELY(HTTP_PARSER_ERRNO(parser) != HPE_OK)) { \
93  return (ER); \
94  } \
95  } \
96 } while (0)
97 
98 /* Run the notify callback FOR and consume the current byte */
99 #define CALLBACK_NOTIFY(FOR) CALLBACK_NOTIFY_(FOR, p - data + 1)
100 
101 /* Run the notify callback FOR and don't consume the current byte */
102 #define CALLBACK_NOTIFY_NOADVANCE(FOR) CALLBACK_NOTIFY_(FOR, p - data)
103 
104 /* Run data callback FOR with LEN bytes, returning ER if it fails */
105 #define CALLBACK_DATA_(FOR, LEN, ER) \
106 do { \
107  assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \
108  \
109  if (FOR##_mark) { \
110  if (LIKELY(settings->on_##FOR)) { \
111  parser->state = CURRENT_STATE(); \
112  if (UNLIKELY(0 != \
113  settings->on_##FOR(parser, FOR##_mark, (LEN)))) { \
114  SET_ERRNO(HPE_CB_##FOR); \
115  } \
116  UPDATE_STATE(parser->state); \
117  \
118  /* We either errored above or got paused; get out */ \
119  if (UNLIKELY(HTTP_PARSER_ERRNO(parser) != HPE_OK)) { \
120  return (ER); \
121  } \
122  } \
123  FOR##_mark = NULL; \
124  } \
125 } while (0)
126 
127 /* Run the data callback FOR and consume the current byte */
128 #define CALLBACK_DATA(FOR) \
129  CALLBACK_DATA_(FOR, p - FOR##_mark, p - data + 1)
130 
131 /* Run the data callback FOR and don't consume the current byte */
132 #define CALLBACK_DATA_NOADVANCE(FOR) \
133  CALLBACK_DATA_(FOR, p - FOR##_mark, p - data)
134 
135 /* Set the mark FOR; non-destructive if mark is already set */
136 #define MARK(FOR) \
137 do { \
138  if (!FOR##_mark) { \
139  FOR##_mark = p; \
140  } \
141 } while (0)
142 
143 /* Don't allow the total size of the HTTP headers (including the status
144  * line) to exceed HTTP_MAX_HEADER_SIZE. This check is here to protect
145  * embedders against denial-of-service attacks where the attacker feeds
146  * us a never-ending header that the embedder keeps buffering.
147  *
148  * This check is arguably the responsibility of embedders but we're doing
149  * it on the embedder's behalf because most won't bother and this way we
150  * make the web a little safer. HTTP_MAX_HEADER_SIZE is still far bigger
151  * than any reasonable request or response so this should never affect
152  * day-to-day operation.
153  */
154 #define COUNT_HEADER_SIZE(V) \
155 do { \
156  parser->nread += (V); \
157  if (UNLIKELY(parser->nread > (HTTP_MAX_HEADER_SIZE))) { \
158  SET_ERRNO(HPE_HEADER_OVERFLOW); \
159  goto error; \
160  } \
161 } while (0)
162 
163 
164 #define PROXY_CONNECTION "proxy-connection"
165 #define CONNECTION "connection"
166 #define CONTENT_LENGTH "content-length"
167 #define TRANSFER_ENCODING "transfer-encoding"
168 #define UPGRADE "upgrade"
169 #define CHUNKED "chunked"
170 #define KEEP_ALIVE "keep-alive"
171 #define CLOSE "close"
172 
173 
174 static const char *method_strings[] =
175  {
176 #define XX(num, name, string) #string,
178 #undef XX
179  };
180 
181 
182 /* Tokens as defined by rfc 2616. Also lowercases them.
183  * token = 1*<any CHAR except CTLs or separators>
184  * separators = "(" | ")" | "<" | ">" | "@"
185  * | "," | ";" | ":" | "\" | <">
186  * | "/" | "[" | "]" | "?" | "="
187  * | "{" | "}" | SP | HT
188  */
189 static const char tokens[256] = {
190 /* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */
191  0, 0, 0, 0, 0, 0, 0, 0,
192 /* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */
193  0, 0, 0, 0, 0, 0, 0, 0,
194 /* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */
195  0, 0, 0, 0, 0, 0, 0, 0,
196 /* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */
197  0, 0, 0, 0, 0, 0, 0, 0,
198 /* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */
199  0, '!', 0, '#', '$', '%', '&', '\'',
200 /* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */
201  0, 0, '*', '+', 0, '-', '.', 0,
202 /* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */
203  '0', '1', '2', '3', '4', '5', '6', '7',
204 /* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */
205  '8', '9', 0, 0, 0, 0, 0, 0,
206 /* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */
207  0, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
208 /* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */
209  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
210 /* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */
211  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
212 /* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */
213  'x', 'y', 'z', 0, 0, 0, '^', '_',
214 /* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */
215  '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
216 /* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */
217  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
218 /* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */
219  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
220 /* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */
221  'x', 'y', 'z', 0, '|', 0, '~', 0 };
222 
223 
224 static const int8_t unhex[256] =
225  {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
226  ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
227  ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
228  , 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1
229  ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
230  ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
231  ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
232  ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
233  };
234 
235 
236 #if HTTP_PARSER_STRICT
237 # define T(v) 0
238 #else
239 # define T(v) v
240 #endif
241 
242 
243 static const uint8_t normal_url_char[32] = {
244 /* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */
245  0 | 0 | 0 | 0 | 0 | 0 | 0 | 0,
246 /* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */
247  0 | T(2) | 0 | 0 | T(16) | 0 | 0 | 0,
248 /* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */
249  0 | 0 | 0 | 0 | 0 | 0 | 0 | 0,
250 /* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */
251  0 | 0 | 0 | 0 | 0 | 0 | 0 | 0,
252 /* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */
253  0 | 2 | 4 | 0 | 16 | 32 | 64 | 128,
254 /* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */
255  1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
256 /* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */
257  1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
258 /* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */
259  1 | 2 | 4 | 8 | 16 | 32 | 64 | 0,
260 /* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */
261  1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
262 /* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */
263  1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
264 /* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */
265  1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
266 /* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */
267  1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
268 /* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */
269  1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
270 /* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */
271  1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
272 /* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */
273  1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
274 /* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */
275  1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, };
276 
277 #undef T
278 
279 enum state
280  { s_dead = 1 /* important that this is > 0 */
281 
298 
300 
324 
333 
335 
340 
343 
344  /* Important: 's_headers_done' must be the last 'header' state. All
345  * states beyond this must be 'body' states. It is used for overflow
346  * checking. See the PARSING_HEADER() macro.
347  */
348 
352 
355 
357  };
358 
359 
360 #define PARSING_HEADER(state) (state <= s_headers_done)
361 
362 
364  { h_general = 0
365  , h_C
368 
374 
379 
386 
391  };
392 
394  {
405 };
406 
407 /* Macros for character classes; depends on strict-mode */
408 #define CR '\r'
409 #define LF '\n'
410 #define LOWER(c) (unsigned char)(c | 0x20)
411 #define IS_ALPHA(c) (LOWER(c) >= 'a' && LOWER(c) <= 'z')
412 #define IS_NUM(c) ((c) >= '0' && (c) <= '9')
413 #define IS_ALPHANUM(c) (IS_ALPHA(c) || IS_NUM(c))
414 #define IS_HEX(c) (IS_NUM(c) || (LOWER(c) >= 'a' && LOWER(c) <= 'f'))
415 #define IS_MARK(c) ((c) == '-' || (c) == '_' || (c) == '.' || \
416  (c) == '!' || (c) == '~' || (c) == '*' || (c) == '\'' || (c) == '(' || \
417  (c) == ')')
418 #define IS_USERINFO_CHAR(c) (IS_ALPHANUM(c) || IS_MARK(c) || (c) == '%' || \
419  (c) == ';' || (c) == ':' || (c) == '&' || (c) == '=' || (c) == '+' || \
420  (c) == '$' || (c) == ',')
421 
422 #define STRICT_TOKEN(c) (tokens[(unsigned char)c])
423 
424 #if HTTP_PARSER_STRICT
425 #define TOKEN(c) (tokens[(unsigned char)c])
426 #define IS_URL_CHAR(c) (BIT_AT(normal_url_char, (unsigned char)c))
427 #define IS_HOST_CHAR(c) (IS_ALPHANUM(c) || (c) == '.' || (c) == '-')
428 #else
429 #define TOKEN(c) ((c == ' ') ? ' ' : tokens[(unsigned char)c])
430 #define IS_URL_CHAR(c) \
431  (BIT_AT(normal_url_char, (unsigned char)c) || ((c) & 0x80))
432 #define IS_HOST_CHAR(c) \
433  (IS_ALPHANUM(c) || (c) == '.' || (c) == '-' || (c) == '_')
434 #endif
435 
436 
437 #define start_state (parser->type == HTTP_REQUEST ? s_start_req : s_start_res)
438 
439 
440 #if HTTP_PARSER_STRICT
441 # define STRICT_CHECK(cond) \
442 do { \
443  if (cond) { \
444  SET_ERRNO(HPE_STRICT); \
445  goto error; \
446  } \
447 } while (0)
448 # define NEW_MESSAGE() (http_should_keep_alive(parser) ? start_state : s_dead)
449 #else
450 # define STRICT_CHECK(cond)
451 # define NEW_MESSAGE() start_state
452 #endif
453 
454 
455 /* Map errno values to strings for human-readable output */
456 #define HTTP_STRERROR_GEN(n, s) { "HPE_" #n, s },
457 static struct {
458  const char *name;
459  const char *description;
460 } http_strerror_tab[] = {
462 };
463 #undef HTTP_STRERROR_GEN
464 
465 int http_message_needs_eof(const http_parser *parser);
466 
467 /* Our URL parser.
468  *
469  * This is designed to be shared by http_parser_execute() for URL validation,
470  * hence it has a state transition + byte-for-byte interface. In addition, it
471  * is meant to be embedded in http_parser_parse_url(), which does the dirty
472  * work of turning state transitions URL components for its API.
473  *
474  * This function should only be invoked with non-space characters. It is
475  * assumed that the caller cares about (and can detect) the transition between
476  * URL and non-URL states by looking for these.
477  */
478 static enum state
479 parse_url_char(enum state s, const char ch)
480 {
481  if (ch == ' ' || ch == '\r' || ch == '\n') {
482  return s_dead;
483  }
484 
485 #if HTTP_PARSER_STRICT
486  if (ch == '\t' || ch == '\f') {
487  return s_dead;
488  }
489 #endif
490 
491  switch (s) {
493  /* Proxied requests are followed by scheme of an absolute URI (alpha).
494  * All methods except CONNECT are followed by '/' or '*'.
495  */
496 
497  if (ch == '/' || ch == '*') {
498  return s_req_path;
499  }
500 
501  if (IS_ALPHA(ch)) {
502  return s_req_schema;
503  }
504 
505  break;
506 
507  case s_req_schema:
508  if (IS_ALPHA(ch)) {
509  return s;
510  }
511 
512  if (ch == ':') {
513  return s_req_schema_slash;
514  }
515 
516  break;
517 
518  case s_req_schema_slash:
519  if (ch == '/') {
521  }
522 
523  break;
524 
526  if (ch == '/') {
527  return s_req_server_start;
528  }
529 
530  break;
531 
533  if (ch == '@') {
534  return s_dead;
535  }
536 
537  /* FALLTHROUGH */
538  case s_req_server_start:
539  case s_req_server:
540  if (ch == '/') {
541  return s_req_path;
542  }
543 
544  if (ch == '?') {
546  }
547 
548  if (ch == '@') {
549  return s_req_server_with_at;
550  }
551 
552  if (IS_USERINFO_CHAR(ch) || ch == '[' || ch == ']') {
553  return s_req_server;
554  }
555 
556  break;
557 
558  case s_req_path:
559  if (IS_URL_CHAR(ch)) {
560  return s;
561  }
562 
563  switch (ch) {
564  case '?':
566 
567  case '#':
568  return s_req_fragment_start;
569  }
570 
571  break;
572 
574  case s_req_query_string:
575  if (IS_URL_CHAR(ch)) {
576  return s_req_query_string;
577  }
578 
579  switch (ch) {
580  case '?':
581  /* allow extra '?' in query string */
582  return s_req_query_string;
583 
584  case '#':
585  return s_req_fragment_start;
586  }
587 
588  break;
589 
591  if (IS_URL_CHAR(ch)) {
592  return s_req_fragment;
593  }
594 
595  switch (ch) {
596  case '?':
597  return s_req_fragment;
598 
599  case '#':
600  return s;
601  }
602 
603  break;
604 
605  case s_req_fragment:
606  if (IS_URL_CHAR(ch)) {
607  return s;
608  }
609 
610  switch (ch) {
611  case '?':
612  case '#':
613  return s;
614  }
615 
616  break;
617 
618  default:
619  break;
620  }
621 
622  /* We should never fall out of the switch above unless there's an error */
623  return s_dead;
624 }
625 
627  const http_parser_settings *settings,
628  const char *data,
629  size_t len)
630 {
631  char c, ch;
632  int8_t unhex_val;
633  const char *p = data;
634  const char *header_field_mark = 0;
635  const char *header_value_mark = 0;
636  const char *url_mark = 0;
637  const char *body_mark = 0;
638  const char *status_mark = 0;
639  enum state p_state = (enum state) parser->state;
640 
641  /* We're in an error state. Don't bother doing anything. */
642  if (HTTP_PARSER_ERRNO(parser) != HPE_OK) {
643  return 0;
644  }
645 
646  if (len == 0) {
647  switch (CURRENT_STATE()) {
648  case s_body_identity_eof:
649  /* Use of CALLBACK_NOTIFY() here would erroneously return 1 byte read if
650  * we got paused.
651  */
652  CALLBACK_NOTIFY_NOADVANCE(message_complete);
653  return 0;
654 
655  case s_dead:
656  case s_start_req_or_res:
657  case s_start_res:
658  case s_start_req:
659  return 0;
660 
661  default:
662  SET_ERRNO(HPE_INVALID_EOF_STATE);
663  return 1;
664  }
665  }
666 
667 
668  if (CURRENT_STATE() == s_header_field)
669  header_field_mark = data;
670  if (CURRENT_STATE() == s_header_value)
671  header_value_mark = data;
672  switch (CURRENT_STATE()) {
673  case s_req_path:
674  case s_req_schema:
675  case s_req_schema_slash:
677  case s_req_server_start:
678  case s_req_server:
681  case s_req_query_string:
683  case s_req_fragment:
684  url_mark = data;
685  break;
686  case s_res_status:
687  status_mark = data;
688  break;
689  default:
690  break;
691  }
692 
693  for (p=data; p != data + len; p++) {
694  ch = *p;
695 
698 
699 reexecute:
700  switch (CURRENT_STATE()) {
701 
702  case s_dead:
703  /* this state is used after a 'Connection: close' message
704  * the parser will error out if it reads another message
705  */
706  if (LIKELY(ch == CR || ch == LF))
707  break;
708 
709  SET_ERRNO(HPE_CLOSED_CONNECTION);
710  goto error;
711 
712  case s_start_req_or_res:
713  {
714  if (ch == CR || ch == LF)
715  break;
716  parser->flags = 0;
717  parser->content_length = ULLONG_MAX;
718 
719  if (ch == 'H') {
721 
722  CALLBACK_NOTIFY(message_begin);
723  } else {
724  parser->type = HTTP_REQUEST;
726  REEXECUTE();
727  }
728 
729  break;
730  }
731 
732  case s_res_or_resp_H:
733  if (ch == 'T') {
734  parser->type = HTTP_RESPONSE;
736  } else {
737  if (UNLIKELY(ch != 'E')) {
738  SET_ERRNO(HPE_INVALID_CONSTANT);
739  goto error;
740  }
741 
742  parser->type = HTTP_REQUEST;
743  parser->method = HTTP_HEAD;
744  parser->index = 2;
746  }
747  break;
748 
749  case s_start_res:
750  {
751  parser->flags = 0;
752  parser->content_length = ULLONG_MAX;
753 
754  switch (ch) {
755  case 'H':
757  break;
758 
759  case CR:
760  case LF:
761  break;
762 
763  default:
764  SET_ERRNO(HPE_INVALID_CONSTANT);
765  goto error;
766  }
767 
768  CALLBACK_NOTIFY(message_begin);
769  break;
770  }
771 
772  case s_res_H:
773  STRICT_CHECK(ch != 'T');
775  break;
776 
777  case s_res_HT:
778  STRICT_CHECK(ch != 'T');
780  break;
781 
782  case s_res_HTT:
783  STRICT_CHECK(ch != 'P');
785  break;
786 
787  case s_res_HTTP:
788  STRICT_CHECK(ch != '/');
790  break;
791 
793  if (UNLIKELY(ch < '0' || ch > '9')) {
794  SET_ERRNO(HPE_INVALID_VERSION);
795  goto error;
796  }
797 
798  parser->http_major = ch - '0';
800  break;
801 
802  /* major HTTP version or dot */
803  case s_res_http_major:
804  {
805  if (ch == '.') {
807  break;
808  }
809 
810  if (!IS_NUM(ch)) {
811  SET_ERRNO(HPE_INVALID_VERSION);
812  goto error;
813  }
814 
815  parser->http_major *= 10;
816  parser->http_major += ch - '0';
817 
818  if (UNLIKELY(parser->http_major > 999)) {
819  SET_ERRNO(HPE_INVALID_VERSION);
820  goto error;
821  }
822 
823  break;
824  }
825 
826  /* first digit of minor HTTP version */
828  if (UNLIKELY(!IS_NUM(ch))) {
829  SET_ERRNO(HPE_INVALID_VERSION);
830  goto error;
831  }
832 
833  parser->http_minor = ch - '0';
835  break;
836 
837  /* minor HTTP version or end of request line */
838  case s_res_http_minor:
839  {
840  if (ch == ' ') {
842  break;
843  }
844 
845  if (UNLIKELY(!IS_NUM(ch))) {
846  SET_ERRNO(HPE_INVALID_VERSION);
847  goto error;
848  }
849 
850  parser->http_minor *= 10;
851  parser->http_minor += ch - '0';
852 
853  if (UNLIKELY(parser->http_minor > 999)) {
854  SET_ERRNO(HPE_INVALID_VERSION);
855  goto error;
856  }
857 
858  break;
859  }
860 
862  {
863  if (!IS_NUM(ch)) {
864  if (ch == ' ') {
865  break;
866  }
867 
868  SET_ERRNO(HPE_INVALID_STATUS);
869  goto error;
870  }
871  parser->status_code = ch - '0';
873  break;
874  }
875 
876  case s_res_status_code:
877  {
878  if (!IS_NUM(ch)) {
879  switch (ch) {
880  case ' ':
882  break;
883  case CR:
885  break;
886  case LF:
888  break;
889  default:
890  SET_ERRNO(HPE_INVALID_STATUS);
891  goto error;
892  }
893  break;
894  }
895 
896  parser->status_code *= 10;
897  parser->status_code += ch - '0';
898 
899  if (UNLIKELY(parser->status_code > 999)) {
900  SET_ERRNO(HPE_INVALID_STATUS);
901  goto error;
902  }
903 
904  break;
905  }
906 
907  case s_res_status_start:
908  {
909  if (ch == CR) {
911  break;
912  }
913 
914  if (ch == LF) {
916  break;
917  }
918 
919  MARK(status);
921  parser->index = 0;
922  break;
923  }
924 
925  case s_res_status:
926  if (ch == CR) {
928  CALLBACK_DATA(status);
929  break;
930  }
931 
932  if (ch == LF) {
934  CALLBACK_DATA(status);
935  break;
936  }
937 
938  break;
939 
941  STRICT_CHECK(ch != LF);
943  break;
944 
945  case s_start_req:
946  {
947  if (ch == CR || ch == LF)
948  break;
949  parser->flags = 0;
950  parser->content_length = ULLONG_MAX;
951 
952  if (UNLIKELY(!IS_ALPHA(ch))) {
953  SET_ERRNO(HPE_INVALID_METHOD);
954  goto error;
955  }
956 
957  parser->method = (enum http_method) 0;
958  parser->index = 1;
959  switch (ch) {
960  case 'C': parser->method = HTTP_CONNECT; /* or COPY, CHECKOUT */ break;
961  case 'D': parser->method = HTTP_DELETE; break;
962  case 'G': parser->method = HTTP_GET; break;
963  case 'H': parser->method = HTTP_HEAD; break;
964  case 'L': parser->method = HTTP_LOCK; break;
965  case 'M': parser->method = HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE, M-SEARCH, MKCALENDAR */ break;
966  case 'N': parser->method = HTTP_NOTIFY; break;
967  case 'O': parser->method = HTTP_OPTIONS; break;
968  case 'P': parser->method = HTTP_POST;
969  /* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */
970  break;
971  case 'R': parser->method = HTTP_REPORT; break;
972  case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH */ break;
973  case 'T': parser->method = HTTP_TRACE; break;
974  case 'U': parser->method = HTTP_UNLOCK; /* or UNSUBSCRIBE */ break;
975  default:
976  SET_ERRNO(HPE_INVALID_METHOD);
977  goto error;
978  }
980 
981  CALLBACK_NOTIFY(message_begin);
982 
983  break;
984  }
985 
986  case s_req_method:
987  {
988  const char *matcher;
989  if (UNLIKELY(ch == '\0')) {
990  SET_ERRNO(HPE_INVALID_METHOD);
991  goto error;
992  }
993 
994  matcher = method_strings[parser->method];
995  if (ch == ' ' && matcher[parser->index] == '\0') {
997  } else if (ch == matcher[parser->index]) {
998  ; /* nada */
999  } else if (parser->method == HTTP_CONNECT) {
1000  if (parser->index == 1 && ch == 'H') {
1001  parser->method = HTTP_CHECKOUT;
1002  } else if (parser->index == 2 && ch == 'P') {
1003  parser->method = HTTP_COPY;
1004  } else {
1005  SET_ERRNO(HPE_INVALID_METHOD);
1006  goto error;
1007  }
1008  } else if (parser->method == HTTP_MKCOL) {
1009  if (parser->index == 1 && ch == 'O') {
1010  parser->method = HTTP_MOVE;
1011  } else if (parser->index == 1 && ch == 'E') {
1012  parser->method = HTTP_MERGE;
1013  } else if (parser->index == 1 && ch == '-') {
1014  parser->method = HTTP_MSEARCH;
1015  } else if (parser->index == 2 && ch == 'A') {
1016  parser->method = HTTP_MKACTIVITY;
1017  } else if (parser->index == 3 && ch == 'A') {
1018  parser->method = HTTP_MKCALENDAR;
1019  } else {
1020  SET_ERRNO(HPE_INVALID_METHOD);
1021  goto error;
1022  }
1023  } else if (parser->method == HTTP_SUBSCRIBE) {
1024  if (parser->index == 1 && ch == 'E') {
1025  parser->method = HTTP_SEARCH;
1026  } else {
1027  SET_ERRNO(HPE_INVALID_METHOD);
1028  goto error;
1029  }
1030  } else if (parser->index == 1 && parser->method == HTTP_POST) {
1031  if (ch == 'R') {
1032  parser->method = HTTP_PROPFIND; /* or HTTP_PROPPATCH */
1033  } else if (ch == 'U') {
1034  parser->method = HTTP_PUT; /* or HTTP_PURGE */
1035  } else if (ch == 'A') {
1036  parser->method = HTTP_PATCH;
1037  } else {
1038  SET_ERRNO(HPE_INVALID_METHOD);
1039  goto error;
1040  }
1041  } else if (parser->index == 2) {
1042  if (parser->method == HTTP_PUT) {
1043  if (ch == 'R') {
1044  parser->method = HTTP_PURGE;
1045  } else {
1046  SET_ERRNO(HPE_INVALID_METHOD);
1047  goto error;
1048  }
1049  } else if (parser->method == HTTP_UNLOCK) {
1050  if (ch == 'S') {
1051  parser->method = HTTP_UNSUBSCRIBE;
1052  } else {
1053  SET_ERRNO(HPE_INVALID_METHOD);
1054  goto error;
1055  }
1056  } else {
1057  SET_ERRNO(HPE_INVALID_METHOD);
1058  goto error;
1059  }
1060  } else if (parser->index == 4 && parser->method == HTTP_PROPFIND && ch == 'P') {
1061  parser->method = HTTP_PROPPATCH;
1062  } else {
1063  SET_ERRNO(HPE_INVALID_METHOD);
1064  goto error;
1065  }
1066 
1067  ++parser->index;
1068  break;
1069  }
1070 
1072  {
1073  if (ch == ' ') break;
1074 
1075  MARK(url);
1076  if (parser->method == HTTP_CONNECT) {
1078  }
1079 
1081  if (UNLIKELY(CURRENT_STATE() == s_dead)) {
1082  SET_ERRNO(HPE_INVALID_URL);
1083  goto error;
1084  }
1085 
1086  break;
1087  }
1088 
1089  case s_req_schema:
1090  case s_req_schema_slash:
1092  case s_req_server_start:
1093  {
1094  switch (ch) {
1095  /* No whitespace allowed here */
1096  case ' ':
1097  case CR:
1098  case LF:
1099  SET_ERRNO(HPE_INVALID_URL);
1100  goto error;
1101  default:
1103  if (UNLIKELY(CURRENT_STATE() == s_dead)) {
1104  SET_ERRNO(HPE_INVALID_URL);
1105  goto error;
1106  }
1107  }
1108 
1109  break;
1110  }
1111 
1112  case s_req_server:
1113  case s_req_server_with_at:
1114  case s_req_path:
1116  case s_req_query_string:
1117  case s_req_fragment_start:
1118  case s_req_fragment:
1119  {
1120  switch (ch) {
1121  case ' ':
1123  CALLBACK_DATA(url);
1124  break;
1125  case CR:
1126  case LF:
1127  parser->http_major = 0;
1128  parser->http_minor = 9;
1129  UPDATE_STATE((ch == CR) ?
1132  CALLBACK_DATA(url);
1133  break;
1134  default:
1136  if (UNLIKELY(CURRENT_STATE() == s_dead)) {
1137  SET_ERRNO(HPE_INVALID_URL);
1138  goto error;
1139  }
1140  }
1141  break;
1142  }
1143 
1144  case s_req_http_start:
1145  switch (ch) {
1146  case 'H':
1148  break;
1149  case ' ':
1150  break;
1151  default:
1152  SET_ERRNO(HPE_INVALID_CONSTANT);
1153  goto error;
1154  }
1155  break;
1156 
1157  case s_req_http_H:
1158  STRICT_CHECK(ch != 'T');
1160  break;
1161 
1162  case s_req_http_HT:
1163  STRICT_CHECK(ch != 'T');
1165  break;
1166 
1167  case s_req_http_HTT:
1168  STRICT_CHECK(ch != 'P');
1170  break;
1171 
1172  case s_req_http_HTTP:
1173  STRICT_CHECK(ch != '/');
1175  break;
1176 
1177  /* first digit of major HTTP version */
1179  if (UNLIKELY(ch < '1' || ch > '9')) {
1180  SET_ERRNO(HPE_INVALID_VERSION);
1181  goto error;
1182  }
1183 
1184  parser->http_major = ch - '0';
1186  break;
1187 
1188  /* major HTTP version or dot */
1189  case s_req_http_major:
1190  {
1191  if (ch == '.') {
1193  break;
1194  }
1195 
1196  if (UNLIKELY(!IS_NUM(ch))) {
1197  SET_ERRNO(HPE_INVALID_VERSION);
1198  goto error;
1199  }
1200 
1201  parser->http_major *= 10;
1202  parser->http_major += ch - '0';
1203 
1204  if (UNLIKELY(parser->http_major > 999)) {
1205  SET_ERRNO(HPE_INVALID_VERSION);
1206  goto error;
1207  }
1208 
1209  break;
1210  }
1211 
1212  /* first digit of minor HTTP version */
1214  if (UNLIKELY(!IS_NUM(ch))) {
1215  SET_ERRNO(HPE_INVALID_VERSION);
1216  goto error;
1217  }
1218 
1219  parser->http_minor = ch - '0';
1221  break;
1222 
1223  /* minor HTTP version or end of request line */
1224  case s_req_http_minor:
1225  {
1226  if (ch == CR) {
1228  break;
1229  }
1230 
1231  if (ch == LF) {
1233  break;
1234  }
1235 
1236  /* XXX allow spaces after digit? */
1237 
1238  if (UNLIKELY(!IS_NUM(ch))) {
1239  SET_ERRNO(HPE_INVALID_VERSION);
1240  goto error;
1241  }
1242 
1243  parser->http_minor *= 10;
1244  parser->http_minor += ch - '0';
1245 
1246  if (UNLIKELY(parser->http_minor > 999)) {
1247  SET_ERRNO(HPE_INVALID_VERSION);
1248  goto error;
1249  }
1250 
1251  break;
1252  }
1253 
1254  /* end of request line */
1256  {
1257  if (UNLIKELY(ch != LF)) {
1258  SET_ERRNO(HPE_LF_EXPECTED);
1259  goto error;
1260  }
1261 
1263  break;
1264  }
1265 
1266  case s_header_field_start:
1267  {
1268  if (ch == CR) {
1270  break;
1271  }
1272 
1273  if (ch == LF) {
1274  /* they might be just sending \n instead of \r\n so this would be
1275  * the second \n to denote the end of headers*/
1277  REEXECUTE();
1278  }
1279 
1280  c = TOKEN(ch);
1281 
1282  if (UNLIKELY(!c)) {
1283  SET_ERRNO(HPE_INVALID_HEADER_TOKEN);
1284  goto error;
1285  }
1286 
1287  MARK(header_field);
1288 
1289  parser->index = 0;
1291 
1292  switch (c) {
1293  case 'c':
1294  parser->header_state = h_C;
1295  break;
1296 
1297  case 'p':
1299  break;
1300 
1301  case 't':
1303  break;
1304 
1305  case 'u':
1306  parser->header_state = h_matching_upgrade;
1307  break;
1308 
1309  default:
1310  parser->header_state = h_general;
1311  break;
1312  }
1313  break;
1314  }
1315 
1316  case s_header_field:
1317  {
1318  const char* start = p;
1319  for (; p != data + len; p++) {
1320  ch = *p;
1321  c = TOKEN(ch);
1322 
1323  if (!c)
1324  break;
1325 
1326  switch (parser->header_state) {
1327  case h_general:
1328  break;
1329 
1330  case h_C:
1331  parser->index++;
1332  parser->header_state = (c == 'o' ? h_CO : h_general);
1333  break;
1334 
1335  case h_CO:
1336  parser->index++;
1337  parser->header_state = (c == 'n' ? h_CON : h_general);
1338  break;
1339 
1340  case h_CON:
1341  parser->index++;
1342  switch (c) {
1343  case 'n':
1345  break;
1346  case 't':
1348  break;
1349  default:
1350  parser->header_state = h_general;
1351  break;
1352  }
1353  break;
1354 
1355  /* connection */
1356 
1357  case h_matching_connection:
1358  parser->index++;
1359  if (parser->index > sizeof(CONNECTION)-1
1360  || c != CONNECTION[parser->index]) {
1361  parser->header_state = h_general;
1362  } else if (parser->index == sizeof(CONNECTION)-2) {
1363  parser->header_state = h_connection;
1364  }
1365  break;
1366 
1367  /* proxy-connection */
1368 
1370  parser->index++;
1371  if (parser->index > sizeof(PROXY_CONNECTION)-1
1372  || c != PROXY_CONNECTION[parser->index]) {
1373  parser->header_state = h_general;
1374  } else if (parser->index == sizeof(PROXY_CONNECTION)-2) {
1375  parser->header_state = h_connection;
1376  }
1377  break;
1378 
1379  /* content-length */
1380 
1382  parser->index++;
1383  if (parser->index > sizeof(CONTENT_LENGTH)-1
1384  || c != CONTENT_LENGTH[parser->index]) {
1385  parser->header_state = h_general;
1386  } else if (parser->index == sizeof(CONTENT_LENGTH)-2) {
1387  parser->header_state = h_content_length;
1388  }
1389  break;
1390 
1391  /* transfer-encoding */
1392 
1394  parser->index++;
1395  if (parser->index > sizeof(TRANSFER_ENCODING)-1
1396  || c != TRANSFER_ENCODING[parser->index]) {
1397  parser->header_state = h_general;
1398  } else if (parser->index == sizeof(TRANSFER_ENCODING)-2) {
1400  }
1401  break;
1402 
1403  /* upgrade */
1404 
1405  case h_matching_upgrade:
1406  parser->index++;
1407  if (parser->index > sizeof(UPGRADE)-1
1408  || c != UPGRADE[parser->index]) {
1409  parser->header_state = h_general;
1410  } else if (parser->index == sizeof(UPGRADE)-2) {
1411  parser->header_state = h_upgrade;
1412  }
1413  break;
1414 
1415  case h_connection:
1416  case h_content_length:
1417  case h_transfer_encoding:
1418  case h_upgrade:
1419  if (ch != ' ') parser->header_state = h_general;
1420  break;
1421 
1422  default:
1423  assert(0 && "Unknown header_state");
1424  break;
1425  }
1426  }
1427 
1428  COUNT_HEADER_SIZE(p - start);
1429 
1430  if (p == data + len) {
1431  --p;
1432  break;
1433  }
1434 
1435  if (ch == ':') {
1437  CALLBACK_DATA(header_field);
1438  break;
1439  }
1440 
1441  SET_ERRNO(HPE_INVALID_HEADER_TOKEN);
1442  goto error;
1443  }
1444 
1446  if (ch == ' ' || ch == '\t') break;
1447 
1448  if (ch == CR) {
1450  break;
1451  }
1452 
1453  if (ch == LF) {
1455  break;
1456  }
1457 
1458  /* FALLTHROUGH */
1459 
1460  case s_header_value_start:
1461  {
1462  MARK(header_value);
1463 
1465  parser->index = 0;
1466 
1467  c = LOWER(ch);
1468 
1469  switch (parser->header_state) {
1470  case h_upgrade:
1471  parser->flags |= F_UPGRADE;
1472  parser->header_state = h_general;
1473  break;
1474 
1475  case h_transfer_encoding:
1476  /* looking for 'Transfer-Encoding: chunked' */
1477  if ('c' == c) {
1479  } else {
1480  parser->header_state = h_general;
1481  }
1482  break;
1483 
1484  case h_content_length:
1485  if (UNLIKELY(!IS_NUM(ch))) {
1486  SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
1487  goto error;
1488  }
1489 
1490  parser->content_length = ch - '0';
1491  break;
1492 
1493  case h_connection:
1494  /* looking for 'Connection: keep-alive' */
1495  if (c == 'k') {
1497  /* looking for 'Connection: close' */
1498  } else if (c == 'c') {
1500  } else if (c == 'u') {
1502  } else {
1504  }
1505  break;
1506 
1507  /* Multi-value `Connection` header */
1509  break;
1510 
1511  default:
1512  parser->header_state = h_general;
1513  break;
1514  }
1515  break;
1516  }
1517 
1518  case s_header_value:
1519  {
1520  const char* start = p;
1521  enum header_states h_state = (enum header_states) parser->header_state;
1522  for (; p != data + len; p++) {
1523  ch = *p;
1524  if (ch == CR) {
1526  parser->header_state = h_state;
1527  CALLBACK_DATA(header_value);
1528  break;
1529  }
1530 
1531  if (ch == LF) {
1533  COUNT_HEADER_SIZE(p - start);
1534  parser->header_state = h_state;
1535  CALLBACK_DATA_NOADVANCE(header_value);
1536  REEXECUTE();
1537  }
1538 
1539  c = LOWER(ch);
1540 
1541  switch (h_state) {
1542  case h_general:
1543  {
1544  const char* p_cr;
1545  const char* p_lf;
1546  size_t limit = data + len - p;
1547 
1548  limit = MIN(limit, HTTP_MAX_HEADER_SIZE);
1549 
1550  p_cr = (const char*) memchr(p, CR, limit);
1551  p_lf = (const char*) memchr(p, LF, limit);
1552  if (p_cr != NULL) {
1553  if (p_lf != NULL && p_cr >= p_lf)
1554  p = p_lf;
1555  else
1556  p = p_cr;
1557  } else if (UNLIKELY(p_lf != NULL)) {
1558  p = p_lf;
1559  } else {
1560  p = data + len;
1561  }
1562  --p;
1563 
1564  break;
1565  }
1566 
1567  case h_connection:
1568  case h_transfer_encoding:
1569  assert(0 && "Shouldn't get here.");
1570  break;
1571 
1572  case h_content_length:
1573  {
1574  uint64_t t;
1575 
1576  if (ch == ' ') break;
1577 
1578  if (UNLIKELY(!IS_NUM(ch))) {
1579  SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
1580  parser->header_state = h_state;
1581  goto error;
1582  }
1583 
1584  t = parser->content_length;
1585  t *= 10;
1586  t += ch - '0';
1587 
1588  /* Overflow? Test against a conservative limit for simplicity. */
1589  if (UNLIKELY((ULLONG_MAX - 10) / 10 < parser->content_length)) {
1590  SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
1591  parser->header_state = h_state;
1592  goto error;
1593  }
1594 
1595  parser->content_length = t;
1596  break;
1597  }
1598 
1599  /* Transfer-Encoding: chunked */
1601  parser->index++;
1602  if (parser->index > sizeof(CHUNKED)-1
1603  || c != CHUNKED[parser->index]) {
1604  h_state = h_general;
1605  } else if (parser->index == sizeof(CHUNKED)-2) {
1606  h_state = h_transfer_encoding_chunked;
1607  }
1608  break;
1609 
1611  /* looking for 'Connection: keep-alive' */
1612  if (c == 'k') {
1614  /* looking for 'Connection: close' */
1615  } else if (c == 'c') {
1616  h_state = h_matching_connection_close;
1617  } else if (c == 'u') {
1619  } else if (STRICT_TOKEN(c)) {
1620  h_state = h_matching_connection_token;
1621  } else if (c == ' ' || c == '\t') {
1622  /* Skip lws */
1623  } else {
1624  h_state = h_general;
1625  }
1626  break;
1627 
1628  /* looking for 'Connection: keep-alive' */
1630  parser->index++;
1631  if (parser->index > sizeof(KEEP_ALIVE)-1
1632  || c != KEEP_ALIVE[parser->index]) {
1633  h_state = h_matching_connection_token;
1634  } else if (parser->index == sizeof(KEEP_ALIVE)-2) {
1635  h_state = h_connection_keep_alive;
1636  }
1637  break;
1638 
1639  /* looking for 'Connection: close' */
1641  parser->index++;
1642  if (parser->index > sizeof(CLOSE)-1 || c != CLOSE[parser->index]) {
1643  h_state = h_matching_connection_token;
1644  } else if (parser->index == sizeof(CLOSE)-2) {
1645  h_state = h_connection_close;
1646  }
1647  break;
1648 
1649  /* looking for 'Connection: upgrade' */
1651  parser->index++;
1652  if (parser->index > sizeof(UPGRADE) - 1 ||
1653  c != UPGRADE[parser->index]) {
1654  h_state = h_matching_connection_token;
1655  } else if (parser->index == sizeof(UPGRADE)-2) {
1656  h_state = h_connection_upgrade;
1657  }
1658  break;
1659 
1661  if (ch == ',') {
1663  parser->index = 0;
1664  }
1665  break;
1666 
1668  if (ch != ' ') h_state = h_general;
1669  break;
1670 
1672  case h_connection_close:
1673  case h_connection_upgrade:
1674  if (ch == ',') {
1675  if (h_state == h_connection_keep_alive) {
1676  parser->flags |= F_CONNECTION_KEEP_ALIVE;
1677  } else if (h_state == h_connection_close) {
1678  parser->flags |= F_CONNECTION_CLOSE;
1679  } else if (h_state == h_connection_upgrade) {
1680  parser->flags |= F_CONNECTION_UPGRADE;
1681  }
1683  parser->index = 0;
1684  } else if (ch != ' ') {
1685  h_state = h_matching_connection_token;
1686  }
1687  break;
1688 
1689  default:
1691  h_state = h_general;
1692  break;
1693  }
1694  }
1695  parser->header_state = h_state;
1696 
1697  COUNT_HEADER_SIZE(p - start);
1698 
1699  if (p == data + len)
1700  --p;
1701  break;
1702  }
1703 
1704  case s_header_almost_done:
1705  {
1706  STRICT_CHECK(ch != LF);
1707 
1709  break;
1710  }
1711 
1712  case s_header_value_lws:
1713  {
1714  if (ch == ' ' || ch == '\t') {
1716  REEXECUTE();
1717  }
1718 
1719  /* finished the header */
1720  switch (parser->header_state) {
1722  parser->flags |= F_CONNECTION_KEEP_ALIVE;
1723  break;
1724  case h_connection_close:
1725  parser->flags |= F_CONNECTION_CLOSE;
1726  break;
1728  parser->flags |= F_CHUNKED;
1729  break;
1730  case h_connection_upgrade:
1731  parser->flags |= F_CONNECTION_UPGRADE;
1732  break;
1733  default:
1734  break;
1735  }
1736 
1738  REEXECUTE();
1739  }
1740 
1742  {
1743  STRICT_CHECK(ch != LF);
1745  break;
1746  }
1747 
1749  {
1750  if (ch == ' ' || ch == '\t') {
1752  break;
1753  } else {
1754  switch (parser->header_state) {
1756  parser->flags |= F_CONNECTION_KEEP_ALIVE;
1757  break;
1758  case h_connection_close:
1759  parser->flags |= F_CONNECTION_CLOSE;
1760  break;
1761  case h_connection_upgrade:
1762  parser->flags |= F_CONNECTION_UPGRADE;
1763  break;
1765  parser->flags |= F_CHUNKED;
1766  break;
1767  default:
1768  break;
1769  }
1770 
1771  /* header value was empty */
1772  MARK(header_value);
1774  CALLBACK_DATA_NOADVANCE(header_value);
1775  REEXECUTE();
1776  }
1777  }
1778 
1779  case s_headers_almost_done:
1780  {
1781  STRICT_CHECK(ch != LF);
1782 
1783  if (parser->flags & F_TRAILING) {
1784  /* End of a chunked request */
1786  CALLBACK_NOTIFY_NOADVANCE(chunk_complete);
1787  REEXECUTE();
1788  }
1789 
1791 
1792  /* Set this here so that on_headers_complete() callbacks can see it */
1793  parser->upgrade =
1794  ((parser->flags & (F_UPGRADE | F_CONNECTION_UPGRADE)) ==
1796  parser->method == HTTP_CONNECT);
1797 
1798  /* Here we call the headers_complete callback. This is somewhat
1799  * different than other callbacks because if the user returns 1, we
1800  * will interpret that as saying that this message has no body. This
1801  * is needed for the annoying case of recieving a response to a HEAD
1802  * request.
1803  *
1804  * We'd like to use CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so
1805  * we have to simulate it by handling a change in errno below.
1806  */
1807  if (settings->on_headers_complete) {
1808  switch (settings->on_headers_complete(parser)) {
1809  case 0:
1810  break;
1811 
1812  case 1:
1813  parser->flags |= F_SKIPBODY;
1814  break;
1815 
1816  default:
1817  SET_ERRNO(HPE_CB_headers_complete);
1818  RETURN(p - data); /* Error */
1819  }
1820  }
1821 
1822  if (HTTP_PARSER_ERRNO(parser) != HPE_OK) {
1823  RETURN(p - data);
1824  }
1825 
1826  REEXECUTE();
1827  }
1828 
1829  case s_headers_done:
1830  {
1831  STRICT_CHECK(ch != LF);
1832 
1833  parser->nread = 0;
1834 
1835  int hasBody = parser->flags & F_CHUNKED ||
1836  (parser->content_length > 0 && parser->content_length != ULLONG_MAX);
1837  if (parser->upgrade && (parser->method == HTTP_CONNECT ||
1838  (parser->flags & F_SKIPBODY) || !hasBody)) {
1839  /* Exit, the rest of the message is in a different protocol. */
1841  CALLBACK_NOTIFY(message_complete);
1842  RETURN((p - data) + 1);
1843  }
1844 
1845  if (parser->flags & F_SKIPBODY) {
1847  CALLBACK_NOTIFY(message_complete);
1848  } else if (parser->flags & F_CHUNKED) {
1849  /* chunked encoding - ignore Content-Length header */
1851  } else {
1852  if (parser->content_length == 0) {
1853  /* Content-Length header given but zero: Content-Length: 0\r\n */
1855  CALLBACK_NOTIFY(message_complete);
1856  } else if (parser->content_length != ULLONG_MAX) {
1857  /* Content-Length header given and non-zero */
1859  } else {
1860  if (parser->type == HTTP_REQUEST ||
1861  !http_message_needs_eof(parser)) {
1862  /* Assume content-length 0 - read the next */
1864  CALLBACK_NOTIFY(message_complete);
1865  } else {
1866  /* Read body until EOF */
1868  }
1869  }
1870  }
1871 
1872  break;
1873  }
1874 
1875  case s_body_identity:
1876  {
1877  uint64_t to_read = MIN(parser->content_length,
1878  (uint64_t) ((data + len) - p));
1879 
1880  assert(parser->content_length != 0
1881  && parser->content_length != ULLONG_MAX);
1882 
1883  /* The difference between advancing content_length and p is because
1884  * the latter will automaticaly advance on the next loop iteration.
1885  * Further, if content_length ends up at 0, we want to see the last
1886  * byte again for our message complete callback.
1887  */
1888  MARK(body);
1889  parser->content_length -= to_read;
1890  p += to_read - 1;
1891 
1892  if (parser->content_length == 0) {
1894 
1895  /* Mimic CALLBACK_DATA_NOADVANCE() but with one extra byte.
1896  *
1897  * The alternative to doing this is to wait for the next byte to
1898  * trigger the data callback, just as in every other case. The
1899  * problem with this is that this makes it difficult for the test
1900  * harness to distinguish between complete-on-EOF and
1901  * complete-on-length. It's not clear that this distinction is
1902  * important for applications, but let's keep it for now.
1903  */
1904  CALLBACK_DATA_(body, p - body_mark + 1, p - data);
1905  REEXECUTE();
1906  }
1907 
1908  break;
1909  }
1910 
1911  /* read until EOF */
1912  case s_body_identity_eof:
1913  MARK(body);
1914  p = data + len - 1;
1915 
1916  break;
1917 
1918  case s_message_done:
1920  CALLBACK_NOTIFY(message_complete);
1921  if (parser->upgrade) {
1922  /* Exit, the rest of the message is in a different protocol. */
1923  RETURN((p - data) + 1);
1924  }
1925  break;
1926 
1927  case s_chunk_size_start:
1928  {
1929  assert(parser->nread == 1);
1930  assert(parser->flags & F_CHUNKED);
1931 
1932  unhex_val = unhex[(unsigned char)ch];
1933  if (UNLIKELY(unhex_val == -1)) {
1934  SET_ERRNO(HPE_INVALID_CHUNK_SIZE);
1935  goto error;
1936  }
1937 
1938  parser->content_length = unhex_val;
1940  break;
1941  }
1942 
1943  case s_chunk_size:
1944  {
1945  uint64_t t;
1946 
1947  assert(parser->flags & F_CHUNKED);
1948 
1949  if (ch == CR) {
1951  break;
1952  }
1953 
1954  unhex_val = unhex[(unsigned char)ch];
1955 
1956  if (unhex_val == -1) {
1957  if (ch == ';' || ch == ' ') {
1959  break;
1960  }
1961 
1962  SET_ERRNO(HPE_INVALID_CHUNK_SIZE);
1963  goto error;
1964  }
1965 
1966  t = parser->content_length;
1967  t *= 16;
1968  t += unhex_val;
1969 
1970  /* Overflow? Test against a conservative limit for simplicity. */
1971  if (UNLIKELY((ULLONG_MAX - 16) / 16 < parser->content_length)) {
1972  SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
1973  goto error;
1974  }
1975 
1976  parser->content_length = t;
1977  break;
1978  }
1979 
1980  case s_chunk_parameters:
1981  {
1982  assert(parser->flags & F_CHUNKED);
1983  /* just ignore this shit. TODO check for overflow */
1984  if (ch == CR) {
1986  break;
1987  }
1988  break;
1989  }
1990 
1992  {
1993  assert(parser->flags & F_CHUNKED);
1994  STRICT_CHECK(ch != LF);
1995 
1996  parser->nread = 0;
1997 
1998  if (parser->content_length == 0) {
1999  parser->flags |= F_TRAILING;
2001  } else {
2003  }
2004  CALLBACK_NOTIFY(chunk_header);
2005  break;
2006  }
2007 
2008  case s_chunk_data:
2009  {
2010  uint64_t to_read = MIN(parser->content_length,
2011  (uint64_t) ((data + len) - p));
2012 
2013  assert(parser->flags & F_CHUNKED);
2014  assert(parser->content_length != 0
2015  && parser->content_length != ULLONG_MAX);
2016 
2017  /* See the explanation in s_body_identity for why the content
2018  * length and data pointers are managed this way.
2019  */
2020  MARK(body);
2021  parser->content_length -= to_read;
2022  p += to_read - 1;
2023 
2024  if (parser->content_length == 0) {
2026  }
2027 
2028  break;
2029  }
2030 
2032  assert(parser->flags & F_CHUNKED);
2033  assert(parser->content_length == 0);
2034  STRICT_CHECK(ch != CR);
2036  CALLBACK_DATA(body);
2037  break;
2038 
2039  case s_chunk_data_done:
2040  assert(parser->flags & F_CHUNKED);
2041  STRICT_CHECK(ch != LF);
2042  parser->nread = 0;
2044  CALLBACK_NOTIFY(chunk_complete);
2045  break;
2046 
2047  default:
2048  assert(0 && "unhandled state");
2049  SET_ERRNO(HPE_INVALID_INTERNAL_STATE);
2050  goto error;
2051  }
2052  }
2053 
2054  /* Run callbacks for any marks that we have leftover after we ran our of
2055  * bytes. There should be at most one of these set, so it's OK to invoke
2056  * them in series (unset marks will not result in callbacks).
2057  *
2058  * We use the NOADVANCE() variety of callbacks here because 'p' has already
2059  * overflowed 'data' and this allows us to correct for the off-by-one that
2060  * we'd otherwise have (since CALLBACK_DATA() is meant to be run with a 'p'
2061  * value that's in-bounds).
2062  */
2063 
2064  assert(((header_field_mark ? 1 : 0) +
2065  (header_value_mark ? 1 : 0) +
2066  (url_mark ? 1 : 0) +
2067  (body_mark ? 1 : 0) +
2068  (status_mark ? 1 : 0)) <= 1);
2069 
2070  CALLBACK_DATA_NOADVANCE(header_field);
2071  CALLBACK_DATA_NOADVANCE(header_value);
2074  CALLBACK_DATA_NOADVANCE(status);
2075 
2076  RETURN(len);
2077 
2078 error:
2079  if (HTTP_PARSER_ERRNO(parser) == HPE_OK) {
2080  SET_ERRNO(HPE_UNKNOWN);
2081  }
2082 
2083  RETURN(p - data);
2084 }
2085 
2086 
2087 /* Does the parser need to see an EOF to find the end of the message? */
2088 int
2090 {
2091  if (parser->type == HTTP_REQUEST) {
2092  return 0;
2093  }
2094 
2095  /* See RFC 2616 section 4.4 */
2096  if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */
2097  parser->status_code == 204 || /* No Content */
2098  parser->status_code == 304 || /* Not Modified */
2099  parser->flags & F_SKIPBODY) { /* response to a HEAD request */
2100  return 0;
2101  }
2102 
2103  if ((parser->flags & F_CHUNKED) || parser->content_length != ULLONG_MAX) {
2104  return 0;
2105  }
2106 
2107  return 1;
2108 }
2109 
2110 
2111 int
2113 {
2114  if (parser->http_major > 0 && parser->http_minor > 0) {
2115  /* HTTP/1.1 */
2116  if (parser->flags & F_CONNECTION_CLOSE) {
2117  return 0;
2118  }
2119  } else {
2120  /* HTTP/1.0 or earlier */
2121  if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) {
2122  return 0;
2123  }
2124  }
2125 
2126  return !http_message_needs_eof(parser);
2127 }
2128 
2129 
2130 const char *
2132 {
2133  return ELEM_AT(method_strings, m, "<unknown>");
2134 }
2135 
2136 
2137 void
2139 {
2140  void *data = parser->data; /* preserve application data */
2141  memset(parser, 0, sizeof(*parser));
2142  parser->data = data;
2143  parser->type = t;
2145  parser->http_errno = HPE_OK;
2146 }
2147 
2148 void
2150 {
2151  memset(settings, 0, sizeof(*settings));
2152 }
2153 
2154 const char *
2156  assert(((size_t) err) <
2157  (sizeof(http_strerror_tab) / sizeof(http_strerror_tab[0])));
2158  return http_strerror_tab[err].name;
2159 }
2160 
2161 const char *
2163  assert(((size_t) err) <
2164  (sizeof(http_strerror_tab) / sizeof(http_strerror_tab[0])));
2165  return http_strerror_tab[err].description;
2166 }
2167 
2168 static enum http_host_state
2169 http_parse_host_char(enum http_host_state s, const char ch) {
2170  switch(s) {
2171  case s_http_userinfo:
2172  case s_http_userinfo_start:
2173  if (ch == '@') {
2174  return s_http_host_start;
2175  }
2176 
2177  if (IS_USERINFO_CHAR(ch)) {
2178  return s_http_userinfo;
2179  }
2180  break;
2181 
2182  case s_http_host_start:
2183  if (ch == '[') {
2184  return s_http_host_v6_start;
2185  }
2186 
2187  if (IS_HOST_CHAR(ch)) {
2188  return s_http_host;
2189  }
2190 
2191  break;
2192 
2193  case s_http_host:
2194  if (IS_HOST_CHAR(ch)) {
2195  return s_http_host;
2196  }
2197 
2198  /* FALLTHROUGH */
2199  case s_http_host_v6_end:
2200  if (ch == ':') {
2201  return s_http_host_port_start;
2202  }
2203 
2204  break;
2205 
2206  case s_http_host_v6:
2207  if (ch == ']') {
2208  return s_http_host_v6_end;
2209  }
2210 
2211  /* FALLTHROUGH */
2212  case s_http_host_v6_start:
2213  if (IS_HEX(ch) || ch == ':' || ch == '.') {
2214  return s_http_host_v6;
2215  }
2216 
2217  break;
2218 
2219  case s_http_host_port:
2221  if (IS_NUM(ch)) {
2222  return s_http_host_port;
2223  }
2224 
2225  break;
2226 
2227  default:
2228  break;
2229  }
2230  return s_http_host_dead;
2231 }
2232 
2233 static int
2234 http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
2235  enum http_host_state s;
2236 
2237  const char *p;
2238  size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len;
2239 
2240  u->field_data[UF_HOST].len = 0;
2241 
2242  s = found_at ? s_http_userinfo_start : s_http_host_start;
2243 
2244  for (p = buf + u->field_data[UF_HOST].off; p < buf + buflen; p++) {
2245  enum http_host_state new_s = http_parse_host_char(s, *p);
2246 
2247  if (new_s == s_http_host_dead) {
2248  return 1;
2249  }
2250 
2251  switch(new_s) {
2252  case s_http_host:
2253  if (s != s_http_host) {
2254  u->field_data[UF_HOST].off = p - buf;
2255  }
2256  u->field_data[UF_HOST].len++;
2257  break;
2258 
2259  case s_http_host_v6:
2260  if (s != s_http_host_v6) {
2261  u->field_data[UF_HOST].off = p - buf;
2262  }
2263  u->field_data[UF_HOST].len++;
2264  break;
2265 
2266  case s_http_host_port:
2267  if (s != s_http_host_port) {
2268  u->field_data[UF_PORT].off = p - buf;
2269  u->field_data[UF_PORT].len = 0;
2270  u->field_set |= (1 << UF_PORT);
2271  }
2272  u->field_data[UF_PORT].len++;
2273  break;
2274 
2275  case s_http_userinfo:
2276  if (s != s_http_userinfo) {
2277  u->field_data[UF_USERINFO].off = p - buf ;
2278  u->field_data[UF_USERINFO].len = 0;
2279  u->field_set |= (1 << UF_USERINFO);
2280  }
2281  u->field_data[UF_USERINFO].len++;
2282  break;
2283 
2284  default:
2285  break;
2286  }
2287  s = new_s;
2288  }
2289 
2290  /* Make sure we don't end somewhere unexpected */
2291  switch (s) {
2292  case s_http_host_start:
2293  case s_http_host_v6_start:
2294  case s_http_host_v6:
2296  case s_http_userinfo:
2297  case s_http_userinfo_start:
2298  return 1;
2299  default:
2300  break;
2301  }
2302 
2303  return 0;
2304 }
2305 
2306 int
2307 http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
2308  struct http_parser_url *u)
2309 {
2310  enum state s;
2311  const char *p;
2312  enum http_parser_url_fields uf, old_uf;
2313  int found_at = 0;
2314 
2315  u->port = u->field_set = 0;
2316  s = is_connect ? s_req_server_start : s_req_spaces_before_url;
2317  old_uf = UF_MAX;
2318 
2319  for (p = buf; p < buf + buflen; p++) {
2320  s = parse_url_char(s, *p);
2321 
2322  /* Figure out the next field that we're operating on */
2323  switch (s) {
2324  case s_dead:
2325  return 1;
2326 
2327  /* Skip delimeters */
2328  case s_req_schema_slash:
2330  case s_req_server_start:
2332  case s_req_fragment_start:
2333  continue;
2334 
2335  case s_req_schema:
2336  uf = UF_SCHEMA;
2337  break;
2338 
2339  case s_req_server_with_at:
2340  found_at = 1;
2341  uf = UF_HOST;
2342  break;
2343 
2344  case s_req_server:
2345  uf = UF_HOST;
2346  break;
2347 
2348  case s_req_path:
2349  uf = UF_PATH;
2350  break;
2351 
2352  case s_req_query_string:
2353  uf = UF_QUERY;
2354  break;
2355 
2356  case s_req_fragment:
2357  uf = UF_FRAGMENT;
2358  break;
2359 
2360  default:
2361  assert(!"Unexpected state");
2362  return 1;
2363  }
2364 
2365  /* Nothing's changed; soldier on */
2366  if (uf == old_uf) {
2367  u->field_data[uf].len++;
2368  continue;
2369  }
2370 
2371  u->field_data[uf].off = p - buf;
2372  u->field_data[uf].len = 1;
2373 
2374  u->field_set |= (1 << uf);
2375  old_uf = uf;
2376  }
2377 
2378  /* host must be present if there is a schema */
2379  /* parsing http:///toto will fail */
2380  if ((u->field_set & ((1 << UF_SCHEMA) | (1 << UF_HOST))) != 0) {
2381  if (http_parse_host(buf, u, found_at) != 0) {
2382  return 1;
2383  }
2384  }
2385 
2386  /* CONNECT requests can only contain "hostname:port" */
2387  if (is_connect && u->field_set != ((1 << UF_HOST)|(1 << UF_PORT))) {
2388  return 1;
2389  }
2390 
2391  if (u->field_set & (1 << UF_PORT)) {
2392  /* Don't bother with endp; we've already validated the string */
2393  unsigned long v = strtoul(buf + u->field_data[UF_PORT].off, NULL, 10);
2394 
2395  /* Ports have a max value of 2^16 */
2396  if (v > 0xffff) {
2397  return 1;
2398  }
2399 
2400  u->port = (uint16_t) v;
2401  }
2402 
2403  return 0;
2404 }
2405 
2406 void
2407 http_parser_pause(http_parser *parser, int paused) {
2408  /* Users should only be pausing/unpausing a parser that is not in an error
2409  * state. In non-debug builds, there's not much that we can do about this
2410  * other than ignore it.
2411  */
2412  if (HTTP_PARSER_ERRNO(parser) == HPE_OK ||
2413  HTTP_PARSER_ERRNO(parser) == HPE_PAUSED) {
2414  SET_ERRNO((paused) ? HPE_PAUSED : HPE_OK);
2415  } else {
2416  assert(0 && "Attempting to pause parser in error state");
2417  }
2418 }
2419 
2420 int
2421 http_body_is_final(const struct http_parser *parser) {
2422  return parser->state == s_message_done;
2423 }
2424 
2425 unsigned long
2427  return HTTP_PARSER_VERSION_MAJOR * 0x10000 |
2428  HTTP_PARSER_VERSION_MINOR * 0x00100 |
2429  HTTP_PARSER_VERSION_PATCH * 0x00001;
2430 }
static const int8_t unhex[256]
Definition: http_parser.c:224
static struct @0 http_strerror_tab[]
static const uint8_t normal_url_char[32]
Definition: http_parser.c:243
#define HTTP_MAX_HEADER_SIZE
Definition: http_parser.h:63
#define UNLIKELY(X)
Definition: http_parser.c:75
#define LIKELY(X)
Definition: http_parser.c:74
uint32_t nread
Definition: http_parser.h:214
#define T(v)
Definition: http_parser.c:237
static int http_parse_host(const char *buf, struct http_parser_url *u, int found_at)
Definition: http_parser.c:2234
#define HTTP_ERRNO_MAP(XX)
Definition: http_parser.h:150
unsigned int method
Definition: http_parser.h:221
#define CR
Definition: http_parser.c:408
#define KEEP_ALIVE
Definition: http_parser.c:170
#define CALLBACK_NOTIFY_NOADVANCE(FOR)
Definition: http_parser.c:102
#define IS_ALPHA(c)
Definition: http_parser.c:411
const char * http_errno_name(enum http_errno err)
Definition: http_parser.c:2155
#define CALLBACK_DATA_NOADVANCE(FOR)
Definition: http_parser.c:132
#define CURRENT_STATE()
Definition: http_parser.c:59
http_cb on_headers_complete
Definition: http_parser.h:242
unsigned short http_minor
Definition: http_parser.h:219
#define CONTENT_LENGTH
Definition: http_parser.c:166
#define HTTP_PARSER_VERSION_MAJOR
Definition: http_parser.h:28
#define CONNECTION
Definition: http_parser.c:165
#define PARSING_HEADER(state)
Definition: http_parser.c:360
unsigned int state
Definition: http_parser.h:210
#define HTTP_METHOD_MAP(XX)
Definition: http_parser.h:88
uint16_t field_set
Definition: http_parser.h:273
size_t http_parser_execute(http_parser *parser, const http_parser_settings *settings, const char *data, size_t len)
Definition: http_parser.c:626
#define CALLBACK_DATA(FOR)
Definition: http_parser.c:128
#define TOKEN(c)
Definition: http_parser.c:425
#define CLOSE
Definition: http_parser.c:171
#define ELEM_AT(a, i, v)
Definition: http_parser.c:51
#define MIN(a, b)
Definition: http_parser.c:37
unsigned int status_code
Definition: http_parser.h:220
#define IS_NUM(c)
Definition: http_parser.c:412
void http_parser_settings_init(http_parser_settings *settings)
Definition: http_parser.c:2149
unsigned int header_state
Definition: http_parser.h:211
voidpf void * buf
Definition: ioapi.h:136
unsigned int index
Definition: http_parser.h:212
#define UPDATE_STATE(V)
Definition: http_parser.c:60
#define LOWER(c)
Definition: http_parser.c:410
unsigned int flags
Definition: http_parser.h:209
#define HTTP_PARSER_VERSION_PATCH
Definition: http_parser.h:30
#define TRANSFER_ENCODING
Definition: http_parser.c:167
#define HTTP_STRERROR_GEN(n, s)
Definition: http_parser.c:456
#define RETURN(V)
Definition: http_parser.c:61
const char * http_method_str(enum http_method m)
Definition: http_parser.c:2131
const char * name
Definition: http_parser.c:458
void http_parser_pause(http_parser *parser, int paused)
Definition: http_parser.c:2407
#define MARK(FOR)
Definition: http_parser.c:136
uint64_t content_length
Definition: http_parser.h:215
#define HTTP_PARSER_ERRNO(p)
Definition: http_parser.h:203
static const char * method_strings[]
Definition: http_parser.c:174
int http_parser_parse_url(const char *buf, size_t buflen, int is_connect, struct http_parser_url *u)
Definition: http_parser.c:2307
int http_should_keep_alive(const http_parser *parser)
Definition: http_parser.c:2112
#define HTTP_PARSER_VERSION_MINOR
Definition: http_parser.h:29
#define UPGRADE
Definition: http_parser.c:168
int http_body_is_final(const struct http_parser *parser)
Definition: http_parser.c:2421
unsigned int http_errno
Definition: http_parser.h:222
const char * http_errno_description(enum http_errno err)
Definition: http_parser.c:2162
unsigned short http_major
READ-ONLY.
Definition: http_parser.h:218
#define STRICT_TOKEN(c)
Definition: http_parser.c:422
#define NEW_MESSAGE()
Definition: http_parser.c:448
#define COUNT_HEADER_SIZE(V)
Definition: http_parser.c:154
#define CALLBACK_NOTIFY(FOR)
Definition: http_parser.c:99
int http_message_needs_eof(const http_parser *parser)
Definition: http_parser.c:2089
http_method
Definition: http_parser.h:123
void http_parser_init(http_parser *parser, enum http_parser_type t)
Definition: http_parser.c:2138
#define CHUNKED
Definition: http_parser.c:169
http_parser_type
Definition: http_parser.h:131
#define SET_ERRNO(e)
Definition: http_parser.c:54
#define XX(num, name, string)
unsigned long http_parser_version(void)
Definition: http_parser.c:2426
void * data
PUBLIC.
Definition: http_parser.h:232
#define REEXECUTE()
Definition: http_parser.c:66
#define LF
Definition: http_parser.c:409
http_errno
Definition: http_parser.h:196
static enum state parse_url_char(enum state s, const char ch)
Definition: http_parser.c:479
#define IS_URL_CHAR(c)
Definition: http_parser.c:426
#define IS_HEX(c)
Definition: http_parser.c:414
#define PROXY_CONNECTION
Definition: http_parser.c:164
unsigned int upgrade
Definition: http_parser.h:229
http_host_state
Definition: http_parser.c:393
#define IS_USERINFO_CHAR(c)
Definition: http_parser.c:418
#define STRICT_CHECK(cond)
Definition: http_parser.c:441
#define ULLONG_MAX
Definition: http_parser.c:33
header_states
Definition: http_parser.c:363
#define CALLBACK_DATA_(FOR, LEN, ER)
Definition: http_parser.c:105
struct http_parser_url::@1 field_data[UF_MAX]
unsigned int type
PRIVATE.
Definition: http_parser.h:208
http_parser_url_fields
Definition: http_parser.h:253
#define IS_HOST_CHAR(c)
Definition: http_parser.c:427
static const char tokens[256]
Definition: http_parser.c:189
state
Definition: http_parser.c:279
static enum http_host_state http_parse_host_char(enum http_host_state s, const char ch)
Definition: http_parser.c:2169
const char * description
Definition: http_parser.c:459