| 1 |
%% This module defines many callbacks relevant to range requests |
| 2 |
%% and return something different depending on query string. |
| 3 |
|
| 4 |
-module(provide_range_callback_h). |
| 5 |
|
| 6 |
-export([init/2]). |
| 7 |
-export([content_types_provided/2]). |
| 8 |
-export([ranges_provided/2]). |
| 9 |
-export([expires/2]). |
| 10 |
-export([generate_etag/2]). |
| 11 |
-export([last_modified/2]). |
| 12 |
-export([get_text_plain/2]). |
| 13 |
-export([get_text_plain_bytes/2]). |
| 14 |
|
| 15 |
init(Req, State) -> |
| 16 |
{cowboy_rest, Req, State}. |
| 17 |
|
| 18 |
content_types_provided(Req, State) -> |
| 19 |
{[ |
| 20 |
{{<<"text">>, <<"plain">>, []}, get_text_plain}, |
| 21 |
%% This one only exists so we generate a vary header. |
| 22 |
{{<<"text">>, <<"html">>, []}, get_text_html} |
| 23 |
], Req, State}. |
| 24 |
|
| 25 |
ranges_provided(Req, State) -> |
| 26 |
{[{<<"bytes">>, get_text_plain_bytes}], Req, State}. |
| 27 |
|
| 28 |
generate_etag(Req=#{qs := <<"weak-etag">>}, State) -> |
| 29 |
{{weak, <<"weak-no-match">>}, Req, State}; |
| 30 |
generate_etag(Req, State) -> |
| 31 |
{{strong, <<"strong-and-match">>}, Req, State}. |
| 32 |
|
| 33 |
last_modified(Req, State) -> |
| 34 |
{{{2222, 2, 22}, {11, 11, 11}}, Req, State}. |
| 35 |
|
| 36 |
expires(Req, State) -> |
| 37 |
{{{3333, 3, 3}, {11, 11, 11}}, Req, State}. |
| 38 |
|
| 39 |
get_text_plain(Req, State) -> |
| 40 |
{<<"This is REST!">>, Req, State}. |
| 41 |
|
| 42 |
%% Simulate the callback being missing, otherwise expect true/false. |
| 43 |
get_text_plain_bytes(#{qs := <<"missing">>}, _) -> |
| 44 |
ct_helper_error_h:ignore(cowboy_rest, set_ranged_body_callback, 3), |
| 45 |
no_call; |
| 46 |
get_text_plain_bytes(Req=#{qs := <<"sendfile">>, range := {_, [{From=0, infinity}]}}, State) -> |
| 47 |
Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app", |
| 48 |
Size = filelib:file_size(Path), |
| 49 |
{[{{From, Size - 1, Size}, {sendfile, From, Size, Path}}], Req, State}; |
| 50 |
get_text_plain_bytes(Req=#{range := {_, [{From=0, infinity}]}}, State) -> |
| 51 |
%% We send everything in one part. |
| 52 |
Body = <<"This is ranged REST!">>, |
| 53 |
Total = byte_size(Body), |
| 54 |
{[{{From, Total - 1, Total}, Body}], Req, State}; |
| 55 |
get_text_plain_bytes(Req=#{qs := <<"sendfile">>, range := {_, Range}}, State) -> |
| 56 |
%% We check the range header we get and send everything hardcoded. |
| 57 |
[ |
| 58 |
{50, 99}, |
| 59 |
{150, 199}, |
| 60 |
{250, 299}, |
| 61 |
-99 |
| 62 |
] = Range, |
| 63 |
Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app", |
| 64 |
Size = filelib:file_size(Path), |
| 65 |
{[ |
| 66 |
{{50, 99, Size}, {sendfile, 50, 50, Path}}, |
| 67 |
{{150, 199, Size}, {sendfile, 150, 50, Path}}, |
| 68 |
{{250, 299, Size}, {sendfile, 250, 50, Path}}, |
| 69 |
{{Size - 99, Size - 1, Size}, {sendfile, Size - 99, 99, Path}} |
| 70 |
], Req, State}; |
| 71 |
get_text_plain_bytes(Req=#{range := {_, Range}}, State) -> |
| 72 |
%% We check the range header we get and send everything hardcoded. |
| 73 |
[ |
| 74 |
{0, 3}, |
| 75 |
{5, 6}, |
| 76 |
{8, 13}, |
| 77 |
{15, infinity} |
| 78 |
] = Range, |
| 79 |
Body = <<"This is ranged REST!">>, |
| 80 |
Total = byte_size(Body), |
| 81 |
{[ |
| 82 |
{{0, 3, Total}, <<"This">>}, |
| 83 |
{{5, 6, Total}, <<"is">>}, |
| 84 |
{{8, 13, Total}, <<"ranged">>}, |
| 85 |
{{15, 19, Total}, <<"REST!">>} |
| 86 |
], Req, State}. |