Skip to content

Commit

Permalink
Sync http parser with upstream (#2730)
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov authored Feb 12, 2018
1 parent 9ba319e commit 185e3f7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 12 deletions.
4 changes: 2 additions & 2 deletions vendor/http-parser/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ HELPER ?=
BINEXT ?=
SOLIBNAME = libhttp_parser
SOMAJOR = 2
SOMINOR = 7
SOREV = 1
SOMINOR = 8
SOREV = 0
ifeq (darwin,$(PLATFORM))
SOEXT ?= dylib
SONAME ?= $(SOLIBNAME).$(SOMAJOR).$(SOMINOR).$(SOEXT)
Expand Down
31 changes: 23 additions & 8 deletions vendor/http-parser/http_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <assert.h>
#include <stddef.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

Expand Down Expand Up @@ -943,7 +942,7 @@ size_t http_parser_execute (http_parser *parser,
/* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */
break;
case 'R': parser->method = HTTP_REPORT; /* or REBIND */ break;
case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH */ break;
case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH, SOURCE */ break;
case 'T': parser->method = HTTP_TRACE; break;
case 'U': parser->method = HTTP_UNLOCK; /* or UNSUBSCRIBE, UNBIND, UNLINK */ break;
default:
Expand Down Expand Up @@ -989,6 +988,7 @@ size_t http_parser_execute (http_parser *parser,
XX(MKCOL, 2, 'A', MKACTIVITY)
XX(MKCOL, 3, 'A', MKCALENDAR)
XX(SUBSCRIBE, 1, 'E', SEARCH)
XX(SUBSCRIBE, 1, 'O', SOURCE)
XX(REPORT, 2, 'B', REBIND)
XX(PROPFIND, 4, 'P', PROPPATCH)
XX(LOCK, 1, 'I', LINK)
Expand Down Expand Up @@ -2367,12 +2367,27 @@ http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
}

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

/* Ports have a max value of 2^16 */
if (v > 0xffff) {
return 1;
uint16_t off;
uint16_t len;
const char* p;
const char* end;
unsigned long v;

off = u->field_data[UF_PORT].off;
len = u->field_data[UF_PORT].len;
end = buf + off + len;

/* NOTE: The characters are already validated and are in the [0-9] range */
assert(off + len <= buflen && "Port number overflow");
v = 0;
for (p = buf + off; p < end; p++) {
v *= 10;
v += *p - '0';

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

u->port = (uint16_t) v;
Expand Down
6 changes: 4 additions & 2 deletions vendor/http-parser/http_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ extern "C" {

/* Also update SONAME in the Makefile whenever you change these. */
#define HTTP_PARSER_VERSION_MAJOR 2
#define HTTP_PARSER_VERSION_MINOR 7
#define HTTP_PARSER_VERSION_PATCH 1
#define HTTP_PARSER_VERSION_MINOR 8
#define HTTP_PARSER_VERSION_PATCH 0

#include <stddef.h>
#if defined(_WIN32) && !defined(__MINGW32__) && \
Expand Down Expand Up @@ -201,6 +201,8 @@ enum http_status
/* RFC-2068, section 19.6.1.2 */ \
XX(31, LINK, LINK) \
XX(32, UNLINK, UNLINK) \
/* icecast */ \
XX(33, SOURCE, SOURCE) \

enum http_method
{
Expand Down
45 changes: 45 additions & 0 deletions vendor/http-parser/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,26 @@ const struct message requests[] =
,.body= ""
}

#define SOURCE_REQUEST 42
, {.name = "source request"
,.type= HTTP_REQUEST
,.raw= "SOURCE /music/sweet/music HTTP/1.1\r\n"
"Host: example.com\r\n"
"\r\n"
,.should_keep_alive= TRUE
,.message_complete_on_eof= FALSE
,.http_major= 1
,.http_minor= 1
,.method= HTTP_SOURCE
,.request_path= "/music/sweet/music"
,.request_url= "/music/sweet/music"
,.query_string= ""
,.fragment= ""
,.num_headers= 1
,.headers= { { "Host", "example.com" } }
,.body= ""
}

, {.name= NULL } /* sentinel */
};

Expand Down Expand Up @@ -3664,6 +3684,30 @@ test_header_cr_no_lf_error (int req)
abort();
}

void
test_no_overflow_parse_url (void)
{
int rv;
struct http_parser_url u;

http_parser_url_init(&u);
rv = http_parser_parse_url("http://example.com:8001", 22, 0, &u);

if (rv != 0) {
fprintf(stderr,
"\n*** test_no_overflow_parse_url invalid return value=%d\n",
rv);
abort();
}

if (u.port != 800) {
fprintf(stderr,
"\n*** test_no_overflow_parse_url invalid port number=%d\n",
u.port);
abort();
}
}

void
test_header_overflow_error (int req)
{
Expand Down Expand Up @@ -4099,6 +4143,7 @@ main (void)
test_header_nread_value();

//// OVERFLOW CONDITIONS
test_no_overflow_parse_url();

test_header_overflow_error(HTTP_REQUEST);
test_no_overflow_long_body(HTTP_REQUEST, 1000);
Expand Down

0 comments on commit 185e3f7

Please sign in to comment.