http_SUITE.erl 44 KB

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