ranges_provided_auto_h.erl 848 B

123456789101112131415161718192021222324252627
  1. %% This module defines the ranges_provided callback
  2. %% which returns the auto option for bytes ranges
  3. %% and the normal ProvideCallback that returns
  4. %% something different depending on query string.
  5. -module(ranges_provided_auto_h).
  6. -export([init/2]).
  7. -export([content_types_provided/2]).
  8. -export([ranges_provided/2]).
  9. -export([get_text_plain/2]).
  10. init(Req, State) ->
  11. {cowboy_rest, Req, State}.
  12. content_types_provided(Req, State) ->
  13. {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}.
  14. ranges_provided(Req, State) ->
  15. {[{<<"bytes">>, auto}], Req, State}.
  16. get_text_plain(Req=#{qs := <<"data">>}, State) ->
  17. {<<"This is ranged REST!">>, Req, State};
  18. get_text_plain(Req=#{qs := <<"sendfile">>}, State) ->
  19. Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app",
  20. Size = filelib:file_size(Path),
  21. {{sendfile, 0, Size, Path}, Req, State}.