aboutsummaryrefslogtreecommitdiffstats
path: root/test/handlers
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2018-11-09 17:42:37 +0100
committerLoïc Hoguin <[email protected]>2018-11-09 17:42:37 +0100
commitd7b7580b3913c17b404319cc4c153748d5e59194 (patch)
tree3026f38e4bcfdcb26d067a4e5aec6d401700c3cc /test/handlers
parent29043aa7b4d11e377bc76d453f592ea5a6df1f43 (diff)
downloadcowboy-d7b7580b3913c17b404319cc4c153748d5e59194.tar.gz
cowboy-d7b7580b3913c17b404319cc4c153748d5e59194.tar.bz2
cowboy-d7b7580b3913c17b404319cc4c153748d5e59194.zip
Add sendfile support to cowboy_req:stream_body
It is now possible to stream one or more sendfile tuples. A simple example of what can now be done would be for example to build a tar file on the fly using the sendfile syscall for sending the files, or to support Range requests with more than one range with the sendfile syscall. When using cowboy_compress_h unfortunately we have to read the file in order to send it. More options will be added at a later time to make sure users don't read too much into memory. This is a new feature however so existing code is not affected. Also rework cowboy_http's data sending to be flatter.
Diffstat (limited to 'test/handlers')
-rw-r--r--test/handlers/compress_h.erl25
-rw-r--r--test/handlers/resp_h.erl17
2 files changed, 41 insertions, 1 deletions
diff --git a/test/handlers/compress_h.erl b/test/handlers/compress_h.erl
index 4b4dbe7..1509d82 100644
--- a/test/handlers/compress_h.erl
+++ b/test/handlers/compress_h.erl
@@ -27,7 +27,30 @@ init(Req0, State=stream_reply) ->
<<"large">> ->
stream_reply(#{}, Req0);
<<"content-encoding">> ->
- stream_reply(#{<<"content-encoding">> => <<"compress">>}, Req0)
+ stream_reply(#{<<"content-encoding">> => <<"compress">>}, Req0);
+ <<"sendfile">> ->
+ Data = lists:duplicate(10000, $a),
+ AppFile = code:where_is_file("cowboy.app"),
+ Size = filelib:file_size(AppFile),
+ Req1 = cowboy_req:stream_reply(200, Req0),
+ %% We send a few files interspersed into other data.
+ cowboy_req:stream_body(Data, nofin, Req1),
+ cowboy_req:stream_body({sendfile, 0, Size, AppFile}, nofin, Req1),
+ cowboy_req:stream_body(Data, nofin, Req1),
+ cowboy_req:stream_body({sendfile, 0, Size, AppFile}, nofin, Req1),
+ cowboy_req:stream_body(Data, fin, Req1),
+ Req1;
+ <<"sendfile_fin">> ->
+ Data = lists:duplicate(10000, $a),
+ AppFile = code:where_is_file("cowboy.app"),
+ Size = filelib:file_size(AppFile),
+ Req1 = cowboy_req:stream_reply(200, Req0),
+ %% We send a few files interspersed into other data.
+ cowboy_req:stream_body(Data, nofin, Req1),
+ cowboy_req:stream_body({sendfile, 0, Size, AppFile}, nofin, Req1),
+ cowboy_req:stream_body(Data, nofin, Req1),
+ cowboy_req:stream_body({sendfile, 0, Size, AppFile}, fin, Req1),
+ Req1
end,
{ok, Req, State}.
diff --git a/test/handlers/resp_h.erl b/test/handlers/resp_h.erl
index bfcb95c..7a80e75 100644
--- a/test/handlers/resp_h.erl
+++ b/test/handlers/resp_h.erl
@@ -219,6 +219,23 @@ do(<<"stream_body">>, Req0, Opts) ->
Req = cowboy_req:stream_reply(200, Req0),
cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
{ok, Req, Opts};
+ <<"sendfile">> ->
+ AppFile = code:where_is_file("cowboy.app"),
+ AppSize = filelib:file_size(AppFile),
+ Req = cowboy_req:stream_reply(200, Req0),
+ cowboy_req:stream_body(<<"Hello ">>, nofin, Req),
+ cowboy_req:stream_body({sendfile, 0, AppSize, AppFile}, nofin, Req),
+ cowboy_req:stream_body(<<" interspersed ">>, nofin, Req),
+ cowboy_req:stream_body({sendfile, 0, AppSize, AppFile}, nofin, Req),
+ cowboy_req:stream_body(<<" world!">>, fin, Req),
+ {ok, Req, Opts};
+ <<"sendfile_fin">> ->
+ AppFile = code:where_is_file("cowboy.app"),
+ AppSize = filelib:file_size(AppFile),
+ Req = cowboy_req:stream_reply(200, Req0),
+ cowboy_req:stream_body(<<"Hello! ">>, nofin, Req),
+ cowboy_req:stream_body({sendfile, 0, AppSize, AppFile}, fin, Req),
+ {ok, Req, Opts};
_ ->
%% Call stream_body without initiating streaming.
cowboy_req:stream_body(<<0:800000>>, fin, Req0),