From dd0fbab6b79e1cefa31adf6e9647f0d76bbade06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Sun, 11 Nov 2018 13:57:26 +0100 Subject: 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. --- test/handlers/provide_range_callback_h.erl | 4 ++-- test/handlers/ranges_provided_auto_h.erl | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/handlers/ranges_provided_auto_h.erl (limited to 'test/handlers') 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}. -- cgit v1.2.3