http_SUITE.erl 46 KB

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