http_SUITE.erl 49 KB

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