static_handler_SUITE.erl 29 KB

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