static_handler_SUITE.erl 27 KB

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