Skip to content

Commit c9e2143

Browse files
committed
Upgrade http-parser
1 parent b3349eb commit c9e2143

File tree

7 files changed

+137
-118
lines changed

7 files changed

+137
-118
lines changed

deps/http_parser/LICENSE-MIT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2009 Ryan Dahl <[email protected]>
1+
Copyright 2009,2010 Ryan Dahl <[email protected]>
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to

deps/http_parser/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ Usage
2929

3030
One `http_parser` object is used per TCP connection. Initialize the struct
3131
using `http_parser_init()` and set the callbacks. That might look something
32-
like this:
32+
like this for a request parser:
3333

3434
http_parser *parser = malloc(sizeof(http_parser));
35-
http_parser_init(parser);
35+
http_parser_init(parser, HTTP_REQUEST);
3636
parser->on_path = my_path_callback;
3737
parser->on_header_field = my_header_field_callback;
3838
/* ... */
@@ -54,7 +54,7 @@ When data is received on the socket execute the parser and check for errors.
5454
* Note we pass the recved==0 to http_parse_requests to signal
5555
* that EOF has been recieved.
5656
*/
57-
nparsed = http_parse_requests(parser, buf, recved);
57+
nparsed = http_parser_execute(parser, buf, recved);
5858

5959
if (nparsed != recved) {
6060
/* Handle error. Usually just close the connection. */
@@ -63,7 +63,7 @@ When data is received on the socket execute the parser and check for errors.
6363
HTTP needs to know where the end of the stream is. For example, sometimes
6464
servers send responses without Content-Length and expect the client to
6565
consume input (for the body) until EOF. To tell http_parser about EOF, give
66-
`0` as the third parameter to `http_parse_requests()`. Callbacks and errors
66+
`0` as the third parameter to `http_parser_execute()`. Callbacks and errors
6767
can still be encountered during an EOF, so one must still be prepared
6868
to receive them.
6969

@@ -85,7 +85,7 @@ parser, for example, would not want such a feature.
8585
Callbacks
8686
---------
8787

88-
During the `http_parse_requests()` call, the callbacks set in `http_parser`
88+
During the `http_parser_execute()` call, the callbacks set in `http_parser`
8989
will be executed. The parser maintains state and never looks behind, so
9090
buffering the data is not necessary. If you need to save certain data for
9191
later usage, you can do that from the callbacks.

deps/http_parser/http_parser.c

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2009 Ryan Dahl <[email protected]>
1+
/* Copyright 2009,2010 Ryan Dahl <[email protected]>
22
*
33
* Some parts of this source file were taken from NGINX
44
* (src/http/ngx_http_parser.c) copyright (C) 2002-2009 Igor Sysoev.
@@ -97,6 +97,7 @@ static inline int message_complete_callback (http_parser *parser)
9797
return parser->on_message_complete(parser);
9898
}
9999

100+
#define PROXY_CONNECTION "proxy-connection"
100101
#define CONNECTION "connection"
101102
#define CONTENT_LENGTH "content-length"
102103
#define TRANSFER_ENCODING "transfer-encoding"
@@ -218,6 +219,7 @@ enum header_states
218219
, h_CON
219220

220221
, h_matching_connection
222+
, h_matching_proxy_connection
221223
, h_matching_content_length
222224
, h_matching_transfer_encoding
223225

@@ -245,6 +247,8 @@ enum flags
245247
#define LF '\n'
246248
#define LOWER(c) (unsigned char)(c | 0x20)
247249

250+
#define start_state (parser->type == HTTP_REQUEST ? s_start_req : s_start_res)
251+
248252
#if HTTP_PARSER_STRICT
249253
# define STRICT_CHECK(cond) if (cond) goto error
250254
# define NEW_MESSAGE() (http_should_keep_alive(parser) ? start_state : s_dead)
@@ -253,8 +257,9 @@ enum flags
253257
# define NEW_MESSAGE() start_state
254258
#endif
255259

256-
static inline
257-
size_t parse (http_parser *parser, const char *data, size_t len, int start_state)
260+
size_t http_parser_execute (http_parser *parser,
261+
const char *data,
262+
size_t len)
258263
{
259264
char c, ch;
260265
const char *p, *pe;
@@ -950,6 +955,10 @@ size_t parse (http_parser *parser, const char *data, size_t len, int start_state
950955
header_state = h_C;
951956
break;
952957

958+
case 'p':
959+
header_state = h_matching_proxy_connection;
960+
break;
961+
953962
case 't':
954963
header_state = h_matching_transfer_encoding;
955964
break;
@@ -1007,6 +1016,18 @@ size_t parse (http_parser *parser, const char *data, size_t len, int start_state
10071016
}
10081017
break;
10091018

1019+
/* proxy-connection */
1020+
1021+
case h_matching_proxy_connection:
1022+
index++;
1023+
if (index > sizeof(PROXY_CONNECTION)-1
1024+
|| c != PROXY_CONNECTION[index]) {
1025+
header_state = h_general;
1026+
} else if (index == sizeof(PROXY_CONNECTION)-2) {
1027+
header_state = h_connection;
1028+
}
1029+
break;
1030+
10101031
/* content-length */
10111032

10121033
case h_matching_content_length:
@@ -1256,7 +1277,7 @@ size_t parse (http_parser *parser, const char *data, size_t len, int start_state
12561277
/* Content-Length header given and non-zero */
12571278
state = s_body_identity;
12581279
} else {
1259-
if (start_state == s_start_req || http_should_keep_alive(parser)) {
1280+
if (parser->type == HTTP_REQUEST || http_should_keep_alive(parser)) {
12601281
/* Assume content-length 0 - read the next */
12611282
CALLBACK2(message_complete);
12621283
state = NEW_MESSAGE();
@@ -1408,22 +1429,6 @@ size_t parse (http_parser *parser, const char *data, size_t len, int start_state
14081429
}
14091430

14101431

1411-
size_t
1412-
http_parse_requests (http_parser *parser, const char *data, size_t len)
1413-
{
1414-
if (!parser->state) parser->state = s_start_req;
1415-
return parse(parser, data, len, s_start_req);
1416-
}
1417-
1418-
1419-
size_t
1420-
http_parse_responses (http_parser *parser, const char *data, size_t len)
1421-
{
1422-
if (!parser->state) parser->state = s_start_res;
1423-
return parse(parser, data, len, s_start_res);
1424-
}
1425-
1426-
14271432
int
14281433
http_should_keep_alive (http_parser *parser)
14291434
{
@@ -1446,9 +1451,10 @@ http_should_keep_alive (http_parser *parser)
14461451

14471452

14481453
void
1449-
http_parser_init (http_parser *parser)
1454+
http_parser_init (http_parser *parser, enum http_parser_type t)
14501455
{
1451-
parser->state = 0;
1456+
parser->type = t;
1457+
parser->state = (t == HTTP_REQUEST ? s_start_req : s_start_res);
14521458
parser->on_message_begin = NULL;
14531459
parser->on_path = NULL;
14541460
parser->on_query_string = NULL;

deps/http_parser/http_parser.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2009 Ryan Dahl <[email protected]>
1+
/* Copyright 2009,2010 Ryan Dahl <[email protected]>
22
*
33
* Permission is hereby granted, free of charge, to any person obtaining a copy
44
* of this software and associated documentation files (the "Software"), to
@@ -74,8 +74,11 @@ enum http_method
7474
, HTTP_UNLOCK = 0x4000
7575
};
7676

77+
enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE };
78+
7779
struct http_parser {
7880
/** PRIVATE **/
81+
enum http_parser_type type;
7982
unsigned short state;
8083
unsigned short header_state;
8184
size_t index;
@@ -125,9 +128,8 @@ struct http_parser {
125128
http_cb on_message_complete;
126129
};
127130

128-
void http_parser_init(http_parser *parser);
129-
size_t http_parse_requests(http_parser *parser, const char *data, size_t len);
130-
size_t http_parse_responses(http_parser *parser, const char *data, size_t len);
131+
void http_parser_init(http_parser *parser, enum http_parser_type type);
132+
size_t http_parser_execute(http_parser *parser, const char *data, size_t len);
131133
/* Call this in the on_headers_complete or on_message_complete callback to
132134
* determine if this will be the last message on the connection.
133135
* If you are the server, respond with the "Connection: close" header

0 commit comments

Comments
 (0)