0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Handle expect: 100-continue request headers

The 100 continue response will only be sent if the client
has not sent the body yet (at all), if the connection is
HTTP/1.1 or above and if the user has not sent it yet.

The 100 continue response is sent when the user calls
read_body and it is cowboy_stream_h's responsibility
to send it. This means projects that don't use the
cowboy_stream_h stream handler will need to handle the
expect header themselves (but that's okay because they
might have different considerations than normal Cowboy).
This commit is contained in:
Loïc Hoguin 2017-10-30 16:21:25 +00:00
parent f3d6b05b86
commit 217fac7f44
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
3 changed files with 77 additions and 7 deletions

View file

@ -18,6 +18,9 @@ echo(<<"read_body">>, Req0, Opts) ->
_ -> ok
end,
{_, Body, Req} = case cowboy_req:path(Req0) of
<<"/100-continue", _/bits>> ->
cowboy_req:inform(100, Req0),
cowboy_req:read_body(Req0);
<<"/full", _/bits>> -> read_body(Req0, <<>>);
<<"/opts", _/bits>> -> cowboy_req:read_body(Req0, Opts);
_ -> cowboy_req:read_body(Req0)

View file

@ -54,6 +54,7 @@ init_dispatch(Config) ->
{"/opts/:key/length", echo_h, #{length => 1000}},
{"/opts/:key/period", echo_h, #{length => 999999999, period => 1000}},
{"/opts/:key/timeout", echo_h, #{timeout => 1000, crash => true}},
{"/100-continue/:key", echo_h, []},
{"/full/:key", echo_h, []},
{"/no/:key", echo_h, []},
{"/direct/:key/[...]", echo_h, []},
@ -456,6 +457,33 @@ do_read_body_timeout(Path, Body, Config) ->
{response, _, 500, _} = gun:await(ConnPid, Ref),
gun:close(ConnPid).
read_body_expect_100_continue(Config) ->
doc("Request body with a 100-continue expect header."),
do_read_body_expect_100_continue("/read_body", Config).
read_body_expect_100_continue_user_sent(Config) ->
doc("Request body with a 100-continue expect header, 100 response sent by handler."),
do_read_body_expect_100_continue("/100-continue/read_body", Config).
do_read_body_expect_100_continue(Path, Config) ->
ConnPid = gun_open(Config),
Body = <<0:8000000>>,
Headers = [
{<<"accept-encoding">>, <<"gzip">>},
{<<"expect">>, <<"100-continue">>},
{<<"content-length">>, integer_to_binary(byte_size(Body))}
],
Ref = gun:post(ConnPid, Path, Headers),
{inform, 100, []} = gun:await(ConnPid, Ref),
gun:data(ConnPid, Ref, fin, Body),
{response, IsFin, 200, RespHeaders} = gun:await(ConnPid, Ref),
{ok, RespBody} = case IsFin of
nofin -> gun:await_body(ConnPid, Ref);
fin -> {ok, <<>>}
end,
gun:close(ConnPid),
do_decode(RespHeaders, RespBody).
read_urlencoded_body(Config) ->
doc("application/x-www-form-urlencoded request body."),
<<"[]">> = do_body("POST", "/read_urlencoded_body", [], <<>>, Config),