mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
Add chunked reply support.
Send the status line and headers using cowboy_http_req:chunked_reply/3, and individual chunks with cowboy_http_req:chunk/2.
This commit is contained in:
parent
18582a7a39
commit
420f5baf98
5 changed files with 51 additions and 8 deletions
|
@ -65,5 +65,5 @@
|
||||||
buffer = <<>> :: binary(),
|
buffer = <<>> :: binary(),
|
||||||
|
|
||||||
%% Response.
|
%% Response.
|
||||||
resp_state = locked :: locked | waiting | done
|
resp_state = locked :: locked | waiting | chunks | done
|
||||||
}).
|
}).
|
||||||
|
|
|
@ -198,9 +198,9 @@ handler_terminate(HandlerState, Req=#http_req{buffer=Buffer},
|
||||||
HandlerRes = (catch Handler:terminate(
|
HandlerRes = (catch Handler:terminate(
|
||||||
Req#http_req{resp_state=locked}, HandlerState)),
|
Req#http_req{resp_state=locked}, HandlerState)),
|
||||||
BodyRes = ensure_body_processed(Req),
|
BodyRes = ensure_body_processed(Req),
|
||||||
ensure_response(Req, State),
|
RespRes = ensure_response(Req, State),
|
||||||
case {HandlerRes, BodyRes, State#state.connection} of
|
case {HandlerRes, BodyRes, RespRes, State#state.connection} of
|
||||||
{ok, ok, keepalive} ->
|
{ok, ok, ok, keepalive} ->
|
||||||
?MODULE:parse_request(State#state{buffer=Buffer});
|
?MODULE:parse_request(State#state{buffer=Buffer});
|
||||||
_Closed ->
|
_Closed ->
|
||||||
terminate(State)
|
terminate(State)
|
||||||
|
@ -223,7 +223,12 @@ ensure_response(#http_req{resp_state=done}, _State) ->
|
||||||
%% No response has been sent but everything apparently went fine.
|
%% No response has been sent but everything apparently went fine.
|
||||||
%% Reply with 204 No Content to indicate this.
|
%% Reply with 204 No Content to indicate this.
|
||||||
ensure_response(#http_req{resp_state=waiting}, State) ->
|
ensure_response(#http_req{resp_state=waiting}, State) ->
|
||||||
error_response(204, State).
|
error_response(204, State);
|
||||||
|
%% Close the chunked reply.
|
||||||
|
ensure_response(#http_req{socket=Socket, transport=Transport,
|
||||||
|
resp_state=chunks}, _State) ->
|
||||||
|
Transport:send(Socket, <<"0\r\n\r\n">>),
|
||||||
|
close.
|
||||||
|
|
||||||
-spec error_response(Code::http_status(), State::#state{}) -> ok.
|
-spec error_response(Code::http_status(), State::#state{}) -> ok.
|
||||||
error_response(Code, #state{socket=Socket,
|
error_response(Code, #state{socket=Socket,
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
]). %% Request Body API.
|
]). %% Request Body API.
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
reply/4
|
reply/4, chunked_reply/3, chunk/2
|
||||||
]). %% Response API.
|
]). %% Response API.
|
||||||
|
|
||||||
-include("include/http.hrl").
|
-include("include/http.hrl").
|
||||||
|
@ -192,6 +192,22 @@ reply(Code, Headers, Body, Req=#http_req{socket=Socket,
|
||||||
Transport:send(Socket, [Head, Body]),
|
Transport:send(Socket, [Head, Body]),
|
||||||
{ok, Req#http_req{resp_state=done}}.
|
{ok, Req#http_req{resp_state=done}}.
|
||||||
|
|
||||||
|
-spec chunked_reply(Code::http_status(), Headers::http_headers(),
|
||||||
|
Req::#http_req{}) -> {ok, Req::#http_req{}}.
|
||||||
|
chunked_reply(Code, Headers, Req=#http_req{socket=Socket, transport=Transport,
|
||||||
|
resp_state=waiting}) ->
|
||||||
|
Head = response_head(Code, Headers, [
|
||||||
|
{<<"Connection">>, <<"close">>},
|
||||||
|
{<<"Transfer-Encoding">>, <<"chunked">>}
|
||||||
|
]),
|
||||||
|
Transport:send(Socket, Head),
|
||||||
|
{ok, Req#http_req{resp_state=chunks}}.
|
||||||
|
|
||||||
|
-spec chunk(Data::iodata(), Req::#http_req{}) -> ok.
|
||||||
|
chunk(Data, #http_req{socket=Socket, transport=Transport, resp_state=chunks}) ->
|
||||||
|
Transport:send(Socket, [integer_to_list(iolist_size(Data), 16),
|
||||||
|
<<"\r\n">>, Data, <<"\r\n">>]).
|
||||||
|
|
||||||
%% Internal.
|
%% Internal.
|
||||||
|
|
||||||
-spec parse_qs(Qs::binary()) -> list({Name::binary(), Value::binary() | true}).
|
-spec parse_qs(Qs::binary()) -> list({Name::binary(), Value::binary() | true}).
|
||||||
|
|
17
test/chunked_handler.erl
Normal file
17
test/chunked_handler.erl
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
%% Feel free to use, reuse and abuse the code in this file.
|
||||||
|
|
||||||
|
-module(chunked_handler).
|
||||||
|
-behaviour(cowboy_http_handler).
|
||||||
|
-export([init/3, handle/2, terminate/2]).
|
||||||
|
|
||||||
|
init({_Transport, http}, Req, _Opts) ->
|
||||||
|
{ok, Req, undefined}.
|
||||||
|
|
||||||
|
handle(Req, State) ->
|
||||||
|
{ok, Req2} = cowboy_http_req:chunked_reply(200, [], Req),
|
||||||
|
cowboy_http_req:chunk("chunked_handler\r\n", Req2),
|
||||||
|
cowboy_http_req:chunk("works fine!", Req2),
|
||||||
|
{ok, Req2, State}.
|
||||||
|
|
||||||
|
terminate(_Req, _State) ->
|
||||||
|
ok.
|
|
@ -18,7 +18,7 @@
|
||||||
|
|
||||||
-export([all/0, groups/0, init_per_suite/1, end_per_suite/1,
|
-export([all/0, groups/0, init_per_suite/1, end_per_suite/1,
|
||||||
init_per_group/2, end_per_group/2]). %% ct.
|
init_per_group/2, end_per_group/2]). %% ct.
|
||||||
-export([headers_dupe/1, headers_huge/1,
|
-export([chunked_response/1, headers_dupe/1, headers_huge/1,
|
||||||
nc_rand/1, pipeline/1, raw/1]). %% http.
|
nc_rand/1, pipeline/1, raw/1]). %% http.
|
||||||
-export([http_200/1, http_404/1, websocket/1]). %% http and https.
|
-export([http_200/1, http_404/1, websocket/1]). %% http and https.
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ all() ->
|
||||||
|
|
||||||
groups() ->
|
groups() ->
|
||||||
BaseTests = [http_200, http_404],
|
BaseTests = [http_200, http_404],
|
||||||
[{http, [], [headers_dupe, headers_huge,
|
[{http, [], [chunked_response, headers_dupe, headers_huge,
|
||||||
nc_rand, pipeline, raw, websocket] ++ BaseTests},
|
nc_rand, pipeline, raw, websocket] ++ BaseTests},
|
||||||
{https, [], BaseTests}].
|
{https, [], BaseTests}].
|
||||||
|
|
||||||
|
@ -79,6 +79,7 @@ end_per_group(https, _Config) ->
|
||||||
init_http_dispatch() ->
|
init_http_dispatch() ->
|
||||||
[
|
[
|
||||||
{[<<"localhost">>], [
|
{[<<"localhost">>], [
|
||||||
|
{[<<"chunked_response">>], chunked_handler, []},
|
||||||
{[<<"websocket">>], websocket_handler, []},
|
{[<<"websocket">>], websocket_handler, []},
|
||||||
{[<<"headers">>, <<"dupe">>], http_handler,
|
{[<<"headers">>, <<"dupe">>], http_handler,
|
||||||
[{headers, [{<<"Connection">>, <<"close">>}]}]},
|
[{headers, [{<<"Connection">>, <<"close">>}]}]},
|
||||||
|
@ -91,6 +92,10 @@ init_https_dispatch() ->
|
||||||
|
|
||||||
%% http.
|
%% http.
|
||||||
|
|
||||||
|
chunked_response(Config) ->
|
||||||
|
{ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "chunked_handler\r\nworks fine!"}} =
|
||||||
|
httpc:request(build_url("/chunked_response", Config)).
|
||||||
|
|
||||||
headers_dupe(Config) ->
|
headers_dupe(Config) ->
|
||||||
{port, Port} = lists:keyfind(port, 1, Config),
|
{port, Port} = lists:keyfind(port, 1, Config),
|
||||||
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue