Skip to content

Commit cacb07d

Browse files
bnoordhuisindutny
authored andcommitted
parser: fix Content-Length header parsing.
Commit e2e467b ("Update http-parser to 2.6.1") enforces that messages contain no more than one Content-Length header but it considers any header that starts with "Content-Length" as a duplicate. Fix: nodejs#324 PR-URL: nodejs#325 Reviewed-By: Fedor Indutny <fedor@indutny.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b2cc8e4 commit cacb07d

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

http_parser.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,12 +1366,7 @@ size_t http_parser_execute (http_parser *parser,
13661366
|| c != CONTENT_LENGTH[parser->index]) {
13671367
parser->header_state = h_general;
13681368
} else if (parser->index == sizeof(CONTENT_LENGTH)-2) {
1369-
if (parser->flags & F_CONTENTLENGTH) {
1370-
SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH);
1371-
goto error;
1372-
}
13731369
parser->header_state = h_content_length;
1374-
parser->flags |= F_CONTENTLENGTH;
13751370
}
13761371
break;
13771372

@@ -1474,6 +1469,12 @@ size_t http_parser_execute (http_parser *parser,
14741469
goto error;
14751470
}
14761471

1472+
if (parser->flags & F_CONTENTLENGTH) {
1473+
SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH);
1474+
goto error;
1475+
}
1476+
1477+
parser->flags |= F_CONTENTLENGTH;
14771478
parser->content_length = ch - '0';
14781479
break;
14791480

test.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,32 @@ const struct message responses[] =
17451745
,.body= ""
17461746
}
17471747

1748+
#define CONTENT_LENGTH_X 21
1749+
, {.name= "Content-Length-X"
1750+
,.type= HTTP_RESPONSE
1751+
,.raw= "HTTP/1.1 200 OK\r\n"
1752+
"Content-Length-X: 0\r\n"
1753+
"Transfer-Encoding: chunked\r\n"
1754+
"\r\n"
1755+
"2\r\n"
1756+
"OK\r\n"
1757+
"0\r\n"
1758+
"\r\n"
1759+
,.should_keep_alive= TRUE
1760+
,.message_complete_on_eof= FALSE
1761+
,.http_major= 1
1762+
,.http_minor= 1
1763+
,.status_code= 200
1764+
,.response_status= "OK"
1765+
,.num_headers= 2
1766+
,.headers= { { "Content-Length-X", "0" }
1767+
, { "Transfer-Encoding", "chunked" }
1768+
}
1769+
,.body= "OK"
1770+
,.num_chunks_complete= 2
1771+
,.chunk_lengths= { 2 }
1772+
}
1773+
17481774
, {.name= NULL } /* sentinel */
17491775
};
17501776

0 commit comments

Comments
 (0)