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

Commit

Permalink
src: support body in Upgrade requests
Browse files Browse the repository at this point in the history
Invoke message_complete cb for upgrade with body.

(D1364677 + D1380182 orig author afrind@fb.com)

Fix: #234
PR-URL: #235
Reviewed-By: Fedor Indutny <fedor@indutny.com>
  • Loading branch information
KjellSchubert authored and indutny committed Apr 23, 2015
1 parent d767545 commit dff604d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
11 changes: 9 additions & 2 deletions http_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1832,8 +1832,11 @@ size_t http_parser_execute (http_parser *parser,

parser->nread = 0;

/* Exit, the rest of the connect is in a different protocol. */
if (parser->upgrade) {
int hasBody = parser->flags & F_CHUNKED ||
(parser->content_length > 0 && parser->content_length != ULLONG_MAX);
if (parser->upgrade && (parser->method == HTTP_CONNECT ||
(parser->flags & F_SKIPBODY) || !hasBody)) {
/* Exit, the rest of the message is in a different protocol. */
UPDATE_STATE(NEW_MESSAGE());
CALLBACK_NOTIFY(message_complete);
RETURN((p - data) + 1);
Expand Down Expand Up @@ -1915,6 +1918,10 @@ size_t http_parser_execute (http_parser *parser,
case s_message_done:
UPDATE_STATE(NEW_MESSAGE());
CALLBACK_NOTIFY(message_complete);
if (parser->upgrade) {
/* Exit, the rest of the message is in a different protocol. */
RETURN((p - data) + 1);
}
break;

case s_chunk_size_start:
Expand Down
54 changes: 53 additions & 1 deletion test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,58 @@ const struct message requests[] =
,.body= ""
}

#define UPGRADE_POST_REQUEST 38
, {.name = "upgrade post request"
,.type= HTTP_REQUEST
,.raw= "POST /demo HTTP/1.1\r\n"
"Host: example.com\r\n"
"Connection: Upgrade\r\n"
"Upgrade: HTTP/2.0\r\n"
"Content-Length: 15\r\n"
"\r\n"
"sweet post body"
"Hot diggity dogg"
,.should_keep_alive= TRUE
,.message_complete_on_eof= FALSE
,.http_major= 1
,.http_minor= 1
,.method= HTTP_POST
,.request_path= "/demo"
,.request_url= "/demo"
,.num_headers= 4
,.upgrade="Hot diggity dogg"
,.headers= { { "Host", "example.com" }
, { "Connection", "Upgrade" }
, { "Upgrade", "HTTP/2.0" }
, { "Content-Length", "15" }
}
,.body= "sweet post body"
}

#define CONNECT_WITH_BODY_REQUEST 39
, {.name = "connect with body request"
,.type= HTTP_REQUEST
,.raw= "CONNECT foo.bar.com:443 HTTP/1.0\r\n"
"User-agent: Mozilla/1.1N\r\n"
"Proxy-authorization: basic aGVsbG86d29ybGQ=\r\n"
"Content-Length: 10\r\n"
"\r\n"
"blarfcicle"
,.should_keep_alive= FALSE
,.message_complete_on_eof= FALSE
,.http_major= 1
,.http_minor= 0
,.method= HTTP_CONNECT
,.request_url= "foo.bar.com:443"
,.num_headers= 3
,.upgrade="blarfcicle"
,.headers= { { "User-agent", "Mozilla/1.1N" }
, { "Proxy-authorization", "basic aGVsbG86d29ybGQ=" }
, { "Content-Length", "10" }
}
,.body= ""
}

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

Expand Down Expand Up @@ -3006,7 +3058,7 @@ test_message (const struct message *message)
if (msg1len) {
read = parse(msg1, msg1len);

if (message->upgrade && parser->upgrade) {
if (message->upgrade && parser->upgrade && num_messages > 0) {
messages[num_messages - 1].upgrade = msg1 + read;
goto test;
}
Expand Down

0 comments on commit dff604d

Please sign in to comment.