| 1 |
%% Copyright (c) Loïc Hoguin <[email protected]> |
| 2 |
%% |
| 3 |
%% Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
%% purpose with or without fee is hereby granted, provided that the above |
| 5 |
%% copyright notice and this permission notice appear in all copies. |
| 6 |
%% |
| 7 |
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 |
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 |
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 |
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 |
|
| 15 |
-module(static_handler_SUITE). |
| 16 |
-compile(export_all). |
| 17 |
-compile(nowarn_export_all). |
| 18 |
|
| 19 |
-import(ct_helper, [config/2]). |
| 20 |
-import(ct_helper, [doc/1]). |
| 21 |
-import(cowboy_test, [gun_open/1]). |
| 22 |
|
| 23 |
%% Import useful functions from req_SUITE. |
| 24 |
%% @todo Maybe move these functions to cowboy_test. |
| 25 |
-import(req_SUITE, [do_get/2]). |
| 26 |
-import(req_SUITE, [do_get/3]). |
| 27 |
-import(req_SUITE, [do_maybe_h3_error3/1]). |
| 28 |
|
| 29 |
%% ct. |
| 30 |
|
| 31 |
all() -> |
| 32 |
cowboy_test:common_all() ++ [ |
| 33 |
{group, http_no_sendfile}, |
| 34 |
{group, h2c_no_sendfile} |
| 35 |
]. |
| 36 |
|
| 37 |
groups() -> |
| 38 |
AllTests = ct_helper:all(?MODULE), |
| 39 |
%% The directory tests are shared between dir and priv_dir options. |
| 40 |
DirTests = lists:usort([F || {F, 1} <- ?MODULE:module_info(exports), |
| 41 |
string:substr(atom_to_list(F), 1, 4) =:= "dir_" |
| 42 |
]), |
| 43 |
OtherTests = AllTests -- DirTests, |
| 44 |
GroupTests = OtherTests ++ [ |
| 45 |
{dir, [parallel], DirTests}, |
| 46 |
{priv_dir, [parallel], DirTests} |
| 47 |
], |
| 48 |
GroupTestsNoParallel = OtherTests ++ [ |
| 49 |
{dir, [], DirTests}, |
| 50 |
{priv_dir, [], DirTests} |
| 51 |
], |
| 52 |
[ |
| 53 |
{http, [parallel], GroupTests}, |
| 54 |
{https, [parallel], GroupTests}, |
| 55 |
{h2, [parallel], GroupTests}, |
| 56 |
{h2c, [parallel], GroupTests}, |
| 57 |
{h3, [], GroupTestsNoParallel}, %% @todo Enable parallel when it works better. |
| 58 |
{http_compress, [parallel], GroupTests}, |
| 59 |
{https_compress, [parallel], GroupTests}, |
| 60 |
{h2_compress, [parallel], GroupTests}, |
| 61 |
{h2c_compress, [parallel], GroupTests}, |
| 62 |
{h3_compress, [], GroupTestsNoParallel}, %% @todo Enable parallel when it works better. |
| 63 |
%% No real need to test sendfile disabled against https, h2 or h3. |
| 64 |
{http_no_sendfile, [parallel], GroupTests}, |
| 65 |
{h2c_no_sendfile, [parallel], GroupTests} |
| 66 |
]. |
| 67 |
|
| 68 |
init_per_suite(Config) -> |
| 69 |
%% Two static folders are created: one in ct_helper's private directory, |
| 70 |
%% and one in the test run private directory. |
| 71 |
PrivDir = code:priv_dir(ct_helper) ++ "/static", |
| 72 |
StaticDir = config(priv_dir, Config) ++ "/static", |
| 73 |
ct_helper:create_static_dir(PrivDir), |
| 74 |
ct_helper:create_static_dir(StaticDir), |
| 75 |
init_large_file(PrivDir ++ "/large.bin"), |
| 76 |
init_large_file(StaticDir ++ "/large.bin"), |
| 77 |
%% Add a simple Erlang application archive containing one file |
| 78 |
%% in its priv directory. |
| 79 |
true = code:add_pathz(filename:join( |
| 80 |
[config(data_dir, Config), "static_files_app.ez", "static_files_app", "ebin"])), |
| 81 |
ok = application:load(static_files_app), |
| 82 |
%% A special folder contains files of 1 character from 1 to 127 |
| 83 |
%% excluding / and \ as they are always rejected. |
| 84 |
CharDir = config(priv_dir, Config) ++ "/char", |
| 85 |
ok = filelib:ensure_dir(CharDir ++ "/file"), |
| 86 |
Chars0 = lists:flatten([case file:write_file(CharDir ++ [$/, C], [C]) of |
| 87 |
ok -> C; |
| 88 |
{error, _} -> [] |
| 89 |
end || C <- (lists:seq(1, 127) -- "/\\")]), |
| 90 |
%% Determine whether we are on a case insensitive filesystem and |
| 91 |
%% remove uppercase characters in that case. On case insensitive |
| 92 |
%% filesystems we end up overwriting the "A" file with the "a" contents. |
| 93 |
{CaseSensitive, Chars} = case file:read_file(CharDir ++ "/A") of |
| 94 |
{ok, <<"A">>} -> {true, Chars0}; |
| 95 |
{ok, <<"a">>} -> {false, Chars0 -- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"} |
| 96 |
end, |
| 97 |
[{static_dir, StaticDir}, {char_dir, CharDir}, |
| 98 |
{chars, Chars}, {case_sensitive, CaseSensitive}|Config]. |
| 99 |
|
| 100 |
end_per_suite(Config) -> |
| 101 |
%% Special directory. |
| 102 |
CharDir = config(char_dir, Config), |
| 103 |
_ = [file:delete(CharDir ++ [$/, C]) || C <- lists:seq(0, 127)], |
| 104 |
_ = file:del_dir(CharDir), |
| 105 |
%% Static directories. |
| 106 |
StaticDir = config(static_dir, Config), |
| 107 |
PrivDir = code:priv_dir(ct_helper) ++ "/static", |
| 108 |
%% This file is not created on Windows. |
| 109 |
_ = file:delete(StaticDir ++ "/large.bin"), |
| 110 |
_ = file:delete(PrivDir ++ "/large.bin"), |
| 111 |
ct_helper:delete_static_dir(StaticDir), |
| 112 |
ct_helper:delete_static_dir(PrivDir). |
| 113 |
|
| 114 |
init_per_group(dir, Config) -> |
| 115 |
[{prefix, "/dir"}|Config]; |
| 116 |
init_per_group(priv_dir, Config) -> |
| 117 |
[{prefix, "/priv_dir"}|Config]; |
| 118 |
init_per_group(Name=http_no_sendfile, Config) -> |
| 119 |
cowboy_test:init_http(Name, #{ |
| 120 |
env => #{dispatch => init_dispatch(Config)}, |
| 121 |
middlewares => [?MODULE, cowboy_router, cowboy_handler], |
| 122 |
sendfile => false |
| 123 |
}, [{flavor, vanilla}|Config]); |
| 124 |
init_per_group(Name=h2c_no_sendfile, Config) -> |
| 125 |
Config1 = cowboy_test:init_http(Name, #{ |
| 126 |
env => #{dispatch => init_dispatch(Config)}, |
| 127 |
middlewares => [?MODULE, cowboy_router, cowboy_handler], |
| 128 |
sendfile => false |
| 129 |
}, [{flavor, vanilla}|Config]), |
| 130 |
lists:keyreplace(protocol, 1, Config1, {protocol, http2}); |
| 131 |
init_per_group(Name=h3, Config) -> |
| 132 |
cowboy_test:init_http3(Name, #{ |
| 133 |
env => #{dispatch => init_dispatch(Config)}, |
| 134 |
middlewares => [?MODULE, cowboy_router, cowboy_handler] |
| 135 |
}, [{flavor, vanilla}|Config]); |
| 136 |
init_per_group(Name=h3_compress, Config) -> |
| 137 |
cowboy_test:init_http3(Name, #{ |
| 138 |
env => #{dispatch => init_dispatch(Config)}, |
| 139 |
middlewares => [?MODULE, cowboy_router, cowboy_handler], |
| 140 |
stream_handlers => [cowboy_compress_h, cowboy_stream_h] |
| 141 |
}, [{flavor, vanilla}|Config]); |
| 142 |
init_per_group(Name, Config) -> |
| 143 |
Config1 = cowboy_test:init_common_groups(Name, Config, ?MODULE), |
| 144 |
Opts = ranch:get_protocol_options(Name), |
| 145 |
ok = ranch:set_protocol_options(Name, Opts#{ |
| 146 |
middlewares => [?MODULE, cowboy_router, cowboy_handler] |
| 147 |
}), |
| 148 |
Config1. |
| 149 |
|
| 150 |
end_per_group(dir, _) -> |
| 151 |
ok; |
| 152 |
end_per_group(priv_dir, _) -> |
| 153 |
ok; |
| 154 |
end_per_group(Name, _) -> |
| 155 |
cowboy_test:stop_group(Name). |
| 156 |
|
| 157 |
%% Large file. |
| 158 |
|
| 159 |
init_large_file(Filename) -> |
| 160 |
case os:type() of |
| 161 |
{unix, _} -> |
| 162 |
"" = os:cmd("truncate -s 32M " ++ Filename), |
| 163 |
ok; |
| 164 |
{win32, _} -> |
| 165 |
Size = 32*1024*1024, |
| 166 |
ok = file:write_file(Filename, <<0:Size/unit:8>>) |
| 167 |
end. |
| 168 |
|
| 169 |
%% Routes. |
| 170 |
|
| 171 |
init_dispatch(Config) -> |
| 172 |
cowboy_router:compile([{'_', [ |
| 173 |
{"/priv_dir/[...]", cowboy_static, {priv_dir, ct_helper, "static"}}, |
| 174 |
{"/dir/[...]", cowboy_static, {dir, config(static_dir, Config)}}, |
| 175 |
{"/priv_file/style.css", cowboy_static, {priv_file, ct_helper, "static/style.css"}}, |
| 176 |
{"/file/style.css", cowboy_static, {file, config(static_dir, Config) ++ "/style.css"}}, |
| 177 |
{"/index", cowboy_static, {file, config(static_dir, Config) ++ "/index.html"}}, |
| 178 |
{"/mime/all/[...]", cowboy_static, {priv_dir, ct_helper, "static", |
| 179 |
[{mimetypes, cow_mimetypes, all}]}}, |
| 180 |
{"/mime/custom/[...]", cowboy_static, {priv_dir, ct_helper, "static", |
| 181 |
[{mimetypes, ?MODULE, do_mime_custom}]}}, |
| 182 |
{"/mime/crash/[...]", cowboy_static, {priv_dir, ct_helper, "static", |
| 183 |
[{mimetypes, ?MODULE, do_mime_crash}]}}, |
| 184 |
{"/mime/hardcode/binary-form", cowboy_static, {priv_file, ct_helper, "static/file.cowboy", |
| 185 |
[{mimetypes, <<"application/vnd.ninenines.cowboy+xml;v=1">>}]}}, |
| 186 |
{"/mime/hardcode/tuple-form", cowboy_static, {priv_file, ct_helper, "static/file.cowboy", |
| 187 |
[{mimetypes, {<<"application">>, <<"vnd.ninenines.cowboy+xml">>, [{<<"v">>, <<"1">>}]}}]}}, |
| 188 |
{"/charset/custom/[...]", cowboy_static, {priv_dir, ct_helper, "static", |
| 189 |
[{charset, ?MODULE, do_charset_custom}]}}, |
| 190 |
{"/charset/crash/[...]", cowboy_static, {priv_dir, ct_helper, "static", |
| 191 |
[{charset, ?MODULE, do_charset_crash}]}}, |
| 192 |
{"/charset/hardcode/[...]", cowboy_static, {priv_file, ct_helper, "static/index.html", |
| 193 |
[{charset, <<"utf-8">>}]}}, |
| 194 |
{"/etag/custom", cowboy_static, {file, config(static_dir, Config) ++ "/style.css", |
| 195 |
[{etag, ?MODULE, do_etag_custom}]}}, |
| 196 |
{"/etag/crash", cowboy_static, {file, config(static_dir, Config) ++ "/style.css", |
| 197 |
[{etag, ?MODULE, do_etag_crash}]}}, |
| 198 |
{"/etag/disable", cowboy_static, {file, config(static_dir, Config) ++ "/style.css", |
| 199 |
[{etag, false}]}}, |
| 200 |
{"/bad", cowboy_static, bad}, |
| 201 |
{"/bad/priv_dir/app/[...]", cowboy_static, {priv_dir, bad_app, "static"}}, |
| 202 |
{"/bad/priv_dir/no-priv/[...]", cowboy_static, {priv_dir, cowboy, "static"}}, |
| 203 |
{"/bad/priv_dir/path/[...]", cowboy_static, {priv_dir, ct_helper, "bad"}}, |
| 204 |
{"/bad/priv_dir/route", cowboy_static, {priv_dir, ct_helper, "static"}}, |
| 205 |
{"/bad/dir/path/[...]", cowboy_static, {dir, "/bad/path"}}, |
| 206 |
{"/bad/dir/route", cowboy_static, {dir, config(static_dir, Config)}}, |
| 207 |
{"/bad/priv_file/app", cowboy_static, {priv_file, bad_app, "static/style.css"}}, |
| 208 |
{"/bad/priv_file/no-priv", cowboy_static, {priv_file, cowboy, "static/style.css"}}, |
| 209 |
{"/bad/priv_file/path", cowboy_static, {priv_file, ct_helper, "bad/style.css"}}, |
| 210 |
{"/bad/file/path", cowboy_static, {file, "/bad/path/style.css"}}, |
| 211 |
{"/bad/options", cowboy_static, {priv_file, ct_helper, "static/style.css", bad}}, |
| 212 |
{"/bad/options/mime", cowboy_static, {priv_file, ct_helper, "static/style.css", [{mimetypes, bad}]}}, |
| 213 |
{"/bad/options/charset", cowboy_static, {priv_file, ct_helper, "static/style.css", [{charset, bad}]}}, |
| 214 |
{"/bad/options/etag", cowboy_static, {priv_file, ct_helper, "static/style.css", [{etag, true}]}}, |
| 215 |
{"/unknown/option", cowboy_static, {priv_file, ct_helper, "static/style.css", [{bad, option}]}}, |
| 216 |
{"/char/[...]", cowboy_static, {dir, config(char_dir, Config)}}, |
| 217 |
{"/ez_priv_file/index.html", cowboy_static, {priv_file, static_files_app, "www/index.html"}}, |
| 218 |
{"/bad/ez_priv_file/index.php", cowboy_static, {priv_file, static_files_app, "www/index.php"}}, |
| 219 |
{"/ez_priv_dir/[...]", cowboy_static, {priv_dir, static_files_app, "www"}}, |
| 220 |
{"/bad/ez_priv_dir/[...]", cowboy_static, {priv_dir, static_files_app, "cgi-bin"}} |
| 221 |
]}]). |
| 222 |
|
| 223 |
%% Middleware interface to silence expected errors. |
| 224 |
|
| 225 |
execute(Req=#{path := Path}, Env) -> |
| 226 |
case Path of |
| 227 |
<<"/bad/priv_dir/app/", _/bits>> -> ct_helper:ignore(cowboy_static, priv_path, 2); |
| 228 |
<<"/bad/priv_file/app">> -> ct_helper:ignore(cowboy_static, priv_path, 2); |
| 229 |
<<"/bad/priv_dir/route">> -> ct_helper:ignore(cowboy_static, escape_reserved, 1); |
| 230 |
<<"/bad/dir/route">> -> ct_helper:ignore(cowboy_static, escape_reserved, 1); |
| 231 |
<<"/bad">> -> ct_helper:ignore(cowboy_static, init_opts, 2); |
| 232 |
<<"/bad/options">> -> ct_helper:ignore(cowboy_static, content_types_provided, 2); |
| 233 |
<<"/bad/options/mime">> -> ct_helper:ignore(cowboy_rest, normalize_content_types, 2); |
| 234 |
<<"/bad/options/etag">> -> ct_helper:ignore(cowboy_static, generate_etag, 2); |
| 235 |
<<"/bad/options/charset">> -> ct_helper:ignore(cowboy_static, charsets_provided, 2); |
| 236 |
_ -> ok |
| 237 |
end, |
| 238 |
{ok, Req, Env}. |
| 239 |
|
| 240 |
%% Internal functions. |
| 241 |
|
| 242 |
-spec do_charset_crash(_) -> no_return(). |
| 243 |
do_charset_crash(_) -> |
| 244 |
ct_helper_error_h:ignore(?MODULE, do_charset_crash, 1), |
| 245 |
exit(crash). |
| 246 |
|
| 247 |
do_charset_custom(Path) -> |
| 248 |
case filename:extension(Path) of |
| 249 |
<<".cowboy">> -> <<"utf-32">>; |
| 250 |
<<".html">> -> <<"utf-16">>; |
| 251 |
_ -> <<"utf-8">> |
| 252 |
end. |
| 253 |
|
| 254 |
-spec do_etag_crash(_, _, _) -> no_return(). |
| 255 |
do_etag_crash(_, _, _) -> |
| 256 |
ct_helper_error_h:ignore(?MODULE, do_etag_crash, 3), |
| 257 |
exit(crash). |
| 258 |
|
| 259 |
do_etag_custom(_, _, _) -> |
| 260 |
{strong, <<"etag">>}. |
| 261 |
|
| 262 |
-spec do_mime_crash(_) -> no_return(). |
| 263 |
do_mime_crash(_) -> |
| 264 |
ct_helper_error_h:ignore(?MODULE, do_mime_crash, 1), |
| 265 |
exit(crash). |
| 266 |
|
| 267 |
do_mime_custom(Path) -> |
| 268 |
case filename:extension(Path) of |
| 269 |
<<".cowboy">> -> <<"application/vnd.ninenines.cowboy+xml;v=1">>; |
| 270 |
<<".txt">> -> <<"text/plain">>; |
| 271 |
_ -> {<<"application">>, <<"octet-stream">>, []} |
| 272 |
end. |
| 273 |
|
| 274 |
%% Tests. |
| 275 |
|
| 276 |
bad(Config) -> |
| 277 |
doc("Bad cowboy_static options: not a tuple."), |
| 278 |
{500, _, _} = do_maybe_h3_error3(do_get("/bad", Config)), |
| 279 |
ok. |
| 280 |
|
| 281 |
bad_dir_path(Config) -> |
| 282 |
doc("Bad cowboy_static options: wrong path."), |
| 283 |
{404, _, _} = do_get("/bad/dir/path/style.css", Config), |
| 284 |
ok. |
| 285 |
|
| 286 |
bad_dir_route(Config) -> |
| 287 |
doc("Bad cowboy_static options: missing [...] in route."), |
| 288 |
{500, _, _} = do_maybe_h3_error3(do_get("/bad/dir/route", Config)), |
| 289 |
ok. |
| 290 |
|
| 291 |
bad_file_in_priv_dir_in_ez_archive(Config) -> |
| 292 |
doc("Get a missing file from a priv_dir stored in Erlang application .ez archive."), |
| 293 |
{404, _, _} = do_get("/ez_priv_dir/index.php", Config), |
| 294 |
ok. |
| 295 |
|
| 296 |
bad_file_path(Config) -> |
| 297 |
doc("Bad cowboy_static options: wrong path."), |
| 298 |
{404, _, _} = do_get("/bad/file/path", Config), |
| 299 |
ok. |
| 300 |
|
| 301 |
bad_options(Config) -> |
| 302 |
doc("Bad cowboy_static extra options: not a list."), |
| 303 |
{500, _, _} = do_maybe_h3_error3(do_get("/bad/options", Config)), |
| 304 |
ok. |
| 305 |
|
| 306 |
bad_options_charset(Config) -> |
| 307 |
doc("Bad cowboy_static extra options: invalid charset option."), |
| 308 |
{500, _, _} = do_maybe_h3_error3(do_get("/bad/options/charset", Config)), |
| 309 |
ok. |
| 310 |
|
| 311 |
bad_options_etag(Config) -> |
| 312 |
doc("Bad cowboy_static extra options: invalid etag option."), |
| 313 |
{500, _, _} = do_maybe_h3_error3(do_get("/bad/options/etag", Config)), |
| 314 |
ok. |
| 315 |
|
| 316 |
bad_options_mime(Config) -> |
| 317 |
doc("Bad cowboy_static extra options: invalid mimetypes option."), |
| 318 |
{500, _, _} = do_maybe_h3_error3(do_get("/bad/options/mime", Config)), |
| 319 |
ok. |
| 320 |
|
| 321 |
bad_priv_dir_app(Config) -> |
| 322 |
doc("Bad cowboy_static options: wrong application name."), |
| 323 |
{500, _, _} = do_maybe_h3_error3(do_get("/bad/priv_dir/app/style.css", Config)), |
| 324 |
ok. |
| 325 |
|
| 326 |
bad_priv_dir_in_ez_archive(Config) -> |
| 327 |
doc("Bad cowboy_static options: priv_dir path missing from Erlang application .ez archive."), |
| 328 |
{404, _, _} = do_get("/bad/ez_priv_dir/index.html", Config), |
| 329 |
ok. |
| 330 |
|
| 331 |
bad_priv_dir_no_priv(Config) -> |
| 332 |
doc("Bad cowboy_static options: application has no priv directory."), |
| 333 |
{404, _, _} = do_get("/bad/priv_dir/no-priv/style.css", Config), |
| 334 |
ok. |
| 335 |
|
| 336 |
bad_priv_dir_path(Config) -> |
| 337 |
doc("Bad cowboy_static options: wrong path."), |
| 338 |
{404, _, _} = do_get("/bad/priv_dir/path/style.css", Config), |
| 339 |
ok. |
| 340 |
|
| 341 |
bad_priv_dir_route(Config) -> |
| 342 |
doc("Bad cowboy_static options: missing [...] in route."), |
| 343 |
{500, _, _} = do_maybe_h3_error3(do_get("/bad/priv_dir/route", Config)), |
| 344 |
ok. |
| 345 |
|
| 346 |
bad_priv_file_app(Config) -> |
| 347 |
doc("Bad cowboy_static options: wrong application name."), |
| 348 |
{500, _, _} = do_maybe_h3_error3(do_get("/bad/priv_file/app", Config)), |
| 349 |
ok. |
| 350 |
|
| 351 |
bad_priv_file_in_ez_archive(Config) -> |
| 352 |
doc("Bad cowboy_static options: priv_file path missing from Erlang application .ez archive."), |
| 353 |
{404, _, _} = do_get("/bad/ez_priv_file/index.php", Config), |
| 354 |
ok. |
| 355 |
|
| 356 |
bad_priv_file_no_priv(Config) -> |
| 357 |
doc("Bad cowboy_static options: application has no priv directory."), |
| 358 |
{404, _, _} = do_get("/bad/priv_file/no-priv", Config), |
| 359 |
ok. |
| 360 |
|
| 361 |
bad_priv_file_path(Config) -> |
| 362 |
doc("Bad cowboy_static options: wrong path."), |
| 363 |
{404, _, _} = do_get("/bad/priv_file/path", Config), |
| 364 |
ok. |
| 365 |
|
| 366 |
dir_cowboy(Config) -> |
| 367 |
doc("Get a .cowboy file."), |
| 368 |
{200, Headers, <<"File with custom extension.\n">>} |
| 369 |
= do_get(config(prefix, Config) ++ "/file.cowboy", Config), |
| 370 |
{_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 371 |
ok. |
| 372 |
|
| 373 |
dir_css(Config) -> |
| 374 |
doc("Get a .css file."), |
| 375 |
{200, Headers, <<"body{color:red}\n">>} |
| 376 |
= do_get(config(prefix, Config) ++ "/style.css", Config), |
| 377 |
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 378 |
ok. |
| 379 |
|
| 380 |
dir_css_urlencoded(Config) -> |
| 381 |
doc("Get a .css file with the extension dot urlencoded."), |
| 382 |
{200, Headers, <<"body{color:red}\n">>} |
| 383 |
= do_get(config(prefix, Config) ++ "/style%2ecss", Config), |
| 384 |
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 385 |
ok. |
| 386 |
|
| 387 |
dir_dot_file(Config) -> |
| 388 |
doc("Get a file with extra dot segments in the path."), |
| 389 |
%% All these are equivalent. |
| 390 |
{200, _, _} = do_get(config(prefix, Config) ++ "/./style.css", Config), |
| 391 |
{200, _, _} = do_get(config(prefix, Config) ++ "/././style.css", Config), |
| 392 |
{200, _, _} = do_get(config(prefix, Config) ++ "/./././style.css", Config), |
| 393 |
{200, _, _} = do_get("/./priv_dir/style.css", Config), |
| 394 |
{200, _, _} = do_get("/././priv_dir/style.css", Config), |
| 395 |
{200, _, _} = do_get("/./././priv_dir/style.css", Config), |
| 396 |
ok. |
| 397 |
|
| 398 |
dir_dotdot_file(Config) -> |
| 399 |
doc("Get a file with extra dotdot segments in the path."), |
| 400 |
%% All these are equivalent. |
| 401 |
{200, _, _} = do_get("/../priv_dir/style.css", Config), |
| 402 |
{200, _, _} = do_get("/../../priv_dir/style.css", Config), |
| 403 |
{200, _, _} = do_get("/../../../priv_dir/style.css", Config), |
| 404 |
{200, _, _} = do_get(config(prefix, Config) ++ "/../priv_dir/style.css", Config), |
| 405 |
{200, _, _} = do_get(config(prefix, Config) ++ "/../../priv_dir/style.css", Config), |
| 406 |
{200, _, _} = do_get(config(prefix, Config) ++ "/../../../priv_dir/style.css", Config), |
| 407 |
{200, _, _} = do_get("/../priv_dir/../priv_dir/style.css", Config), |
| 408 |
{200, _, _} = do_get("/../../priv_dir/../../priv_dir/style.css", Config), |
| 409 |
{200, _, _} = do_get("/../../../priv_dir/../../../priv_dir/style.css", Config), |
| 410 |
%% Try with non-existing segments, which may correspond to real folders. |
| 411 |
{200, _, _} = do_get("/anything/../priv_dir/style.css", Config), |
| 412 |
{200, _, _} = do_get(config(prefix, Config) ++ "/anything/../style.css", Config), |
| 413 |
{200, _, _} = do_get(config(prefix, Config) ++ "/directory/../style.css", Config), |
| 414 |
{200, _, _} = do_get(config(prefix, Config) ++ "/static/../style.css", Config), |
| 415 |
%% Try with segments corresponding to real files. It works because |
| 416 |
%% URI normalization happens before looking at the filesystem. |
| 417 |
{200, _, _} = do_get(config(prefix, Config) ++ "/style.css/../style.css", Config), |
| 418 |
{200, _, _} = do_get(config(prefix, Config) ++ "/style.css/../../priv_dir/style.css", Config), |
| 419 |
%% Try to fool the server to accept segments corresponding to real folders. |
| 420 |
{404, _, _} = do_get(config(prefix, Config) ++ "/../static/style.css", Config), |
| 421 |
{404, _, _} = do_get(config(prefix, Config) ++ "/directory/../../static/style.css", Config), |
| 422 |
ok. |
| 423 |
|
| 424 |
dir_empty_file(Config) -> |
| 425 |
doc("Get an empty .txt file."), |
| 426 |
{200, _, <<>>} = do_get(config(prefix, Config) ++ "/empty.txt", Config), |
| 427 |
ok. |
| 428 |
|
| 429 |
dir_error_directory(Config) -> |
| 430 |
doc("Try to get a directory."), |
| 431 |
{403, _, _} = do_get(config(prefix, Config) ++ "/directory", Config), |
| 432 |
ok. |
| 433 |
|
| 434 |
dir_error_directory_slash(Config) -> |
| 435 |
doc("Try to get a directory with an extra slash in the path."), |
| 436 |
{403, _, _} = do_get(config(prefix, Config) ++ "/directory/", Config), |
| 437 |
ok. |
| 438 |
|
| 439 |
dir_error_doesnt_exist(Config) -> |
| 440 |
doc("Try to get a file that does not exist."), |
| 441 |
{404, Headers, _} = do_get(config(prefix, Config) ++ "/not.found", Config), |
| 442 |
false = lists:keyfind(<<"content-type">>, 1, Headers), |
| 443 |
ok. |
| 444 |
|
| 445 |
dir_error_dot(Config) -> |
| 446 |
doc("Try to get a file named '.'."), |
| 447 |
{403, _, _} = do_get(config(prefix, Config) ++ "/.", Config), |
| 448 |
ok. |
| 449 |
|
| 450 |
dir_error_dot_urlencoded(Config) -> |
| 451 |
doc("Try to get a file named '.' percent encoded."), |
| 452 |
{403, _, _} = do_get(config(prefix, Config) ++ "/%2e", Config), |
| 453 |
ok. |
| 454 |
|
| 455 |
dir_error_dotdot(Config) -> |
| 456 |
doc("Try to get a file named '..'."), |
| 457 |
{404, _, _} = do_get(config(prefix, Config) ++ "/..", Config), |
| 458 |
ok. |
| 459 |
|
| 460 |
dir_error_dotdot_urlencoded(Config) -> |
| 461 |
doc("Try to get a file named '..' percent encoded."), |
| 462 |
{404, _, _} = do_get(config(prefix, Config) ++ "/%2e%2e", Config), |
| 463 |
ok. |
| 464 |
|
| 465 |
dir_error_empty(Config) -> |
| 466 |
doc("Try to get the configured directory."), |
| 467 |
{403, _, _} = do_get(config(prefix, Config) ++ "", Config), |
| 468 |
ok. |
| 469 |
|
| 470 |
dir_error_slash(Config) -> |
| 471 |
%% I know the description isn't that good considering / has a meaning in URIs. |
| 472 |
doc("Try to get a file named '/'."), |
| 473 |
{403, _, _} = do_get(config(prefix, Config) ++ "//", Config), |
| 474 |
ok. |
| 475 |
|
| 476 |
dir_error_reserved_urlencoded(Config) -> |
| 477 |
doc("Try to get a file named '/' or '\\' or 'NUL' percent encoded."), |
| 478 |
{400, _, _} = do_get(config(prefix, Config) ++ "/%2f", Config), |
| 479 |
{400, _, _} = do_get(config(prefix, Config) ++ "/%5c", Config), |
| 480 |
{400, _, _} = do_get(config(prefix, Config) ++ "/%00", Config), |
| 481 |
ok. |
| 482 |
|
| 483 |
dir_error_slash_urlencoded_dotdot_file(Config) -> |
| 484 |
doc("Try to use a percent encoded slash to access an existing file."), |
| 485 |
{200, _, _} = do_get(config(prefix, Config) ++ "/directory/../style.css", Config), |
| 486 |
{400, _, _} = do_get(config(prefix, Config) ++ "/directory%2f../style.css", Config), |
| 487 |
ok. |
| 488 |
|
| 489 |
dir_error_unreadable(Config) -> |
| 490 |
case os:type() of |
| 491 |
{win32, _} -> |
| 492 |
{skip, "ACL not enabled by default under MSYS2."}; |
| 493 |
{unix, _} -> |
| 494 |
doc("Try to get a file that can't be read."), |
| 495 |
{403, _, _} = do_get(config(prefix, Config) ++ "/unreadable", Config), |
| 496 |
ok |
| 497 |
end. |
| 498 |
|
| 499 |
dir_html(Config) -> |
| 500 |
doc("Get a .html file."), |
| 501 |
{200, Headers, <<"<html><body>Hello!</body></html>\n">>} |
| 502 |
= do_get(config(prefix, Config) ++ "/index.html", Config), |
| 503 |
{_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 504 |
ok. |
| 505 |
|
| 506 |
dir_large_file(Config) -> |
| 507 |
doc("Get a large file."), |
| 508 |
ConnPid = gun_open(Config), |
| 509 |
Ref = gun:get(ConnPid, config(prefix, Config) ++ "/large.bin", |
| 510 |
[{<<"accept-encoding">>, <<"gzip">>}]), |
| 511 |
{response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref), |
| 512 |
{_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, RespHeaders), |
| 513 |
Size = 32*1024*1024, |
| 514 |
{ok, Size} = do_dir_large_file(ConnPid, Ref, 0), |
| 515 |
ok. |
| 516 |
|
| 517 |
do_dir_large_file(ConnPid, Ref, N) -> |
| 518 |
receive |
| 519 |
{gun_data, ConnPid, Ref, nofin, Data} -> |
| 520 |
do_dir_large_file(ConnPid, Ref, N + byte_size(Data)); |
| 521 |
{gun_data, ConnPid, Ref, fin, Data} -> |
| 522 |
{ok, N + byte_size(Data)}; |
| 523 |
{gun_error, ConnPid, Ref, Reason} -> |
| 524 |
{error, Reason}; |
| 525 |
{gun_error, ConnPid, Reason} -> |
| 526 |
{error, Reason} |
| 527 |
after 5000 -> |
| 528 |
{error, timeout} |
| 529 |
end. |
| 530 |
|
| 531 |
dir_text(Config) -> |
| 532 |
doc("Get a .txt file. The extension is unknown by default."), |
| 533 |
{200, Headers, <<"Timeless space.\n">>} |
| 534 |
= do_get(config(prefix, Config) ++ "/plain.txt", Config), |
| 535 |
{_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 536 |
ok. |
| 537 |
|
| 538 |
dir_unknown(Config) -> |
| 539 |
doc("Get a file with no extension."), |
| 540 |
{200, Headers, <<"File with no extension.\n">>} |
| 541 |
= do_get(config(prefix, Config) ++ "/unknown", Config), |
| 542 |
{_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 543 |
ok. |
| 544 |
|
| 545 |
etag_crash(Config) -> |
| 546 |
doc("Get a file with a crashing etag function."), |
| 547 |
{500, _, _} = do_maybe_h3_error3(do_get("/etag/crash", Config)), |
| 548 |
ok. |
| 549 |
|
| 550 |
etag_custom(Config) -> |
| 551 |
doc("Get a file with custom Etag function and make sure it is used."), |
| 552 |
{200, Headers, _} = do_get("/etag/custom", Config), |
| 553 |
{_, <<"\"etag\"">>} = lists:keyfind(<<"etag">>, 1, Headers), |
| 554 |
ok. |
| 555 |
|
| 556 |
etag_default(Config) -> |
| 557 |
doc("Get a file twice and make sure the Etag matches."), |
| 558 |
{200, Headers1, _} = do_get("/dir/style.css", Config), |
| 559 |
{200, Headers2, _} = do_get("/dir/style.css", Config), |
| 560 |
{_, Etag} = lists:keyfind(<<"etag">>, 1, Headers1), |
| 561 |
{_, Etag} = lists:keyfind(<<"etag">>, 1, Headers2), |
| 562 |
ok. |
| 563 |
|
| 564 |
etag_default_change(Config) -> |
| 565 |
doc("Get a file, modify it, get it again and make sure the Etag doesn't match."), |
| 566 |
%% We set the file to the current time first, then to a time in the past. |
| 567 |
ok = file:change_time(config(static_dir, Config) ++ "/index.html", |
| 568 |
calendar:universal_time()), |
| 569 |
{200, Headers1, _} = do_get("/dir/index.html", Config), |
| 570 |
{_, Etag1} = lists:keyfind(<<"etag">>, 1, Headers1), |
| 571 |
ok = file:change_time(config(static_dir, Config) ++ "/index.html", |
| 572 |
{{2019, 1, 1}, {1, 1, 1}}), |
| 573 |
{200, Headers2, _} = do_get("/dir/index.html", Config), |
| 574 |
{_, Etag2} = lists:keyfind(<<"etag">>, 1, Headers2), |
| 575 |
true = Etag1 =/= Etag2, |
| 576 |
ok. |
| 577 |
|
| 578 |
etag_disable(Config) -> |
| 579 |
doc("Get a file with disabled Etag and make sure no Etag is provided."), |
| 580 |
{200, Headers, _} = do_get("/etag/disable", Config), |
| 581 |
false = lists:keyfind(<<"etag">>, 1, Headers), |
| 582 |
ok. |
| 583 |
|
| 584 |
file(Config) -> |
| 585 |
doc("Get a file with hardcoded route."), |
| 586 |
{200, Headers, <<"body{color:red}\n">>} = do_get("/file/style.css", Config), |
| 587 |
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 588 |
ok. |
| 589 |
|
| 590 |
if_match(Config) -> |
| 591 |
doc("Get a file with If-Match matching."), |
| 592 |
{200, _, _} = do_get("/etag/custom", [ |
| 593 |
{<<"if-match">>, <<"\"etag\"">>} |
| 594 |
], Config), |
| 595 |
ok. |
| 596 |
|
| 597 |
if_match_fail(Config) -> |
| 598 |
doc("Get a file with If-Match not matching."), |
| 599 |
{412, _, _} = do_get("/etag/custom", [ |
| 600 |
{<<"if-match">>, <<"\"invalid\"">>} |
| 601 |
], Config), |
| 602 |
ok. |
| 603 |
|
| 604 |
if_match_invalid(Config) -> |
| 605 |
doc("Try to get a file with an invalid If-Match header."), |
| 606 |
{400, _, _} = do_get("/etag/custom", [ |
| 607 |
{<<"if-match">>, <<"bad input">>} |
| 608 |
], Config), |
| 609 |
ok. |
| 610 |
|
| 611 |
if_match_list(Config) -> |
| 612 |
doc("Get a file with If-Match matching."), |
| 613 |
{200, _, _} = do_get("/etag/custom", [ |
| 614 |
{<<"if-match">>, <<"\"invalid\", \"etag\", \"cowboy\"">>} |
| 615 |
], Config), |
| 616 |
ok. |
| 617 |
|
| 618 |
if_match_list_fail(Config) -> |
| 619 |
doc("Get a file with If-Match not matching."), |
| 620 |
{412, _, _} = do_get("/etag/custom", [ |
| 621 |
{<<"if-match">>, <<"\"invalid\", W/\"etag\", \"cowboy\"">>} |
| 622 |
], Config), |
| 623 |
ok. |
| 624 |
|
| 625 |
if_match_weak(Config) -> |
| 626 |
doc("Try to get a file with a weak If-Match header."), |
| 627 |
{412, _, _} = do_get("/etag/custom", [ |
| 628 |
{<<"if-match">>, <<"W/\"etag\"">>} |
| 629 |
], Config), |
| 630 |
ok. |
| 631 |
|
| 632 |
if_match_wildcard(Config) -> |
| 633 |
doc("Get a file with a wildcard If-Match."), |
| 634 |
{200, _, _} = do_get("/etag/custom", [ |
| 635 |
{<<"if-match">>, <<"*">>} |
| 636 |
], Config), |
| 637 |
ok. |
| 638 |
|
| 639 |
if_modified_since(Config) -> |
| 640 |
doc("Get a file with If-Modified-Since in the past."), |
| 641 |
{200, _, _} = do_get("/etag/custom", [ |
| 642 |
{<<"if-modified-since">>, <<"Sat, 29 Oct 1994 19:43:31 GMT">>} |
| 643 |
], Config), |
| 644 |
ok. |
| 645 |
|
| 646 |
if_modified_since_fail(Config) -> |
| 647 |
doc("Get a file with If-Modified-Since equal to file modification time."), |
| 648 |
LastModified = filelib:last_modified(config(static_dir, Config) ++ "/style.css"), |
| 649 |
{304, _, _} = do_get("/etag/custom", [ |
| 650 |
{<<"if-modified-since">>, httpd_util:rfc1123_date(LastModified)} |
| 651 |
], Config), |
| 652 |
ok. |
| 653 |
|
| 654 |
if_modified_since_future(Config) -> |
| 655 |
doc("Get a file with If-Modified-Since in the future."), |
| 656 |
{{Year, _, _}, {_, _, _}} = calendar:universal_time(), |
| 657 |
{200, _, _} = do_get("/etag/custom", [ |
| 658 |
{<<"if-modified-since">>, [ |
| 659 |
<<"Sat, 29 Oct ">>, |
| 660 |
integer_to_binary(Year + 1), |
| 661 |
<<" 19:43:31 GMT">>]} |
| 662 |
], Config), |
| 663 |
ok. |
| 664 |
|
| 665 |
if_modified_since_if_none_match(Config) -> |
| 666 |
doc("Get a file with both If-Modified-Since and If-None-Match headers." |
| 667 |
"If-None-Match takes precedence and If-Modified-Since is ignored. (RFC7232 3.3)"), |
| 668 |
LastModified = filelib:last_modified(config(static_dir, Config) ++ "/style.css"), |
| 669 |
{200, _, _} = do_get("/etag/custom", [ |
| 670 |
{<<"if-modified-since">>, httpd_util:rfc1123_date(LastModified)}, |
| 671 |
{<<"if-none-match">>, <<"\"not-etag\"">>} |
| 672 |
], Config), |
| 673 |
ok. |
| 674 |
|
| 675 |
if_modified_since_invalid(Config) -> |
| 676 |
doc("Get a file with an invalid If-Modified-Since header."), |
| 677 |
{200, _, _} = do_get("/etag/custom", [ |
| 678 |
{<<"if-modified-since">>, <<"\"not a date\"">>} |
| 679 |
], Config), |
| 680 |
ok. |
| 681 |
|
| 682 |
if_none_match(Config) -> |
| 683 |
doc("Get a file with If-None-Match not matching."), |
| 684 |
{200, _, _} = do_get("/etag/custom", [ |
| 685 |
{<<"if-none-match">>, <<"\"not-etag\"">>} |
| 686 |
], Config), |
| 687 |
ok. |
| 688 |
|
| 689 |
if_none_match_fail(Config) -> |
| 690 |
doc("Get a file with If-None-Match matching."), |
| 691 |
{304, _, _} = do_get("/etag/custom", [ |
| 692 |
{<<"if-none-match">>, <<"\"etag\"">>} |
| 693 |
], Config), |
| 694 |
ok. |
| 695 |
|
| 696 |
if_none_match_invalid(Config) -> |
| 697 |
doc("Try to get a file with an invalid If-None-Match header."), |
| 698 |
{400, _, _} = do_get("/etag/custom", [ |
| 699 |
{<<"if-none-match">>, <<"bad input">>} |
| 700 |
], Config), |
| 701 |
ok. |
| 702 |
|
| 703 |
if_none_match_list(Config) -> |
| 704 |
doc("Get a file with If-None-Match not matching."), |
| 705 |
{200, _, _} = do_get("/etag/custom", [ |
| 706 |
{<<"if-none-match">>, <<"\"invalid\", W/\"not-etag\", \"cowboy\"">>} |
| 707 |
], Config), |
| 708 |
ok. |
| 709 |
|
| 710 |
if_none_match_list_fail(Config) -> |
| 711 |
doc("Get a file with If-None-Match matching."), |
| 712 |
{304, _, _} = do_get("/etag/custom", [ |
| 713 |
{<<"if-none-match">>, <<"\"invalid\", \"etag\", \"cowboy\"">>} |
| 714 |
], Config), |
| 715 |
ok. |
| 716 |
|
| 717 |
if_none_match_weak(Config) -> |
| 718 |
doc("Try to get a file with a weak If-None-Match header matching."), |
| 719 |
{304, _, _} = do_get("/etag/custom", [ |
| 720 |
{<<"if-none-match">>, <<"W/\"etag\"">>} |
| 721 |
], Config), |
| 722 |
ok. |
| 723 |
|
| 724 |
if_none_match_wildcard(Config) -> |
| 725 |
doc("Try to get a file with a wildcard If-None-Match."), |
| 726 |
{304, _, _} = do_get("/etag/custom", [ |
| 727 |
{<<"if-none-match">>, <<"*">>} |
| 728 |
], Config), |
| 729 |
ok. |
| 730 |
|
| 731 |
if_unmodified_since(Config) -> |
| 732 |
doc("Get a file with If-Unmodified-Since equal to file modification time."), |
| 733 |
LastModified = filelib:last_modified(config(static_dir, Config) ++ "/style.css"), |
| 734 |
{200, _, _} = do_get("/etag/custom", [ |
| 735 |
{<<"if-unmodified-since">>, httpd_util:rfc1123_date(LastModified)} |
| 736 |
], Config), |
| 737 |
ok. |
| 738 |
|
| 739 |
if_unmodified_since_fail(Config) -> |
| 740 |
doc("Get a file with If-Unmodified-Since in the past."), |
| 741 |
{412, _, _} = do_get("/etag/custom", [ |
| 742 |
{<<"if-unmodified-since">>, <<"Sat, 29 Oct 1994 19:43:31 GMT">>} |
| 743 |
], Config), |
| 744 |
ok. |
| 745 |
|
| 746 |
if_unmodified_since_future(Config) -> |
| 747 |
doc("Get a file with If-Unmodified-Since in the future."), |
| 748 |
{{Year, _, _}, {_, _, _}} = calendar:universal_time(), |
| 749 |
{200, _, _} = do_get("/etag/custom", [ |
| 750 |
{<<"if-unmodified-since">>, [ |
| 751 |
<<"Sat, 29 Oct ">>, |
| 752 |
integer_to_binary(Year + 1), |
| 753 |
<<" 19:43:31 GMT">>]} |
| 754 |
], Config), |
| 755 |
ok. |
| 756 |
|
| 757 |
if_unmodified_since_if_match(Config) -> |
| 758 |
doc("Get a file with both If-Unmodified-Since and If-Match headers." |
| 759 |
"If-Match takes precedence and If-Unmodified-Since is ignored. (RFC7232 3.4)"), |
| 760 |
{200, _, _} = do_get("/etag/custom", [ |
| 761 |
{<<"if-unmodified-since">>, <<"Sat, 29 Oct 1994 19:43:31 GMT">>}, |
| 762 |
{<<"if-match">>, <<"\"etag\"">>} |
| 763 |
], Config), |
| 764 |
ok. |
| 765 |
|
| 766 |
if_unmodified_since_invalid(Config) -> |
| 767 |
doc("Get a file with an invalid If-Unmodified-Since header."), |
| 768 |
{200, _, _} = do_get("/etag/custom", [ |
| 769 |
{<<"if-unmodified-since">>, <<"\"not a date\"">>} |
| 770 |
], Config), |
| 771 |
ok. |
| 772 |
|
| 773 |
index_file(Config) -> |
| 774 |
doc("Get an index file."), |
| 775 |
{200, Headers, <<"<html><body>Hello!</body></html>\n">>} = do_get("/index", Config), |
| 776 |
{_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 777 |
ok. |
| 778 |
|
| 779 |
index_file_slash(Config) -> |
| 780 |
doc("Get an index file with extra slash."), |
| 781 |
{200, Headers, <<"<html><body>Hello!</body></html>\n">>} = do_get("/index/", Config), |
| 782 |
{_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 783 |
ok. |
| 784 |
|
| 785 |
last_modified(Config) -> |
| 786 |
doc("Get a file, modify it, get it again and make sure Last-Modified changes."), |
| 787 |
%% We set the file to the current time first, then to a time in the past. |
| 788 |
ok = file:change_time(config(static_dir, Config) ++ "/file.cowboy", |
| 789 |
calendar:universal_time()), |
| 790 |
{200, Headers1, _} = do_get("/dir/file.cowboy", Config), |
| 791 |
{_, LastModified1} = lists:keyfind(<<"last-modified">>, 1, Headers1), |
| 792 |
ok = file:change_time(config(static_dir, Config) ++ "/file.cowboy", |
| 793 |
{{2019, 1, 1}, {1, 1, 1}}), |
| 794 |
{200, Headers2, _} = do_get("/dir/file.cowboy", Config), |
| 795 |
{_, LastModified2} = lists:keyfind(<<"last-modified">>, 1, Headers2), |
| 796 |
true = LastModified1 =/= LastModified2, |
| 797 |
ok. |
| 798 |
|
| 799 |
mime_all_cowboy(Config) -> |
| 800 |
doc("Get a .cowboy file. The extension is unknown."), |
| 801 |
{200, Headers, _} = do_get("/mime/all/file.cowboy", Config), |
| 802 |
{_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 803 |
ok. |
| 804 |
|
| 805 |
mime_all_css(Config) -> |
| 806 |
doc("Get a .css file."), |
| 807 |
{200, Headers, _} = do_get("/mime/all/style.css", Config), |
| 808 |
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 809 |
ok. |
| 810 |
|
| 811 |
mime_all_txt(Config) -> |
| 812 |
doc("Get a .txt file."), |
| 813 |
{200, Headers, _} = do_get("/mime/all/plain.txt", Config), |
| 814 |
{_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 815 |
ok. |
| 816 |
|
| 817 |
mime_all_uppercase(Config) -> |
| 818 |
doc("Get an uppercase .TXT file."), |
| 819 |
{200, Headers, _} = do_get("/mime/all/UPPER.TXT", Config), |
| 820 |
{_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 821 |
ok. |
| 822 |
|
| 823 |
mime_crash(Config) -> |
| 824 |
doc("Get a file with a crashing mimetype function."), |
| 825 |
{500, _, _} = do_maybe_h3_error3(do_get("/mime/crash/style.css", Config)), |
| 826 |
ok. |
| 827 |
|
| 828 |
mime_custom_cowboy(Config) -> |
| 829 |
doc("Get a .cowboy file."), |
| 830 |
{200, Headers, _} = do_get("/mime/custom/file.cowboy", Config), |
| 831 |
{_, <<"application/vnd.ninenines.cowboy+xml;v=1">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 832 |
ok. |
| 833 |
|
| 834 |
mime_custom_css(Config) -> |
| 835 |
doc("Get a .css file. The extension is unknown."), |
| 836 |
{200, Headers, _} = do_get("/mime/custom/style.css", Config), |
| 837 |
{_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 838 |
ok. |
| 839 |
|
| 840 |
mime_custom_txt(Config) -> |
| 841 |
doc("Get a .txt file."), |
| 842 |
{200, Headers, _} = do_get("/mime/custom/plain.txt", Config), |
| 843 |
{_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 844 |
ok. |
| 845 |
|
| 846 |
mime_hardcode_binary(Config) -> |
| 847 |
doc("Get a .cowboy file with hardcoded route and media type in binary form."), |
| 848 |
{200, Headers, _} = do_get("/mime/hardcode/binary-form", Config), |
| 849 |
{_, <<"application/vnd.ninenines.cowboy+xml;v=1">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 850 |
ok. |
| 851 |
|
| 852 |
mime_hardcode_tuple(Config) -> |
| 853 |
doc("Get a .cowboy file with hardcoded route and media type in tuple form."), |
| 854 |
{200, Headers, _} = do_get("/mime/hardcode/tuple-form", Config), |
| 855 |
{_, <<"application/vnd.ninenines.cowboy+xml;v=1">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 856 |
ok. |
| 857 |
|
| 858 |
charset_crash(Config) -> |
| 859 |
doc("Get a file with a crashing charset function."), |
| 860 |
{500, _, _} = do_maybe_h3_error3(do_get("/charset/crash/style.css", Config)), |
| 861 |
ok. |
| 862 |
|
| 863 |
charset_custom_cowboy(Config) -> |
| 864 |
doc("Get a .cowboy file."), |
| 865 |
{200, Headers, _} = do_get("/charset/custom/file.cowboy", Config), |
| 866 |
{_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 867 |
ok. |
| 868 |
|
| 869 |
charset_custom_css(Config) -> |
| 870 |
doc("Get a .css file."), |
| 871 |
{200, Headers, _} = do_get("/charset/custom/style.css", Config), |
| 872 |
{_, <<"text/css; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 873 |
ok. |
| 874 |
|
| 875 |
charset_custom_html(Config) -> |
| 876 |
doc("Get a .html file."), |
| 877 |
{200, Headers, _} = do_get("/charset/custom/index.html", Config), |
| 878 |
{_, <<"text/html; charset=utf-16">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 879 |
ok. |
| 880 |
|
| 881 |
charset_hardcode_binary(Config) -> |
| 882 |
doc("Get a .html file with hardcoded route and charset."), |
| 883 |
{200, Headers, _} = do_get("/charset/hardcode", Config), |
| 884 |
{_, <<"text/html; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 885 |
ok. |
| 886 |
|
| 887 |
priv_dir_in_ez_archive(Config) -> |
| 888 |
doc("Get a file from a priv_dir stored in Erlang application .ez archive."), |
| 889 |
{200, Headers, <<"<h1>It works!</h1>\n">>} = do_get("/ez_priv_dir/index.html", Config), |
| 890 |
{_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 891 |
ok. |
| 892 |
|
| 893 |
priv_file(Config) -> |
| 894 |
doc("Get a file with hardcoded route."), |
| 895 |
{200, Headers, <<"body{color:red}\n">>} = do_get("/priv_file/style.css", Config), |
| 896 |
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 897 |
ok. |
| 898 |
|
| 899 |
priv_file_in_ez_archive(Config) -> |
| 900 |
doc("Get a file stored in Erlang application .ez archive."), |
| 901 |
{200, Headers, <<"<h1>It works!</h1>\n">>} = do_get("/ez_priv_file/index.html", Config), |
| 902 |
{_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 903 |
ok. |
| 904 |
|
| 905 |
range_request(Config) -> |
| 906 |
doc("Confirm that range requests are enabled."), |
| 907 |
{206, Headers, <<"less space.\n">>} = do_get("/dir/plain.txt", |
| 908 |
[{<<"range">>, <<"bytes=4-">>}], Config), |
| 909 |
{_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers), |
| 910 |
{_, <<"bytes 4-15/16">>} = lists:keyfind(<<"content-range">>, 1, Headers), |
| 911 |
{_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 912 |
ok. |
| 913 |
|
| 914 |
unicode_basic_latin(Config) -> |
| 915 |
doc("Get a file with non-urlencoded characters from Unicode Basic Latin block."), |
| 916 |
%% Excluding the dot which has a special meaning in URLs |
| 917 |
%% when they are the only content in a path segment, |
| 918 |
%% and is tested as part of filenames in other test cases. |
| 919 |
Chars0 = |
| 920 |
"abcdefghijklmnopqrstuvwxyz" |
| 921 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 922 |
"0123456789" |
| 923 |
":@-_~!$&'()*+,;=", |
| 924 |
Chars1 = case config(case_sensitive, Config) of |
| 925 |
false -> Chars0 -- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 926 |
true -> Chars0 |
| 927 |
end, |
| 928 |
%% Remove the characters for which we have no corresponding file. |
| 929 |
Chars = Chars1 -- (Chars1 -- config(chars, Config)), |
| 930 |
_ = [case do_get("/char/" ++ [C], Config) of |
| 931 |
{200, _, << C >>} -> ok; |
| 932 |
Error -> exit({error, C, Error}) |
| 933 |
end || C <- Chars], |
| 934 |
ok. |
| 935 |
|
| 936 |
unicode_basic_error(Config) -> |
| 937 |
doc("Try to get a file with invalid non-urlencoded characters from Unicode Basic Latin block."), |
| 938 |
Exclude = case config(protocol, Config) of |
| 939 |
%% Some characters trigger different errors in HTTP/1.1 |
| 940 |
%% because they are used for the protocol. |
| 941 |
%% |
| 942 |
%% # and ? indicate fragment and query components |
| 943 |
%% and are therefore not part of the path. |
| 944 |
http -> "\r\s#?"; |
| 945 |
http2 -> "#?"; |
| 946 |
http3 -> "#?" |
| 947 |
end, |
| 948 |
_ = [case do_get("/char/" ++ [C], Config) of |
| 949 |
{400, _, _} -> ok; |
| 950 |
{stream_error, protocol_error, _} -> ok; |
| 951 |
Error -> exit({error, C, Error}) |
| 952 |
end || C <- (config(chars, Config) -- Exclude) -- |
| 953 |
"abcdefghijklmnopqrstuvwxyz" |
| 954 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 955 |
"0123456789" |
| 956 |
":@-_~!$&'()*+,;=" |
| 957 |
], |
| 958 |
ok. |
| 959 |
|
| 960 |
unicode_basic_latin_urlencoded(Config) -> |
| 961 |
doc("Get a file with urlencoded characters from Unicode Basic Latin block."), |
| 962 |
_ = [case do_get(lists:flatten(["/char/%", io_lib:format("~2.16.0b", [C])]), Config) of |
| 963 |
{200, _, << C >>} -> ok; |
| 964 |
Error -> exit({error, C, Error}) |
| 965 |
end || C <- config(chars, Config)], |
| 966 |
ok. |
| 967 |
|
| 968 |
unknown_option(Config) -> |
| 969 |
doc("Get a file configured with unknown extra options."), |
| 970 |
{200, Headers, <<"body{color:red}\n">>} = do_get("/unknown/option", Config), |
| 971 |
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 972 |
ok. |