static_handler_SUITE.erl 29 KB

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