http_SUITE.erl 49 KB

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