Skip to content
This repository has been archived by the owner on Nov 6, 2022. It is now read-only.

Add an ability to disable body parsing #478

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions http_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,25 @@ do { \
} \
} while (0)

#define APPLY_ON_HEADERS_COMPLETE_RESULT(V, DEFAULT) \
do { \
switch (V) { \
case 0: \
break; \
\
case 2: \
parser->upgrade = 1; \
\
/* fall through */ \
case 1: \
parser->flags |= F_SKIPBODY; \
break; \
\
default: \
DEFAULT \
} \
} while (0)


#define PROXY_CONNECTION "proxy-connection"
#define CONNECTION "connection"
Expand Down Expand Up @@ -1798,22 +1817,10 @@ size_t http_parser_execute (http_parser *parser,
* we have to simulate it by handling a change in errno below.
*/
if (settings->on_headers_complete) {
switch (settings->on_headers_complete(parser)) {
case 0:
break;

case 2:
parser->upgrade = 1;

/* fall through */
case 1:
parser->flags |= F_SKIPBODY;
break;

default:
APPLY_ON_HEADERS_COMPLETE_RESULT(settings->on_headers_complete(parser),
SET_ERRNO(HPE_CB_headers_complete);
RETURN(p - data); /* Error */
}
);
}

if (HTTP_PARSER_ERRNO(parser) != HPE_OK) {
Expand Down Expand Up @@ -2480,6 +2487,20 @@ http_parser_pause(http_parser *parser, int paused) {
}
}

void
http_parser_continue_after_on_headers_complete(http_parser *parser, int result) {
uint32_t nread;
assert(parser->state == s_headers_done &&
(HTTP_PARSER_ERRNO(parser) == HPE_OK ||
HTTP_PARSER_ERRNO(parser) == HPE_PAUSED));
APPLY_ON_HEADERS_COMPLETE_RESULT(result,
;
);
nread = parser->nread; /* used by the SET_ERRNO macro */
/* unpause the parser - after all, it says "continue", right? */
SET_ERRNO(HPE_OK);
}

int
http_body_is_final(const struct http_parser *parser) {
return parser->state == s_message_done;
Expand Down
8 changes: 8 additions & 0 deletions http_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,14 @@ int http_parser_parse_url(const char *buf, size_t buflen,
/* Pause or un-pause the parser; a nonzero value pauses */
void http_parser_pause(http_parser *parser, int paused);

/* Change the parser state as if the on_headers_complete callback
* returned `result` and un-pause the parser - only makes sense
* between two consecutive invocations of http_parser_execute
* when the first one returned due to the on_headers_complete pausing
* the parser and returning 0.
*/
void http_parser_continue_after_on_headers_complete(http_parser *parser, int result);

/* Checks if this is the final chunk of the body. */
int http_body_is_final(const http_parser *parser);

Expand Down