http_SUITE.erl 47 KB

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