static_handler_SUITE.erl 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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. dir_large_file(Config) ->
  341. doc("Get a large file."),
  342. ConnPid = gun_open(Config),
  343. Ref = gun:get(ConnPid, config(prefix, Config) ++ "/large.bin",
  344. [{<<"accept-encoding">>, <<"gzip">>}]),
  345. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  346. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, RespHeaders),
  347. Size = 512*1024*1024,
  348. {ok, Size} = do_dir_large_file(ConnPid, Ref, 0),
  349. ok.
  350. do_dir_large_file(ConnPid, Ref, N) ->
  351. receive
  352. {gun_data, ConnPid, Ref, nofin, Data} ->
  353. do_dir_large_file(ConnPid, Ref, N + byte_size(Data));
  354. {gun_data, ConnPid, Ref, fin, Data} ->
  355. {ok, N + byte_size(Data)};
  356. {gun_error, ConnPid, Ref, Reason} ->
  357. {error, Reason};
  358. {gun_error, ConnPid, Reason} ->
  359. {error, Reason}
  360. after 5000 ->
  361. {error, timeout}
  362. end.
  363. dir_text(Config) ->
  364. doc("Get a .txt file. The extension is unknown by default."),
  365. {200, Headers, <<"Timeless space.\n">>}
  366. = do_get(config(prefix, Config) ++ "/plain.txt", Config),
  367. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  368. ok.
  369. dir_unknown(Config) ->
  370. doc("Get a file with no extension."),
  371. {200, Headers, <<"File with no extension.\n">>}
  372. = do_get(config(prefix, Config) ++ "/unknown", Config),
  373. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  374. ok.
  375. etag_crash(Config) ->
  376. doc("Get a file with a crashing etag function."),
  377. {500, _, _} = do_get("/etag/crash", Config),
  378. ok.
  379. etag_custom(Config) ->
  380. doc("Get a file with custom Etag function and make sure it is used."),
  381. {200, Headers, _} = do_get("/etag/custom", Config),
  382. {_, <<"\"etag\"">>} = lists:keyfind(<<"etag">>, 1, Headers),
  383. ok.
  384. etag_default(Config) ->
  385. doc("Get a file twice and make sure the Etag matches."),
  386. {200, Headers1, _} = do_get("/dir/style.css", Config),
  387. {200, Headers2, _} = do_get("/dir/style.css", Config),
  388. {_, Etag} = lists:keyfind(<<"etag">>, 1, Headers1),
  389. {_, Etag} = lists:keyfind(<<"etag">>, 1, Headers2),
  390. ok.
  391. etag_default_change(Config) ->
  392. doc("Get a file, modify it, get it again and make sure the Etag doesn't match."),
  393. {200, Headers1, _} = do_get("/dir/index.html", Config),
  394. {_, Etag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  395. ok = file:change_time(config(static_dir, Config) ++ "/index.html",
  396. {{config(port, Config), 1, 1}, {1, 1, 1}}),
  397. {200, Headers2, _} = do_get("/dir/index.html", Config),
  398. {_, Etag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  399. true = Etag1 =/= Etag2,
  400. ok.
  401. etag_disable(Config) ->
  402. doc("Get a file with disabled Etag and make sure no Etag is provided."),
  403. {200, Headers, _} = do_get("/etag/disable", Config),
  404. false = lists:keyfind(<<"etag">>, 1, Headers),
  405. ok.
  406. file(Config) ->
  407. doc("Get a file with hardcoded route."),
  408. {200, Headers, <<"body{color:red}\n">>} = do_get("/file/style.css", Config),
  409. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  410. ok.
  411. if_match(Config) ->
  412. doc("Get a file with If-Match matching."),
  413. {200, _, _} = do_get("/etag/custom", [
  414. {<<"if-match">>, <<"\"etag\"">>}
  415. ], Config),
  416. ok.
  417. if_match_fail(Config) ->
  418. doc("Get a file with If-Match not matching."),
  419. {412, _, _} = do_get("/etag/custom", [
  420. {<<"if-match">>, <<"\"invalid\"">>}
  421. ], Config),
  422. ok.
  423. if_match_invalid(Config) ->
  424. doc("Try to get a file with an invalid If-Match header."),
  425. {400, _, _} = do_get("/etag/custom", [
  426. {<<"if-match">>, <<"bad input">>}
  427. ], Config),
  428. ok.
  429. if_match_list(Config) ->
  430. doc("Get a file with If-Match matching."),
  431. {200, _, _} = do_get("/etag/custom", [
  432. {<<"if-match">>, <<"\"invalid\", \"etag\", \"cowboy\"">>}
  433. ], Config),
  434. ok.
  435. if_match_list_fail(Config) ->
  436. doc("Get a file with If-Match not matching."),
  437. {412, _, _} = do_get("/etag/custom", [
  438. {<<"if-match">>, <<"\"invalid\", W/\"etag\", \"cowboy\"">>}
  439. ], Config),
  440. ok.
  441. if_match_weak(Config) ->
  442. doc("Try to get a file with a weak If-Match header."),
  443. {412, _, _} = do_get("/etag/custom", [
  444. {<<"if-match">>, <<"W/\"etag\"">>}
  445. ], Config),
  446. ok.
  447. if_match_wildcard(Config) ->
  448. doc("Get a file with a wildcard If-Match."),
  449. {200, _, _} = do_get("/etag/custom", [
  450. {<<"if-match">>, <<"*">>}
  451. ], Config),
  452. ok.
  453. if_modified_since(Config) ->
  454. doc("Get a file with If-Modified-Since in the past."),
  455. {200, _, _} = do_get("/etag/custom", [
  456. {<<"if-modified-since">>, <<"Sat, 29 Oct 1994 19:43:31 GMT">>}
  457. ], Config),
  458. ok.
  459. if_modified_since_fail(Config) ->
  460. doc("Get a file with If-Modified-Since equal to file modification time."),
  461. LastModified = filelib:last_modified(config(static_dir, Config) ++ "/style.css"),
  462. {304, _, _} = do_get("/etag/custom", [
  463. {<<"if-modified-since">>, httpd_util:rfc1123_date(LastModified)}
  464. ], Config),
  465. ok.
  466. if_modified_since_future(Config) ->
  467. doc("Get a file with If-Modified-Since in the future."),
  468. {{Year, _, _}, {_, _, _}} = calendar:universal_time(),
  469. {200, _, _} = do_get("/etag/custom", [
  470. {<<"if-modified-since">>, [
  471. <<"Sat, 29 Oct ">>,
  472. integer_to_binary(Year + 1),
  473. <<" 19:43:31 GMT">>]}
  474. ], Config),
  475. ok.
  476. if_modified_since_if_none_match(Config) ->
  477. doc("Get a file with both If-Modified-Since and If-None-Match headers."
  478. "If-None-Match takes precedence and If-Modified-Since is ignored. (RFC7232 3.3)"),
  479. LastModified = filelib:last_modified(config(static_dir, Config) ++ "/style.css"),
  480. {200, _, _} = do_get("/etag/custom", [
  481. {<<"if-modified-since">>, httpd_util:rfc1123_date(LastModified)},
  482. {<<"if-none-match">>, <<"\"not-etag\"">>}
  483. ], Config),
  484. ok.
  485. if_modified_since_invalid(Config) ->
  486. doc("Get a file with an invalid If-Modified-Since header."),
  487. {200, _, _} = do_get("/etag/custom", [
  488. {<<"if-modified-since">>, <<"\"not a date\"">>}
  489. ], Config),
  490. ok.
  491. if_none_match(Config) ->
  492. doc("Get a file with If-None-Match not matching."),
  493. {200, _, _} = do_get("/etag/custom", [
  494. {<<"if-none-match">>, <<"\"not-etag\"">>}
  495. ], Config),
  496. ok.
  497. if_none_match_fail(Config) ->
  498. doc("Get a file with If-None-Match matching."),
  499. {304, _, _} = do_get("/etag/custom", [
  500. {<<"if-none-match">>, <<"\"etag\"">>}
  501. ], Config),
  502. ok.
  503. if_none_match_invalid(Config) ->
  504. doc("Try to get a file with an invalid If-None-Match header."),
  505. {400, _, _} = do_get("/etag/custom", [
  506. {<<"if-none-match">>, <<"bad input">>}
  507. ], Config),
  508. ok.
  509. if_none_match_list(Config) ->
  510. doc("Get a file with If-None-Match not matching."),
  511. {200, _, _} = do_get("/etag/custom", [
  512. {<<"if-none-match">>, <<"\"invalid\", W/\"not-etag\", \"cowboy\"">>}
  513. ], Config),
  514. ok.
  515. if_none_match_list_fail(Config) ->
  516. doc("Get a file with If-None-Match matching."),
  517. {304, _, _} = do_get("/etag/custom", [
  518. {<<"if-none-match">>, <<"\"invalid\", \"etag\", \"cowboy\"">>}
  519. ], Config),
  520. ok.
  521. if_none_match_weak(Config) ->
  522. doc("Try to get a file with a weak If-None-Match header matching."),
  523. {304, _, _} = do_get("/etag/custom", [
  524. {<<"if-none-match">>, <<"W/\"etag\"">>}
  525. ], Config),
  526. ok.
  527. if_none_match_wildcard(Config) ->
  528. doc("Try to get a file with a wildcard If-None-Match."),
  529. {304, _, _} = do_get("/etag/custom", [
  530. {<<"if-none-match">>, <<"*">>}
  531. ], Config),
  532. ok.
  533. if_unmodified_since(Config) ->
  534. doc("Get a file with If-Unmodified-Since equal to file modification time."),
  535. LastModified = filelib:last_modified(config(static_dir, Config) ++ "/style.css"),
  536. {200, _, _} = do_get("/etag/custom", [
  537. {<<"if-unmodified-since">>, httpd_util:rfc1123_date(LastModified)}
  538. ], Config),
  539. ok.
  540. if_unmodified_since_fail(Config) ->
  541. doc("Get a file with If-Unmodified-Since in the past."),
  542. {412, _, _} = do_get("/etag/custom", [
  543. {<<"if-unmodified-since">>, <<"Sat, 29 Oct 1994 19:43:31 GMT">>}
  544. ], Config),
  545. ok.
  546. if_unmodified_since_future(Config) ->
  547. doc("Get a file with If-Unmodified-Since in the future."),
  548. {{Year, _, _}, {_, _, _}} = calendar:universal_time(),
  549. {200, _, _} = do_get("/etag/custom", [
  550. {<<"if-unmodified-since">>, [
  551. <<"Sat, 29 Oct ">>,
  552. integer_to_binary(Year + 1),
  553. <<" 19:43:31 GMT">>]}
  554. ], Config),
  555. ok.
  556. if_unmodified_since_if_match(Config) ->
  557. doc("Get a file with both If-Unmodified-Since and If-Match headers."
  558. "If-Match takes precedence and If-Unmodified-Since is ignored. (RFC7232 3.4)"),
  559. {200, _, _} = do_get("/etag/custom", [
  560. {<<"if-unmodified-since">>, <<"Sat, 29 Oct 1994 19:43:31 GMT">>},
  561. {<<"if-match">>, <<"\"etag\"">>}
  562. ], Config),
  563. ok.
  564. if_unmodified_since_invalid(Config) ->
  565. doc("Get a file with an invalid If-Unmodified-Since header."),
  566. {200, _, _} = do_get("/etag/custom", [
  567. {<<"if-unmodified-since">>, <<"\"not a date\"">>}
  568. ], Config),
  569. ok.
  570. index_file(Config) ->
  571. doc("Get an index file."),
  572. {200, Headers, <<"<html><body>Hello!</body></html>\n">>} = do_get("/index", Config),
  573. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  574. ok.
  575. index_file_slash(Config) ->
  576. doc("Get an index file with extra slash."),
  577. {200, Headers, <<"<html><body>Hello!</body></html>\n">>} = do_get("/index/", Config),
  578. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  579. ok.
  580. last_modified(Config) ->
  581. doc("Get a file, modify it, get it again and make sure Last-Modified changes."),
  582. {200, Headers1, _} = do_get("/dir/file.cowboy", Config),
  583. {_, LastModified1} = lists:keyfind(<<"last-modified">>, 1, Headers1),
  584. ok = file:change_time(config(static_dir, Config) ++ "/file.cowboy",
  585. {{config(port, Config), 1, 1}, {1, 1, 1}}),
  586. {200, Headers2, _} = do_get("/dir/file.cowboy", Config),
  587. {_, LastModified2} = lists:keyfind(<<"last-modified">>, 1, Headers2),
  588. true = LastModified1 =/= LastModified2,
  589. ok.
  590. mime_all_cowboy(Config) ->
  591. doc("Get a .cowboy file. The extension is unknown."),
  592. {200, Headers, _} = do_get("/mime/all/file.cowboy", Config),
  593. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  594. ok.
  595. mime_all_css(Config) ->
  596. doc("Get a .css file."),
  597. {200, Headers, _} = do_get("/mime/all/style.css", Config),
  598. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  599. ok.
  600. mime_all_txt(Config) ->
  601. doc("Get a .txt file."),
  602. {200, Headers, _} = do_get("/mime/all/plain.txt", Config),
  603. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  604. ok.
  605. mime_crash(Config) ->
  606. doc("Get a file with a crashing mimetype function."),
  607. {500, _, _} = do_get("/mime/crash/style.css", Config),
  608. ok.
  609. mime_custom_cowboy(Config) ->
  610. doc("Get a .cowboy file."),
  611. {200, Headers, _} = do_get("/mime/custom/file.cowboy", Config),
  612. {_, <<"application/vnd.ninenines.cowboy+xml;v=1">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  613. ok.
  614. mime_custom_css(Config) ->
  615. doc("Get a .css file. The extension is unknown."),
  616. {200, Headers, _} = do_get("/mime/custom/style.css", Config),
  617. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  618. ok.
  619. mime_custom_txt(Config) ->
  620. doc("Get a .txt file."),
  621. {200, Headers, _} = do_get("/mime/custom/plain.txt", Config),
  622. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  623. ok.
  624. mime_hardcode_binary(Config) ->
  625. doc("Get a .cowboy file with hardcoded route."),
  626. {200, Headers, _} = do_get("/mime/hardcode/binary-form", Config),
  627. {_, <<"application/vnd.ninenines.cowboy+xml;v=1">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  628. ok.
  629. mime_hardcode_tuple(Config) ->
  630. doc("Get a .cowboy file with hardcoded route."),
  631. {200, Headers, _} = do_get("/mime/hardcode/tuple-form", Config),
  632. {_, <<"application/vnd.ninenines.cowboy+xml;v=1">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  633. ok.
  634. priv_file(Config) ->
  635. doc("Get a file with hardcoded route."),
  636. {200, Headers, <<"body{color:red}\n">>} = do_get("/priv_file/style.css", Config),
  637. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  638. ok.
  639. unicode_basic_latin(Config) ->
  640. doc("Get a file with non-urlencoded characters from Unicode Basic Latin block."),
  641. _ = [case do_get("/char/" ++ [C], Config) of
  642. {200, _, << C >>} -> ok;
  643. Error -> exit({error, C, Error})
  644. end || C <-
  645. %% Excluding the dot which has a special meaning in URLs
  646. %% when they are the only content in a path segment,
  647. %% and is tested as part of filenames in other test cases.
  648. "abcdefghijklmnopqrstuvwxyz"
  649. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  650. "0123456789"
  651. ":@-_~!$&'()*+,;="
  652. ],
  653. ok.
  654. unicode_basic_error(Config) ->
  655. doc("Try to get a file with invalid non-urlencoded characters from Unicode Basic Latin block."),
  656. Exclude = case config(protocol, Config) of
  657. %% Some characters trigger different errors in HTTP/1.1
  658. %% because they are used for the protocol.
  659. %%
  660. %% # and ? indicate fragment and query components
  661. %% and are therefore not part of the path.
  662. http -> "\r\s#?";
  663. http2 -> "#?"
  664. end,
  665. _ = [case do_get("/char/" ++ [C], Config) of
  666. {500, _, _} -> ok;
  667. Error -> exit({error, C, Error})
  668. end || C <- (config(chars, Config) -- Exclude) --
  669. "abcdefghijklmnopqrstuvwxyz"
  670. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  671. "0123456789"
  672. ":@-_~!$&'()*+,;="
  673. ],
  674. ok.
  675. unicode_basic_latin_urlencoded(Config) ->
  676. doc("Get a file with urlencoded characters from Unicode Basic Latin block."),
  677. _ = [case do_get(lists:flatten(["/char/%", io_lib:format("~2.16.0b", [C])]), Config) of
  678. {200, _, << C >>} -> ok;
  679. Error -> exit({error, C, Error})
  680. end || C <- config(chars, Config)],
  681. ok.
  682. unknown_option(Config) ->
  683. doc("Get a file configured with unknown extra options."),
  684. {200, Headers, <<"body{color:red}\n">>} = do_get("/unknown/option", Config),
  685. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  686. ok.
  687. priv_file_in_ez_archive(Config) ->
  688. doc("Get a file stored in Erlang application .ez archive."),
  689. {200, Headers, <<"<h1>It works!</h1>\n">>} = do_get("/ez_priv_file/index.html", Config),
  690. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  691. ok.
  692. bad_priv_file_in_ez_archive(Config) ->
  693. doc("Bad cowboy_static options: priv_file path missing from Erlang application .ez archive."),
  694. {404, _, _} = do_get("/bad/ez_priv_file/index.php", Config),
  695. ok.
  696. priv_dir_in_ez_archive(Config) ->
  697. doc("Get a file from a priv_dir stored in Erlang application .ez archive."),
  698. {200, Headers, <<"<h1>It works!</h1>\n">>} = do_get("/ez_priv_dir/index.html", Config),
  699. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  700. ok.
  701. bad_file_in_priv_dir_in_ez_archive(Config) ->
  702. doc("Get a missing file from a priv_dir stored in Erlang application .ez archive."),
  703. {404, _, _} = do_get("/ez_priv_dir/index.php", Config),
  704. ok.
  705. bad_priv_dir_in_ez_archive(Config) ->
  706. doc("Bad cowboy_static options: priv_dir path missing from Erlang application .ez archive."),
  707. {404, _, _} = do_get("/bad/ez_priv_dir/index.html", Config),
  708. ok.