From c8242ab396db2fab7468fd5b0eaac54b5c8f3f39 Mon Sep 17 00:00:00 2001 From: James Fish Date: Mon, 18 Feb 2013 21:20:36 +0000 Subject: Add chunked response body fun Adds a new type of streaming response fun. It can be set in a similar way to a streaming body fun with known length: Req2 = cowboy_req:set_resp_body_fun(chunked, StreamFun, Req) The fun, StreamFun, should accept a fun as its single argument. This fun, ChunkFun, is used to send chunks of iodata: ok = ChunkFun(IoData) ChunkFun should not be called with an empty binary or iolist as this will cause HTTP 1.1 clients to believe the stream is over. The final (0 length) chunk will be sent automatically - even if it has already been sent - assuming no exception is raised. Also note that the connection will close after the last chunk for HTTP 1.0 clients. --- test/http_SUITE_data/http_stream_body.erl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'test/http_SUITE_data') diff --git a/test/http_SUITE_data/http_stream_body.erl b/test/http_SUITE_data/http_stream_body.erl index 4f45656..d896797 100644 --- a/test/http_SUITE_data/http_stream_body.erl +++ b/test/http_SUITE_data/http_stream_body.erl @@ -19,7 +19,11 @@ handle(Req, State=#state{headers=_Headers, body=Body, reply=Reply}) -> SLen = iolist_size(Body), cowboy_req:set_resp_body_fun(SLen, SFun, Req); set_resp_close -> - cowboy_req:set_resp_body_fun(SFun, Req) + cowboy_req:set_resp_body_fun(SFun, Req); + set_resp_chunked -> + %% Here Body should be a list of chunks, not a binary. + SFun2 = fun(SendFun) -> lists:foreach(SendFun, Body) end, + cowboy_req:set_resp_body_fun(chunked, SFun2, Req) end, {ok, Req3} = cowboy_req:reply(200, Req2), {ok, Req3, State}. -- cgit v1.2.3