http_SUITE.erl 49 KB

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