Skip to content

Commit

Permalink
fun with http_parser
Browse files Browse the repository at this point in the history
  • Loading branch information
cjihrig committed Nov 29, 2018
1 parent 7b8058a commit e55765d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
3 changes: 2 additions & 1 deletion deps/http_parser/http_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ do { \
#define COUNT_HEADER_SIZE(V) \
do { \
parser->nread += (V); \
if (UNLIKELY(parser->nread > (HTTP_MAX_HEADER_SIZE))) { \
if (UNLIKELY(parser->nread > parser->max_header_size)) { \
SET_ERRNO(HPE_HEADER_OVERFLOW); \
goto error; \
} \
Expand Down Expand Up @@ -2105,6 +2105,7 @@ http_parser_init (http_parser *parser, enum http_parser_type t)
parser->type = t;
parser->state = (t == HTTP_REQUEST ? s_start_req : (t == HTTP_RESPONSE ? s_start_res : s_start_req_or_res));
parser->http_errno = HPE_OK;
parser->max_header_size = HTTP_MAX_HEADER_SIZE;
}

void
Expand Down
2 changes: 2 additions & 0 deletions deps/http_parser/http_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ struct http_parser {

/** PUBLIC **/
void *data; /* A pointer to get hook to the "connection" or "socket" object */

uint32_t max_header_size;
};


Expand Down
2 changes: 1 addition & 1 deletion lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function parserOnMessageComplete() {


const parsers = new FreeList('parsers', 1000, function parsersCb() {
const parser = new HTTPParser(HTTPParser.REQUEST);
const parser = new HTTPParser(HTTPParser.REQUEST, 1024);

cleanParser(parser);

Expand Down
15 changes: 10 additions & 5 deletions src/node_http_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,12 @@ struct StringPtr {

class Parser : public AsyncWrap, public StreamListener {
public:
Parser(Environment* env, Local<Object> wrap, parser_type_t type)
Parser(Environment* env, Local<Object> wrap, parser_type_t type,
uint32_t max_header_size)
: AsyncWrap(env, wrap, AsyncWrap::PROVIDER_HTTPPARSER),
current_buffer_len_(0),
current_buffer_data_(nullptr) {
Init(type);
Init(type, max_header_size);
}


Expand Down Expand Up @@ -423,7 +424,9 @@ class Parser : public AsyncWrap, public StreamListener {
parser_type_t type =
static_cast<parser_type_t>(args[0].As<Int32>()->Value());
CHECK(type == HTTP_REQUEST || type == HTTP_RESPONSE);
new Parser(env, args.This(), type);
uint32_t max_header_size = args[1]->IsUint32() ?
args[1].As<Uint32>()->Value(): 0;
new Parser(env, args.This(), type, max_header_size);
}


Expand Down Expand Up @@ -515,7 +518,7 @@ class Parser : public AsyncWrap, public StreamListener {
if (isReused) {
parser->AsyncReset();
}
parser->Init(type);
parser->Init(type, 1024);
}


Expand Down Expand Up @@ -781,12 +784,14 @@ class Parser : public AsyncWrap, public StreamListener {
}


void Init(parser_type_t type) {
void Init(parser_type_t type, uint32_t max_header_size) {
#ifdef NODE_EXPERIMENTAL_HTTP
llhttp_init(&parser_, type, &settings);
header_nread_ = 0;
#else /* !NODE_EXPERIMENTAL_HTTP */
http_parser_init(&parser_, type);
parser_.max_header_size = max_header_size;
printf("max_header_size is %d %d\n", max_header_size, parser_.max_header_size);
#endif /* NODE_EXPERIMENTAL_HTTP */
url_.Reset();
status_message_.Reset();
Expand Down

0 comments on commit e55765d

Please sign in to comment.