bossan 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ require 'mkmf'
2
+
3
+ $CFLAGS << " -std=gnu99 -fPIC"
4
+
5
+ $DLDFLAGS << " -fPIC"
6
+
7
+ bossan_dir = File.expand_path("../", __FILE__)
8
+
9
+ Dir.chdir bossan_dir
10
+
11
+ dir_config bossan_dir
12
+
13
+ create_makefile "bossan/bossan_ext"
@@ -0,0 +1,1578 @@
1
+ /* Copyright 2009,2010 Ryan Dahl <ry@tinyclouds.org>
2
+ *
3
+ * Some parts of this source file were taken from NGINX
4
+ * (src/http/ngx_http_parser.c) copyright (C) 2002-2009 Igor Sysoev.
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
+ #ifdef _WIN32
26
+ typedef __int8 int8_t;
27
+ typedef unsigned __int8 uint8_t;
28
+ typedef __int16 int16_t;
29
+ typedef unsigned __int16 uint16_t;
30
+ typedef __int16 int32_t;
31
+ typedef unsigned __int32 uint32_t;
32
+ #else
33
+ #include <stdint.h>
34
+ #endif
35
+ #include <assert.h>
36
+ #include <stddef.h>
37
+
38
+
39
+ #ifndef MIN
40
+ # define MIN(a,b) ((a) < (b) ? (a) : (b))
41
+ #endif
42
+
43
+
44
+ #define CALLBACK2(FOR) \
45
+ do { \
46
+ if (settings->on_##FOR) { \
47
+ if (0 != settings->on_##FOR(parser)) return (p - data); \
48
+ } \
49
+ } while (0)
50
+
51
+
52
+ #define MARK(FOR) \
53
+ do { \
54
+ FOR##_mark = p; \
55
+ } while (0)
56
+
57
+ #define CALLBACK_NOCLEAR(FOR) \
58
+ do { \
59
+ if (FOR##_mark) { \
60
+ if (settings->on_##FOR) { \
61
+ if (0 != settings->on_##FOR(parser, \
62
+ FOR##_mark, \
63
+ p - FOR##_mark, 1)) \
64
+ { \
65
+ return (p - data); \
66
+ } \
67
+ } \
68
+ } \
69
+ } while (0)
70
+
71
+ #define CALLBACK_CLEAR(FOR) \
72
+ do { \
73
+ if (FOR##_mark) { \
74
+ if (settings->on_##FOR) { \
75
+ if (0 != settings->on_##FOR(parser, \
76
+ FOR##_mark, \
77
+ p - FOR##_mark, 0)) \
78
+ { \
79
+ return (p - data); \
80
+ } \
81
+ } \
82
+ } \
83
+ } while (0)
84
+
85
+ #define CALLBACK(FOR) \
86
+ do { \
87
+ CALLBACK_CLEAR(FOR); \
88
+ FOR##_mark = NULL; \
89
+ } while (0)
90
+
91
+
92
+ #define PROXY_CONNECTION "proxy-connection"
93
+ #define CONNECTION "connection"
94
+ #define CONTENT_LENGTH "content-length"
95
+ #define TRANSFER_ENCODING "transfer-encoding"
96
+ #define UPGRADE "upgrade"
97
+ #define CHUNKED "chunked"
98
+ #define KEEP_ALIVE "keep-alive"
99
+ #define CLOSE "close"
100
+
101
+
102
+ static const char *method_strings[] =
103
+ { "DELETE"
104
+ , "GET"
105
+ , "HEAD"
106
+ , "POST"
107
+ , "PUT"
108
+ , "CONNECT"
109
+ , "OPTIONS"
110
+ , "TRACE"
111
+ , "COPY"
112
+ , "LOCK"
113
+ , "MKCOL"
114
+ , "MOVE"
115
+ , "PROPFIND"
116
+ , "PROPPATCH"
117
+ , "UNLOCK"
118
+ , "REPORT"
119
+ , "MKACTIVITY"
120
+ , "CHECKOUT"
121
+ , "MERGE"
122
+ };
123
+
124
+
125
+ static const char lowcase[256] =
126
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
127
+ " \0\0\0\0\0\0\0\0\0\0\0\0-\0\0" "0123456789\0\0\0\0\0\0"
128
+ "\0abcdefghijklmnopqrstuvwxyz\0\0\0\0_"
129
+ "\0abcdefghijklmnopqrstuvwxyz\0\0\0\0\0"
130
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
131
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
132
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
133
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
134
+
135
+
136
+ static const int unhex[] =
137
+ {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
138
+ ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
139
+ ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
140
+ , 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1
141
+ ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
142
+ ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
143
+ ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
144
+ ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
145
+ };
146
+
147
+
148
+
149
+ static const uint32_t usual[] = {
150
+ 0xffffdbfe, /* 1111 1111 1111 1111 1101 1011 1111 1110 */
151
+
152
+ /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
153
+ 0x7ffffff6, /* 0111 1111 1111 1111 1111 1111 1111 0110 */
154
+
155
+ /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
156
+ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
157
+
158
+ /* ~}| {zyx wvut srqp onml kjih gfed cba` */
159
+ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
160
+
161
+ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
162
+ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
163
+ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
164
+ 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
165
+ };
166
+
167
+ #define USUAL(c) (usual[c >> 5] & (1 << (c & 0x1f)))
168
+
169
+
170
+ enum state
171
+ { s_dead = 1 /* important that this is > 0 */
172
+
173
+ , s_start_req_or_res
174
+ , s_res_or_resp_H
175
+ , s_start_res
176
+ , s_res_H
177
+ , s_res_HT
178
+ , s_res_HTT
179
+ , s_res_HTTP
180
+ , s_res_first_http_major
181
+ , s_res_http_major
182
+ , s_res_first_http_minor
183
+ , s_res_http_minor
184
+ , s_res_first_status_code
185
+ , s_res_status_code
186
+ , s_res_status
187
+ , s_res_line_almost_done
188
+
189
+ , s_start_req
190
+
191
+ , s_req_method
192
+ , s_req_spaces_before_url
193
+ , s_req_schema
194
+ , s_req_schema_slash
195
+ , s_req_schema_slash_slash
196
+ , s_req_host
197
+ , s_req_port
198
+ , s_req_path
199
+ , s_req_query_string_start
200
+ , s_req_query_string
201
+ , s_req_fragment_start
202
+ , s_req_fragment
203
+ , s_req_http_start
204
+ , s_req_http_H
205
+ , s_req_http_HT
206
+ , s_req_http_HTT
207
+ , s_req_http_HTTP
208
+ , s_req_first_http_major
209
+ , s_req_http_major
210
+ , s_req_first_http_minor
211
+ , s_req_http_minor
212
+ , s_req_line_almost_done
213
+
214
+ , s_header_field_start
215
+ , s_header_field
216
+ , s_header_value_start
217
+ , s_header_value
218
+
219
+ , s_header_almost_done
220
+
221
+ , s_headers_almost_done
222
+ /* Important: 's_headers_almost_done' must be the last 'header' state. All
223
+ * states beyond this must be 'body' states. It is used for overflow
224
+ * checking. See the PARSING_HEADER() macro.
225
+ */
226
+ , s_chunk_size_start
227
+ , s_chunk_size
228
+ , s_chunk_size_almost_done
229
+ , s_chunk_parameters
230
+ , s_chunk_data
231
+ , s_chunk_data_almost_done
232
+ , s_chunk_data_done
233
+
234
+ , s_body_identity
235
+ , s_body_identity_eof
236
+ };
237
+
238
+
239
+ #define PARSING_HEADER(state) (state <= s_headers_almost_done && 0 == (parser->flags & F_TRAILING))
240
+
241
+
242
+ enum header_states
243
+ { h_general = 0
244
+ , h_C
245
+ , h_CO
246
+ , h_CON
247
+
248
+ , h_matching_connection
249
+ , h_matching_proxy_connection
250
+ , h_matching_content_length
251
+ , h_matching_transfer_encoding
252
+ , h_matching_upgrade
253
+
254
+ , h_connection
255
+ , h_content_length
256
+ , h_transfer_encoding
257
+ , h_upgrade
258
+
259
+ , h_matching_transfer_encoding_chunked
260
+ , h_matching_connection_keep_alive
261
+ , h_matching_connection_close
262
+
263
+ , h_transfer_encoding_chunked
264
+ , h_connection_keep_alive
265
+ , h_connection_close
266
+ };
267
+
268
+
269
+ enum flags
270
+ { F_CHUNKED = 1 << 0
271
+ , F_CONNECTION_KEEP_ALIVE = 1 << 1
272
+ , F_CONNECTION_CLOSE = 1 << 2
273
+ , F_TRAILING = 1 << 3
274
+ , F_UPGRADE = 1 << 4
275
+ , F_SKIPBODY = 1 << 5
276
+ };
277
+
278
+
279
+ #define CR '\r'
280
+ #define LF '\n'
281
+ #define LOWER(c) (unsigned char)(c | 0x20)
282
+
283
+
284
+ #define start_state (parser->type == HTTP_REQUEST ? s_start_req : s_start_res)
285
+
286
+
287
+ #if HTTP_PARSER_STRICT
288
+ # define STRICT_CHECK(cond) if (cond) goto error
289
+ # define NEW_MESSAGE() (http_should_keep_alive(parser) ? start_state : s_dead)
290
+ #else
291
+ # define STRICT_CHECK(cond)
292
+ # define NEW_MESSAGE() start_state
293
+ #endif
294
+
295
+
296
+ size_t http_parser_execute (http_parser *parser,
297
+ const http_parser_settings *settings,
298
+ const char *data,
299
+ size_t len)
300
+ {
301
+ char c, ch;
302
+ const char *p = data, *pe;
303
+ int64_t to_read;
304
+
305
+ enum state state = (enum state) parser->state;
306
+ enum header_states header_state = (enum header_states) parser->header_state;
307
+ uint64_t index = parser->index;
308
+ uint64_t nread = parser->nread;
309
+
310
+ if (len == 0) {
311
+ if (state == s_body_identity_eof) {
312
+ CALLBACK2(message_complete);
313
+ }
314
+ return 0;
315
+ }
316
+
317
+ /* technically we could combine all of these (except for url_mark) into one
318
+ variable, saving stack space, but it seems more clear to have them
319
+ separated. */
320
+ const char *header_field_mark = 0;
321
+ const char *header_value_mark = 0;
322
+ const char *fragment_mark = 0;
323
+ const char *query_string_mark = 0;
324
+ const char *path_mark = 0;
325
+ const char *url_mark = 0;
326
+
327
+ if (state == s_header_field)
328
+ header_field_mark = data;
329
+ if (state == s_header_value)
330
+ header_value_mark = data;
331
+ if (state == s_req_fragment)
332
+ fragment_mark = data;
333
+ if (state == s_req_query_string)
334
+ query_string_mark = data;
335
+ if (state == s_req_path)
336
+ path_mark = data;
337
+ if (state == s_req_path || state == s_req_schema || state == s_req_schema_slash
338
+ || state == s_req_schema_slash_slash || state == s_req_port
339
+ || state == s_req_query_string_start || state == s_req_query_string
340
+ || state == s_req_host
341
+ || state == s_req_fragment_start || state == s_req_fragment)
342
+ url_mark = data;
343
+
344
+ for (p=data, pe=data+len; p != pe; p++) {
345
+ ch = *p;
346
+
347
+ if (PARSING_HEADER(state)) {
348
+ ++nread;
349
+ /* Buffer overflow attack */
350
+ if (nread > HTTP_MAX_HEADER_SIZE) goto error;
351
+ }
352
+
353
+ switch (state) {
354
+
355
+ case s_dead:
356
+ /* this state is used after a 'Connection: close' message
357
+ * the parser will error out if it reads another message
358
+ */
359
+ goto error;
360
+
361
+ case s_start_req_or_res:
362
+ {
363
+ if (ch == CR || ch == LF)
364
+ break;
365
+ parser->flags = 0;
366
+ parser->content_length = -1;
367
+
368
+ CALLBACK2(message_begin);
369
+
370
+ if (ch == 'H')
371
+ state = s_res_or_resp_H;
372
+ else {
373
+ parser->type = HTTP_REQUEST;
374
+ goto start_req_method_assign;
375
+ }
376
+ break;
377
+ }
378
+
379
+ case s_res_or_resp_H:
380
+ if (ch == 'T') {
381
+ parser->type = HTTP_RESPONSE;
382
+ state = s_res_HT;
383
+ } else {
384
+ if (ch != 'E') goto error;
385
+ parser->type = HTTP_REQUEST;
386
+ parser->method = HTTP_HEAD;
387
+ index = 2;
388
+ state = s_req_method;
389
+ }
390
+ break;
391
+
392
+ case s_start_res:
393
+ {
394
+ parser->flags = 0;
395
+ parser->content_length = -1;
396
+
397
+ CALLBACK2(message_begin);
398
+
399
+ switch (ch) {
400
+ case 'H':
401
+ state = s_res_H;
402
+ break;
403
+
404
+ case CR:
405
+ case LF:
406
+ break;
407
+
408
+ default:
409
+ goto error;
410
+ }
411
+ break;
412
+ }
413
+
414
+ case s_res_H:
415
+ STRICT_CHECK(ch != 'T');
416
+ state = s_res_HT;
417
+ break;
418
+
419
+ case s_res_HT:
420
+ STRICT_CHECK(ch != 'T');
421
+ state = s_res_HTT;
422
+ break;
423
+
424
+ case s_res_HTT:
425
+ STRICT_CHECK(ch != 'P');
426
+ state = s_res_HTTP;
427
+ break;
428
+
429
+ case s_res_HTTP:
430
+ STRICT_CHECK(ch != '/');
431
+ state = s_res_first_http_major;
432
+ break;
433
+
434
+ case s_res_first_http_major:
435
+ if (ch < '1' || ch > '9') goto error;
436
+ parser->http_major = ch - '0';
437
+ state = s_res_http_major;
438
+ break;
439
+
440
+ /* major HTTP version or dot */
441
+ case s_res_http_major:
442
+ {
443
+ if (ch == '.') {
444
+ state = s_res_first_http_minor;
445
+ break;
446
+ }
447
+
448
+ if (ch < '0' || ch > '9') goto error;
449
+
450
+ parser->http_major *= 10;
451
+ parser->http_major += ch - '0';
452
+
453
+ if (parser->http_major > 999) goto error;
454
+ break;
455
+ }
456
+
457
+ /* first digit of minor HTTP version */
458
+ case s_res_first_http_minor:
459
+ if (ch < '0' || ch > '9') goto error;
460
+ parser->http_minor = ch - '0';
461
+ state = s_res_http_minor;
462
+ break;
463
+
464
+ /* minor HTTP version or end of request line */
465
+ case s_res_http_minor:
466
+ {
467
+ if (ch == ' ') {
468
+ state = s_res_first_status_code;
469
+ break;
470
+ }
471
+
472
+ if (ch < '0' || ch > '9') goto error;
473
+
474
+ parser->http_minor *= 10;
475
+ parser->http_minor += ch - '0';
476
+
477
+ if (parser->http_minor > 999) goto error;
478
+ break;
479
+ }
480
+
481
+ case s_res_first_status_code:
482
+ {
483
+ if (ch < '0' || ch > '9') {
484
+ if (ch == ' ') {
485
+ break;
486
+ }
487
+ goto error;
488
+ }
489
+ parser->status_code = ch - '0';
490
+ state = s_res_status_code;
491
+ break;
492
+ }
493
+
494
+ case s_res_status_code:
495
+ {
496
+ if (ch < '0' || ch > '9') {
497
+ switch (ch) {
498
+ case ' ':
499
+ state = s_res_status;
500
+ break;
501
+ case CR:
502
+ state = s_res_line_almost_done;
503
+ break;
504
+ case LF:
505
+ state = s_header_field_start;
506
+ break;
507
+ default:
508
+ goto error;
509
+ }
510
+ break;
511
+ }
512
+
513
+ parser->status_code *= 10;
514
+ parser->status_code += ch - '0';
515
+
516
+ if (parser->status_code > 999) goto error;
517
+ break;
518
+ }
519
+
520
+ case s_res_status:
521
+ /* the human readable status. e.g. "NOT FOUND"
522
+ * we are not humans so just ignore this */
523
+ if (ch == CR) {
524
+ state = s_res_line_almost_done;
525
+ break;
526
+ }
527
+
528
+ if (ch == LF) {
529
+ state = s_header_field_start;
530
+ break;
531
+ }
532
+ break;
533
+
534
+ case s_res_line_almost_done:
535
+ STRICT_CHECK(ch != LF);
536
+ state = s_header_field_start;
537
+ break;
538
+
539
+ case s_start_req:
540
+ {
541
+ if (ch == CR || ch == LF)
542
+ break;
543
+ parser->flags = 0;
544
+ parser->content_length = -1;
545
+
546
+ CALLBACK2(message_begin);
547
+
548
+ if (ch < 'A' || 'Z' < ch) goto error;
549
+
550
+ start_req_method_assign:
551
+ parser->method = (enum http_method) 0;
552
+ index = 1;
553
+ switch (ch) {
554
+ case 'C': parser->method = HTTP_CONNECT; /* or COPY, CHECKOUT */ break;
555
+ case 'D': parser->method = HTTP_DELETE; break;
556
+ case 'G': parser->method = HTTP_GET; break;
557
+ case 'H': parser->method = HTTP_HEAD; break;
558
+ case 'L': parser->method = HTTP_LOCK; break;
559
+ case 'M': parser->method = HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE */ break;
560
+ case 'O': parser->method = HTTP_OPTIONS; break;
561
+ case 'P': parser->method = HTTP_POST; /* or PROPFIND or PROPPATCH or PUT */ break;
562
+ case 'R': parser->method = HTTP_REPORT; break;
563
+ case 'T': parser->method = HTTP_TRACE; break;
564
+ case 'U': parser->method = HTTP_UNLOCK; break;
565
+ default: goto error;
566
+ }
567
+ state = s_req_method;
568
+ break;
569
+ }
570
+
571
+ case s_req_method:
572
+ {
573
+ if (ch == '\0')
574
+ goto error;
575
+
576
+ const char *matcher = method_strings[parser->method];
577
+ if (ch == ' ' && matcher[index] == '\0') {
578
+ state = s_req_spaces_before_url;
579
+ } else if (ch == matcher[index]) {
580
+ ; // nada
581
+ } else if (parser->method == HTTP_CONNECT) {
582
+ if (index == 1 && ch == 'H') {
583
+ parser->method = HTTP_CHECKOUT;
584
+ } else if (index == 2 && ch == 'P') {
585
+ parser->method = HTTP_COPY;
586
+ }
587
+ } else if (parser->method == HTTP_MKCOL) {
588
+ if (index == 1 && ch == 'O') {
589
+ parser->method = HTTP_MOVE;
590
+ } else if (index == 1 && ch == 'E') {
591
+ parser->method = HTTP_MERGE;
592
+ } else if (index == 2 && ch == 'A') {
593
+ parser->method = HTTP_MKACTIVITY;
594
+ }
595
+ } else if (index == 1 && parser->method == HTTP_POST && ch == 'R') {
596
+ parser->method = HTTP_PROPFIND; /* or HTTP_PROPPATCH */
597
+ } else if (index == 1 && parser->method == HTTP_POST && ch == 'U') {
598
+ parser->method = HTTP_PUT;
599
+ } else if (index == 4 && parser->method == HTTP_PROPFIND && ch == 'P') {
600
+ parser->method = HTTP_PROPPATCH;
601
+ } else {
602
+ goto error;
603
+ }
604
+
605
+ ++index;
606
+ break;
607
+ }
608
+ case s_req_spaces_before_url:
609
+ {
610
+ if (ch == ' ') break;
611
+
612
+ if (ch == '/') {
613
+ MARK(url);
614
+ MARK(path);
615
+ state = s_req_path;
616
+ break;
617
+ }
618
+
619
+ c = LOWER(ch);
620
+
621
+ if (c >= 'a' && c <= 'z') {
622
+ MARK(url);
623
+ state = s_req_schema;
624
+ break;
625
+ }
626
+
627
+ goto error;
628
+ }
629
+
630
+ case s_req_schema:
631
+ {
632
+ c = LOWER(ch);
633
+
634
+ if (c >= 'a' && c <= 'z') break;
635
+
636
+ if (ch == ':') {
637
+ state = s_req_schema_slash;
638
+ break;
639
+ } else if (ch == '.') {
640
+ state = s_req_host;
641
+ break;
642
+ }
643
+
644
+ goto error;
645
+ }
646
+
647
+ case s_req_schema_slash:
648
+ STRICT_CHECK(ch != '/');
649
+ state = s_req_schema_slash_slash;
650
+ break;
651
+
652
+ case s_req_schema_slash_slash:
653
+ STRICT_CHECK(ch != '/');
654
+ state = s_req_host;
655
+ break;
656
+
657
+ case s_req_host:
658
+ {
659
+ c = LOWER(ch);
660
+ if (c >= 'a' && c <= 'z') break;
661
+ if ((ch >= '0' && ch <= '9') || ch == '.' || ch == '-') break;
662
+ switch (ch) {
663
+ case ':':
664
+ state = s_req_port;
665
+ break;
666
+ case '/':
667
+ MARK(path);
668
+ state = s_req_path;
669
+ break;
670
+ case ' ':
671
+ /* The request line looks like:
672
+ * "GET http://foo.bar.com HTTP/1.1"
673
+ * That is, there is no path.
674
+ */
675
+ CALLBACK(url);
676
+ state = s_req_http_start;
677
+ break;
678
+ default:
679
+ goto error;
680
+ }
681
+ break;
682
+ }
683
+
684
+ case s_req_port:
685
+ {
686
+ if (ch >= '0' && ch <= '9') break;
687
+ switch (ch) {
688
+ case '/':
689
+ MARK(path);
690
+ state = s_req_path;
691
+ break;
692
+ case ' ':
693
+ /* The request line looks like:
694
+ * "GET http://foo.bar.com:1234 HTTP/1.1"
695
+ * That is, there is no path.
696
+ */
697
+ CALLBACK(url);
698
+ state = s_req_http_start;
699
+ break;
700
+ default:
701
+ goto error;
702
+ }
703
+ break;
704
+ }
705
+
706
+ case s_req_path:
707
+ {
708
+ if (USUAL(ch)) break;
709
+
710
+ switch (ch) {
711
+ case ' ':
712
+ CALLBACK(url);
713
+ CALLBACK(path);
714
+ state = s_req_http_start;
715
+ break;
716
+ case CR:
717
+ CALLBACK(url);
718
+ CALLBACK(path);
719
+ parser->http_minor = 9;
720
+ state = s_req_line_almost_done;
721
+ break;
722
+ case LF:
723
+ CALLBACK(url);
724
+ CALLBACK(path);
725
+ parser->http_minor = 9;
726
+ state = s_header_field_start;
727
+ break;
728
+ case '?':
729
+ CALLBACK(path);
730
+ state = s_req_query_string_start;
731
+ break;
732
+ case '#':
733
+ CALLBACK(path);
734
+ state = s_req_fragment_start;
735
+ break;
736
+ default:
737
+ goto error;
738
+ }
739
+ break;
740
+ }
741
+
742
+ case s_req_query_string_start:
743
+ {
744
+ if (USUAL(ch)) {
745
+ MARK(query_string);
746
+ state = s_req_query_string;
747
+ break;
748
+ }
749
+
750
+ switch (ch) {
751
+ case '?':
752
+ break; // XXX ignore extra '?' ... is this right?
753
+ case ' ':
754
+ CALLBACK(url);
755
+ state = s_req_http_start;
756
+ break;
757
+ case CR:
758
+ CALLBACK(url);
759
+ parser->http_minor = 9;
760
+ state = s_req_line_almost_done;
761
+ break;
762
+ case LF:
763
+ CALLBACK(url);
764
+ parser->http_minor = 9;
765
+ state = s_header_field_start;
766
+ break;
767
+ case '#':
768
+ state = s_req_fragment_start;
769
+ break;
770
+ default:
771
+ goto error;
772
+ }
773
+ break;
774
+ }
775
+
776
+ case s_req_query_string:
777
+ {
778
+ if (USUAL(ch)) break;
779
+
780
+ switch (ch) {
781
+ case '?':
782
+ // allow extra '?' in query string
783
+ break;
784
+ case ' ':
785
+ CALLBACK(url);
786
+ CALLBACK(query_string);
787
+ state = s_req_http_start;
788
+ break;
789
+ case CR:
790
+ CALLBACK(url);
791
+ CALLBACK(query_string);
792
+ parser->http_minor = 9;
793
+ state = s_req_line_almost_done;
794
+ break;
795
+ case LF:
796
+ CALLBACK(url);
797
+ CALLBACK(query_string);
798
+ parser->http_minor = 9;
799
+ state = s_header_field_start;
800
+ break;
801
+ case '#':
802
+ CALLBACK(query_string);
803
+ state = s_req_fragment_start;
804
+ break;
805
+ default:
806
+ goto error;
807
+ }
808
+ break;
809
+ }
810
+
811
+ case s_req_fragment_start:
812
+ {
813
+ if (USUAL(ch)) {
814
+ MARK(fragment);
815
+ state = s_req_fragment;
816
+ break;
817
+ }
818
+
819
+ switch (ch) {
820
+ case ' ':
821
+ CALLBACK(url);
822
+ state = s_req_http_start;
823
+ break;
824
+ case CR:
825
+ CALLBACK(url);
826
+ parser->http_minor = 9;
827
+ state = s_req_line_almost_done;
828
+ break;
829
+ case LF:
830
+ CALLBACK(url);
831
+ parser->http_minor = 9;
832
+ state = s_header_field_start;
833
+ break;
834
+ case '?':
835
+ MARK(fragment);
836
+ state = s_req_fragment;
837
+ break;
838
+ case '#':
839
+ break;
840
+ default:
841
+ goto error;
842
+ }
843
+ break;
844
+ }
845
+
846
+ case s_req_fragment:
847
+ {
848
+ if (USUAL(ch)) break;
849
+
850
+ switch (ch) {
851
+ case ' ':
852
+ CALLBACK(url);
853
+ CALLBACK(fragment);
854
+ state = s_req_http_start;
855
+ break;
856
+ case CR:
857
+ CALLBACK(url);
858
+ CALLBACK(fragment);
859
+ parser->http_minor = 9;
860
+ state = s_req_line_almost_done;
861
+ break;
862
+ case LF:
863
+ CALLBACK(url);
864
+ CALLBACK(fragment);
865
+ parser->http_minor = 9;
866
+ state = s_header_field_start;
867
+ break;
868
+ case '?':
869
+ case '#':
870
+ break;
871
+ default:
872
+ goto error;
873
+ }
874
+ break;
875
+ }
876
+
877
+ case s_req_http_start:
878
+ switch (ch) {
879
+ case 'H':
880
+ state = s_req_http_H;
881
+ break;
882
+ case ' ':
883
+ break;
884
+ default:
885
+ goto error;
886
+ }
887
+ break;
888
+
889
+ case s_req_http_H:
890
+ STRICT_CHECK(ch != 'T');
891
+ state = s_req_http_HT;
892
+ break;
893
+
894
+ case s_req_http_HT:
895
+ STRICT_CHECK(ch != 'T');
896
+ state = s_req_http_HTT;
897
+ break;
898
+
899
+ case s_req_http_HTT:
900
+ STRICT_CHECK(ch != 'P');
901
+ state = s_req_http_HTTP;
902
+ break;
903
+
904
+ case s_req_http_HTTP:
905
+ STRICT_CHECK(ch != '/');
906
+ state = s_req_first_http_major;
907
+ break;
908
+
909
+ /* first digit of major HTTP version */
910
+ case s_req_first_http_major:
911
+ if (ch < '1' || ch > '9') goto error;
912
+ parser->http_major = ch - '0';
913
+ state = s_req_http_major;
914
+ break;
915
+
916
+ /* major HTTP version or dot */
917
+ case s_req_http_major:
918
+ {
919
+ if (ch == '.') {
920
+ state = s_req_first_http_minor;
921
+ break;
922
+ }
923
+
924
+ if (ch < '0' || ch > '9') goto error;
925
+
926
+ parser->http_major *= 10;
927
+ parser->http_major += ch - '0';
928
+
929
+ if (parser->http_major > 999) goto error;
930
+ break;
931
+ }
932
+
933
+ /* first digit of minor HTTP version */
934
+ case s_req_first_http_minor:
935
+ if (ch < '0' || ch > '9') goto error;
936
+ parser->http_minor = ch - '0';
937
+ state = s_req_http_minor;
938
+ break;
939
+
940
+ /* minor HTTP version or end of request line */
941
+ case s_req_http_minor:
942
+ {
943
+ if (ch == CR) {
944
+ state = s_req_line_almost_done;
945
+ break;
946
+ }
947
+
948
+ if (ch == LF) {
949
+ state = s_header_field_start;
950
+ break;
951
+ }
952
+
953
+ /* XXX allow spaces after digit? */
954
+
955
+ if (ch < '0' || ch > '9') goto error;
956
+
957
+ parser->http_minor *= 10;
958
+ parser->http_minor += ch - '0';
959
+
960
+ if (parser->http_minor > 999) goto error;
961
+ break;
962
+ }
963
+
964
+ /* end of request line */
965
+ case s_req_line_almost_done:
966
+ {
967
+ if (ch != LF) goto error;
968
+ state = s_header_field_start;
969
+ break;
970
+ }
971
+
972
+ case s_header_field_start:
973
+ {
974
+ if (ch == CR) {
975
+ state = s_headers_almost_done;
976
+ break;
977
+ }
978
+
979
+ if (ch == LF) {
980
+ /* they might be just sending \n instead of \r\n so this would be
981
+ * the second \n to denote the end of headers*/
982
+ state = s_headers_almost_done;
983
+ goto headers_almost_done;
984
+ }
985
+
986
+ if (parser->maybe_ml && (ch == ' '|| ch == '\t')) goto s_header_value_start_;
987
+ c = LOWER(ch);
988
+
989
+ if (c < 'a' || 'z' < c) goto error;
990
+
991
+ parser->maybe_ml = 0;
992
+
993
+ MARK(header_field);
994
+
995
+ index = 0;
996
+ state = s_header_field;
997
+
998
+ switch (c) {
999
+ case 'c':
1000
+ header_state = h_C;
1001
+ break;
1002
+
1003
+ case 'p':
1004
+ header_state = h_matching_proxy_connection;
1005
+ break;
1006
+
1007
+ case 't':
1008
+ header_state = h_matching_transfer_encoding;
1009
+ break;
1010
+
1011
+ case 'u':
1012
+ header_state = h_matching_upgrade;
1013
+ break;
1014
+
1015
+ default:
1016
+ header_state = h_general;
1017
+ break;
1018
+ }
1019
+ break;
1020
+ }
1021
+
1022
+ case s_header_field:
1023
+ {
1024
+ c = lowcase[(unsigned char)ch];
1025
+
1026
+ if (c) {
1027
+ switch (header_state) {
1028
+ case h_general:
1029
+ break;
1030
+
1031
+ case h_C:
1032
+ index++;
1033
+ header_state = (c == 'o' ? h_CO : h_general);
1034
+ break;
1035
+
1036
+ case h_CO:
1037
+ index++;
1038
+ header_state = (c == 'n' ? h_CON : h_general);
1039
+ break;
1040
+
1041
+ case h_CON:
1042
+ index++;
1043
+ switch (c) {
1044
+ case 'n':
1045
+ header_state = h_matching_connection;
1046
+ break;
1047
+ case 't':
1048
+ header_state = h_matching_content_length;
1049
+ break;
1050
+ default:
1051
+ header_state = h_general;
1052
+ break;
1053
+ }
1054
+ break;
1055
+
1056
+ /* connection */
1057
+
1058
+ case h_matching_connection:
1059
+ index++;
1060
+ if (index > sizeof(CONNECTION)-1
1061
+ || c != CONNECTION[index]) {
1062
+ header_state = h_general;
1063
+ } else if (index == sizeof(CONNECTION)-2) {
1064
+ header_state = h_connection;
1065
+ }
1066
+ break;
1067
+
1068
+ /* proxy-connection */
1069
+
1070
+ case h_matching_proxy_connection:
1071
+ index++;
1072
+ if (index > sizeof(PROXY_CONNECTION)-1
1073
+ || c != PROXY_CONNECTION[index]) {
1074
+ header_state = h_general;
1075
+ } else if (index == sizeof(PROXY_CONNECTION)-2) {
1076
+ header_state = h_connection;
1077
+ }
1078
+ break;
1079
+
1080
+ /* content-length */
1081
+
1082
+ case h_matching_content_length:
1083
+ index++;
1084
+ if (index > sizeof(CONTENT_LENGTH)-1
1085
+ || c != CONTENT_LENGTH[index]) {
1086
+ header_state = h_general;
1087
+ } else if (index == sizeof(CONTENT_LENGTH)-2) {
1088
+ header_state = h_content_length;
1089
+ }
1090
+ break;
1091
+
1092
+ /* transfer-encoding */
1093
+
1094
+ case h_matching_transfer_encoding:
1095
+ index++;
1096
+ if (index > sizeof(TRANSFER_ENCODING)-1
1097
+ || c != TRANSFER_ENCODING[index]) {
1098
+ header_state = h_general;
1099
+ } else if (index == sizeof(TRANSFER_ENCODING)-2) {
1100
+ header_state = h_transfer_encoding;
1101
+ }
1102
+ break;
1103
+
1104
+ /* upgrade */
1105
+
1106
+ case h_matching_upgrade:
1107
+ index++;
1108
+ if (index > sizeof(UPGRADE)-1
1109
+ || c != UPGRADE[index]) {
1110
+ header_state = h_general;
1111
+ } else if (index == sizeof(UPGRADE)-2) {
1112
+ header_state = h_upgrade;
1113
+ }
1114
+ break;
1115
+
1116
+ case h_connection:
1117
+ case h_content_length:
1118
+ case h_transfer_encoding:
1119
+ case h_upgrade:
1120
+ if (ch != ' ') header_state = h_general;
1121
+ break;
1122
+
1123
+ default:
1124
+ assert(0 && "Unknown header_state");
1125
+ break;
1126
+ }
1127
+ break;
1128
+ }
1129
+
1130
+ if (ch == ':') {
1131
+ CALLBACK(header_field);
1132
+ state = s_header_value_start;
1133
+ break;
1134
+ }
1135
+
1136
+ if (ch == CR) {
1137
+ state = s_header_almost_done;
1138
+ CALLBACK(header_field);
1139
+ break;
1140
+ }
1141
+
1142
+ if (ch == LF) {
1143
+ CALLBACK(header_field);
1144
+ state = s_header_field_start;
1145
+ break;
1146
+ }
1147
+
1148
+ goto error;
1149
+ }
1150
+
1151
+ case s_header_value_start:
1152
+ s_header_value_start_:
1153
+ {
1154
+ if (!parser->maybe_ml && (ch == ' ' || ch == '\t')) break;
1155
+
1156
+ MARK(header_value);
1157
+
1158
+ state = s_header_value;
1159
+ index = 0;
1160
+
1161
+ c = lowcase[(unsigned char)ch];
1162
+
1163
+ if (!c) {
1164
+ if (ch == CR) {
1165
+ CALLBACK(header_value);
1166
+ header_state = h_general;
1167
+ state = s_header_almost_done;
1168
+ parser->maybe_ml = 1;
1169
+ break;
1170
+ }
1171
+
1172
+ if (ch == LF) {
1173
+ CALLBACK(header_value);
1174
+ state = s_header_field_start;
1175
+ parser->maybe_ml = 1;
1176
+ break;
1177
+ }
1178
+
1179
+ header_state = h_general;
1180
+ break;
1181
+ }
1182
+
1183
+ switch (header_state) {
1184
+ case h_upgrade:
1185
+ parser->flags |= F_UPGRADE;
1186
+ header_state = h_general;
1187
+ break;
1188
+
1189
+ case h_transfer_encoding:
1190
+ /* looking for 'Transfer-Encoding: chunked' */
1191
+ if ('c' == c) {
1192
+ header_state = h_matching_transfer_encoding_chunked;
1193
+ } else {
1194
+ header_state = h_general;
1195
+ }
1196
+ break;
1197
+
1198
+ case h_content_length:
1199
+ if (ch < '0' || ch > '9') goto error;
1200
+ parser->content_length = ch - '0';
1201
+ break;
1202
+
1203
+ case h_connection:
1204
+ /* looking for 'Connection: keep-alive' */
1205
+ if (c == 'k') {
1206
+ header_state = h_matching_connection_keep_alive;
1207
+ /* looking for 'Connection: close' */
1208
+ } else if (c == 'c') {
1209
+ header_state = h_matching_connection_close;
1210
+ } else {
1211
+ header_state = h_general;
1212
+ }
1213
+ break;
1214
+
1215
+ default:
1216
+ header_state = h_general;
1217
+ break;
1218
+ }
1219
+ break;
1220
+ }
1221
+
1222
+ case s_header_value:
1223
+ {
1224
+ c = lowcase[(unsigned char)ch];
1225
+
1226
+ if (!c) {
1227
+ if (ch == CR) {
1228
+ CALLBACK(header_value);
1229
+ state = s_header_almost_done;
1230
+ if(header_state == h_general){
1231
+ parser->maybe_ml = 1;
1232
+ }
1233
+
1234
+ break;
1235
+ }
1236
+
1237
+ if (ch == LF) {
1238
+ CALLBACK(header_value);
1239
+ if(header_state == h_general){
1240
+ parser->maybe_ml = 1;
1241
+ }
1242
+ goto header_almost_done;
1243
+ }
1244
+ break;
1245
+ }
1246
+
1247
+ switch (header_state) {
1248
+ case h_general:
1249
+ break;
1250
+
1251
+ case h_connection:
1252
+ case h_transfer_encoding:
1253
+ assert(0 && "Shouldn't get here.");
1254
+ break;
1255
+
1256
+ case h_content_length:
1257
+ if (ch < '0' || ch > '9') goto error;
1258
+ parser->content_length *= 10;
1259
+ parser->content_length += ch - '0';
1260
+ break;
1261
+
1262
+ /* Transfer-Encoding: chunked */
1263
+ case h_matching_transfer_encoding_chunked:
1264
+ index++;
1265
+ if (index > sizeof(CHUNKED)-1
1266
+ || c != CHUNKED[index]) {
1267
+ header_state = h_general;
1268
+ } else if (index == sizeof(CHUNKED)-2) {
1269
+ header_state = h_transfer_encoding_chunked;
1270
+ }
1271
+ break;
1272
+
1273
+ /* looking for 'Connection: keep-alive' */
1274
+ case h_matching_connection_keep_alive:
1275
+ index++;
1276
+ if (index > sizeof(KEEP_ALIVE)-1
1277
+ || c != KEEP_ALIVE[index]) {
1278
+ header_state = h_general;
1279
+ } else if (index == sizeof(KEEP_ALIVE)-2) {
1280
+ header_state = h_connection_keep_alive;
1281
+ }
1282
+ break;
1283
+
1284
+ /* looking for 'Connection: close' */
1285
+ case h_matching_connection_close:
1286
+ index++;
1287
+ if (index > sizeof(CLOSE)-1 || c != CLOSE[index]) {
1288
+ header_state = h_general;
1289
+ } else if (index == sizeof(CLOSE)-2) {
1290
+ header_state = h_connection_close;
1291
+ }
1292
+ break;
1293
+
1294
+ case h_transfer_encoding_chunked:
1295
+ case h_connection_keep_alive:
1296
+ case h_connection_close:
1297
+ if (ch != ' ') header_state = h_general;
1298
+ break;
1299
+
1300
+ default:
1301
+ state = s_header_value;
1302
+ header_state = h_general;
1303
+ break;
1304
+ }
1305
+ break;
1306
+ }
1307
+
1308
+ case s_header_almost_done:
1309
+ header_almost_done:
1310
+ {
1311
+ STRICT_CHECK(ch != LF);
1312
+
1313
+ state = s_header_field_start;
1314
+
1315
+ switch (header_state) {
1316
+ case h_connection_keep_alive:
1317
+ parser->flags |= F_CONNECTION_KEEP_ALIVE;
1318
+ break;
1319
+ case h_connection_close:
1320
+ parser->flags |= F_CONNECTION_CLOSE;
1321
+ break;
1322
+ case h_transfer_encoding_chunked:
1323
+ parser->flags |= F_CHUNKED;
1324
+ break;
1325
+ default:
1326
+ break;
1327
+ }
1328
+ break;
1329
+ }
1330
+
1331
+ case s_headers_almost_done:
1332
+ headers_almost_done:
1333
+ {
1334
+ parser->maybe_ml = 0;
1335
+ STRICT_CHECK(ch != LF);
1336
+
1337
+ if (parser->flags & F_TRAILING) {
1338
+ /* End of a chunked request */
1339
+ CALLBACK2(message_complete);
1340
+ state = NEW_MESSAGE();
1341
+ break;
1342
+ }
1343
+
1344
+ nread = 0;
1345
+
1346
+ if (parser->flags & F_UPGRADE || parser->method == HTTP_CONNECT) {
1347
+ parser->upgrade = 1;
1348
+ }
1349
+
1350
+ /* Here we call the headers_complete callback. This is somewhat
1351
+ * different than other callbacks because if the user returns 1, we
1352
+ * will interpret that as saying that this message has no body. This
1353
+ * is needed for the annoying case of recieving a response to a HEAD
1354
+ * request.
1355
+ */
1356
+ if (settings->on_headers_complete) {
1357
+ switch (settings->on_headers_complete(parser)) {
1358
+ case 0:
1359
+ break;
1360
+
1361
+ case 1:
1362
+ parser->flags |= F_SKIPBODY;
1363
+ break;
1364
+
1365
+ default:
1366
+ return p - data; /* Error */
1367
+ }
1368
+ }
1369
+
1370
+ // Exit, the rest of the connect is in a different protocol.
1371
+ if (parser->upgrade) {
1372
+ CALLBACK2(message_complete);
1373
+ return (p - data);
1374
+ }
1375
+
1376
+ if (parser->flags & F_SKIPBODY) {
1377
+ CALLBACK2(message_complete);
1378
+ state = NEW_MESSAGE();
1379
+ } else if (parser->flags & F_CHUNKED) {
1380
+ /* chunked encoding - ignore Content-Length header */
1381
+ state = s_chunk_size_start;
1382
+ } else {
1383
+ if (parser->content_length == 0) {
1384
+ /* Content-Length header given but zero: Content-Length: 0\r\n */
1385
+ CALLBACK2(message_complete);
1386
+ state = NEW_MESSAGE();
1387
+ } else if (parser->content_length > 0) {
1388
+ /* Content-Length header given and non-zero */
1389
+ state = s_body_identity;
1390
+ } else {
1391
+ if (parser->type == HTTP_REQUEST || http_should_keep_alive(parser)) {
1392
+ /* Assume content-length 0 - read the next */
1393
+ CALLBACK2(message_complete);
1394
+ state = NEW_MESSAGE();
1395
+ } else {
1396
+ /* Read body until EOF */
1397
+ state = s_body_identity_eof;
1398
+ }
1399
+ }
1400
+ }
1401
+
1402
+ break;
1403
+ }
1404
+
1405
+ case s_body_identity:
1406
+ to_read = MIN(pe - p, (int64_t)parser->content_length);
1407
+ if (to_read > 0) {
1408
+ if (settings->on_body) settings->on_body(parser, p, to_read, 0);
1409
+ p += to_read - 1;
1410
+ parser->content_length -= to_read;
1411
+ if (parser->content_length == 0) {
1412
+ CALLBACK2(message_complete);
1413
+ state = NEW_MESSAGE();
1414
+ }
1415
+ }
1416
+ break;
1417
+
1418
+ /* read until EOF */
1419
+ case s_body_identity_eof:
1420
+ to_read = pe - p;
1421
+ if (to_read > 0) {
1422
+ if (settings->on_body) settings->on_body(parser, p, to_read, 0);
1423
+ p += to_read - 1;
1424
+ }
1425
+ break;
1426
+
1427
+ case s_chunk_size_start:
1428
+ {
1429
+ assert(parser->flags & F_CHUNKED);
1430
+
1431
+ c = unhex[(int)ch];
1432
+ if (c == -1) goto error;
1433
+ parser->content_length = c;
1434
+ state = s_chunk_size;
1435
+ break;
1436
+ }
1437
+
1438
+ case s_chunk_size:
1439
+ {
1440
+ assert(parser->flags & F_CHUNKED);
1441
+
1442
+ if (ch == CR) {
1443
+ state = s_chunk_size_almost_done;
1444
+ break;
1445
+ }
1446
+
1447
+ c = unhex[(int)ch];
1448
+
1449
+ if (c == -1) {
1450
+ if (ch == ';' || ch == ' ') {
1451
+ state = s_chunk_parameters;
1452
+ break;
1453
+ }
1454
+ goto error;
1455
+ }
1456
+
1457
+ parser->content_length *= 16;
1458
+ parser->content_length += c;
1459
+ break;
1460
+ }
1461
+
1462
+ case s_chunk_parameters:
1463
+ {
1464
+ assert(parser->flags & F_CHUNKED);
1465
+ /* just ignore this shit. TODO check for overflow */
1466
+ if (ch == CR) {
1467
+ state = s_chunk_size_almost_done;
1468
+ break;
1469
+ }
1470
+ break;
1471
+ }
1472
+
1473
+ case s_chunk_size_almost_done:
1474
+ {
1475
+ assert(parser->flags & F_CHUNKED);
1476
+ STRICT_CHECK(ch != LF);
1477
+
1478
+ if (parser->content_length == 0) {
1479
+ parser->flags |= F_TRAILING;
1480
+ state = s_header_field_start;
1481
+ } else {
1482
+ state = s_chunk_data;
1483
+ }
1484
+ break;
1485
+ }
1486
+
1487
+ case s_chunk_data:
1488
+ {
1489
+ assert(parser->flags & F_CHUNKED);
1490
+
1491
+ to_read = MIN(pe - p, (int64_t)(parser->content_length));
1492
+
1493
+ if (to_read > 0) {
1494
+ if (settings->on_body) settings->on_body(parser, p, to_read, 0);
1495
+ p += to_read - 1;
1496
+ }
1497
+
1498
+ if (to_read == parser->content_length) {
1499
+ state = s_chunk_data_almost_done;
1500
+ }
1501
+
1502
+ parser->content_length -= to_read;
1503
+ break;
1504
+ }
1505
+
1506
+ case s_chunk_data_almost_done:
1507
+ assert(parser->flags & F_CHUNKED);
1508
+ STRICT_CHECK(ch != CR);
1509
+ state = s_chunk_data_done;
1510
+ break;
1511
+
1512
+ case s_chunk_data_done:
1513
+ assert(parser->flags & F_CHUNKED);
1514
+ STRICT_CHECK(ch != LF);
1515
+ state = s_chunk_size_start;
1516
+ break;
1517
+
1518
+ default:
1519
+ assert(0 && "unhandled state");
1520
+ goto error;
1521
+ }
1522
+ }
1523
+
1524
+ CALLBACK_NOCLEAR(header_field);
1525
+ CALLBACK_NOCLEAR(header_value);
1526
+ CALLBACK_NOCLEAR(fragment);
1527
+ CALLBACK_NOCLEAR(query_string);
1528
+ CALLBACK_NOCLEAR(path);
1529
+ CALLBACK_NOCLEAR(url);
1530
+
1531
+ parser->state = state;
1532
+ parser->header_state = header_state;
1533
+ parser->index = index;
1534
+ parser->nread = nread;
1535
+
1536
+ return len;
1537
+
1538
+ error:
1539
+ return (p - data);
1540
+ }
1541
+
1542
+
1543
+ int
1544
+ http_should_keep_alive (http_parser *parser)
1545
+ {
1546
+ if (parser->http_major > 0 && parser->http_minor > 0) {
1547
+ /* HTTP/1.1 */
1548
+ if (parser->flags & F_CONNECTION_CLOSE) {
1549
+ return 0;
1550
+ } else {
1551
+ return 1;
1552
+ }
1553
+ } else {
1554
+ /* HTTP/1.0 or earlier */
1555
+ if (parser->flags & F_CONNECTION_KEEP_ALIVE) {
1556
+ return 1;
1557
+ } else {
1558
+ return 0;
1559
+ }
1560
+ }
1561
+ }
1562
+
1563
+
1564
+ const char * http_method_str (enum http_method m)
1565
+ {
1566
+ return method_strings[m];
1567
+ }
1568
+
1569
+
1570
+ void
1571
+ http_parser_init (http_parser *parser, enum http_parser_type t)
1572
+ {
1573
+ parser->type = t;
1574
+ parser->state = (t == HTTP_REQUEST ? s_start_req : (t == HTTP_RESPONSE ? s_start_res : s_start_req_or_res));
1575
+ parser->nread = 0;
1576
+ parser->upgrade = 0;
1577
+ parser->flags = 0;
1578
+ }