static_handler_SUITE.erl 29 KB

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