| 1 |
%% This module defines the ranges_provided callback |
| 2 |
%% and a generate_etag callback that returns something |
| 3 |
%% different depending on query string. It also defines |
| 4 |
%% a last_modified callback that must be ignored when a |
| 5 |
%% date is provided in if_range. |
| 6 |
|
| 7 |
-module(if_range_h). |
| 8 |
|
| 9 |
-export([init/2]). |
| 10 |
-export([content_types_provided/2]). |
| 11 |
-export([ranges_provided/2]). |
| 12 |
-export([generate_etag/2]). |
| 13 |
-export([last_modified/2]). |
| 14 |
-export([get_text_plain/2]). |
| 15 |
-export([get_text_plain_bytes/2]). |
| 16 |
|
| 17 |
init(Req, State) -> |
| 18 |
{cowboy_rest, Req, State}. |
| 19 |
|
| 20 |
content_types_provided(Req, State) -> |
| 21 |
{[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. |
| 22 |
|
| 23 |
%% Simulate the callback being missing. |
| 24 |
ranges_provided(#{qs := <<"missing-ranges_provided">>}, _) -> |
| 25 |
no_call; |
| 26 |
ranges_provided(Req=#{qs := <<"empty-ranges_provided">>}, State) -> |
| 27 |
{[], Req, State}; |
| 28 |
ranges_provided(Req, State) -> |
| 29 |
{[{<<"bytes">>, get_text_plain_bytes}], Req, State}. |
| 30 |
|
| 31 |
generate_etag(Req=#{qs := <<"weak-etag">>}, State) -> |
| 32 |
{{weak, <<"weak-no-match">>}, Req, State}; |
| 33 |
generate_etag(Req, State) -> |
| 34 |
{{strong, <<"strong-and-match">>}, Req, State}. |
| 35 |
|
| 36 |
last_modified(Req, State) -> |
| 37 |
{{{2222, 2, 22}, {11, 11, 11}}, Req, State}. |
| 38 |
|
| 39 |
get_text_plain(Req, State) -> |
| 40 |
{<<"This is REST!">>, Req, State}. |
| 41 |
|
| 42 |
get_text_plain_bytes(Req, State) -> |
| 43 |
%% We send everything in one part, since we are not testing |
| 44 |
%% this callback specifically. |
| 45 |
Body = <<"This is ranged REST!">>, |
| 46 |
{[{{0, byte_size(Body) - 1, byte_size(Body)}, Body}], Req, State}. |