Skip to content

Commit 9b1ff07

Browse files
TooTallNatery
authored andcommitted
Upgrade http-parser with a fix for spaces in headers
1 parent 73b29d7 commit 9b1ff07

3 files changed

Lines changed: 37 additions & 13 deletions

File tree

deps/http_parser/http_parser.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static const char *method_strings[] =
108108

109109
/* ' ', '_', '-' and all alpha-numeric ascii characters are accepted by acceptable_header.
110110
The 'A'-'Z' are lower-cased. */
111-
static const unsigned char acceptable_header[256] = {
111+
static const char acceptable_header[256] = {
112112
/* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */
113113
0, 0, 0, 0, 0, 0, 0, 0,
114114
/* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */
@@ -143,7 +143,7 @@ static const unsigned char acceptable_header[256] = {
143143
'x', 'y', 'z', 0, 0, 0, 0, 0 };
144144

145145

146-
static const int unhex[256] =
146+
static const int8_t unhex[256] =
147147
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
148148
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
149149
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
@@ -155,7 +155,7 @@ static const int unhex[256] =
155155
};
156156

157157

158-
static const int normal_url_char[256] = {
158+
static const uint8_t normal_url_char[256] = {
159159
/* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */
160160
0, 0, 0, 0, 0, 0, 0, 0,
161161
/* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */
@@ -600,7 +600,7 @@ size_t http_parser_execute (http_parser *parser,
600600
if (ch == ' ' && matcher[index] == '\0') {
601601
state = s_req_spaces_before_url;
602602
} else if (ch == matcher[index]) {
603-
; // nada
603+
; /* nada */
604604
} else if (parser->method == HTTP_CONNECT) {
605605
if (index == 1 && ch == 'H') {
606606
parser->method = HTTP_CHECKOUT;
@@ -772,7 +772,7 @@ size_t http_parser_execute (http_parser *parser,
772772

773773
switch (ch) {
774774
case '?':
775-
break; // XXX ignore extra '?' ... is this right?
775+
break; /* XXX ignore extra '?' ... is this right? */
776776
case ' ':
777777
CALLBACK(url);
778778
state = s_req_http_start;
@@ -802,7 +802,7 @@ size_t http_parser_execute (http_parser *parser,
802802

803803
switch (ch) {
804804
case '?':
805-
// allow extra '?' in query string
805+
/* allow extra '?' in query string */
806806
break;
807807
case ' ':
808808
CALLBACK(url);
@@ -1264,6 +1264,7 @@ size_t http_parser_execute (http_parser *parser,
12641264
break;
12651265

12661266
case h_content_length:
1267+
if (ch == ' ') break;
12671268
if (ch < '0' || ch > '9') goto error;
12681269
parser->content_length *= 10;
12691270
parser->content_length += ch - '0';
@@ -1376,7 +1377,7 @@ size_t http_parser_execute (http_parser *parser,
13761377
}
13771378
}
13781379

1379-
// Exit, the rest of the connect is in a different protocol.
1380+
/* Exit, the rest of the connect is in a different protocol. */
13801381
if (parser->upgrade) {
13811382
CALLBACK2(message_complete);
13821383
return (p - data);
@@ -1437,7 +1438,7 @@ size_t http_parser_execute (http_parser *parser,
14371438
{
14381439
assert(parser->flags & F_CHUNKED);
14391440

1440-
c = unhex[(int)ch];
1441+
c = unhex[(unsigned char)ch];
14411442
if (c == -1) goto error;
14421443
parser->content_length = c;
14431444
state = s_chunk_size;
@@ -1453,7 +1454,7 @@ size_t http_parser_execute (http_parser *parser,
14531454
break;
14541455
}
14551456

1456-
c = unhex[(int)ch];
1457+
c = unhex[(unsigned char)ch];
14571458

14581459
if (c == -1) {
14591460
if (ch == ';' || ch == ' ') {
@@ -1545,6 +1546,7 @@ size_t http_parser_execute (http_parser *parser,
15451546
return len;
15461547

15471548
error:
1549+
parser->state = s_dead;
15481550
return (p - data);
15491551
}
15501552

deps/http_parser/http_parser.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,13 @@ enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
100100

101101
struct http_parser {
102102
/** PRIVATE **/
103-
unsigned char type;
103+
unsigned char type : 2;
104+
unsigned char flags : 6;
104105
unsigned char state;
105106
unsigned char header_state;
106107
unsigned char index;
107108

108-
char flags;
109-
110-
uint64_t nread;
109+
uint32_t nread;
111110
int64_t content_length;
112111

113112
/** READ-ONLY **/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var common = require('../common');
2+
var http = require("http");
3+
4+
// Simple test of Node's HTTP Client choking on a response with a "Content-Length: 0 " response header.
5+
// I.E. a space character after the "Content-Length" throws an `error` event.
6+
7+
8+
var s = http.createServer(function(req, res) {
9+
res.writeHead(200, { "Content-Length": "0 " });
10+
res.end();
11+
});
12+
s.listen(common.PORT, function() {
13+
14+
var r = http.createClient(common.PORT);
15+
var request = r.request('GET', '/');
16+
17+
request.on('response', function (response) {
18+
console.log('STATUS: ' + response.statusCode);
19+
s.close();
20+
});
21+
22+
request.end();
23+
});

0 commit comments

Comments
 (0)