static_handler_SUITE.erl 27 KB

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