http_SUITE.erl 47 KB

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