static_handler_SUITE.erl 33 KB

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