http_SUITE.erl 46 KB

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