mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
Add cowboy_http_req:set_resp_body_fun/3.
This commit is contained in:
parent
612b8f21fe
commit
937a2b0326
4 changed files with 73 additions and 10 deletions
|
@ -21,7 +21,7 @@
|
|||
-export([chunked_response/1, headers_dupe/1, headers_huge/1,
|
||||
keepalive_nl/1, max_keepalive/1, nc_rand/1, nc_zero/1,
|
||||
pipeline/1, raw/1, set_resp_header/1, set_resp_overwrite/1,
|
||||
set_resp_body/1, response_as_req/1]). %% http.
|
||||
set_resp_body/1, stream_body_set_resp/1, response_as_req/1]). %% http.
|
||||
-export([http_200/1, http_404/1]). %% http and https.
|
||||
-export([http_10_hostless/1]). %% misc.
|
||||
-export([rest_simple/1, rest_keepalive/1]). %% rest.
|
||||
|
@ -36,7 +36,7 @@ groups() ->
|
|||
[{http, [], [chunked_response, headers_dupe, headers_huge,
|
||||
keepalive_nl, max_keepalive, nc_rand, nc_zero, pipeline, raw,
|
||||
set_resp_header, set_resp_overwrite,
|
||||
set_resp_body, response_as_req] ++ BaseTests},
|
||||
set_resp_body, response_as_req, stream_body_set_resp] ++ BaseTests},
|
||||
{https, [], BaseTests},
|
||||
{misc, [], [http_10_hostless]},
|
||||
{rest, [], [rest_simple, rest_keepalive]}].
|
||||
|
@ -115,6 +115,8 @@ init_http_dispatch() ->
|
|||
[{headers, [{<<"Server">>, <<"DesireDrive/1.0">>}]}]},
|
||||
{[<<"set_resp">>, <<"body">>], http_handler_set_resp,
|
||||
[{body, <<"A flameless dance does not equal a cycle">>}]},
|
||||
{[<<"stream_body">>, <<"set_resp">>], http_handler_stream_body,
|
||||
[{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
|
||||
{[], http_handler, []}
|
||||
]}
|
||||
].
|
||||
|
@ -328,6 +330,16 @@ The document has moved
|
|||
</BODY></HTML>",
|
||||
{Packet, 400} = raw_req(Packet, Config).
|
||||
|
||||
stream_body_set_resp(Config) ->
|
||||
{port, Port} = lists:keyfind(port, 1, Config),
|
||||
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
||||
[binary, {active, false}, {packet, raw}]),
|
||||
ok = gen_tcp:send(Socket, "GET /stream_body/set_resp HTTP/1.1\r\n"
|
||||
"Host: localhost\r\nConnection: close\r\n\r\n"),
|
||||
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
|
||||
{_Start, _Length} = binary:match(Data, <<"stream_body_set_resp">>).
|
||||
|
||||
|
||||
%% http and https.
|
||||
|
||||
build_url(Path, Config) ->
|
||||
|
|
24
test/http_handler_stream_body.erl
Normal file
24
test/http_handler_stream_body.erl
Normal file
|
@ -0,0 +1,24 @@
|
|||
%% Feel free to use, reuse and abuse the code in this file.
|
||||
|
||||
-module(http_handler_stream_body).
|
||||
-behaviour(cowboy_http_handler).
|
||||
-export([init/3, handle/2, terminate/2]).
|
||||
|
||||
-record(state, {headers, body, reply}).
|
||||
|
||||
init({_Transport, http}, Req, Opts) ->
|
||||
Headers = proplists:get_value(headers, Opts, []),
|
||||
Body = proplists:get_value(body, Opts, "http_handler_stream_body"),
|
||||
Reply = proplists:get_value(reply, Opts),
|
||||
{ok, Req, #state{headers=Headers, body=Body, reply=Reply}}.
|
||||
|
||||
handle(Req, State=#state{headers=_Headers, body=Body, reply=set_resp}) ->
|
||||
{ok, Transport, Socket} = cowboy_http_req:transport(Req),
|
||||
SFun = fun() -> Transport:send(Socket, Body), sent end,
|
||||
SLen = iolist_size(Body),
|
||||
{ok, Req2} = cowboy_http_req:set_resp_body_fun(SLen, SFun, Req),
|
||||
{ok, Req3} = cowboy_http_req:reply(200, Req2),
|
||||
{ok, Req3, State}.
|
||||
|
||||
terminate(_Req, _State) ->
|
||||
ok.
|
Loading…
Add table
Add a link
Reference in a new issue