http_SUITE.erl 49 KB

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