http_SUITE.erl 45 KB

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