efl

Форк
0
/
http_parser.c 
2215 строк · 61.2 Кб
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

60
/* Run the notify callback FOR, returning ER if it fails */
61
#define CALLBACK_NOTIFY_(FOR, ER)                                    \
62
do {                                                                 \
63
  assert(HTTP_PARSER_ERRNO(parser) == HPE_OK);                       \
64
                                                                     \
65
  if (settings->on_##FOR) {                                          \
66
    if (0 != settings->on_##FOR(parser)) {                           \
67
      SET_ERRNO(HPE_CB_##FOR);                                       \
68
    }                                                                \
69
                                                                     \
70
    /* We either errored above or got paused; get out */             \
71
    if (HTTP_PARSER_ERRNO(parser) != HPE_OK) {                       \
72
      return (ER);                                                   \
73
    }                                                                \
74
  }                                                                  \
75
} while (0)
76

77
/* Run the notify callback FOR and consume the current byte */
78
#define CALLBACK_NOTIFY(FOR)            CALLBACK_NOTIFY_(FOR, p - data + 1)
79

80
/* Run the notify callback FOR and don't consume the current byte */
81
#define CALLBACK_NOTIFY_NOADVANCE(FOR)  CALLBACK_NOTIFY_(FOR, p - data)
82

83
/* Run data callback FOR with LEN bytes, returning ER if it fails */
84
#define CALLBACK_DATA_(FOR, LEN, ER)                                 \
85
do {                                                                 \
86
  assert(HTTP_PARSER_ERRNO(parser) == HPE_OK);                       \
87
                                                                     \
88
  if (FOR##_mark) {                                                  \
89
    if (settings->on_##FOR) {                                        \
90
      if (0 != settings->on_##FOR(parser, FOR##_mark, (LEN))) {      \
91
        SET_ERRNO(HPE_CB_##FOR);                                     \
92
      }                                                              \
93
                                                                     \
94
      /* We either errored above or got paused; get out */           \
95
      if (HTTP_PARSER_ERRNO(parser) != HPE_OK) {                     \
96
        return (ER);                                                 \
97
      }                                                              \
98
    }                                                                \
99
    FOR##_mark = NULL;                                               \
100
  }                                                                  \
101
} while (0)
102
  
103
/* Run the data callback FOR and consume the current byte */
104
#define CALLBACK_DATA(FOR)                                           \
105
    CALLBACK_DATA_(FOR, p - FOR##_mark, p - data + 1)
106

107
/* Run the data callback FOR and don't consume the current byte */
108
#define CALLBACK_DATA_NOADVANCE(FOR)                                 \
109
    CALLBACK_DATA_(FOR, p - FOR##_mark, p - data)
110

111
/* Set the mark FOR; non-destructive if mark is already set */
112
#define MARK(FOR)                                                    \
113
do {                                                                 \
114
  if (!FOR##_mark) {                                                 \
115
    FOR##_mark = p;                                                  \
116
  }                                                                  \
117
} while (0)
118

119

120
#define PROXY_CONNECTION "proxy-connection"
121
#define CONNECTION "connection"
122
#define CONTENT_LENGTH "content-length"
123
#define TRANSFER_ENCODING "transfer-encoding"
124
#define UPGRADE "upgrade"
125
#define CHUNKED "chunked"
126
#define KEEP_ALIVE "keep-alive"
127
#define CLOSE "close"
128

129

130
static const char *method_strings[] =
131
  {
132
#define XX(num, name, string) #string,
133
  HTTP_METHOD_MAP(XX)
134
#undef XX
135
  };
136

137

138
/* Tokens as defined by rfc 2616. Also lowercases them.
139
 *        token       = 1*<any CHAR except CTLs or separators>
140
 *     separators     = "(" | ")" | "<" | ">" | "@"
141
 *                    | "," | ";" | ":" | "\" | <">
142
 *                    | "/" | "[" | "]" | "?" | "="
143
 *                    | "{" | "}" | SP | HT
144
 */
145
static const char tokens[256] = {
146
/*   0 nul    1 soh    2 stx    3 etx    4 eot    5 enq    6 ack    7 bel  */
147
        0,       0,       0,       0,       0,       0,       0,       0,
148
/*   8 bs     9 ht    10 nl    11 vt    12 np    13 cr    14 so    15 si   */
149
        0,       0,       0,       0,       0,       0,       0,       0,
150
/*  16 dle   17 dc1   18 dc2   19 dc3   20 dc4   21 nak   22 syn   23 etb */
151
        0,       0,       0,       0,       0,       0,       0,       0,
152
/*  24 can   25 em    26 sub   27 esc   28 fs    29 gs    30 rs    31 us  */
153
        0,       0,       0,       0,       0,       0,       0,       0,
154
/*  32 sp    33  !    34  "    35  #    36  $    37  %    38  &    39  '  */
155
        0,      '!',      0,      '#',     '$',     '%',     '&',    '\'',
156
/*  40  (    41  )    42  *    43  +    44  ,    45  -    46  .    47  /  */
157
        0,       0,      '*',     '+',      0,      '-',     '.',      0,
158
/*  48  0    49  1    50  2    51  3    52  4    53  5    54  6    55  7  */
159
       '0',     '1',     '2',     '3',     '4',     '5',     '6',     '7',
160
/*  56  8    57  9    58  :    59  ;    60  <    61  =    62  >    63  ?  */
161
       '8',     '9',      0,       0,       0,       0,       0,       0,
162
/*  64  @    65  A    66  B    67  C    68  D    69  E    70  F    71  G  */
163
        0,      'a',     'b',     'c',     'd',     'e',     'f',     'g',
164
/*  72  H    73  I    74  J    75  K    76  L    77  M    78  N    79  O  */
165
       'h',     'i',     'j',     'k',     'l',     'm',     'n',     'o',
166
/*  80  P    81  Q    82  R    83  S    84  T    85  U    86  V    87  W  */
167
       'p',     'q',     'r',     's',     't',     'u',     'v',     'w',
168
/*  88  X    89  Y    90  Z    91  [    92  \    93  ]    94  ^    95  _  */
169
       'x',     'y',     'z',      0,       0,       0,      '^',     '_',
170
/*  96  `    97  a    98  b    99  c   100  d   101  e   102  f   103  g  */
171
       '`',     'a',     'b',     'c',     'd',     'e',     'f',     'g',
172
/* 104  h   105  i   106  j   107  k   108  l   109  m   110  n   111  o  */
173
       'h',     'i',     'j',     'k',     'l',     'm',     'n',     'o',
174
/* 112  p   113  q   114  r   115  s   116  t   117  u   118  v   119  w  */
175
       'p',     'q',     'r',     's',     't',     'u',     'v',     'w',
176
/* 120  x   121  y   122  z   123  {   124  |   125  }   126  ~   127 del */
177
       'x',     'y',     'z',      0,      '|',      0,      '~',       0 };
178

179

180
static const int8_t unhex[256] =
181
  {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
182
  ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
183
  ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
184
  , 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1
185
  ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
186
  ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
187
  ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
188
  ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
189
  };
190

191

192
#if HTTP_PARSER_STRICT
193
# define T(v) 0
194
#else
195
# define T(v) v
196
#endif
197

198

199
static const uint8_t normal_url_char[32] = {
200
/*   0 nul    1 soh    2 stx    3 etx    4 eot    5 enq    6 ack    7 bel  */
201
        0    |   0    |   0    |   0    |   0    |   0    |   0    |   0,
202
/*   8 bs     9 ht    10 nl    11 vt    12 np    13 cr    14 so    15 si   */
203
        0    | T(2)   |   0    |   0    | T(16)  |   0    |   0    |   0,
204
/*  16 dle   17 dc1   18 dc2   19 dc3   20 dc4   21 nak   22 syn   23 etb */
205
        0    |   0    |   0    |   0    |   0    |   0    |   0    |   0,
206
/*  24 can   25 em    26 sub   27 esc   28 fs    29 gs    30 rs    31 us  */
207
        0    |   0    |   0    |   0    |   0    |   0    |   0    |   0,
208
/*  32 sp    33  !    34  "    35  #    36  $    37  %    38  &    39  '  */
209
        0    |   2    |   4    |   0    |   16   |   32   |   64   |  128,
210
/*  40  (    41  )    42  *    43  +    44  ,    45  -    46  .    47  /  */
211
        1    |   2    |   4    |   8    |   16   |   32   |   64   |  128,
212
/*  48  0    49  1    50  2    51  3    52  4    53  5    54  6    55  7  */
213
        1    |   2    |   4    |   8    |   16   |   32   |   64   |  128,
214
/*  56  8    57  9    58  :    59  ;    60  <    61  =    62  >    63  ?  */
215
        1    |   2    |   4    |   8    |   16   |   32   |   64   |   0,
216
/*  64  @    65  A    66  B    67  C    68  D    69  E    70  F    71  G  */
217
        1    |   2    |   4    |   8    |   16   |   32   |   64   |  128,
218
/*  72  H    73  I    74  J    75  K    76  L    77  M    78  N    79  O  */
219
        1    |   2    |   4    |   8    |   16   |   32   |   64   |  128,
220
/*  80  P    81  Q    82  R    83  S    84  T    85  U    86  V    87  W  */
221
        1    |   2    |   4    |   8    |   16   |   32   |   64   |  128,
222
/*  88  X    89  Y    90  Z    91  [    92  \    93  ]    94  ^    95  _  */
223
        1    |   2    |   4    |   8    |   16   |   32   |   64   |  128,
224
/*  96  `    97  a    98  b    99  c   100  d   101  e   102  f   103  g  */
225
        1    |   2    |   4    |   8    |   16   |   32   |   64   |  128,
226
/* 104  h   105  i   106  j   107  k   108  l   109  m   110  n   111  o  */
227
        1    |   2    |   4    |   8    |   16   |   32   |   64   |  128,
228
/* 112  p   113  q   114  r   115  s   116  t   117  u   118  v   119  w  */
229
        1    |   2    |   4    |   8    |   16   |   32   |   64   |  128,
230
/* 120  x   121  y   122  z   123  {   124  |   125  }   126  ~   127 del */
231
        1    |   2    |   4    |   8    |   16   |   32   |   64   |   0, };
232

233
#undef T
234

235
enum state
236
  { s_dead = 1 /* important that this is > 0 */
237

238
  , s_start_req_or_res
239
  , s_res_or_resp_H
240
  , s_start_res
241
  , s_res_H
242
  , s_res_HT
243
  , s_res_HTT
244
  , s_res_HTTP
245
  , s_res_first_http_major
246
  , s_res_http_major
247
  , s_res_first_http_minor
248
  , s_res_http_minor
249
  , s_res_first_status_code
250
  , s_res_status_code
251
  , s_res_status
252
  , s_res_line_almost_done
253

254
  , s_start_req
255

256
  , s_req_method
257
  , s_req_spaces_before_url
258
  , s_req_schema
259
  , s_req_schema_slash
260
  , s_req_schema_slash_slash
261
  , s_req_server_start
262
  , s_req_server
263
  , s_req_server_with_at
264
  , s_req_path
265
  , s_req_query_string_start
266
  , s_req_query_string
267
  , s_req_fragment_start
268
  , s_req_fragment
269
  , s_req_http_start
270
  , s_req_http_H
271
  , s_req_http_HT
272
  , s_req_http_HTT
273
  , s_req_http_HTTP
274
  , s_req_first_http_major
275
  , s_req_http_major
276
  , s_req_first_http_minor
277
  , s_req_http_minor
278
  , s_req_line_almost_done
279

280
  , s_header_field_start
281
  , s_header_field
282
  , s_header_value_start
283
  , s_header_value
284
  , s_header_value_lws
285

286
  , s_header_almost_done
287

288
  , s_chunk_size_start
289
  , s_chunk_size
290
  , s_chunk_parameters
291
  , s_chunk_size_almost_done
292

293
  , s_headers_almost_done
294
  , s_headers_done
295

296
  /* Important: 's_headers_done' must be the last 'header' state. All
297
   * states beyond this must be 'body' states. It is used for overflow
298
   * checking. See the PARSING_HEADER() macro.
299
   */
300

301
  , s_chunk_data
302
  , s_chunk_data_almost_done
303
  , s_chunk_data_done
304

305
  , s_body_identity
306
  , s_body_identity_eof
307

308
  , s_message_done
309
  };
310

311

312
#define PARSING_HEADER(state) (state <= s_headers_done)
313

314

315
enum header_states
316
  { h_general = 0
317
  , h_C
318
  , h_CO
319
  , h_CON
320

321
  , h_matching_connection
322
  , h_matching_proxy_connection
323
  , h_matching_content_length
324
  , h_matching_transfer_encoding
325
  , h_matching_upgrade
326

327
  , h_connection
328
  , h_content_length
329
  , h_transfer_encoding
330
  , h_upgrade
331

332
  , h_matching_transfer_encoding_chunked
333
  , h_matching_connection_keep_alive
334
  , h_matching_connection_close
335

336
  , h_transfer_encoding_chunked
337
  , h_connection_keep_alive
338
  , h_connection_close
339
  };
340

341
enum http_host_state
342
  {
343
    s_http_host_dead = 1
344
  , s_http_userinfo_start
345
  , s_http_userinfo
346
  , s_http_host_start
347
  , s_http_host_v6_start
348
  , s_http_host
349
  , s_http_host_v6
350
  , s_http_host_v6_end
351
  , s_http_host_port_start
352
  , s_http_host_port
353
};
354

355
/* Macros for character classes; depends on strict-mode  */
356
#define CR                  '\r'
357
#define LF                  '\n'
358
#define LOWER(c)            (unsigned char)(c | 0x20)
359
#define IS_ALPHA(c)         (LOWER(c) >= 'a' && LOWER(c) <= 'z')
360
#define IS_NUM(c)           ((c) >= '0' && (c) <= '9')
361
#define IS_ALPHANUM(c)      (IS_ALPHA(c) || IS_NUM(c))
362
#define IS_HEX(c)           (IS_NUM(c) || (LOWER(c) >= 'a' && LOWER(c) <= 'f'))
363
#define IS_MARK(c)          ((c) == '-' || (c) == '_' || (c) == '.' || \
364
  (c) == '!' || (c) == '~' || (c) == '*' || (c) == '\'' || (c) == '(' || \
365
  (c) == ')')
366
#define IS_USERINFO_CHAR(c) (IS_ALPHANUM(c) || IS_MARK(c) || (c) == '%' || \
367
  (c) == ';' || (c) == ':' || (c) == '&' || (c) == '=' || (c) == '+' || \
368
  (c) == '$' || (c) == ',')
369

370
#if HTTP_PARSER_STRICT
371
#define TOKEN(c)            (tokens[(unsigned char)c])
372
#define IS_URL_CHAR(c)      (BIT_AT(normal_url_char, (unsigned char)c))
373
#define IS_HOST_CHAR(c)     (IS_ALPHANUM(c) || (c) == '.' || (c) == '-')
374
#else
375
#define TOKEN(c)            ((c == ' ') ? ' ' : tokens[(unsigned char)c])
376
#define IS_URL_CHAR(c)                                                         \
377
  (BIT_AT(normal_url_char, (unsigned char)c) || ((c) & 0x80))
378
#define IS_HOST_CHAR(c)                                                        \
379
  (IS_ALPHANUM(c) || (c) == '.' || (c) == '-' || (c) == '_')
380
#endif
381

382

383
#define start_state (parser->type == HTTP_REQUEST ? s_start_req : s_start_res)
384

385

386
#if HTTP_PARSER_STRICT
387
# define STRICT_CHECK(cond)                                          \
388
do {                                                                 \
389
  if (cond) {                                                        \
390
    SET_ERRNO(HPE_STRICT);                                           \
391
    goto error;                                                      \
392
  }                                                                  \
393
} while (0)
394
# define NEW_MESSAGE() (http_should_keep_alive(parser) ? start_state : s_dead)
395
#else
396
# define STRICT_CHECK(cond)
397
# define NEW_MESSAGE() start_state
398
#endif
399

400

401
/* Map errno values to strings for human-readable output */
402
#define HTTP_STRERROR_GEN(n, s) { "HPE_" #n, s },
403
static struct {
404
  const char *name;
405
  const char *description;
406
} http_strerror_tab[] = {
407
  HTTP_ERRNO_MAP(HTTP_STRERROR_GEN)
408
};
409
#undef HTTP_STRERROR_GEN
410

411
int http_message_needs_eof(const http_parser *parser);
412

413
/* Our URL parser.
414
 *
415
 * This is designed to be shared by http_parser_execute() for URL validation,
416
 * hence it has a state transition + byte-for-byte interface. In addition, it
417
 * is meant to be embedded in http_parser_parse_url(), which does the dirty
418
 * work of turning state transitions URL components for its API.
419
 *
420
 * This function should only be invoked with non-space characters. It is
421
 * assumed that the caller cares about (and can detect) the transition between
422
 * URL and non-URL states by looking for these.
423
 */
424
static enum state
425
parse_url_char(enum state s, const char ch)
426
{
427
  if (ch == ' ' || ch == '\r' || ch == '\n') {
428
    return s_dead;
429
  }
430

431
#if HTTP_PARSER_STRICT
432
  if (ch == '\t' || ch == '\f') {
433
    return s_dead;
434
  }
435
#endif
436

437
  switch (s) {
438
    case s_req_spaces_before_url:
439
      /* Proxied requests are followed by scheme of an absolute URI (alpha).
440
       * All methods except CONNECT are followed by '/' or '*'.
441
       */
442

443
      if (ch == '/' || ch == '*') {
444
        return s_req_path;
445
      }
446

447
      if (IS_ALPHA(ch)) {
448
        return s_req_schema;
449
      }
450

451
      break;
452

453
    case s_req_schema:
454
      if (IS_ALPHA(ch)) {
455
        return s;
456
      }
457

458
      if (ch == ':') {
459
        return s_req_schema_slash;
460
      }
461

462
      break;
463

464
    case s_req_schema_slash:
465
      if (ch == '/') {
466
        return s_req_schema_slash_slash;
467
      }
468

469
      break;
470

471
    case s_req_schema_slash_slash:
472
      if (ch == '/') {
473
        return s_req_server_start;
474
      }
475

476
      break;
477

478
    case s_req_server_with_at:
479
      if (ch == '@') {
480
        return s_dead;
481
      }
482

483
    /* FALLTHROUGH */
484
    case s_req_server_start:
485
    case s_req_server:
486
      if (ch == '/') {
487
        return s_req_path;
488
      }
489

490
      if (ch == '?') {
491
        return s_req_query_string_start;
492
      }
493

494
      if (ch == '@') {
495
        return s_req_server_with_at;
496
      }
497

498
      if (IS_USERINFO_CHAR(ch) || ch == '[' || ch == ']') {
499
        return s_req_server;
500
      }
501

502
      break;
503

504
    case s_req_path:
505
      if (IS_URL_CHAR(ch)) {
506
        return s;
507
      }
508

509
      switch (ch) {
510
        case '?':
511
          return s_req_query_string_start;
512

513
        case '#':
514
          return s_req_fragment_start;
515
      }
516

517
      break;
518

519
    case s_req_query_string_start:
520
    case s_req_query_string:
521
      if (IS_URL_CHAR(ch)) {
522
        return s_req_query_string;
523
      }
524

525
      switch (ch) {
526
        case '?':
527
          /* allow extra '?' in query string */
528
          return s_req_query_string;
529

530
        case '#':
531
          return s_req_fragment_start;
532
      }
533

534
      break;
535

536
    case s_req_fragment_start:
537
      if (IS_URL_CHAR(ch)) {
538
        return s_req_fragment;
539
      }
540

541
      switch (ch) {
542
        case '?':
543
          return s_req_fragment;
544

545
        case '#':
546
          return s;
547
      }
548

549
      break;
550

551
    case s_req_fragment:
552
      if (IS_URL_CHAR(ch)) {
553
        return s;
554
      }
555

556
      switch (ch) {
557
        case '?':
558
        case '#':
559
          return s;
560
      }
561

562
      break;
563

564
    default:
565
      break;
566
  }
567

568
  /* We should never fall out of the switch above unless there's an error */
569
  return s_dead;
570
}
571

572
size_t http_parser_execute (http_parser *parser,
573
                            const http_parser_settings *settings,
574
                            const char *data,
575
                            size_t len)
576
{
577
  char c, ch;
578
  int8_t unhex_val;
579
  const char *p = data;
580
  const char *header_field_mark = 0;
581
  const char *header_value_mark = 0;
582
  const char *url_mark = 0;
583
  const char *body_mark = 0;
584

585
  /* We're in an error state. Don't bother doing anything. */
586
  if (HTTP_PARSER_ERRNO(parser) != HPE_OK) {
587
    return 0;
588
  }
589

590
  if (len == 0) {
591
    switch (parser->state) {
592
      case s_body_identity_eof:
593
        /* Use of CALLBACK_NOTIFY() here would erroneously return 1 byte read if
594
         * we got paused.
595
         */
596
        CALLBACK_NOTIFY_NOADVANCE(message_complete);
597
        return 0;
598

599
      case s_dead:
600
      case s_start_req_or_res:
601
      case s_start_res:
602
      case s_start_req:
603
        return 0;
604

605
      default:
606
        SET_ERRNO(HPE_INVALID_EOF_STATE);
607
        return 1;
608
    }
609
  }
610

611

612
  if (parser->state == s_header_field)
613
    header_field_mark = data;
614
  if (parser->state == s_header_value)
615
    header_value_mark = data;
616
  switch (parser->state) {
617
  case s_req_path:
618
  case s_req_schema:
619
  case s_req_schema_slash:
620
  case s_req_schema_slash_slash:
621
  case s_req_server_start:
622
  case s_req_server:
623
  case s_req_server_with_at:
624
  case s_req_query_string_start:
625
  case s_req_query_string:
626
  case s_req_fragment_start:
627
  case s_req_fragment:
628
    url_mark = data;
629
    break;
630
  }
631

632
  for (p=data; p != data + len; p++) {
633
    ch = *p;
634

635
    if (PARSING_HEADER(parser->state)) {
636
      ++parser->nread;
637
      /* Don't allow the total size of the HTTP headers (including the status
638
       * line) to exceed HTTP_MAX_HEADER_SIZE.  This check is here to protect
639
       * embedders against denial-of-service attacks where the attacker feeds
640
       * us a never-ending header that the embedder keeps buffering.
641
       *
642
       * This check is arguably the responsibility of embedders but we're doing
643
       * it on the embedder's behalf because most won't bother and this way we
644
       * make the web a little safer.  HTTP_MAX_HEADER_SIZE is still far bigger
645
       * than any reasonable request or response so this should never affect
646
       * day-to-day operation.
647
       */
648
      if (parser->nread > HTTP_MAX_HEADER_SIZE) {
649
        SET_ERRNO(HPE_HEADER_OVERFLOW);
650
        goto error;
651
      }
652
    }
653

654
    reexecute_byte:
655
    switch (parser->state) {
656

657
      case s_dead:
658
        /* this state is used after a 'Connection: close' message
659
         * the parser will error out if it reads another message
660
         */
661
        if (ch == CR || ch == LF)
662
          break;
663

664
        SET_ERRNO(HPE_CLOSED_CONNECTION);
665
        goto error;
666

667
      case s_start_req_or_res:
668
      {
669
        if (ch == CR || ch == LF)
670
          break;
671
        parser->flags = 0;
672
        parser->content_length = ULLONG_MAX;
673

674
        if (ch == 'H') {
675
          parser->state = s_res_or_resp_H;
676

677
          CALLBACK_NOTIFY(message_begin);
678
        } else {
679
          parser->type = HTTP_REQUEST;
680
          parser->state = s_start_req;
681
          goto reexecute_byte;
682
        }
683

684
        break;
685
      }
686

687
      case s_res_or_resp_H:
688
        if (ch == 'T') {
689
          parser->type = HTTP_RESPONSE;
690
          parser->state = s_res_HT;
691
        } else {
692
          if (ch != 'E') {
693
            SET_ERRNO(HPE_INVALID_CONSTANT);
694
            goto error;
695
          }
696

697
          parser->type = HTTP_REQUEST;
698
          parser->method = HTTP_HEAD;
699
          parser->index = 2;
700
          parser->state = s_req_method;
701
        }
702
        break;
703

704
      case s_start_res:
705
      {
706
        parser->flags = 0;
707
        parser->content_length = ULLONG_MAX;
708

709
        switch (ch) {
710
          case 'H':
711
            parser->state = s_res_H;
712
            break;
713

714
          case CR:
715
          case LF:
716
            break;
717

718
          default:
719
            SET_ERRNO(HPE_INVALID_CONSTANT);
720
            goto error;
721
        }
722

723
        CALLBACK_NOTIFY(message_begin);
724
        break;
725
      }
726

727
      case s_res_H:
728
        STRICT_CHECK(ch != 'T');
729
        parser->state = s_res_HT;
730
        break;
731

732
      case s_res_HT:
733
        STRICT_CHECK(ch != 'T');
734
        parser->state = s_res_HTT;
735
        break;
736

737
      case s_res_HTT:
738
        STRICT_CHECK(ch != 'P');
739
        parser->state = s_res_HTTP;
740
        break;
741

742
      case s_res_HTTP:
743
        STRICT_CHECK(ch != '/');
744
        parser->state = s_res_first_http_major;
745
        break;
746

747
      case s_res_first_http_major:
748
        if (ch < '0' || ch > '9') {
749
          SET_ERRNO(HPE_INVALID_VERSION);
750
          goto error;
751
        }
752

753
        parser->http_major = ch - '0';
754
        parser->state = s_res_http_major;
755
        break;
756

757
      /* major HTTP version or dot */
758
      case s_res_http_major:
759
      {
760
        if (ch == '.') {
761
          parser->state = s_res_first_http_minor;
762
          break;
763
        }
764

765
        if (!IS_NUM(ch)) {
766
          SET_ERRNO(HPE_INVALID_VERSION);
767
          goto error;
768
        }
769

770
        parser->http_major *= 10;
771
        parser->http_major += ch - '0';
772

773
        if (parser->http_major > 999) {
774
          SET_ERRNO(HPE_INVALID_VERSION);
775
          goto error;
776
        }
777

778
        break;
779
      }
780

781
      /* first digit of minor HTTP version */
782
      case s_res_first_http_minor:
783
        if (!IS_NUM(ch)) {
784
          SET_ERRNO(HPE_INVALID_VERSION);
785
          goto error;
786
        }
787

788
        parser->http_minor = ch - '0';
789
        parser->state = s_res_http_minor;
790
        break;
791

792
      /* minor HTTP version or end of request line */
793
      case s_res_http_minor:
794
      {
795
        if (ch == ' ') {
796
          parser->state = s_res_first_status_code;
797
          break;
798
        }
799

800
        if (!IS_NUM(ch)) {
801
          SET_ERRNO(HPE_INVALID_VERSION);
802
          goto error;
803
        }
804

805
        parser->http_minor *= 10;
806
        parser->http_minor += ch - '0';
807

808
        if (parser->http_minor > 999) {
809
          SET_ERRNO(HPE_INVALID_VERSION);
810
          goto error;
811
        }
812

813
        break;
814
      }
815

816
      case s_res_first_status_code:
817
      {
818
        if (!IS_NUM(ch)) {
819
          if (ch == ' ') {
820
            break;
821
          }
822

823
          SET_ERRNO(HPE_INVALID_STATUS);
824
          goto error;
825
        }
826
        parser->status_code = ch - '0';
827
        parser->state = s_res_status_code;
828
        break;
829
      }
830

831
      case s_res_status_code:
832
      {
833
        if (!IS_NUM(ch)) {
834
          switch (ch) {
835
            case ' ':
836
              parser->state = s_res_status;
837
              break;
838
            case CR:
839
              parser->state = s_res_line_almost_done;
840
              break;
841
            case LF:
842
              parser->state = s_header_field_start;
843
              break;
844
            default:
845
              SET_ERRNO(HPE_INVALID_STATUS);
846
              goto error;
847
          }
848
          break;
849
        }
850

851
        parser->status_code *= 10;
852
        parser->status_code += ch - '0';
853

854
        if (parser->status_code > 999) {
855
          SET_ERRNO(HPE_INVALID_STATUS);
856
          goto error;
857
        }
858

859
        break;
860
      }
861

862
      case s_res_status:
863
        /* the human readable status. e.g. "NOT FOUND"
864
         * we are not humans so just ignore this */
865
        if (ch == CR) {
866
          parser->state = s_res_line_almost_done;
867
          break;
868
        }
869

870
        if (ch == LF) {
871
          parser->state = s_header_field_start;
872
          break;
873
        }
874
        break;
875

876
      case s_res_line_almost_done:
877
        STRICT_CHECK(ch != LF);
878
        parser->state = s_header_field_start;
879
        CALLBACK_NOTIFY(status_complete);
880
        break;
881

882
      case s_start_req:
883
      {
884
        if (ch == CR || ch == LF)
885
          break;
886
        parser->flags = 0;
887
        parser->content_length = ULLONG_MAX;
888

889
        if (!IS_ALPHA(ch)) {
890
          SET_ERRNO(HPE_INVALID_METHOD);
891
          goto error;
892
        }
893

894
        parser->method = (enum http_method) 0;
895
        parser->index = 1;
896
        switch (ch) {
897
          case 'C': parser->method = HTTP_CONNECT; /* or COPY, CHECKOUT */ break;
898
          case 'D': parser->method = HTTP_DELETE; break;
899
          case 'G': parser->method = HTTP_GET; break;
900
          case 'H': parser->method = HTTP_HEAD; break;
901
          case 'L': parser->method = HTTP_LOCK; break;
902
          case 'M': parser->method = HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE, M-SEARCH */ break;
903
          case 'N': parser->method = HTTP_NOTIFY; break;
904
          case 'O': parser->method = HTTP_OPTIONS; break;
905
          case 'P': parser->method = HTTP_POST;
906
            /* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */
907
            break;
908
          case 'R': parser->method = HTTP_REPORT; break;
909
          case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH */ break;
910
          case 'T': parser->method = HTTP_TRACE; break;
911
          case 'U': parser->method = HTTP_UNLOCK; /* or UNSUBSCRIBE */ break;
912
          default:
913
            SET_ERRNO(HPE_INVALID_METHOD);
914
            goto error;
915
        }
916
        parser->state = s_req_method;
917

918
        CALLBACK_NOTIFY(message_begin);
919

920
        break;
921
      }
922

923
      case s_req_method:
924
      {
925
        const char *matcher;
926
        if (ch == '\0') {
927
          SET_ERRNO(HPE_INVALID_METHOD);
928
          goto error;
929
        }
930

931
        matcher = method_strings[parser->method];
932
        if (ch == ' ' && matcher[parser->index] == '\0') {
933
          parser->state = s_req_spaces_before_url;
934
        } else if (ch == matcher[parser->index]) {
935
          ; /* nada */
936
        } else if (parser->method == HTTP_CONNECT) {
937
          if (parser->index == 1 && ch == 'H') {
938
            parser->method = HTTP_CHECKOUT;
939
          } else if (parser->index == 2  && ch == 'P') {
940
            parser->method = HTTP_COPY;
941
          } else {
942
            SET_ERRNO(HPE_INVALID_METHOD);
943
            goto error;
944
          }
945
        } else if (parser->method == HTTP_MKCOL) {
946
          if (parser->index == 1 && ch == 'O') {
947
            parser->method = HTTP_MOVE;
948
          } else if (parser->index == 1 && ch == 'E') {
949
            parser->method = HTTP_MERGE;
950
          } else if (parser->index == 1 && ch == '-') {
951
            parser->method = HTTP_MSEARCH;
952
          } else if (parser->index == 2 && ch == 'A') {
953
            parser->method = HTTP_MKACTIVITY;
954
          } else {
955
            SET_ERRNO(HPE_INVALID_METHOD);
956
            goto error;
957
          }
958
        } else if (parser->method == HTTP_SUBSCRIBE) {
959
          if (parser->index == 1 && ch == 'E') {
960
            parser->method = HTTP_SEARCH;
961
          } else {
962
            SET_ERRNO(HPE_INVALID_METHOD);
963
            goto error;
964
          }
965
        } else if (parser->index == 1 && parser->method == HTTP_POST) {
966
          if (ch == 'R') {
967
            parser->method = HTTP_PROPFIND; /* or HTTP_PROPPATCH */
968
          } else if (ch == 'U') {
969
            parser->method = HTTP_PUT; /* or HTTP_PURGE */
970
          } else if (ch == 'A') {
971
            parser->method = HTTP_PATCH;
972
          } else {
973
            SET_ERRNO(HPE_INVALID_METHOD);
974
            goto error;
975
          }
976
        } else if (parser->index == 2) {
977
          if (parser->method == HTTP_PUT) {
978
            if (ch == 'R') {
979
              parser->method = HTTP_PURGE;
980
            } else {
981
              SET_ERRNO(HPE_INVALID_METHOD);
982
              goto error;
983
            }
984
          } else if (parser->method == HTTP_UNLOCK) {
985
            if (ch == 'S') {
986
              parser->method = HTTP_UNSUBSCRIBE;
987
            } else {
988
              SET_ERRNO(HPE_INVALID_METHOD);
989
              goto error;
990
            }
991
          } else {
992
            SET_ERRNO(HPE_INVALID_METHOD);
993
            goto error;
994
          }
995
        } else if (parser->index == 4 && parser->method == HTTP_PROPFIND && ch == 'P') {
996
          parser->method = HTTP_PROPPATCH;
997
        } else {
998
          SET_ERRNO(HPE_INVALID_METHOD);
999
          goto error;
1000
        }
1001

1002
        ++parser->index;
1003
        break;
1004
      }
1005

1006
      case s_req_spaces_before_url:
1007
      {
1008
        if (ch == ' ') break;
1009

1010
        MARK(url);
1011
        if (parser->method == HTTP_CONNECT) {
1012
          parser->state = s_req_server_start;
1013
        }
1014

1015
        parser->state = parse_url_char((enum state)parser->state, ch);
1016
        if (parser->state == s_dead) {
1017
          SET_ERRNO(HPE_INVALID_URL);
1018
          goto error;
1019
        }
1020

1021
        break;
1022
      }
1023

1024
      case s_req_schema:
1025
      case s_req_schema_slash:
1026
      case s_req_schema_slash_slash:
1027
      case s_req_server_start:
1028
      {
1029
        switch (ch) {
1030
          /* No whitespace allowed here */
1031
          case ' ':
1032
          case CR:
1033
          case LF:
1034
            SET_ERRNO(HPE_INVALID_URL);
1035
            goto error;
1036
          default:
1037
            parser->state = parse_url_char((enum state)parser->state, ch);
1038
            if (parser->state == s_dead) {
1039
              SET_ERRNO(HPE_INVALID_URL);
1040
              goto error;
1041
            }
1042
        }
1043

1044
        break;
1045
      }
1046

1047
      case s_req_server:
1048
      case s_req_server_with_at:
1049
      case s_req_path:
1050
      case s_req_query_string_start:
1051
      case s_req_query_string:
1052
      case s_req_fragment_start:
1053
      case s_req_fragment:
1054
      {
1055
        switch (ch) {
1056
          case ' ':
1057
            parser->state = s_req_http_start;
1058
            CALLBACK_DATA(url);
1059
            break;
1060
          case CR:
1061
          case LF:
1062
            parser->http_major = 0;
1063
            parser->http_minor = 9;
1064
            parser->state = (ch == CR) ?
1065
              s_req_line_almost_done :
1066
              s_header_field_start;
1067
            CALLBACK_DATA(url);
1068
            break;
1069
          default:
1070
            parser->state = parse_url_char((enum state)parser->state, ch);
1071
            if (parser->state == s_dead) {
1072
              SET_ERRNO(HPE_INVALID_URL);
1073
              goto error;
1074
            }
1075
        }
1076
        break;
1077
      }
1078

1079
      case s_req_http_start:
1080
        switch (ch) {
1081
          case 'H':
1082
            parser->state = s_req_http_H;
1083
            break;
1084
          case ' ':
1085
            break;
1086
          default:
1087
            SET_ERRNO(HPE_INVALID_CONSTANT);
1088
            goto error;
1089
        }
1090
        break;
1091

1092
      case s_req_http_H:
1093
        STRICT_CHECK(ch != 'T');
1094
        parser->state = s_req_http_HT;
1095
        break;
1096

1097
      case s_req_http_HT:
1098
        STRICT_CHECK(ch != 'T');
1099
        parser->state = s_req_http_HTT;
1100
        break;
1101

1102
      case s_req_http_HTT:
1103
        STRICT_CHECK(ch != 'P');
1104
        parser->state = s_req_http_HTTP;
1105
        break;
1106

1107
      case s_req_http_HTTP:
1108
        STRICT_CHECK(ch != '/');
1109
        parser->state = s_req_first_http_major;
1110
        break;
1111

1112
      /* first digit of major HTTP version */
1113
      case s_req_first_http_major:
1114
        if (ch < '1' || ch > '9') {
1115
          SET_ERRNO(HPE_INVALID_VERSION);
1116
          goto error;
1117
        }
1118

1119
        parser->http_major = ch - '0';
1120
        parser->state = s_req_http_major;
1121
        break;
1122

1123
      /* major HTTP version or dot */
1124
      case s_req_http_major:
1125
      {
1126
        if (ch == '.') {
1127
          parser->state = s_req_first_http_minor;
1128
          break;
1129
        }
1130

1131
        if (!IS_NUM(ch)) {
1132
          SET_ERRNO(HPE_INVALID_VERSION);
1133
          goto error;
1134
        }
1135

1136
        parser->http_major *= 10;
1137
        parser->http_major += ch - '0';
1138

1139
        if (parser->http_major > 999) {
1140
          SET_ERRNO(HPE_INVALID_VERSION);
1141
          goto error;
1142
        }
1143

1144
        break;
1145
      }
1146

1147
      /* first digit of minor HTTP version */
1148
      case s_req_first_http_minor:
1149
        if (!IS_NUM(ch)) {
1150
          SET_ERRNO(HPE_INVALID_VERSION);
1151
          goto error;
1152
        }
1153

1154
        parser->http_minor = ch - '0';
1155
        parser->state = s_req_http_minor;
1156
        break;
1157

1158
      /* minor HTTP version or end of request line */
1159
      case s_req_http_minor:
1160
      {
1161
        if (ch == CR) {
1162
          parser->state = s_req_line_almost_done;
1163
          break;
1164
        }
1165

1166
        if (ch == LF) {
1167
          parser->state = s_header_field_start;
1168
          break;
1169
        }
1170

1171
        /* XXX allow spaces after digit? */
1172

1173
        if (!IS_NUM(ch)) {
1174
          SET_ERRNO(HPE_INVALID_VERSION);
1175
          goto error;
1176
        }
1177

1178
        parser->http_minor *= 10;
1179
        parser->http_minor += ch - '0';
1180

1181
        if (parser->http_minor > 999) {
1182
          SET_ERRNO(HPE_INVALID_VERSION);
1183
          goto error;
1184
        }
1185

1186
        break;
1187
      }
1188

1189
      /* end of request line */
1190
      case s_req_line_almost_done:
1191
      {
1192
        if (ch != LF) {
1193
          SET_ERRNO(HPE_LF_EXPECTED);
1194
          goto error;
1195
        }
1196

1197
        parser->state = s_header_field_start;
1198
        break;
1199
      }
1200

1201
      case s_header_field_start:
1202
      {
1203
        if (ch == CR) {
1204
          parser->state = s_headers_almost_done;
1205
          break;
1206
        }
1207

1208
        if (ch == LF) {
1209
          /* they might be just sending \n instead of \r\n so this would be
1210
           * the second \n to denote the end of headers*/
1211
          parser->state = s_headers_almost_done;
1212
          goto reexecute_byte;
1213
        }
1214

1215
        c = TOKEN(ch);
1216

1217
        if (!c) {
1218
          SET_ERRNO(HPE_INVALID_HEADER_TOKEN);
1219
          goto error;
1220
        }
1221

1222
        MARK(header_field);
1223

1224
        parser->index = 0;
1225
        parser->state = s_header_field;
1226

1227
        switch (c) {
1228
          case 'c':
1229
            parser->header_state = h_C;
1230
            break;
1231

1232
          case 'p':
1233
            parser->header_state = h_matching_proxy_connection;
1234
            break;
1235

1236
          case 't':
1237
            parser->header_state = h_matching_transfer_encoding;
1238
            break;
1239

1240
          case 'u':
1241
            parser->header_state = h_matching_upgrade;
1242
            break;
1243

1244
          default:
1245
            parser->header_state = h_general;
1246
            break;
1247
        }
1248
        break;
1249
      }
1250

1251
      case s_header_field:
1252
      {
1253
        c = TOKEN(ch);
1254

1255
        if (c) {
1256
          switch (parser->header_state) {
1257
            case h_general:
1258
              break;
1259

1260
            case h_C:
1261
              parser->index++;
1262
              parser->header_state = (c == 'o' ? h_CO : h_general);
1263
              break;
1264

1265
            case h_CO:
1266
              parser->index++;
1267
              parser->header_state = (c == 'n' ? h_CON : h_general);
1268
              break;
1269

1270
            case h_CON:
1271
              parser->index++;
1272
              switch (c) {
1273
                case 'n':
1274
                  parser->header_state = h_matching_connection;
1275
                  break;
1276
                case 't':
1277
                  parser->header_state = h_matching_content_length;
1278
                  break;
1279
                default:
1280
                  parser->header_state = h_general;
1281
                  break;
1282
              }
1283
              break;
1284

1285
            /* connection */
1286

1287
            case h_matching_connection:
1288
              parser->index++;
1289
              if (parser->index > sizeof(CONNECTION)-1
1290
                  || c != CONNECTION[parser->index]) {
1291
                parser->header_state = h_general;
1292
              } else if (parser->index == sizeof(CONNECTION)-2) {
1293
                parser->header_state = h_connection;
1294
              }
1295
              break;
1296

1297
            /* proxy-connection */
1298

1299
            case h_matching_proxy_connection:
1300
              parser->index++;
1301
              if (parser->index > sizeof(PROXY_CONNECTION)-1
1302
                  || c != PROXY_CONNECTION[parser->index]) {
1303
                parser->header_state = h_general;
1304
              } else if (parser->index == sizeof(PROXY_CONNECTION)-2) {
1305
                parser->header_state = h_connection;
1306
              }
1307
              break;
1308

1309
            /* content-length */
1310

1311
            case h_matching_content_length:
1312
              parser->index++;
1313
              if (parser->index > sizeof(CONTENT_LENGTH)-1
1314
                  || c != CONTENT_LENGTH[parser->index]) {
1315
                parser->header_state = h_general;
1316
              } else if (parser->index == sizeof(CONTENT_LENGTH)-2) {
1317
                parser->header_state = h_content_length;
1318
              }
1319
              break;
1320

1321
            /* transfer-encoding */
1322

1323
            case h_matching_transfer_encoding:
1324
              parser->index++;
1325
              if (parser->index > sizeof(TRANSFER_ENCODING)-1
1326
                  || c != TRANSFER_ENCODING[parser->index]) {
1327
                parser->header_state = h_general;
1328
              } else if (parser->index == sizeof(TRANSFER_ENCODING)-2) {
1329
                parser->header_state = h_transfer_encoding;
1330
              }
1331
              break;
1332

1333
            /* upgrade */
1334

1335
            case h_matching_upgrade:
1336
              parser->index++;
1337
              if (parser->index > sizeof(UPGRADE)-1
1338
                  || c != UPGRADE[parser->index]) {
1339
                parser->header_state = h_general;
1340
              } else if (parser->index == sizeof(UPGRADE)-2) {
1341
                parser->header_state = h_upgrade;
1342
              }
1343
              break;
1344

1345
            case h_connection:
1346
            case h_content_length:
1347
            case h_transfer_encoding:
1348
            case h_upgrade:
1349
              if (ch != ' ') parser->header_state = h_general;
1350
              break;
1351

1352
            default:
1353
              assert(0 && "Unknown header_state");
1354
              break;
1355
          }
1356
          break;
1357
        }
1358

1359
        if (ch == ':') {
1360
          parser->state = s_header_value_start;
1361
          CALLBACK_DATA(header_field);
1362
          break;
1363
        }
1364

1365
        if (ch == CR) {
1366
          parser->state = s_header_almost_done;
1367
          CALLBACK_DATA(header_field);
1368
          break;
1369
        }
1370

1371
        if (ch == LF) {
1372
          parser->state = s_header_field_start;
1373
          CALLBACK_DATA(header_field);
1374
          break;
1375
        }
1376

1377
        SET_ERRNO(HPE_INVALID_HEADER_TOKEN);
1378
        goto error;
1379
      }
1380

1381
      case s_header_value_start:
1382
      {
1383
        if (ch == ' ' || ch == '\t') break;
1384

1385
        MARK(header_value);
1386

1387
        parser->state = s_header_value;
1388
        parser->index = 0;
1389

1390
        if (ch == CR) {
1391
          parser->header_state = h_general;
1392
          parser->state = s_header_almost_done;
1393
          CALLBACK_DATA(header_value);
1394
          break;
1395
        }
1396

1397
        if (ch == LF) {
1398
          parser->state = s_header_field_start;
1399
          CALLBACK_DATA(header_value);
1400
          break;
1401
        }
1402

1403
        c = LOWER(ch);
1404

1405
        switch (parser->header_state) {
1406
          case h_upgrade:
1407
            parser->flags |= F_UPGRADE;
1408
            parser->header_state = h_general;
1409
            break;
1410

1411
          case h_transfer_encoding:
1412
            /* looking for 'Transfer-Encoding: chunked' */
1413
            if ('c' == c) {
1414
              parser->header_state = h_matching_transfer_encoding_chunked;
1415
            } else {
1416
              parser->header_state = h_general;
1417
            }
1418
            break;
1419

1420
          case h_content_length:
1421
            if (!IS_NUM(ch)) {
1422
              SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
1423
              goto error;
1424
            }
1425

1426
            parser->content_length = ch - '0';
1427
            break;
1428

1429
          case h_connection:
1430
            /* looking for 'Connection: keep-alive' */
1431
            if (c == 'k') {
1432
              parser->header_state = h_matching_connection_keep_alive;
1433
            /* looking for 'Connection: close' */
1434
            } else if (c == 'c') {
1435
              parser->header_state = h_matching_connection_close;
1436
            } else {
1437
              parser->header_state = h_general;
1438
            }
1439
            break;
1440

1441
          default:
1442
            parser->header_state = h_general;
1443
            break;
1444
        }
1445
        break;
1446
      }
1447

1448
      case s_header_value:
1449
      {
1450

1451
        if (ch == CR) {
1452
          parser->state = s_header_almost_done;
1453
          CALLBACK_DATA(header_value);
1454
          break;
1455
        }
1456

1457
        if (ch == LF) {
1458
          parser->state = s_header_almost_done;
1459
          CALLBACK_DATA_NOADVANCE(header_value);
1460
          goto reexecute_byte;
1461
        }
1462

1463
        c = LOWER(ch);
1464

1465
        switch (parser->header_state) {
1466
          case h_general:
1467
            break;
1468

1469
          case h_connection:
1470
          case h_transfer_encoding:
1471
            assert(0 && "Shouldn't get here.");
1472
            break;
1473

1474
          case h_content_length:
1475
          {
1476
            uint64_t t;
1477

1478
            if (ch == ' ') break;
1479

1480
            if (!IS_NUM(ch)) {
1481
              SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
1482
              goto error;
1483
            }
1484

1485
            t = parser->content_length;
1486
            t *= 10;
1487
            t += ch - '0';
1488

1489
            /* Overflow? */
1490
            if (t < parser->content_length || t == ULLONG_MAX) {
1491
              SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
1492
              goto error;
1493
            }
1494

1495
            parser->content_length = t;
1496
            break;
1497
          }
1498

1499
          /* Transfer-Encoding: chunked */
1500
          case h_matching_transfer_encoding_chunked:
1501
            parser->index++;
1502
            if (parser->index > sizeof(CHUNKED)-1
1503
                || c != CHUNKED[parser->index]) {
1504
              parser->header_state = h_general;
1505
            } else if (parser->index == sizeof(CHUNKED)-2) {
1506
              parser->header_state = h_transfer_encoding_chunked;
1507
            }
1508
            break;
1509

1510
          /* looking for 'Connection: keep-alive' */
1511
          case h_matching_connection_keep_alive:
1512
            parser->index++;
1513
            if (parser->index > sizeof(KEEP_ALIVE)-1
1514
                || c != KEEP_ALIVE[parser->index]) {
1515
              parser->header_state = h_general;
1516
            } else if (parser->index == sizeof(KEEP_ALIVE)-2) {
1517
              parser->header_state = h_connection_keep_alive;
1518
            }
1519
            break;
1520

1521
          /* looking for 'Connection: close' */
1522
          case h_matching_connection_close:
1523
            parser->index++;
1524
            if (parser->index > sizeof(CLOSE)-1 || c != CLOSE[parser->index]) {
1525
              parser->header_state = h_general;
1526
            } else if (parser->index == sizeof(CLOSE)-2) {
1527
              parser->header_state = h_connection_close;
1528
            }
1529
            break;
1530

1531
          case h_transfer_encoding_chunked:
1532
          case h_connection_keep_alive:
1533
          case h_connection_close:
1534
            if (ch != ' ') parser->header_state = h_general;
1535
            break;
1536

1537
          default:
1538
            parser->state = s_header_value;
1539
            parser->header_state = h_general;
1540
            break;
1541
        }
1542
        break;
1543
      }
1544

1545
      case s_header_almost_done:
1546
      {
1547
        STRICT_CHECK(ch != LF);
1548

1549
        parser->state = s_header_value_lws;
1550

1551
        switch (parser->header_state) {
1552
          case h_connection_keep_alive:
1553
            parser->flags |= F_CONNECTION_KEEP_ALIVE;
1554
            break;
1555
          case h_connection_close:
1556
            parser->flags |= F_CONNECTION_CLOSE;
1557
            break;
1558
          case h_transfer_encoding_chunked:
1559
            parser->flags |= F_CHUNKED;
1560
            break;
1561
          default:
1562
            break;
1563
        }
1564

1565
        break;
1566
      }
1567

1568
      case s_header_value_lws:
1569
      {
1570
        if (ch == ' ' || ch == '\t')
1571
          parser->state = s_header_value_start;
1572
        else
1573
        {
1574
          parser->state = s_header_field_start;
1575
          goto reexecute_byte;
1576
        }
1577
        break;
1578
      }
1579

1580
      case s_headers_almost_done:
1581
      {
1582
        STRICT_CHECK(ch != LF);
1583

1584
        if (parser->flags & F_TRAILING) {
1585
          /* End of a chunked request */
1586
          parser->state = NEW_MESSAGE();
1587
          CALLBACK_NOTIFY(message_complete);
1588
          break;
1589
        }
1590

1591
        parser->state = s_headers_done;
1592

1593
        /* Set this here so that on_headers_complete() callbacks can see it */
1594
        parser->upgrade =
1595
          (parser->flags & F_UPGRADE || parser->method == HTTP_CONNECT);
1596

1597
        /* Here we call the headers_complete callback. This is somewhat
1598
         * different than other callbacks because if the user returns 1, we
1599
         * will interpret that as saying that this message has no body. This
1600
         * is needed for the annoying case of recieving a response to a HEAD
1601
         * request.
1602
         *
1603
         * We'd like to use CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so
1604
         * we have to simulate it by handling a change in errno below.
1605
         */
1606
        if (settings->on_headers_complete) {
1607
          switch (settings->on_headers_complete(parser)) {
1608
            case 0:
1609
              break;
1610

1611
            case 1:
1612
              parser->flags |= F_SKIPBODY;
1613
              break;
1614

1615
            default:
1616
              SET_ERRNO(HPE_CB_headers_complete);
1617
              return p - data; /* Error */
1618
          }
1619
        }
1620

1621
        if (HTTP_PARSER_ERRNO(parser) != HPE_OK) {
1622
          return p - data;
1623
        }
1624

1625
        goto reexecute_byte;
1626
      }
1627

1628
      case s_headers_done:
1629
      {
1630
        STRICT_CHECK(ch != LF);
1631

1632
        parser->nread = 0;
1633

1634
        /* Exit, the rest of the connect is in a different protocol. */
1635
        if (parser->upgrade) {
1636
          parser->state = NEW_MESSAGE();
1637
          CALLBACK_NOTIFY(message_complete);
1638
          return (p - data) + 1;
1639
        }
1640

1641
        if (parser->flags & F_SKIPBODY) {
1642
          parser->state = NEW_MESSAGE();
1643
          CALLBACK_NOTIFY(message_complete);
1644
        } else if (parser->flags & F_CHUNKED) {
1645
          /* chunked encoding - ignore Content-Length header */
1646
          parser->state = s_chunk_size_start;
1647
        } else {
1648
          if (parser->content_length == 0) {
1649
            /* Content-Length header given but zero: Content-Length: 0\r\n */
1650
            parser->state = NEW_MESSAGE();
1651
            CALLBACK_NOTIFY(message_complete);
1652
          } else if (parser->content_length != ULLONG_MAX) {
1653
            /* Content-Length header given and non-zero */
1654
            parser->state = s_body_identity;
1655
          } else {
1656
            if (parser->type == HTTP_REQUEST ||
1657
                !http_message_needs_eof(parser)) {
1658
              /* Assume content-length 0 - read the next */
1659
              parser->state = NEW_MESSAGE();
1660
              CALLBACK_NOTIFY(message_complete);
1661
            } else {
1662
              /* Read body until EOF */
1663
              parser->state = s_body_identity_eof;
1664
            }
1665
          }
1666
        }
1667

1668
        break;
1669
      }
1670

1671
      case s_body_identity:
1672
      {
1673
        uint64_t to_read = MIN(parser->content_length,
1674
                               (uint64_t) ((data + len) - p));
1675

1676
        assert(parser->content_length != 0
1677
            && parser->content_length != ULLONG_MAX);
1678

1679
        /* The difference between advancing content_length and p is because
1680
         * the latter will automaticaly advance on the next loop iteration.
1681
         * Further, if content_length ends up at 0, we want to see the last
1682
         * byte again for our message complete callback.
1683
         */
1684
        MARK(body);
1685
        parser->content_length -= to_read;
1686
        p += to_read - 1;
1687

1688
        if (parser->content_length == 0) {
1689
          parser->state = s_message_done;
1690

1691
          /* Mimic CALLBACK_DATA_NOADVANCE() but with one extra byte.
1692
           *
1693
           * The alternative to doing this is to wait for the next byte to
1694
           * trigger the data callback, just as in every other case. The
1695
           * problem with this is that this makes it difficult for the test
1696
           * harness to distinguish between complete-on-EOF and
1697
           * complete-on-length. It's not clear that this distinction is
1698
           * important for applications, but let's keep it for now.
1699
           */
1700
          CALLBACK_DATA_(body, p - body_mark + 1, p - data);
1701
          goto reexecute_byte;
1702
        }
1703

1704
        break;
1705
      }
1706

1707
      /* read until EOF */
1708
      case s_body_identity_eof:
1709
        MARK(body);
1710
        p = data + len - 1;
1711

1712
        break;
1713

1714
      case s_message_done:
1715
        parser->state = NEW_MESSAGE();
1716
        CALLBACK_NOTIFY(message_complete);
1717
        break;
1718

1719
      case s_chunk_size_start:
1720
      {
1721
        assert(parser->nread == 1);
1722
        assert(parser->flags & F_CHUNKED);
1723

1724
        unhex_val = unhex[(unsigned char)ch];
1725
        if (unhex_val == -1) {
1726
          SET_ERRNO(HPE_INVALID_CHUNK_SIZE);
1727
          goto error;
1728
        }
1729

1730
        parser->content_length = unhex_val;
1731
        parser->state = s_chunk_size;
1732
        break;
1733
      }
1734

1735
      case s_chunk_size:
1736
      {
1737
        uint64_t t;
1738

1739
        assert(parser->flags & F_CHUNKED);
1740

1741
        if (ch == CR) {
1742
          parser->state = s_chunk_size_almost_done;
1743
          break;
1744
        }
1745

1746
        unhex_val = unhex[(unsigned char)ch];
1747

1748
        if (unhex_val == -1) {
1749
          if (ch == ';' || ch == ' ') {
1750
            parser->state = s_chunk_parameters;
1751
            break;
1752
          }
1753

1754
          SET_ERRNO(HPE_INVALID_CHUNK_SIZE);
1755
          goto error;
1756
        }
1757

1758
        t = parser->content_length;
1759
        t *= 16;
1760
        t += unhex_val;
1761

1762
        /* Overflow? */
1763
        if (t < parser->content_length || t == ULLONG_MAX) {
1764
          SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
1765
          goto error;
1766
        }
1767

1768
        parser->content_length = t;
1769
        break;
1770
      }
1771

1772
      case s_chunk_parameters:
1773
      {
1774
        assert(parser->flags & F_CHUNKED);
1775
        /* just ignore this shit. TODO check for overflow */
1776
        if (ch == CR) {
1777
          parser->state = s_chunk_size_almost_done;
1778
          break;
1779
        }
1780
        break;
1781
      }
1782

1783
      case s_chunk_size_almost_done:
1784
      {
1785
        assert(parser->flags & F_CHUNKED);
1786
        STRICT_CHECK(ch != LF);
1787

1788
        parser->nread = 0;
1789

1790
        if (parser->content_length == 0) {
1791
          parser->flags |= F_TRAILING;
1792
          parser->state = s_header_field_start;
1793
        } else {
1794
          parser->state = s_chunk_data;
1795
        }
1796
        break;
1797
      }
1798

1799
      case s_chunk_data:
1800
      {
1801
        uint64_t to_read = MIN(parser->content_length,
1802
                               (uint64_t) ((data + len) - p));
1803

1804
        assert(parser->flags & F_CHUNKED);
1805
        assert(parser->content_length != 0
1806
            && parser->content_length != ULLONG_MAX);
1807

1808
        /* See the explanation in s_body_identity for why the content
1809
         * length and data pointers are managed this way.
1810
         */
1811
        MARK(body);
1812
        parser->content_length -= to_read;
1813
        p += to_read - 1;
1814

1815
        if (parser->content_length == 0) {
1816
          parser->state = s_chunk_data_almost_done;
1817
        }
1818

1819
        break;
1820
      }
1821

1822
      case s_chunk_data_almost_done:
1823
        assert(parser->flags & F_CHUNKED);
1824
        assert(parser->content_length == 0);
1825
        STRICT_CHECK(ch != CR);
1826
        parser->state = s_chunk_data_done;
1827
        CALLBACK_DATA(body);
1828
        break;
1829

1830
      case s_chunk_data_done:
1831
        assert(parser->flags & F_CHUNKED);
1832
        STRICT_CHECK(ch != LF);
1833
        parser->nread = 0;
1834
        parser->state = s_chunk_size_start;
1835
        break;
1836

1837
      default:
1838
        assert(0 && "unhandled state");
1839
        SET_ERRNO(HPE_INVALID_INTERNAL_STATE);
1840
        goto error;
1841
    }
1842
  }
1843

1844
  /* Run callbacks for any marks that we have leftover after we ran our of
1845
   * bytes. There should be at most one of these set, so it's OK to invoke
1846
   * them in series (unset marks will not result in callbacks).
1847
   *
1848
   * We use the NOADVANCE() variety of callbacks here because 'p' has already
1849
   * overflowed 'data' and this allows us to correct for the off-by-one that
1850
   * we'd otherwise have (since CALLBACK_DATA() is meant to be run with a 'p'
1851
   * value that's in-bounds).
1852
   */
1853

1854
  assert(((header_field_mark ? 1 : 0) +
1855
          (header_value_mark ? 1 : 0) +
1856
          (url_mark ? 1 : 0)  +
1857
          (body_mark ? 1 : 0)) <= 1);
1858

1859
  CALLBACK_DATA_NOADVANCE(header_field);
1860
  CALLBACK_DATA_NOADVANCE(header_value);
1861
  CALLBACK_DATA_NOADVANCE(url);
1862
  CALLBACK_DATA_NOADVANCE(body);
1863

1864
  return len;
1865

1866
error:
1867
  if (HTTP_PARSER_ERRNO(parser) == HPE_OK) {
1868
    SET_ERRNO(HPE_UNKNOWN);
1869
  }
1870

1871
  return (p - data);
1872
}
1873

1874

1875
/* Does the parser need to see an EOF to find the end of the message? */
1876
int
1877
http_message_needs_eof (const http_parser *parser)
1878
{
1879
  if (parser->type == HTTP_REQUEST) {
1880
    return 0;
1881
  }
1882

1883
  /* See RFC 2616 section 4.4 */
1884
  if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */
1885
      parser->status_code == 204 ||     /* No Content */
1886
      parser->status_code == 304 ||     /* Not Modified */
1887
      parser->flags & F_SKIPBODY) {     /* response to a HEAD request */
1888
    return 0;
1889
  }
1890

1891
  if ((parser->flags & F_CHUNKED) || parser->content_length != ULLONG_MAX) {
1892
    return 0;
1893
  }
1894

1895
  return 1;
1896
}
1897

1898

1899
int
1900
http_should_keep_alive (const http_parser *parser)
1901
{
1902
  if (parser->http_major > 0 && parser->http_minor > 0) {
1903
    /* HTTP/1.1 */
1904
    if (parser->flags & F_CONNECTION_CLOSE) {
1905
      return 0;
1906
    }
1907
  } else {
1908
    /* HTTP/1.0 or earlier */
1909
    if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) {
1910
      return 0;
1911
    }
1912
  }
1913

1914
  return !http_message_needs_eof(parser);
1915
}
1916

1917

1918
const char *
1919
http_method_str (enum http_method m)
1920
{
1921
  return ELEM_AT(method_strings, m, "<unknown>");
1922
}
1923

1924

1925
void
1926
http_parser_init (http_parser *parser, enum http_parser_type t)
1927
{
1928
  void *data = parser->data; /* preserve application data */
1929
  memset(parser, 0, sizeof(*parser));
1930
  parser->data = data;
1931
  parser->type = t;
1932
  parser->state = (t == HTTP_REQUEST ? s_start_req : (t == HTTP_RESPONSE ? s_start_res : s_start_req_or_res));
1933
  parser->http_errno = HPE_OK;
1934
}
1935

1936
const char *
1937
http_errno_name(enum http_errno err) {
1938
  assert(err < (sizeof(http_strerror_tab)/sizeof(http_strerror_tab[0])));
1939
  return http_strerror_tab[err].name;
1940
}
1941

1942
const char *
1943
http_errno_description(enum http_errno err) {
1944
  assert(err < (sizeof(http_strerror_tab)/sizeof(http_strerror_tab[0])));
1945
  return http_strerror_tab[err].description;
1946
}
1947

1948
static enum http_host_state
1949
http_parse_host_char(enum http_host_state s, const char ch) {
1950
  switch(s) {
1951
    case s_http_userinfo:
1952
    case s_http_userinfo_start:
1953
      if (ch == '@') {
1954
        return s_http_host_start;
1955
      }
1956

1957
      if (IS_USERINFO_CHAR(ch)) {
1958
        return s_http_userinfo;
1959
      }
1960
      break;
1961

1962
    case s_http_host_start:
1963
      if (ch == '[') {
1964
        return s_http_host_v6_start;
1965
      }
1966

1967
      if (IS_HOST_CHAR(ch)) {
1968
        return s_http_host;
1969
      }
1970

1971
      break;
1972

1973
    case s_http_host:
1974
      if (IS_HOST_CHAR(ch)) {
1975
        return s_http_host;
1976
      }
1977

1978
    /* FALLTHROUGH */
1979
    case s_http_host_v6_end:
1980
      if (ch == ':') {
1981
        return s_http_host_port_start;
1982
      }
1983

1984
      break;
1985

1986
    case s_http_host_v6:
1987
      if (ch == ']') {
1988
        return s_http_host_v6_end;
1989
      }
1990

1991
    /* FALLTHROUGH */
1992
    case s_http_host_v6_start:
1993
      if (IS_HEX(ch) || ch == ':' || ch == '.') {
1994
        return s_http_host_v6;
1995
      }
1996

1997
      break;
1998

1999
    case s_http_host_port:
2000
    case s_http_host_port_start:
2001
      if (IS_NUM(ch)) {
2002
        return s_http_host_port;
2003
      }
2004

2005
      break;
2006

2007
    default:
2008
      break;
2009
  }
2010
  return s_http_host_dead;
2011
}
2012

2013
static int
2014
http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
2015
  enum http_host_state s;
2016

2017
  const char *p;
2018
  size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len;
2019

2020
  u->field_data[UF_HOST].len = 0;
2021

2022
  s = found_at ? s_http_userinfo_start : s_http_host_start;
2023

2024
  for (p = buf + u->field_data[UF_HOST].off; p < buf + buflen; p++) {
2025
    enum http_host_state new_s = http_parse_host_char(s, *p);
2026

2027
    if (new_s == s_http_host_dead) {
2028
      return 1;
2029
    }
2030

2031
    switch(new_s) {
2032
      case s_http_host:
2033
        if (s != s_http_host) {
2034
          u->field_data[UF_HOST].off = p - buf;
2035
        }
2036
        u->field_data[UF_HOST].len++;
2037
        break;
2038

2039
      case s_http_host_v6:
2040
        if (s != s_http_host_v6) {
2041
          u->field_data[UF_HOST].off = p - buf;
2042
        }
2043
        u->field_data[UF_HOST].len++;
2044
        break;
2045

2046
      case s_http_host_port:
2047
        if (s != s_http_host_port) {
2048
          u->field_data[UF_PORT].off = p - buf;
2049
          u->field_data[UF_PORT].len = 0;
2050
          u->field_set |= (1 << UF_PORT);
2051
        }
2052
        u->field_data[UF_PORT].len++;
2053
        break;
2054

2055
      case s_http_userinfo:
2056
        if (s != s_http_userinfo) {
2057
          u->field_data[UF_USERINFO].off = p - buf ;
2058
          u->field_data[UF_USERINFO].len = 0;
2059
          u->field_set |= (1 << UF_USERINFO);
2060
        }
2061
        u->field_data[UF_USERINFO].len++;
2062
        break;
2063

2064
      default:
2065
        break;
2066
    }
2067
    s = new_s;
2068
  }
2069

2070
  /* Make sure we don't end somewhere unexpected */
2071
  switch (s) {
2072
    case s_http_host_start:
2073
    case s_http_host_v6_start:
2074
    case s_http_host_v6:
2075
    case s_http_host_port_start:
2076
    case s_http_userinfo:
2077
    case s_http_userinfo_start:
2078
      return 1;
2079
    default:
2080
      break;
2081
  }
2082

2083
  return 0;
2084
}
2085

2086
int
2087
http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
2088
                      struct http_parser_url *u)
2089
{
2090
  enum state s;
2091
  const char *p;
2092
  enum http_parser_url_fields uf, old_uf;
2093
  int found_at = 0;
2094

2095
  u->port = u->field_set = 0;
2096
  s = is_connect ? s_req_server_start : s_req_spaces_before_url;
2097
  uf = old_uf = UF_MAX;
2098

2099
  for (p = buf; p < buf + buflen; p++) {
2100
    s = parse_url_char(s, *p);
2101

2102
    /* Figure out the next field that we're operating on */
2103
    switch (s) {
2104
      case s_dead:
2105
        return 1;
2106

2107
      /* Skip delimeters */
2108
      case s_req_schema_slash:
2109
      case s_req_schema_slash_slash:
2110
      case s_req_server_start:
2111
      case s_req_query_string_start:
2112
      case s_req_fragment_start:
2113
        continue;
2114

2115
      case s_req_schema:
2116
        uf = UF_SCHEMA;
2117
        break;
2118

2119
      case s_req_server_with_at:
2120
        found_at = 1;
2121

2122
#ifndef __has_attribute
2123
# define __has_attribute(x) 0
2124
#endif
2125
#if __has_attribute(fallthrough)
2126
       __attribute__((fallthrough));
2127
#endif
2128
      /* FALLTROUGH */
2129
      case s_req_server:
2130
        uf = UF_HOST;
2131
        break;
2132

2133
      case s_req_path:
2134
        uf = UF_PATH;
2135
        break;
2136

2137
      case s_req_query_string:
2138
        uf = UF_QUERY;
2139
        break;
2140

2141
      case s_req_fragment:
2142
        uf = UF_FRAGMENT;
2143
        break;
2144

2145
      default:
2146
        assert(!"Unexpected state");
2147
        return 1;
2148
    }
2149

2150
    /* Nothing's changed; soldier on */
2151
    if (uf == old_uf) {
2152
      u->field_data[uf].len++;
2153
      continue;
2154
    }
2155

2156
    u->field_data[uf].off = p - buf;
2157
    u->field_data[uf].len = 1;
2158

2159
    u->field_set |= (1 << uf);
2160
    old_uf = uf;
2161
  }
2162

2163
  /* host must be present if there is a schema */
2164
  /* parsing http:///toto will fail */
2165
  if ((u->field_set & ((1 << UF_SCHEMA) | (1 << UF_HOST))) != 0) {
2166
    if (http_parse_host(buf, u, found_at) != 0) {
2167
      return 1;
2168
    }
2169
  }
2170

2171
  /* CONNECT requests can only contain "hostname:port" */
2172
  if (is_connect && u->field_set != ((1 << UF_HOST)|(1 << UF_PORT))) {
2173
    return 1;
2174
  }
2175

2176
  if (u->field_set & (1 << UF_PORT)) {
2177
    /* Don't bother with endp; we've already validated the string */
2178
    unsigned long v = strtoul(buf + u->field_data[UF_PORT].off, NULL, 10);
2179

2180
    /* Ports have a max value of 2^16 */
2181
    if (v > 0xffff) {
2182
      return 1;
2183
    }
2184

2185
    u->port = (uint16_t) v;
2186
  }
2187

2188
  return 0;
2189
}
2190

2191
void
2192
http_parser_pause(http_parser *parser, int paused) {
2193
  /* Users should only be pausing/unpausing a parser that is not in an error
2194
   * state. In non-debug builds, there's not much that we can do about this
2195
   * other than ignore it.
2196
   */
2197
  if (HTTP_PARSER_ERRNO(parser) == HPE_OK ||
2198
      HTTP_PARSER_ERRNO(parser) == HPE_PAUSED) {
2199
    SET_ERRNO((paused) ? HPE_PAUSED : HPE_OK);
2200
  } else {
2201
    assert(0 && "Attempting to pause parser in error state");
2202
  }
2203
}
2204

2205
int
2206
http_body_is_final(const struct http_parser *parser) {
2207
    return parser->state == s_message_done;
2208
}
2209

2210
unsigned long
2211
http_parser_version(void) {
2212
  return HTTP_PARSER_VERSION_MAJOR * 0x10000 |
2213
         HTTP_PARSER_VERSION_MINOR * 0x00100 |
2214
         HTTP_PARSER_VERSION_PATCH * 0x00001;
2215
}
2216

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.