static_handler_SUITE.erl 29 KB

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