diff options
author | Loïc Hoguin <[email protected]> | 2018-11-11 13:57:26 +0100 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2018-11-11 13:57:26 +0100 |
commit | dd0fbab6b79e1cefa31adf6e9647f0d76bbade06 (patch) | |
tree | 595eb0d4ff3af00427598b8d2bb57c371b919d25 /test/handlers | |
parent | d7b7580b3913c17b404319cc4c153748d5e59194 (diff) | |
download | cowboy-dd0fbab6b79e1cefa31adf6e9647f0d76bbade06.tar.gz cowboy-dd0fbab6b79e1cefa31adf6e9647f0d76bbade06.tar.bz2 cowboy-dd0fbab6b79e1cefa31adf6e9647f0d76bbade06.zip |
Add automatic ranged request handling for bytes units
Returning the atom auto instead of a callback informs Cowboy
that it needs to handle range requests automatically. This
changes the behavior so that the ProvideCallback function
is called and then Cowboy splits the data on its own and
sends the response without any other user involvement other
than defining the ranges_provided/2 callback.
This is a quick and dirty way to add range request support
to resources, and will be good enough for many cases including
for cowboy_static as it also works when the normal response
body is a sendfile tuple.
Diffstat (limited to 'test/handlers')
-rw-r--r-- | test/handlers/provide_range_callback_h.erl | 4 | ||||
-rw-r--r-- | test/handlers/ranges_provided_auto_h.erl | 27 |
2 files changed, 29 insertions, 2 deletions
diff --git a/test/handlers/provide_range_callback_h.erl b/test/handlers/provide_range_callback_h.erl index f14a544..136e37e 100644 --- a/test/handlers/provide_range_callback_h.erl +++ b/test/handlers/provide_range_callback_h.erl @@ -1,4 +1,4 @@ -%% This module defines the range_satisfiable callback +%% This module defines many callbacks relevant to range requests %% and return something different depending on query string. -module(provide_range_callback_h). @@ -41,7 +41,7 @@ get_text_plain(Req, State) -> %% Simulate the callback being missing, otherwise expect true/false. get_text_plain_bytes(#{qs := <<"missing">>}, _) -> - ct_helper_error_h:ignore(cowboy_rest, set_ranged_body, 3), + ct_helper_error_h:ignore(cowboy_rest, set_ranged_body_callback, 3), no_call; get_text_plain_bytes(Req=#{range := {_, [{From=0, infinity}]}}, State) -> %% We send everything in one part. diff --git a/test/handlers/ranges_provided_auto_h.erl b/test/handlers/ranges_provided_auto_h.erl new file mode 100644 index 0000000..f7e6595 --- /dev/null +++ b/test/handlers/ranges_provided_auto_h.erl @@ -0,0 +1,27 @@ +%% This module defines the ranges_provided callback +%% which returns the auto option for bytes ranges +%% and the normal ProvideCallback that returns +%% something different depending on query string. + +-module(ranges_provided_auto_h). + +-export([init/2]). +-export([content_types_provided/2]). +-export([ranges_provided/2]). +-export([get_text_plain/2]). + +init(Req, State) -> + {cowboy_rest, Req, State}. + +content_types_provided(Req, State) -> + {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. + +ranges_provided(Req, State) -> + {[{<<"bytes">>, auto}], Req, State}. + +get_text_plain(Req=#{qs := <<"data">>}, State) -> + {<<"This is ranged REST!">>, Req, State}; +get_text_plain(Req=#{qs := <<"sendfile">>}, State) -> + Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app", + Size = filelib:file_size(Path), + {{sendfile, 0, Size, Path}, Req, State}. |