mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00

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.
62 lines
2.3 KiB
Erlang
62 lines
2.3 KiB
Erlang
%% This module sends a response body of varying sizes to test
|
|
%% the cowboy_compress_h stream handler.
|
|
|
|
-module(compress_h).
|
|
|
|
-export([init/2]).
|
|
|
|
init(Req0, State=reply) ->
|
|
Req = case cowboy_req:binding(what, Req0) of
|
|
<<"small">> ->
|
|
cowboy_req:reply(200, #{}, lists:duplicate(100, $a), Req0);
|
|
<<"large">> ->
|
|
cowboy_req:reply(200, #{}, lists:duplicate(100000, $a), Req0);
|
|
<<"over-threshold">> ->
|
|
cowboy_req:reply(200, #{}, lists:duplicate(200, $a), Req0);
|
|
<<"content-encoding">> ->
|
|
cowboy_req:reply(200, #{<<"content-encoding">> => <<"compress">>},
|
|
lists:duplicate(100000, $a), Req0);
|
|
<<"sendfile">> ->
|
|
AppFile = code:where_is_file("cowboy.app"),
|
|
Size = filelib:file_size(AppFile),
|
|
cowboy_req:reply(200, #{}, {sendfile, 0, Size, AppFile}, Req0)
|
|
end,
|
|
{ok, Req, State};
|
|
init(Req0, State=stream_reply) ->
|
|
Req = case cowboy_req:binding(what, Req0) of
|
|
<<"large">> ->
|
|
stream_reply(#{}, Req0);
|
|
<<"content-encoding">> ->
|
|
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}.
|
|
|
|
stream_reply(Headers, Req0) ->
|
|
Data = lists:duplicate(10000, $a),
|
|
Req = cowboy_req:stream_reply(200, Headers, Req0),
|
|
_ = [cowboy_req:stream_body(Data, nofin, Req) || _ <- lists:seq(1,9)],
|
|
cowboy_req:stream_body(Data, fin, Req),
|
|
Req.
|