http_SUITE.erl 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. %% Copyright (c) 2011-2013, Loïc Hoguin <essen@ninenines.eu>
  2. %% Copyright (c) 2011, Anthony Ramine <nox@dev-extend.eu>
  3. %%
  4. %% Permission to use, copy, modify, and/or distribute this software for any
  5. %% purpose with or without fee is hereby granted, provided that the above
  6. %% copyright notice and this permission notice appear in all copies.
  7. %%
  8. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. -module(http_SUITE).
  16. -include_lib("common_test/include/ct.hrl").
  17. %% ct.
  18. -export([all/0]).
  19. -export([groups/0]).
  20. -export([init_per_suite/1]).
  21. -export([end_per_suite/1]).
  22. -export([init_per_group/2]).
  23. -export([end_per_group/2]).
  24. %% Tests.
  25. -export([check_raw_status/1]).
  26. -export([check_status/1]).
  27. -export([chunked_response/1]).
  28. -export([echo_body/1]).
  29. -export([echo_body_max_length/1]).
  30. -export([echo_body_qs/1]).
  31. -export([echo_body_qs_max_length/1]).
  32. -export([error_chain_handle_after_reply/1]).
  33. -export([error_chain_handle_before_reply/1]).
  34. -export([error_handle_after_reply/1]).
  35. -export([error_init_after_reply/1]).
  36. -export([error_init_reply_handle_error/1]).
  37. -export([headers_dupe/1]).
  38. -export([http10_chunkless/1]).
  39. -export([http10_hostless/1]).
  40. -export([keepalive_max/1]).
  41. -export([keepalive_nl/1]).
  42. -export([multipart/1]).
  43. -export([nc_rand/1]).
  44. -export([nc_zero/1]).
  45. -export([onrequest/1]).
  46. -export([onrequest_reply/1]).
  47. -export([onresponse_capitalize/1]).
  48. -export([onresponse_crash/1]).
  49. -export([onresponse_reply/1]).
  50. -export([pipeline/1]).
  51. -export([pipeline_long_polling/1]).
  52. -export([rest_bad_accept/1]).
  53. -export([rest_bad_content_type/1]).
  54. -export([rest_expires/1]).
  55. -export([rest_keepalive/1]).
  56. -export([rest_keepalive_post/1]).
  57. -export([rest_missing_get_callbacks/1]).
  58. -export([rest_missing_put_callbacks/1]).
  59. -export([rest_nodelete/1]).
  60. -export([rest_options_default/1]).
  61. -export([rest_param_all/1]).
  62. -export([rest_patch/1]).
  63. -export([rest_resource_etags/1]).
  64. -export([rest_resource_etags_if_none_match/1]).
  65. -export([set_env_dispatch/1]).
  66. -export([set_resp_body/1]).
  67. -export([set_resp_header/1]).
  68. -export([set_resp_overwrite/1]).
  69. -export([slowloris/1]).
  70. -export([slowloris2/1]).
  71. -export([static_attribute_etag/1]).
  72. -export([static_function_etag/1]).
  73. -export([static_mimetypes_function/1]).
  74. -export([static_specify_file/1]).
  75. -export([static_specify_file_catchall/1]).
  76. -export([static_test_file/1]).
  77. -export([static_test_file_css/1]).
  78. -export([stream_body_set_resp/1]).
  79. -export([stream_body_set_resp_close/1]).
  80. -export([te_chunked/1]).
  81. -export([te_chunked_chopped/1]).
  82. -export([te_chunked_delayed/1]).
  83. -export([te_identity/1]).
  84. %% ct.
  85. all() ->
  86. [
  87. {group, http},
  88. {group, https},
  89. {group, http_compress},
  90. {group, https_compress},
  91. {group, onrequest},
  92. {group, onresponse},
  93. {group, onresponse_capitalize},
  94. {group, set_env}
  95. ].
  96. groups() ->
  97. Tests = [
  98. check_raw_status,
  99. check_status,
  100. chunked_response,
  101. echo_body,
  102. echo_body_max_length,
  103. echo_body_qs,
  104. echo_body_qs_max_length,
  105. error_chain_handle_after_reply,
  106. error_chain_handle_before_reply,
  107. error_handle_after_reply,
  108. error_init_after_reply,
  109. error_init_reply_handle_error,
  110. headers_dupe,
  111. http10_chunkless,
  112. http10_hostless,
  113. keepalive_max,
  114. keepalive_nl,
  115. multipart,
  116. nc_rand,
  117. nc_zero,
  118. pipeline,
  119. pipeline_long_polling,
  120. rest_bad_accept,
  121. rest_bad_content_type,
  122. rest_expires,
  123. rest_keepalive,
  124. rest_keepalive_post,
  125. rest_missing_get_callbacks,
  126. rest_missing_put_callbacks,
  127. rest_nodelete,
  128. rest_options_default,
  129. rest_param_all,
  130. rest_patch,
  131. rest_resource_etags,
  132. rest_resource_etags_if_none_match,
  133. set_resp_body,
  134. set_resp_header,
  135. set_resp_overwrite,
  136. slowloris,
  137. slowloris2,
  138. static_attribute_etag,
  139. static_function_etag,
  140. static_mimetypes_function,
  141. static_specify_file,
  142. static_specify_file_catchall,
  143. static_test_file,
  144. static_test_file_css,
  145. stream_body_set_resp,
  146. stream_body_set_resp_close,
  147. te_chunked,
  148. te_chunked_chopped,
  149. te_chunked_delayed,
  150. te_identity
  151. ],
  152. [
  153. {http, [parallel], Tests},
  154. {https, [parallel], Tests},
  155. {http_compress, [parallel], Tests},
  156. {https_compress, [parallel], Tests},
  157. {onrequest, [parallel], [
  158. onrequest,
  159. onrequest_reply
  160. ]},
  161. {onresponse, [parallel], [
  162. onresponse_crash,
  163. onresponse_reply
  164. ]},
  165. {onresponse_capitalize, [parallel], [
  166. onresponse_capitalize
  167. ]},
  168. {set_env, [], [
  169. set_env_dispatch
  170. ]}
  171. ].
  172. init_per_suite(Config) ->
  173. application:start(crypto),
  174. application:start(ranch),
  175. application:start(cowboy),
  176. Config.
  177. end_per_suite(_Config) ->
  178. application:stop(cowboy),
  179. application:stop(ranch),
  180. application:stop(crypto),
  181. ok.
  182. init_per_group(http, Config) ->
  183. Transport = ranch_tcp,
  184. Config1 = init_static_dir(Config),
  185. {ok, _} = cowboy:start_http(http, 100, [{port, 0}], [
  186. {env, [{dispatch, init_dispatch(Config1)}]},
  187. {max_keepalive, 50},
  188. {timeout, 500}
  189. ]),
  190. Port = ranch:get_port(http),
  191. {ok, Client} = cowboy_client:init([]),
  192. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  193. {transport, Transport}, {client, Client}|Config1];
  194. init_per_group(https, Config) ->
  195. Transport = ranch_ssl,
  196. Opts = [
  197. {certfile, ?config(data_dir, Config) ++ "ssl/cert.pem"},
  198. {keyfile, ?config(data_dir, Config) ++ "ssl/key.pem"},
  199. {password, "cowboy"}
  200. ],
  201. Config1 = init_static_dir(Config),
  202. application:start(public_key),
  203. application:start(ssl),
  204. {ok, _} = cowboy:start_https(https, 100, Opts ++ [{port, 0}], [
  205. {env, [{dispatch, init_dispatch(Config1)}]},
  206. {max_keepalive, 50},
  207. {timeout, 500}
  208. ]),
  209. Port = ranch:get_port(https),
  210. {ok, Client} = cowboy_client:init(Opts),
  211. [{scheme, <<"https">>}, {port, Port}, {opts, Opts},
  212. {transport, Transport}, {client, Client}|Config1];
  213. init_per_group(http_compress, Config) ->
  214. Transport = ranch_tcp,
  215. Config1 = init_static_dir(Config),
  216. {ok, _} = cowboy:start_http(http_compress, 100, [{port, 0}], [
  217. {compress, true},
  218. {env, [{dispatch, init_dispatch(Config1)}]},
  219. {max_keepalive, 50},
  220. {timeout, 500}
  221. ]),
  222. Port = ranch:get_port(http_compress),
  223. {ok, Client} = cowboy_client:init([]),
  224. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  225. {transport, Transport}, {client, Client}|Config1];
  226. init_per_group(https_compress, Config) ->
  227. Transport = ranch_ssl,
  228. Opts = [
  229. {certfile, ?config(data_dir, Config) ++ "ssl/cert.pem"},
  230. {keyfile, ?config(data_dir, Config) ++ "ssl/key.pem"},
  231. {password, "cowboy"}
  232. ],
  233. Config1 = init_static_dir(Config),
  234. application:start(public_key),
  235. application:start(ssl),
  236. {ok, _} = cowboy:start_https(https_compress, 100, Opts ++ [{port, 0}], [
  237. {compress, true},
  238. {env, [{dispatch, init_dispatch(Config1)}]},
  239. {max_keepalive, 50},
  240. {timeout, 500}
  241. ]),
  242. Port = ranch:get_port(https_compress),
  243. {ok, Client} = cowboy_client:init(Opts),
  244. [{scheme, <<"https">>}, {port, Port}, {opts, Opts},
  245. {transport, Transport}, {client, Client}|Config1];
  246. init_per_group(onrequest, Config) ->
  247. Transport = ranch_tcp,
  248. {ok, _} = cowboy:start_http(onrequest, 100, [{port, 0}], [
  249. {env, [{dispatch, init_dispatch(Config)}]},
  250. {max_keepalive, 50},
  251. {onrequest, fun onrequest_hook/1},
  252. {timeout, 500}
  253. ]),
  254. Port = ranch:get_port(onrequest),
  255. {ok, Client} = cowboy_client:init([]),
  256. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  257. {transport, Transport}, {client, Client}|Config];
  258. init_per_group(onresponse, Config) ->
  259. Transport = ranch_tcp,
  260. {ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
  261. {env, [{dispatch, init_dispatch(Config)}]},
  262. {max_keepalive, 50},
  263. {onresponse, fun onresponse_hook/4},
  264. {timeout, 500}
  265. ]),
  266. Port = ranch:get_port(onresponse),
  267. {ok, Client} = cowboy_client:init([]),
  268. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  269. {transport, Transport}, {client, Client}|Config];
  270. init_per_group(onresponse_capitalize, Config) ->
  271. Transport = ranch_tcp,
  272. {ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [
  273. {env, [{dispatch, init_dispatch(Config)}]},
  274. {max_keepalive, 50},
  275. {onresponse, fun onresponse_capitalize_hook/4},
  276. {timeout, 500}
  277. ]),
  278. Port = ranch:get_port(onresponse_capitalize),
  279. {ok, Client} = cowboy_client:init([]),
  280. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  281. {transport, Transport}, {client, Client}|Config];
  282. init_per_group(set_env, Config) ->
  283. Transport = ranch_tcp,
  284. {ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [
  285. {env, [{dispatch, []}]},
  286. {max_keepalive, 50},
  287. {timeout, 500}
  288. ]),
  289. Port = ranch:get_port(set_env),
  290. {ok, Client} = cowboy_client:init([]),
  291. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  292. {transport, Transport}, {client, Client}|Config].
  293. end_per_group(Group, Config) when Group =:= https; Group =:= https_compress ->
  294. cowboy:stop_listener(https),
  295. application:stop(ssl),
  296. application:stop(public_key),
  297. end_static_dir(Config),
  298. ok;
  299. end_per_group(Group, Config) when Group =:= http; Group =:= http_compress ->
  300. cowboy:stop_listener(http),
  301. end_static_dir(Config);
  302. end_per_group(Name, _) ->
  303. cowboy:stop_listener(Name),
  304. ok.
  305. %% Dispatch configuration.
  306. init_dispatch(Config) ->
  307. cowboy_router:compile([
  308. {"localhost", [
  309. {"/chunked_response", http_chunked, []},
  310. {"/init_shutdown", http_init_shutdown, []},
  311. {"/long_polling", http_long_polling, []},
  312. {"/headers/dupe", http_handler,
  313. [{headers, [{<<"connection">>, <<"close">>}]}]},
  314. {"/set_resp/header", http_set_resp,
  315. [{headers, [{<<"vary">>, <<"Accept">>}]}]},
  316. {"/set_resp/overwrite", http_set_resp,
  317. [{headers, [{<<"server">>, <<"DesireDrive/1.0">>}]}]},
  318. {"/set_resp/body", http_set_resp,
  319. [{body, <<"A flameless dance does not equal a cycle">>}]},
  320. {"/stream_body/set_resp", http_stream_body,
  321. [{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
  322. {"/stream_body/set_resp_close",
  323. http_stream_body, [
  324. {reply, set_resp_close},
  325. {body, <<"stream_body_set_resp_close">>}]},
  326. {"/static/[...]", cowboy_static,
  327. [{directory, ?config(static_dir, Config)},
  328. {mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
  329. {"/static_mimetypes_function/[...]", cowboy_static,
  330. [{directory, ?config(static_dir, Config)},
  331. {mimetypes, {fun(Path, data) when is_binary(Path) ->
  332. [<<"text/html">>] end, data}}]},
  333. {"/handler_errors", http_errors, []},
  334. {"/static_attribute_etag/[...]", cowboy_static,
  335. [{directory, ?config(static_dir, Config)},
  336. {etag, {attributes, [filepath, filesize, inode, mtime]}}]},
  337. {"/static_function_etag/[...]", cowboy_static,
  338. [{directory, ?config(static_dir, Config)},
  339. {etag, {fun static_function_etag/2, etag_data}}]},
  340. {"/static_specify_file/[...]", cowboy_static,
  341. [{directory, ?config(static_dir, Config)},
  342. {mimetypes, [{<<".css">>, [<<"text/css">>]}]},
  343. {file, <<"test_file.css">>}]},
  344. {"/multipart", http_multipart, []},
  345. {"/echo/body", http_echo_body, []},
  346. {"/echo/body_qs", http_body_qs, []},
  347. {"/param_all", rest_param_all, []},
  348. {"/bad_accept", rest_simple_resource, []},
  349. {"/bad_content_type", rest_patch_resource, []},
  350. {"/simple", rest_simple_resource, []},
  351. {"/forbidden_post", rest_forbidden_resource, [true]},
  352. {"/simple_post", rest_forbidden_resource, [false]},
  353. {"/missing_get_callbacks", rest_missing_callbacks, []},
  354. {"/missing_put_callbacks", rest_missing_callbacks, []},
  355. {"/nodelete", rest_nodelete_resource, []},
  356. {"/patch", rest_patch_resource, []},
  357. {"/resetags", rest_resource_etags, []},
  358. {"/rest_expires", rest_expires, []},
  359. {"/rest_empty_resource", rest_empty_resource, []},
  360. {"/loop_recv", http_loop_recv, []},
  361. {"/loop_timeout", http_loop_timeout, []},
  362. {"/", http_handler, []}
  363. ]}
  364. ]).
  365. init_static_dir(Config) ->
  366. Dir = filename:join(?config(priv_dir, Config), "static"),
  367. Level1 = fun(Name) -> filename:join(Dir, Name) end,
  368. ok = file:make_dir(Dir),
  369. ok = file:write_file(Level1("test_file"), "test_file\n"),
  370. ok = file:write_file(Level1("test_file.css"), "test_file.css\n"),
  371. ok = file:write_file(Level1("test_noread"), "test_noread\n"),
  372. ok = file:change_mode(Level1("test_noread"), 8#0333),
  373. ok = file:write_file(Level1("test.html"), "test.html\n"),
  374. ok = file:make_dir(Level1("test_dir")),
  375. [{static_dir, Dir}|Config].
  376. end_static_dir(Config) ->
  377. Dir = ?config(static_dir, Config),
  378. Level1 = fun(Name) -> filename:join(Dir, Name) end,
  379. ok = file:delete(Level1("test_file")),
  380. ok = file:delete(Level1("test_file.css")),
  381. ok = file:delete(Level1("test_noread")),
  382. ok = file:delete(Level1("test.html")),
  383. ok = file:del_dir(Level1("test_dir")),
  384. ok = file:del_dir(Dir),
  385. Config.
  386. %% Convenience functions.
  387. quick_raw(Data, Config) ->
  388. Client = ?config(client, Config),
  389. Transport = ?config(transport, Config),
  390. {ok, Client2} = cowboy_client:connect(
  391. Transport, "localhost", ?config(port, Config), Client),
  392. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  393. case cowboy_client:response(Client3) of
  394. {ok, Status, _, _} -> Status;
  395. {error, _} -> closed
  396. end.
  397. build_url(Path, Config) ->
  398. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  399. {port, Port} = lists:keyfind(port, 1, Config),
  400. PortBin = list_to_binary(integer_to_list(Port)),
  401. PathBin = list_to_binary(Path),
  402. << Scheme/binary, "://localhost:", PortBin/binary, PathBin/binary >>.
  403. quick_get(URL, Config) ->
  404. Client = ?config(client, Config),
  405. {ok, Client2} = cowboy_client:request(<<"GET">>,
  406. build_url(URL, Config), Client),
  407. {ok, Status, _, _} = cowboy_client:response(Client2),
  408. Status.
  409. body_to_chunks(_, <<>>, Acc) ->
  410. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  411. body_to_chunks(ChunkSize, Body, Acc) ->
  412. BodySize = byte_size(Body),
  413. ChunkSize2 = case BodySize < ChunkSize of
  414. true -> BodySize;
  415. false -> ChunkSize
  416. end,
  417. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  418. ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
  419. body_to_chunks(ChunkSize, Rest,
  420. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  421. %% Tests.
  422. check_raw_status(Config) ->
  423. Huge = [$0 || _ <- lists:seq(1, 5000)],
  424. HugeCookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  425. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _ <- lists:seq(1, 40)]),
  426. ResponsePacket =
  427. "HTTP/1.0 302 Found\r
  428. Location: http://www.google.co.il/\r
  429. Cache-Control: private\r
  430. Content-Type: text/html; charset=UTF-8\r
  431. Set-Cookie: PREF=ID=568f67013d4a7afa:FF=0:TM=1323014101:LM=1323014101:S=XqctDWC65MzKT0zC; expires=Tue, 03-Dec-2013 15:55:01 GMT; path=/; domain=.google.com\r
  432. Date: Sun, 04 Dec 2011 15:55:01 GMT\r
  433. Server: gws\r
  434. Content-Length: 221\r
  435. X-XSS-Protection: 1; mode=block\r
  436. X-Frame-Options: SAMEORIGIN\r
  437. \r
  438. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  439. <TITLE>302 Moved</TITLE></HEAD><BODY>
  440. <H1>302 Moved</H1>
  441. The document has moved
  442. <A HREF=\"http://www.google.co.il/\">here</A>.
  443. </BODY></HTML>",
  444. Tests = [
  445. {102, <<"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n"
  446. "Content-Length: 5000\r\n\r\n", 0:5000/unit:8 >>},
  447. {200, ["GET / HTTP/1.0\r\nHost: localhost\r\n"
  448. "Set-Cookie: ", HugeCookie, "\r\n\r\n"]},
  449. {200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  450. {200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  451. {200, <<"POST /loop_recv HTTP/1.1\r\nHost: localhost\r\n"
  452. "Content-Length: 100000\r\n\r\n", 0:100000/unit:8 >>},
  453. {400, "\n"},
  454. {400, "Garbage\r\n\r\n"},
  455. {400, "\r\n\r\n\r\n\r\n\r\n\r\n"},
  456. {400, "GET / HTTP/1.1\r\nHost: ninenines.eu\r\n\r\n"},
  457. {400, "GET http://proxy/ HTTP/1.1\r\n\r\n"},
  458. {400, "GET / HTTP/1.1\r\nHost: localhost:bad_port\r\n\r\n"},
  459. {505, ResponsePacket},
  460. {408, "GET / HTTP/1.1\r\n"},
  461. {408, "GET / HTTP/1.1\r\nHost: localhost"},
  462. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n"},
  463. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"},
  464. {414, Huge},
  465. {400, "GET / HTTP/1.1\r\n" ++ Huge},
  466. {500, <<"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n"
  467. "Content-Length: 100000\r\n\r\n", 0:100000/unit:8 >>},
  468. {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"},
  469. {closed, ""},
  470. {closed, "\r\n"},
  471. {closed, "\r\n\r\n"},
  472. {closed, "GET / HTTP/1.1"}
  473. ],
  474. _ = [{Status, Packet} = begin
  475. Ret = quick_raw(Packet, Config),
  476. {Ret, Packet}
  477. end || {Status, Packet} <- Tests],
  478. ok.
  479. check_status(Config) ->
  480. Tests = [
  481. {102, "/long_polling"},
  482. {200, "/"},
  483. {200, "/simple"},
  484. {204, "/loop_timeout"},
  485. {400, "/static/%2f"},
  486. {400, "/static/%2e"},
  487. {400, "/static/%2e%2e"},
  488. {403, "/static/test_dir"},
  489. {403, "/static/test_dir/"},
  490. {403, "/static/test_noread"},
  491. {404, "/not/found"},
  492. {404, "/static/not_found"},
  493. {500, "/handler_errors?case=handler_before_reply"},
  494. {500, "/handler_errors?case=init_before_reply"},
  495. {666, "/init_shutdown"}
  496. ],
  497. _ = [{Status, URL} = begin
  498. Ret = quick_get(URL, Config),
  499. {Ret, URL}
  500. end || {Status, URL} <- Tests].
  501. chunked_response(Config) ->
  502. Client = ?config(client, Config),
  503. {ok, Client2} = cowboy_client:request(<<"GET">>,
  504. build_url("/chunked_response", Config), Client),
  505. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  506. true = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  507. {ok, Transport, Socket} = cowboy_client:transport(Client3),
  508. {ok, <<"11\r\nchunked_handler\r\n\r\nB\r\nworks fine!\r\n0\r\n\r\n">>}
  509. = Transport:recv(Socket, 44, 1000),
  510. {error, closed} = cowboy_client:response(Client3).
  511. %% Check if sending requests whose size is around the MTU breaks something.
  512. echo_body(Config) ->
  513. Client = ?config(client, Config),
  514. {ok, [{mtu, MTU}]} = inet:ifget("lo", [mtu]),
  515. _ = [begin
  516. Body = list_to_binary(lists:duplicate(Size, $a)),
  517. {ok, Client2} = cowboy_client:request(<<"POST">>,
  518. build_url("/echo/body", Config),
  519. [{<<"connection">>, <<"close">>}],
  520. Body, Client),
  521. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  522. {ok, Body, _} = cowboy_client:response_body(Client3)
  523. end || Size <- lists:seq(MTU - 500, MTU)],
  524. ok.
  525. %% Check if sending request whose size is bigger than 1000000 bytes causes 413
  526. echo_body_max_length(Config) ->
  527. Client = ?config(client, Config),
  528. Body = <<$a:8000008>>,
  529. {ok, Client2} = cowboy_client:request(<<"POST">>,
  530. build_url("/echo/body", Config),
  531. [{<<"connection">>, <<"close">>}],
  532. Body, Client),
  533. {ok, 413, _, _} = cowboy_client:response(Client2).
  534. % check if body_qs echo's back results
  535. echo_body_qs(Config) ->
  536. Client = ?config(client, Config),
  537. Body = <<"echo=67890">>,
  538. {ok, Client2} = cowboy_client:request(<<"POST">>,
  539. build_url("/echo/body_qs", Config),
  540. [{<<"connection">>, <<"close">>}],
  541. Body, Client),
  542. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  543. {ok, <<"67890">>, _} = cowboy_client:response_body(Client3).
  544. %% Check if sending request whose size is bigger 16000 bytes causes 413
  545. echo_body_qs_max_length(Config) ->
  546. Client = ?config(client, Config),
  547. DefaultMaxBodyQsLength = 16000,
  548. % subtract "echo=" minus 1 byte from max to hit the limit
  549. Bits = (DefaultMaxBodyQsLength - 4) * 8,
  550. AppendedBody = <<$a:Bits>>,
  551. Body = <<"echo=", AppendedBody/binary>>,
  552. {ok, Client2} = cowboy_client:request(<<"POST">>,
  553. build_url("/echo/body_qs", Config),
  554. [{<<"connection">>, <<"close">>}],
  555. Body, Client),
  556. {ok, 413, _, _} = cowboy_client:response(Client2).
  557. error_chain_handle_after_reply(Config) ->
  558. Client = ?config(client, Config),
  559. {ok, Client2} = cowboy_client:request(<<"GET">>,
  560. build_url("/", Config), Client),
  561. {ok, Client3} = cowboy_client:request(<<"GET">>,
  562. build_url("/handler_errors?case=handle_after_reply", Config), Client2),
  563. {ok, 200, _, Client4} = cowboy_client:response(Client3),
  564. {ok, 200, _, Client5} = cowboy_client:response(Client4),
  565. {error, closed} = cowboy_client:response(Client5).
  566. error_chain_handle_before_reply(Config) ->
  567. Client = ?config(client, Config),
  568. {ok, Client2} = cowboy_client:request(<<"GET">>,
  569. build_url("/", Config), Client),
  570. {ok, Client3} = cowboy_client:request(<<"GET">>,
  571. build_url("/handler_errors?case=handle_before_reply", Config), Client2),
  572. {ok, 200, _, Client4} = cowboy_client:response(Client3),
  573. {ok, 500, _, Client5} = cowboy_client:response(Client4),
  574. {error, closed} = cowboy_client:response(Client5).
  575. error_handle_after_reply(Config) ->
  576. Client = ?config(client, Config),
  577. {ok, Client2} = cowboy_client:request(<<"GET">>,
  578. build_url("/handler_errors?case=handle_after_reply", Config), Client),
  579. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  580. {error, closed} = cowboy_client:response(Client3).
  581. error_init_after_reply(Config) ->
  582. Client = ?config(client, Config),
  583. {ok, Client2} = cowboy_client:request(<<"GET">>,
  584. build_url("/handler_errors?case=init_after_reply", Config), Client),
  585. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  586. {error, closed} = cowboy_client:response(Client3).
  587. error_init_reply_handle_error(Config) ->
  588. Client = ?config(client, Config),
  589. {ok, Client2} = cowboy_client:request(<<"GET">>,
  590. build_url("/handler_errors?case=init_reply_handle_error", Config), Client),
  591. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  592. {error, closed} = cowboy_client:response(Client3).
  593. headers_dupe(Config) ->
  594. Client = ?config(client, Config),
  595. {ok, Client2} = cowboy_client:request(<<"GET">>,
  596. build_url("/headers/dupe", Config), Client),
  597. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  598. {<<"connection">>, <<"close">>}
  599. = lists:keyfind(<<"connection">>, 1, Headers),
  600. Connections = [H || H = {Name, _} <- Headers, Name =:= <<"connection">>],
  601. 1 = length(Connections),
  602. {error, closed} = cowboy_client:response(Client3).
  603. http10_chunkless(Config) ->
  604. Client = ?config(client, Config),
  605. Transport = ?config(transport, Config),
  606. {ok, Client2} = cowboy_client:connect(
  607. Transport, "localhost", ?config(port, Config), Client),
  608. Data = "GET /chunked_response HTTP/1.0\r\nHost: localhost\r\n\r\n",
  609. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  610. {ok, 200, Headers, Client4} = cowboy_client:response(Client3),
  611. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  612. %% Hack: we just try to get 28 bytes and compare.
  613. {ok, Transport, Socket} = cowboy_client:transport(Client4),
  614. Buffer = element(7, Client4),
  615. Buffer2 = case Transport:recv(Socket, 28 - byte_size(Buffer), 1000) of
  616. {ok, Recv} -> << Buffer/binary, Recv/binary >>;
  617. _ -> Buffer
  618. end,
  619. <<"chunked_handler\r\nworks fine!">> = Buffer2.
  620. http10_hostless(Config) ->
  621. Port10 = ?config(port, Config) + 10,
  622. Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)),
  623. ranch:start_listener(Name, 5,
  624. ?config(transport, Config), ?config(opts, Config) ++ [{port, Port10}],
  625. cowboy_protocol, [
  626. {env, [{dispatch, cowboy_router:compile([
  627. {'_', [{"/http1.0/hostless", http_handler, []}]}])}]},
  628. {max_keepalive, 50},
  629. {timeout, 500}]
  630. ),
  631. 200 = quick_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
  632. [{port, Port10}|Config]),
  633. cowboy:stop_listener(http10).
  634. keepalive_max(Config) ->
  635. Client = ?config(client, Config),
  636. URL = build_url("/", Config),
  637. ok = keepalive_max_loop(Client, URL, 50).
  638. keepalive_max_loop(Client, _, 0) ->
  639. {error, closed} = cowboy_client:response(Client),
  640. ok;
  641. keepalive_max_loop(Client, URL, N) ->
  642. Headers = [{<<"connection">>, <<"keep-alive">>}],
  643. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  644. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  645. Expected = case N of
  646. 1 -> <<"close">>;
  647. N -> <<"keep-alive">>
  648. end,
  649. {<<"connection">>, Expected}
  650. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  651. keepalive_max_loop(Client3, URL, N - 1).
  652. keepalive_nl(Config) ->
  653. Client = ?config(client, Config),
  654. URL = build_url("/", Config),
  655. ok = keepalive_nl_loop(Client, URL, 10).
  656. keepalive_nl_loop(Client, _, 0) ->
  657. {error, closed} = cowboy_client:response(Client),
  658. ok;
  659. keepalive_nl_loop(Client, URL, N) ->
  660. Headers = [{<<"connection">>, <<"keep-alive">>}],
  661. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  662. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  663. {<<"connection">>, <<"keep-alive">>}
  664. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  665. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  666. ok = Transport:send(Socket, <<"\r\n">>), %% empty line
  667. keepalive_nl_loop(Client3, URL, N - 1).
  668. multipart(Config) ->
  669. Client = ?config(client, Config),
  670. Body = <<
  671. "This is a preamble."
  672. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  673. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  674. "\r\n--OHai--"
  675. "This is an epiloque."
  676. >>,
  677. {ok, Client2} = cowboy_client:request(<<"POST">>,
  678. build_url("/multipart", Config),
  679. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
  680. Body, Client),
  681. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  682. {ok, RespBody, _} = cowboy_client:response_body(Client3),
  683. Parts = binary_to_term(RespBody),
  684. Parts = [
  685. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  686. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  687. ].
  688. nc_reqs(Config, Input) ->
  689. Cat = os:find_executable("cat"),
  690. Nc = os:find_executable("nc"),
  691. case {Cat, Nc} of
  692. {false, _} ->
  693. {skip, {notfound, cat}};
  694. {_, false} ->
  695. {skip, {notfound, nc}};
  696. _Good ->
  697. %% Throw garbage at the server then check if it's still up.
  698. {port, Port} = lists:keyfind(port, 1, Config),
  699. StrPort = integer_to_list(Port),
  700. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  701. || _ <- lists:seq(1, 100)],
  702. 200 = quick_get("/", Config)
  703. end.
  704. nc_rand(Config) ->
  705. nc_reqs(Config, "/dev/urandom").
  706. nc_zero(Config) ->
  707. nc_reqs(Config, "/dev/zero").
  708. onrequest(Config) ->
  709. Client = ?config(client, Config),
  710. {ok, Client2} = cowboy_client:request(<<"GET">>,
  711. build_url("/", Config), Client),
  712. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  713. {<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers),
  714. {ok, <<"http_handler">>, _} = cowboy_client:response_body(Client3).
  715. onrequest_reply(Config) ->
  716. Client = ?config(client, Config),
  717. {ok, Client2} = cowboy_client:request(<<"GET">>,
  718. build_url("/?reply=1", Config), Client),
  719. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  720. {<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
  721. {ok, <<"replied!">>, _} = cowboy_client:response_body(Client3).
  722. %% Hook for the above onrequest tests.
  723. onrequest_hook(Req) ->
  724. case cowboy_req:qs_val(<<"reply">>, Req) of
  725. {undefined, Req2} ->
  726. cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req2);
  727. {_, Req2} ->
  728. {ok, Req3} = cowboy_req:reply(
  729. 200, [], <<"replied!">>, Req2),
  730. Req3
  731. end.
  732. onresponse_capitalize(Config) ->
  733. Client = ?config(client, Config),
  734. {ok, Client2} = cowboy_client:request(<<"GET">>,
  735. build_url("/", Config), Client),
  736. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  737. {ok, Data} = Transport:recv(Socket, 0, 1000),
  738. false = nomatch =:= binary:match(Data, <<"Content-Length">>).
  739. %% Hook for the above onresponse_capitalize test.
  740. onresponse_capitalize_hook(Status, Headers, Body, Req) ->
  741. Headers2 = [{cowboy_bstr:capitalize_token(N), V}
  742. || {N, V} <- Headers],
  743. {ok, Req2} = cowboy_req:reply(Status, Headers2, Body, Req),
  744. Req2.
  745. onresponse_crash(Config) ->
  746. Client = ?config(client, Config),
  747. {ok, Client2} = cowboy_client:request(<<"GET">>,
  748. build_url("/handler_errors?case=init_before_reply", Config), Client),
  749. {ok, 777, Headers, _} = cowboy_client:response(Client2),
  750. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
  751. onresponse_reply(Config) ->
  752. Client = ?config(client, Config),
  753. {ok, Client2} = cowboy_client:request(<<"GET">>,
  754. build_url("/", Config), Client),
  755. {ok, 777, Headers, Client3} = cowboy_client:response(Client2),
  756. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
  757. %% Make sure we don't get the body initially sent.
  758. {error, closed} = cowboy_client:response_body(Client3).
  759. %% Hook for the above onresponse tests.
  760. onresponse_hook(_, Headers, _, Req) ->
  761. {ok, Req2} = cowboy_req:reply(
  762. <<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req),
  763. Req2.
  764. pipeline(Config) ->
  765. Client = ?config(client, Config),
  766. {ok, Client2} = cowboy_client:request(<<"GET">>,
  767. build_url("/", Config), Client),
  768. {ok, Client3} = cowboy_client:request(<<"GET">>,
  769. build_url("/", Config), Client2),
  770. {ok, Client4} = cowboy_client:request(<<"GET">>,
  771. build_url("/", Config), Client3),
  772. {ok, Client5} = cowboy_client:request(<<"GET">>,
  773. build_url("/", Config), Client4),
  774. {ok, Client6} = cowboy_client:request(<<"GET">>,
  775. build_url("/", Config), [{<<"connection">>, <<"close">>}], Client5),
  776. {ok, 200, _, Client7} = cowboy_client:response(Client6),
  777. {ok, 200, _, Client8} = cowboy_client:response(Client7),
  778. {ok, 200, _, Client9} = cowboy_client:response(Client8),
  779. {ok, 200, _, Client10} = cowboy_client:response(Client9),
  780. {ok, 200, _, Client11} = cowboy_client:response(Client10),
  781. {error, closed} = cowboy_client:response(Client11).
  782. pipeline_long_polling(Config) ->
  783. Client = ?config(client, Config),
  784. {ok, Client2} = cowboy_client:request(<<"GET">>,
  785. build_url("/long_polling", Config), Client),
  786. {ok, Client3} = cowboy_client:request(<<"GET">>,
  787. build_url("/long_polling", Config), Client2),
  788. {ok, 102, _, Client4} = cowboy_client:response(Client3),
  789. {ok, 102, _, Client5} = cowboy_client:response(Client4),
  790. {error, closed} = cowboy_client:response(Client5).
  791. rest_param_all(Config) ->
  792. Client = ?config(client, Config),
  793. URL = build_url("/param_all", Config),
  794. % Accept without param
  795. {ok, Client2} = cowboy_client:request(<<"GET">>, URL,
  796. [{<<"accept">>, <<"text/plain">>}], Client),
  797. Client3 = check_response(Client2, <<"[]">>),
  798. % Accept with param
  799. {ok, Client4} = cowboy_client:request(<<"GET">>, URL,
  800. [{<<"accept">>, <<"text/plain;level=1">>}], Client3),
  801. Client5 = check_response(Client4, <<"level=1">>),
  802. % Accept with param and quality
  803. {ok, Client6} = cowboy_client:request(<<"GET">>, URL,
  804. [{<<"accept">>,
  805. <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}],
  806. Client5),
  807. Client7 = check_response(Client6, <<"level=1">>),
  808. {ok, Client8} = cowboy_client:request(<<"GET">>, URL,
  809. [{<<"accept">>,
  810. <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}],
  811. Client7),
  812. Client9 = check_response(Client8, <<"level=2">>),
  813. % Without Accept
  814. {ok, Client10} = cowboy_client:request(<<"GET">>, URL, [], Client9),
  815. Client11 = check_response(Client10, <<"'*'">>),
  816. % Content-Type without param
  817. {ok, Client12} = cowboy_client:request(<<"PUT">>, URL,
  818. [{<<"content-type">>, <<"text/plain">>}], Client11),
  819. {ok, 204, _, Client13} = cowboy_client:response(Client12),
  820. % Content-Type with param
  821. {ok, Client14} = cowboy_client:request(<<"PUT">>, URL,
  822. [{<<"content-type">>, <<"text/plain; charset=utf-8">>}], Client13),
  823. {ok, 204, _, _} = cowboy_client:response(Client14).
  824. check_response(Client, Body) ->
  825. {ok, 200, _, Client2} = cowboy_client:response(Client),
  826. {ok, Body, Client3} = cowboy_client:response_body(Client2),
  827. Client3.
  828. rest_bad_accept(Config) ->
  829. Client = ?config(client, Config),
  830. {ok, Client2} = cowboy_client:request(<<"GET">>,
  831. build_url("/bad_accept", Config),
  832. [{<<"accept">>, <<"1">>}],
  833. Client),
  834. {ok, 400, _, _} = cowboy_client:response(Client2).
  835. rest_bad_content_type(Config) ->
  836. Client = ?config(client, Config),
  837. {ok, Client2} = cowboy_client:request(<<"PATCH">>,
  838. build_url("/bad_content_type", Config),
  839. [{<<"content-type">>, <<"text/plain, text/html">>}],
  840. <<"Whatever">>, Client),
  841. {ok, 415, _, _} = cowboy_client:response(Client2).
  842. rest_expires(Config) ->
  843. Client = ?config(client, Config),
  844. {ok, Client2} = cowboy_client:request(<<"GET">>,
  845. build_url("/rest_expires", Config), Client),
  846. {ok, 200, RespHeaders, _} = cowboy_client:response(Client2),
  847. {_, Expires} = lists:keyfind(<<"expires">>, 1, RespHeaders),
  848. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, RespHeaders),
  849. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  850. ok.
  851. rest_keepalive(Config) ->
  852. Client = ?config(client, Config),
  853. URL = build_url("/simple", Config),
  854. ok = rest_keepalive_loop(Client, URL, 10).
  855. rest_keepalive_loop(_, _, 0) ->
  856. ok;
  857. rest_keepalive_loop(Client, URL, N) ->
  858. Headers = [{<<"connection">>, <<"keep-alive">>}],
  859. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  860. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  861. {<<"connection">>, <<"keep-alive">>}
  862. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  863. rest_keepalive_loop(Client3, URL, N - 1).
  864. rest_keepalive_post(Config) ->
  865. Client = ?config(client, Config),
  866. ok = rest_keepalive_post_loop(Config, Client, forbidden_post, 10).
  867. rest_keepalive_post_loop(_, _, _, 0) ->
  868. ok;
  869. rest_keepalive_post_loop(Config, Client, simple_post, N) ->
  870. Headers = [
  871. {<<"connection">>, <<"keep-alive">>},
  872. {<<"content-type">>, <<"text/plain">>}
  873. ],
  874. {ok, Client2} = cowboy_client:request(<<"POST">>,
  875. build_url("/simple_post", Config), Headers, "12345", Client),
  876. {ok, 303, RespHeaders, Client3} = cowboy_client:response(Client2),
  877. {<<"connection">>, <<"keep-alive">>}
  878. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  879. rest_keepalive_post_loop(Config, Client3, forbidden_post, N - 1);
  880. rest_keepalive_post_loop(Config, Client, forbidden_post, N) ->
  881. Headers = [
  882. {<<"connection">>, <<"keep-alive">>},
  883. {<<"content-type">>, <<"text/plain">>}
  884. ],
  885. {ok, Client2} = cowboy_client:request(<<"POST">>,
  886. build_url("/forbidden_post", Config), Headers, "12345", Client),
  887. {ok, 403, RespHeaders, Client3} = cowboy_client:response(Client2),
  888. {<<"connection">>, <<"keep-alive">>}
  889. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  890. rest_keepalive_post_loop(Config, Client3, simple_post, N - 1).
  891. rest_missing_get_callbacks(Config) ->
  892. Client = ?config(client, Config),
  893. {ok, Client2} = cowboy_client:request(<<"GET">>,
  894. build_url("/missing_get_callbacks", Config), Client),
  895. {ok, 500, _, _} = cowboy_client:response(Client2).
  896. rest_missing_put_callbacks(Config) ->
  897. Client = ?config(client, Config),
  898. {ok, Client2} = cowboy_client:request(<<"PUT">>,
  899. build_url("/missing_put_callbacks", Config),
  900. [{<<"content-type">>, <<"application/json">>}],
  901. <<"{}">>, Client),
  902. {ok, 500, _, _} = cowboy_client:response(Client2).
  903. rest_nodelete(Config) ->
  904. Client = ?config(client, Config),
  905. {ok, Client2} = cowboy_client:request(<<"DELETE">>,
  906. build_url("/nodelete", Config), Client),
  907. {ok, 500, _, _} = cowboy_client:response(Client2).
  908. rest_options_default(Config) ->
  909. Client = ?config(client, Config),
  910. {ok, Client2} = cowboy_client:request(<<"OPTIONS">>,
  911. build_url("/rest_empty_resource", Config), Client),
  912. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  913. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers).
  914. rest_patch(Config) ->
  915. Tests = [
  916. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  917. {422, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  918. {400, [{<<"content-type">>, <<"text/plain">>}], <<"halt">>},
  919. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  920. ],
  921. Client = ?config(client, Config),
  922. _ = [begin
  923. {ok, Client2} = cowboy_client:request(<<"PATCH">>,
  924. build_url("/patch", Config), Headers, Body, Client),
  925. {ok, Status, _, _} = cowboy_client:response(Client2),
  926. ok
  927. end || {Status, Headers, Body} <- Tests].
  928. rest_resource_get_etag(Config, Type) ->
  929. rest_resource_get_etag(Config, Type, []).
  930. rest_resource_get_etag(Config, Type, Headers) ->
  931. Client = ?config(client, Config),
  932. {ok, Client2} = cowboy_client:request(<<"GET">>,
  933. build_url("/resetags?type=" ++ Type, Config), Headers, Client),
  934. {ok, Status, RespHeaders, _} = cowboy_client:response(Client2),
  935. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  936. false -> {Status, false};
  937. {<<"etag">>, ETag} -> {Status, ETag}
  938. end.
  939. rest_resource_etags(Config) ->
  940. Tests = [
  941. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  942. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  943. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  944. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  945. {500, false, "binary-strong-unquoted"},
  946. {500, false, "binary-weak-unquoted"}
  947. ],
  948. _ = [{Status, ETag, Type} = begin
  949. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  950. {Ret, RespETag, Type}
  951. end || {Status, ETag, Type} <- Tests].
  952. rest_resource_etags_if_none_match(Config) ->
  953. Tests = [
  954. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  955. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  956. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  957. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  958. ],
  959. _ = [{Status, Type} = begin
  960. {Ret, _} = rest_resource_get_etag(Config, Type,
  961. [{<<"if-none-match">>, ETag}]),
  962. {Ret, Type}
  963. end || {Status, ETag, Type} <- Tests].
  964. set_env_dispatch(Config) ->
  965. Client = ?config(client, Config),
  966. {ok, Client2} = cowboy_client:request(<<"GET">>,
  967. build_url("/", Config), Client),
  968. {ok, 400, _, _} = cowboy_client:response(Client2),
  969. ok = cowboy:set_env(set_env, dispatch,
  970. cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
  971. {ok, Client3} = cowboy_client:request(<<"GET">>,
  972. build_url("/", Config), Client),
  973. {ok, 200, _, _} = cowboy_client:response(Client3).
  974. set_resp_body(Config) ->
  975. Client = ?config(client, Config),
  976. {ok, Client2} = cowboy_client:request(<<"GET">>,
  977. build_url("/set_resp/body", Config), Client),
  978. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  979. {ok, <<"A flameless dance does not equal a cycle">>, _}
  980. = cowboy_client:response_body(Client3).
  981. set_resp_header(Config) ->
  982. Client = ?config(client, Config),
  983. {ok, Client2} = cowboy_client:request(<<"GET">>,
  984. build_url("/set_resp/header", Config), Client),
  985. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  986. {<<"vary">>, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  987. {<<"set-cookie">>, _} = lists:keyfind(<<"set-cookie">>, 1, Headers).
  988. set_resp_overwrite(Config) ->
  989. Client = ?config(client, Config),
  990. {ok, Client2} = cowboy_client:request(<<"GET">>,
  991. build_url("/set_resp/overwrite", Config), Client),
  992. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  993. {<<"server">>, <<"DesireDrive/1.0">>}
  994. = lists:keyfind(<<"server">>, 1, Headers).
  995. slowloris(Config) ->
  996. Client = ?config(client, Config),
  997. Transport = ?config(transport, Config),
  998. {ok, Client2} = cowboy_client:connect(
  999. Transport, "localhost", ?config(port, Config), Client),
  1000. try
  1001. [begin
  1002. {ok, _} = cowboy_client:raw_request([C], Client2),
  1003. receive after 25 -> ok end
  1004. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  1005. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  1006. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  1007. error(failure)
  1008. catch error:{badmatch, _} ->
  1009. ok
  1010. end.
  1011. slowloris2(Config) ->
  1012. Client = ?config(client, Config),
  1013. Transport = ?config(transport, Config),
  1014. {ok, Client2} = cowboy_client:connect(
  1015. Transport, "localhost", ?config(port, Config), Client),
  1016. {ok, _} = cowboy_client:raw_request("GET / HTTP/1.1\r\n", Client2),
  1017. receive after 300 -> ok end,
  1018. {ok, _} = cowboy_client:raw_request("Host: localhost\r\n", Client2),
  1019. receive after 300 -> ok end,
  1020. {ok, 408, _, _} = cowboy_client:response(Client2).
  1021. static_attribute_etag(Config) ->
  1022. Client = ?config(client, Config),
  1023. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1024. build_url("/static_attribute_etag/test.html", Config), Client),
  1025. {ok, Client3} = cowboy_client:request(<<"GET">>,
  1026. build_url("/static_attribute_etag/test.html", Config), Client2),
  1027. {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
  1028. {ok, 200, Headers2, _} = cowboy_client:response(Client4),
  1029. {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  1030. {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  1031. false = ETag1 =:= undefined,
  1032. ETag1 = ETag2.
  1033. static_function_etag(Config) ->
  1034. Client = ?config(client, Config),
  1035. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1036. build_url("/static_function_etag/test.html", Config), Client),
  1037. {ok, Client3} = cowboy_client:request(<<"GET">>,
  1038. build_url("/static_function_etag/test.html", Config), Client2),
  1039. {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
  1040. {ok, 200, Headers2, _} = cowboy_client:response(Client4),
  1041. {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  1042. {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  1043. false = ETag1 =:= undefined,
  1044. ETag1 = ETag2.
  1045. %% Callback function for generating the ETag for the above test.
  1046. static_function_etag(Arguments, etag_data) ->
  1047. {_, Filepath} = lists:keyfind(filepath, 1, Arguments),
  1048. {_, _Filesize} = lists:keyfind(filesize, 1, Arguments),
  1049. {_, _INode} = lists:keyfind(inode, 1, Arguments),
  1050. {_, _Modified} = lists:keyfind(mtime, 1, Arguments),
  1051. ChecksumCommand = lists:flatten(io_lib:format("sha1sum ~s", [Filepath])),
  1052. [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "),
  1053. {strong, iolist_to_binary(Checksum)}.
  1054. static_mimetypes_function(Config) ->
  1055. Client = ?config(client, Config),
  1056. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1057. build_url("/static_mimetypes_function/test.html", Config), Client),
  1058. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  1059. {<<"content-type">>, <<"text/html">>}
  1060. = lists:keyfind(<<"content-type">>, 1, Headers).
  1061. static_specify_file(Config) ->
  1062. Client = ?config(client, Config),
  1063. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1064. build_url("/static_specify_file", Config), Client),
  1065. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  1066. {<<"content-type">>, <<"text/css">>}
  1067. = lists:keyfind(<<"content-type">>, 1, Headers),
  1068. {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
  1069. static_specify_file_catchall(Config) ->
  1070. Client = ?config(client, Config),
  1071. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1072. build_url("/static_specify_file/none", Config), Client),
  1073. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  1074. {<<"content-type">>, <<"text/css">>}
  1075. = lists:keyfind(<<"content-type">>, 1, Headers),
  1076. {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
  1077. static_test_file(Config) ->
  1078. Client = ?config(client, Config),
  1079. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1080. build_url("/static/test_file", Config), Client),
  1081. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  1082. {<<"content-type">>, <<"application/octet-stream">>}
  1083. = lists:keyfind(<<"content-type">>, 1, Headers).
  1084. static_test_file_css(Config) ->
  1085. Client = ?config(client, Config),
  1086. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1087. build_url("/static/test_file.css", Config), Client),
  1088. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  1089. {<<"content-type">>, <<"text/css">>}
  1090. = lists:keyfind(<<"content-type">>, 1, Headers).
  1091. stream_body_set_resp(Config) ->
  1092. Client = ?config(client, Config),
  1093. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1094. build_url("/stream_body/set_resp", Config), Client),
  1095. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1096. {ok, <<"stream_body_set_resp">>, _}
  1097. = cowboy_client:response_body(Client3).
  1098. stream_body_set_resp_close(Config) ->
  1099. Client = ?config(client, Config),
  1100. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1101. build_url("/stream_body/set_resp_close", Config), Client),
  1102. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1103. {ok, Transport, Socket} = cowboy_client:transport(Client3),
  1104. case element(7, Client3) of
  1105. <<"stream_body_set_resp_close">> ->
  1106. ok;
  1107. Buffer ->
  1108. {ok, Rest} = Transport:recv(Socket, 26 - byte_size(Buffer), 1000),
  1109. <<"stream_body_set_resp_close">> = << Buffer/binary, Rest/binary >>,
  1110. ok
  1111. end,
  1112. {error, closed} = Transport:recv(Socket, 0, 1000).
  1113. te_chunked(Config) ->
  1114. Client = ?config(client, Config),
  1115. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  1116. Chunks = body_to_chunks(50, Body, []),
  1117. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1118. build_url("/echo/body", Config),
  1119. [{<<"transfer-encoding">>, <<"chunked">>}],
  1120. Chunks, Client),
  1121. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1122. {ok, Body, _} = cowboy_client:response_body(Client3).
  1123. te_chunked_chopped(Config) ->
  1124. Client = ?config(client, Config),
  1125. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  1126. Body2 = iolist_to_binary(body_to_chunks(50, Body, [])),
  1127. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1128. build_url("/echo/body", Config),
  1129. [{<<"transfer-encoding">>, <<"chunked">>}], Client),
  1130. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  1131. _ = [begin
  1132. ok = Transport:send(Socket, << C >>),
  1133. ok = timer:sleep(10)
  1134. end || << C >> <= Body2],
  1135. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1136. {ok, Body, _} = cowboy_client:response_body(Client3).
  1137. te_chunked_delayed(Config) ->
  1138. Client = ?config(client, Config),
  1139. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  1140. Chunks = body_to_chunks(50, Body, []),
  1141. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1142. build_url("/echo/body", Config),
  1143. [{<<"transfer-encoding">>, <<"chunked">>}], Client),
  1144. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  1145. _ = [begin
  1146. ok = Transport:send(Socket, Chunk),
  1147. ok = timer:sleep(10)
  1148. end || Chunk <- Chunks],
  1149. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1150. {ok, Body, _} = cowboy_client:response_body(Client3).
  1151. te_identity(Config) ->
  1152. Client = ?config(client, Config),
  1153. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  1154. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1155. build_url("/echo/body", Config), [], Body, Client),
  1156. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1157. {ok, Body, _} = cowboy_client:response_body(Client3).