http_SUITE.erl 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. %% Copyright (c) 2011-2012, 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([error_chain_handle_after_reply/1]).
  30. -export([error_chain_handle_before_reply/1]).
  31. -export([error_handle_after_reply/1]).
  32. -export([error_init_after_reply/1]).
  33. -export([error_init_reply_handle_error/1]).
  34. -export([headers_dupe/1]).
  35. -export([http10_chunkless/1]).
  36. -export([http10_hostless/1]).
  37. -export([keepalive_max/1]).
  38. -export([keepalive_nl/1]).
  39. -export([multipart/1]).
  40. -export([nc_rand/1]).
  41. -export([nc_zero/1]).
  42. -export([onrequest/1]).
  43. -export([onrequest_reply/1]).
  44. -export([onresponse_crash/1]).
  45. -export([onresponse_reply/1]).
  46. -export([pipeline/1]).
  47. -export([rest_bad_accept/1]).
  48. -export([rest_keepalive/1]).
  49. -export([rest_keepalive_post/1]).
  50. -export([rest_missing_get_callbacks/1]).
  51. -export([rest_missing_put_callbacks/1]).
  52. -export([rest_nodelete/1]).
  53. -export([rest_resource_etags/1]).
  54. -export([rest_resource_etags_if_none_match/1]).
  55. -export([set_resp_body/1]).
  56. -export([set_resp_header/1]).
  57. -export([set_resp_overwrite/1]).
  58. -export([static_attribute_etag/1]).
  59. -export([static_function_etag/1]).
  60. -export([static_mimetypes_function/1]).
  61. -export([static_specify_file/1]).
  62. -export([static_specify_file_catchall/1]).
  63. -export([static_test_file/1]).
  64. -export([static_test_file_css/1]).
  65. -export([stream_body_set_resp/1]).
  66. -export([te_chunked/1]).
  67. -export([te_chunked_delayed/1]).
  68. -export([te_identity/1]).
  69. %% ct.
  70. all() ->
  71. [{group, http}, {group, https}, {group, onrequest}, {group, onresponse}].
  72. groups() ->
  73. Tests = [
  74. check_raw_status,
  75. check_status,
  76. chunked_response,
  77. echo_body,
  78. error_chain_handle_after_reply,
  79. error_chain_handle_before_reply,
  80. error_handle_after_reply,
  81. error_init_after_reply,
  82. error_init_reply_handle_error,
  83. headers_dupe,
  84. http10_chunkless,
  85. http10_hostless,
  86. keepalive_max,
  87. keepalive_nl,
  88. multipart,
  89. nc_rand,
  90. nc_zero,
  91. pipeline,
  92. rest_bad_accept,
  93. rest_keepalive,
  94. rest_keepalive_post,
  95. rest_missing_get_callbacks,
  96. rest_missing_put_callbacks,
  97. rest_nodelete,
  98. rest_resource_etags,
  99. rest_resource_etags_if_none_match,
  100. set_resp_body,
  101. set_resp_header,
  102. set_resp_overwrite,
  103. static_attribute_etag,
  104. static_function_etag,
  105. static_mimetypes_function,
  106. static_specify_file,
  107. static_specify_file_catchall,
  108. static_test_file,
  109. static_test_file_css,
  110. stream_body_set_resp,
  111. te_chunked,
  112. te_chunked_delayed,
  113. te_identity
  114. ],
  115. [
  116. {http, [], Tests},
  117. {https, [], Tests},
  118. {onrequest, [], [
  119. onrequest,
  120. onrequest_reply
  121. ]},
  122. {onresponse, [], [
  123. onresponse_crash,
  124. onresponse_reply
  125. ]}
  126. ].
  127. init_per_suite(Config) ->
  128. application:start(inets),
  129. application:start(crypto),
  130. application:start(ranch),
  131. application:start(cowboy),
  132. Config.
  133. end_per_suite(_Config) ->
  134. application:stop(cowboy),
  135. application:stop(ranch),
  136. application:stop(crypto),
  137. application:stop(inets),
  138. ok.
  139. init_per_group(http, Config) ->
  140. Port = 33080,
  141. Transport = ranch_tcp,
  142. Config1 = init_static_dir(Config),
  143. {ok, _} = cowboy:start_http(http, 100, [{port, Port}], [
  144. {dispatch, init_dispatch(Config1)},
  145. {max_keepalive, 50},
  146. {timeout, 500}
  147. ]),
  148. {ok, Client} = cowboy_client:init([]),
  149. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  150. {transport, Transport}, {client, Client}|Config1];
  151. init_per_group(https, Config) ->
  152. Port = 33081,
  153. Transport = ranch_ssl,
  154. Opts = [
  155. {certfile, ?config(data_dir, Config) ++ "cert.pem"},
  156. {keyfile, ?config(data_dir, Config) ++ "key.pem"},
  157. {password, "cowboy"}
  158. ],
  159. Config1 = init_static_dir(Config),
  160. application:start(public_key),
  161. application:start(ssl),
  162. {ok, _} = cowboy:start_https(https, 100, Opts ++ [{port, Port}], [
  163. {dispatch, init_dispatch(Config1)},
  164. {max_keepalive, 50},
  165. {timeout, 500}
  166. ]),
  167. {ok, Client} = cowboy_client:init(Opts),
  168. [{scheme, <<"https">>}, {port, Port}, {opts, Opts},
  169. {transport, Transport}, {client, Client}|Config1];
  170. init_per_group(onrequest, Config) ->
  171. Port = 33082,
  172. Transport = ranch_tcp,
  173. {ok, _} = cowboy:start_http(onrequest, 100, [{port, Port}], [
  174. {dispatch, init_dispatch(Config)},
  175. {max_keepalive, 50},
  176. {onrequest, fun onrequest_hook/1},
  177. {timeout, 500}
  178. ]),
  179. {ok, Client} = cowboy_client:init([]),
  180. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  181. {transport, Transport}, {client, Client}|Config];
  182. init_per_group(onresponse, Config) ->
  183. Port = 33083,
  184. Transport = ranch_tcp,
  185. {ok, _} = cowboy:start_http(onresponse, 100, [{port, Port}], [
  186. {dispatch, init_dispatch(Config)},
  187. {max_keepalive, 50},
  188. {onresponse, fun onresponse_hook/4},
  189. {timeout, 500}
  190. ]),
  191. {ok, Client} = cowboy_client:init([]),
  192. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  193. {transport, Transport}, {client, Client}|Config].
  194. end_per_group(https, Config) ->
  195. cowboy:stop_listener(https),
  196. application:stop(ssl),
  197. application:stop(public_key),
  198. end_static_dir(Config),
  199. ok;
  200. end_per_group(http, Config) ->
  201. cowboy:stop_listener(http),
  202. end_static_dir(Config);
  203. end_per_group(Name, _) ->
  204. cowboy:stop_listener(Name),
  205. ok.
  206. %% Dispatch configuration.
  207. init_dispatch(Config) ->
  208. [
  209. {[<<"localhost">>], [
  210. {[<<"chunked_response">>], chunked_handler, []},
  211. {[<<"init_shutdown">>], http_handler_init_shutdown, []},
  212. {[<<"long_polling">>], http_handler_long_polling, []},
  213. {[<<"headers">>, <<"dupe">>], http_handler,
  214. [{headers, [{<<"connection">>, <<"close">>}]}]},
  215. {[<<"set_resp">>, <<"header">>], http_handler_set_resp,
  216. [{headers, [{<<"vary">>, <<"Accept">>}]}]},
  217. {[<<"set_resp">>, <<"overwrite">>], http_handler_set_resp,
  218. [{headers, [{<<"server">>, <<"DesireDrive/1.0">>}]}]},
  219. {[<<"set_resp">>, <<"body">>], http_handler_set_resp,
  220. [{body, <<"A flameless dance does not equal a cycle">>}]},
  221. {[<<"stream_body">>, <<"set_resp">>], http_handler_stream_body,
  222. [{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
  223. {[<<"static">>, '...'], cowboy_static,
  224. [{directory, ?config(static_dir, Config)},
  225. {mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
  226. {[<<"static_mimetypes_function">>, '...'], cowboy_static,
  227. [{directory, ?config(static_dir, Config)},
  228. {mimetypes, {fun(Path, data) when is_binary(Path) ->
  229. [<<"text/html">>] end, data}}]},
  230. {[<<"handler_errors">>], http_handler_errors, []},
  231. {[<<"static_attribute_etag">>, '...'], cowboy_static,
  232. [{directory, ?config(static_dir, Config)},
  233. {etag, {attributes, [filepath, filesize, inode, mtime]}}]},
  234. {[<<"static_function_etag">>, '...'], cowboy_static,
  235. [{directory, ?config(static_dir, Config)},
  236. {etag, {fun static_function_etag/2, etag_data}}]},
  237. {[<<"static_specify_file">>, '...'], cowboy_static,
  238. [{directory, ?config(static_dir, Config)},
  239. {mimetypes, [{<<".css">>, [<<"text/css">>]}]},
  240. {file, <<"test_file.css">>}]},
  241. {[<<"multipart">>], http_handler_multipart, []},
  242. {[<<"echo">>, <<"body">>], http_handler_echo_body, []},
  243. {[<<"bad_accept">>], rest_simple_resource, []},
  244. {[<<"simple">>], rest_simple_resource, []},
  245. {[<<"forbidden_post">>], rest_forbidden_resource, [true]},
  246. {[<<"simple_post">>], rest_forbidden_resource, [false]},
  247. {[<<"missing_get_callbacks">>], rest_missing_callbacks, []},
  248. {[<<"missing_put_callbacks">>], rest_missing_callbacks, []},
  249. {[<<"nodelete">>], rest_nodelete_resource, []},
  250. {[<<"resetags">>], rest_resource_etags, []},
  251. {[<<"loop_timeout">>], http_handler_loop_timeout, []},
  252. {[], http_handler, []}
  253. ]}
  254. ].
  255. init_static_dir(Config) ->
  256. Dir = filename:join(?config(priv_dir, Config), "static"),
  257. Level1 = fun(Name) -> filename:join(Dir, Name) end,
  258. ok = file:make_dir(Dir),
  259. ok = file:write_file(Level1("test_file"), "test_file\n"),
  260. ok = file:write_file(Level1("test_file.css"), "test_file.css\n"),
  261. ok = file:write_file(Level1("test_noread"), "test_noread\n"),
  262. ok = file:change_mode(Level1("test_noread"), 8#0333),
  263. ok = file:write_file(Level1("test.html"), "test.html\n"),
  264. ok = file:make_dir(Level1("test_dir")),
  265. [{static_dir, Dir}|Config].
  266. end_static_dir(Config) ->
  267. Dir = ?config(static_dir, Config),
  268. Level1 = fun(Name) -> filename:join(Dir, Name) end,
  269. ok = file:delete(Level1("test_file")),
  270. ok = file:delete(Level1("test_file.css")),
  271. ok = file:delete(Level1("test_noread")),
  272. ok = file:delete(Level1("test.html")),
  273. ok = file:del_dir(Level1("test_dir")),
  274. ok = file:del_dir(Dir),
  275. Config.
  276. %% Convenience functions.
  277. quick_raw(Data, Config) ->
  278. Client = ?config(client, Config),
  279. Transport = ?config(transport, Config),
  280. {ok, Client2} = cowboy_client:connect(
  281. Transport, "localhost", ?config(port, Config), Client),
  282. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  283. case cowboy_client:response(Client3) of
  284. {ok, Status, _, _} -> Status;
  285. {error, _} -> closed
  286. end.
  287. build_url(Path, Config) ->
  288. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  289. {port, Port} = lists:keyfind(port, 1, Config),
  290. PortBin = list_to_binary(integer_to_list(Port)),
  291. PathBin = list_to_binary(Path),
  292. << Scheme/binary, "://localhost:", PortBin/binary, PathBin/binary >>.
  293. quick_get(URL, Config) ->
  294. Client = ?config(client, Config),
  295. {ok, Client2} = cowboy_client:request(<<"GET">>,
  296. build_url(URL, Config), Client),
  297. {ok, Status, _, _} = cowboy_client:response(Client2),
  298. Status.
  299. body_to_chunks(_, <<>>, Acc) ->
  300. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  301. body_to_chunks(ChunkSize, Body, Acc) ->
  302. BodySize = byte_size(Body),
  303. ChunkSize2 = case BodySize < ChunkSize of
  304. true -> BodySize;
  305. false -> ChunkSize
  306. end,
  307. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  308. ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
  309. body_to_chunks(ChunkSize, Rest,
  310. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  311. %% Tests.
  312. check_raw_status(Config) ->
  313. Huge = [$0 || _ <- lists:seq(1, 5000)],
  314. HugeCookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  315. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _ <- lists:seq(1, 40)]),
  316. ResponsePacket =
  317. "HTTP/1.0 302 Found\r
  318. Location: http://www.google.co.il/\r
  319. Cache-Control: private\r
  320. Content-Type: text/html; charset=UTF-8\r
  321. 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
  322. Date: Sun, 04 Dec 2011 15:55:01 GMT\r
  323. Server: gws\r
  324. Content-Length: 221\r
  325. X-XSS-Protection: 1; mode=block\r
  326. X-Frame-Options: SAMEORIGIN\r
  327. \r
  328. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  329. <TITLE>302 Moved</TITLE></HEAD><BODY>
  330. <H1>302 Moved</H1>
  331. The document has moved
  332. <A HREF=\"http://www.google.co.il/\">here</A>.
  333. </BODY></HTML>",
  334. Tests = [
  335. {200, ["GET / HTTP/1.0\r\nHost: localhost\r\n"
  336. "Set-Cookie: ", HugeCookie, "\r\n\r\n"]},
  337. {200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  338. {200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  339. {400, "\n"},
  340. {400, "Garbage\r\n\r\n"},
  341. {400, "\r\n\r\n\r\n\r\n\r\n\r\n"},
  342. {400, "GET / HTTP/1.1\r\nHost: ninenines.eu\r\n\r\n"},
  343. {400, "GET http://proxy/ HTTP/1.1\r\n\r\n"},
  344. {505, ResponsePacket},
  345. {408, "GET / HTTP/1.1\r\n"},
  346. {408, "GET / HTTP/1.1\r\nHost: localhost"},
  347. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n"},
  348. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"},
  349. {414, Huge},
  350. {400, "GET / HTTP/1.1\r\n" ++ Huge},
  351. {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"},
  352. {closed, ""},
  353. {closed, "\r\n"},
  354. {closed, "\r\n\r\n"},
  355. {closed, "GET / HTTP/1.1"}
  356. ],
  357. _ = [{Status, Packet} = begin
  358. Ret = quick_raw(Packet, Config),
  359. {Ret, Packet}
  360. end || {Status, Packet} <- Tests].
  361. check_status(Config) ->
  362. Tests = [
  363. {102, "/long_polling"},
  364. {200, "/"},
  365. {200, "/simple"},
  366. {204, "/loop_timeout"},
  367. {400, "/static/%2f"},
  368. {400, "/static/%2e"},
  369. {400, "/static/%2e%2e"},
  370. {403, "/static/test_dir"},
  371. {403, "/static/test_dir/"},
  372. {403, "/static/test_noread"},
  373. {404, "/not/found"},
  374. {404, "/static/not_found"},
  375. {500, "/handler_errors?case=handler_before_reply"},
  376. {500, "/handler_errors?case=init_before_reply"},
  377. {666, "/init_shutdown"}
  378. ],
  379. _ = [{Status, URL} = begin
  380. Ret = quick_get(URL, Config),
  381. {Ret, URL}
  382. end || {Status, URL} <- Tests].
  383. %% @todo Convert to cowboy_client.
  384. chunked_response(Config) ->
  385. {ok, {{"HTTP/1.1", 200, "OK"}, _, "chunked_handler\r\nworks fine!"}}
  386. = httpc:request(binary_to_list(build_url("/chunked_response", Config))).
  387. %% Check if sending requests whose size is around the MTU breaks something.
  388. echo_body(Config) ->
  389. Client = ?config(client, Config),
  390. {ok, [{mtu, MTU}]} = inet:ifget("lo", [mtu]),
  391. [begin
  392. Body = list_to_binary(lists:duplicate(Size, $a)),
  393. {ok, Client2} = cowboy_client:request(<<"POST">>,
  394. build_url("/echo/body", Config),
  395. [{<<"connection">>, <<"close">>}],
  396. Body, Client),
  397. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  398. {ok, Body, _} = cowboy_client:response_body(Client3)
  399. end || Size <- lists:seq(MTU - 500, MTU)].
  400. error_chain_handle_after_reply(Config) ->
  401. Client = ?config(client, Config),
  402. {ok, Client2} = cowboy_client:request(<<"GET">>,
  403. build_url("/", Config), Client),
  404. {ok, Client3} = cowboy_client:request(<<"GET">>,
  405. build_url("/handler_errors?case=handle_after_reply", Config), Client2),
  406. {ok, 200, _, Client4} = cowboy_client:response(Client3),
  407. {ok, 200, _, Client5} = cowboy_client:response(Client4),
  408. {error, closed} = cowboy_client:response(Client5).
  409. error_chain_handle_before_reply(Config) ->
  410. Client = ?config(client, Config),
  411. {ok, Client2} = cowboy_client:request(<<"GET">>,
  412. build_url("/", Config), Client),
  413. {ok, Client3} = cowboy_client:request(<<"GET">>,
  414. build_url("/handler_errors?case=handle_before_reply", Config), Client2),
  415. {ok, 200, _, Client4} = cowboy_client:response(Client3),
  416. {ok, 500, _, Client5} = cowboy_client:response(Client4),
  417. {error, closed} = cowboy_client:response(Client5).
  418. error_handle_after_reply(Config) ->
  419. Client = ?config(client, Config),
  420. {ok, Client2} = cowboy_client:request(<<"GET">>,
  421. build_url("/handler_errors?case=handle_after_reply", Config), Client),
  422. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  423. {error, closed} = cowboy_client:response(Client3).
  424. error_init_after_reply(Config) ->
  425. Client = ?config(client, Config),
  426. {ok, Client2} = cowboy_client:request(<<"GET">>,
  427. build_url("/handler_errors?case=init_after_reply", Config), Client),
  428. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  429. {error, closed} = cowboy_client:response(Client3).
  430. error_init_reply_handle_error(Config) ->
  431. Client = ?config(client, Config),
  432. {ok, Client2} = cowboy_client:request(<<"GET">>,
  433. build_url("/handler_errors?case=init_reply_handle_error", Config), Client),
  434. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  435. {error, closed} = cowboy_client:response(Client3).
  436. headers_dupe(Config) ->
  437. Client = ?config(client, Config),
  438. {ok, Client2} = cowboy_client:request(<<"GET">>,
  439. build_url("/headers/dupe", Config), Client),
  440. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  441. {<<"connection">>, <<"close">>}
  442. = lists:keyfind(<<"connection">>, 1, Headers),
  443. Connections = [H || H = {Name, _} <- Headers, Name =:= <<"connection">>],
  444. 1 = length(Connections),
  445. {error, closed} = cowboy_client:response(Client3).
  446. http10_chunkless(Config) ->
  447. Client = ?config(client, Config),
  448. Transport = ?config(transport, Config),
  449. {ok, Client2} = cowboy_client:connect(
  450. Transport, "localhost", ?config(port, Config), Client),
  451. Data = "GET /chunked_response HTTP/1.0\r\nHost: localhost\r\n\r\n",
  452. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  453. {ok, 200, Headers, Client4} = cowboy_client:response(Client3),
  454. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  455. %% Hack: we just try to get 28 bytes and compare.
  456. {ok, Transport, Socket} = cowboy_client:transport(Client4),
  457. Buffer = element(7, Client4),
  458. Buffer2 = case Transport:recv(Socket, 28 - byte_size(Buffer), 1000) of
  459. {ok, Recv} -> << Buffer/binary, Recv/binary >>;
  460. _ -> Buffer
  461. end,
  462. <<"chunked_handler\r\nworks fine!">> = Buffer2.
  463. http10_hostless(Config) ->
  464. Port10 = ?config(port, Config) + 10,
  465. Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)),
  466. ranch:start_listener(Name, 5,
  467. ?config(transport, Config), ?config(opts, Config) ++ [{port, Port10}],
  468. cowboy_protocol, [
  469. {dispatch, [{'_', [
  470. {[<<"http1.0">>, <<"hostless">>], http_handler, []}]}]},
  471. {max_keepalive, 50},
  472. {timeout, 500}]
  473. ),
  474. 200 = quick_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
  475. [{port, Port10}|Config]),
  476. cowboy:stop_listener(http10).
  477. keepalive_max(Config) ->
  478. Client = ?config(client, Config),
  479. URL = build_url("/", Config),
  480. ok = keepalive_max_loop(Client, URL, 50).
  481. keepalive_max_loop(_, _, 0) ->
  482. ok;
  483. keepalive_max_loop(Client, URL, N) ->
  484. Headers = [{<<"connection">>, <<"keep-alive">>}],
  485. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  486. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  487. Expected = case N of
  488. 1 -> <<"close">>;
  489. N -> <<"keep-alive">>
  490. end,
  491. {<<"connection">>, Expected}
  492. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  493. keepalive_max_loop(Client3, URL, N - 1).
  494. keepalive_nl(Config) ->
  495. Client = ?config(client, Config),
  496. URL = build_url("/", Config),
  497. ok = keepalive_nl_loop(Client, URL, 10).
  498. keepalive_nl_loop(_, _, 0) ->
  499. ok;
  500. keepalive_nl_loop(Client, URL, N) ->
  501. Headers = [{<<"connection">>, <<"keep-alive">>}],
  502. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  503. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  504. {<<"connection">>, <<"keep-alive">>}
  505. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  506. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  507. ok = Transport:send(Socket, <<"\r\n">>), %% empty line
  508. keepalive_nl_loop(Client3, URL, N - 1).
  509. multipart(Config) ->
  510. Client = ?config(client, Config),
  511. Body = <<
  512. "This is a preamble."
  513. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  514. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  515. "\r\n--OHai--"
  516. "This is an epiloque."
  517. >>,
  518. {ok, Client2} = cowboy_client:request(<<"POST">>,
  519. build_url("/multipart", Config),
  520. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
  521. Body, Client),
  522. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  523. {ok, RespBody, _} = cowboy_client:response_body(Client3),
  524. Parts = binary_to_term(RespBody),
  525. Parts = [
  526. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  527. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  528. ].
  529. nc_reqs(Config, Input) ->
  530. Cat = os:find_executable("cat"),
  531. Nc = os:find_executable("nc"),
  532. case {Cat, Nc} of
  533. {false, _} ->
  534. {skip, {notfound, cat}};
  535. {_, false} ->
  536. {skip, {notfound, nc}};
  537. _Good ->
  538. %% Throw garbage at the server then check if it's still up.
  539. {port, Port} = lists:keyfind(port, 1, Config),
  540. StrPort = integer_to_list(Port),
  541. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  542. || _ <- lists:seq(1, 100)],
  543. 200 = quick_get("/", Config)
  544. end.
  545. nc_rand(Config) ->
  546. nc_reqs(Config, "/dev/urandom").
  547. nc_zero(Config) ->
  548. nc_reqs(Config, "/dev/zero").
  549. onrequest(Config) ->
  550. Client = ?config(client, Config),
  551. {ok, Client2} = cowboy_client:request(<<"GET">>,
  552. build_url("/", Config), Client),
  553. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  554. {<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers),
  555. {ok, <<"http_handler">>, _} = cowboy_client:response_body(Client3).
  556. onrequest_reply(Config) ->
  557. Client = ?config(client, Config),
  558. {ok, Client2} = cowboy_client:request(<<"GET">>,
  559. build_url("/?reply=1", Config), Client),
  560. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  561. {<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
  562. {ok, <<"replied!">>, _} = cowboy_client:response_body(Client3).
  563. %% Hook for the above onrequest tests.
  564. onrequest_hook(Req) ->
  565. case cowboy_req:qs_val(<<"reply">>, Req) of
  566. {undefined, Req2} ->
  567. cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req2);
  568. {_, Req2} ->
  569. {ok, Req3} = cowboy_req:reply(
  570. 200, [], <<"replied!">>, Req2),
  571. Req3
  572. end.
  573. onresponse_crash(Config) ->
  574. Client = ?config(client, Config),
  575. {ok, Client2} = cowboy_client:request(<<"GET">>,
  576. build_url("/handler_errors?case=init_before_reply", Config), Client),
  577. {ok, 777, Headers, _} = cowboy_client:response(Client2),
  578. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
  579. onresponse_reply(Config) ->
  580. Client = ?config(client, Config),
  581. {ok, Client2} = cowboy_client:request(<<"GET">>,
  582. build_url("/", Config), Client),
  583. {ok, 777, Headers, Client3} = cowboy_client:response(Client2),
  584. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
  585. %% Make sure we don't get the body initially sent.
  586. {error, closed} = cowboy_client:response_body(Client3).
  587. %% Hook for the above onresponse tests.
  588. onresponse_hook(_, Headers, _, Req) ->
  589. {ok, Req2} = cowboy_req:reply(
  590. <<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req),
  591. Req2.
  592. pipeline(Config) ->
  593. Client = ?config(client, Config),
  594. {ok, Client2} = cowboy_client:request(<<"GET">>,
  595. build_url("/", Config), Client),
  596. {ok, Client3} = cowboy_client:request(<<"GET">>,
  597. build_url("/", Config), Client2),
  598. {ok, Client4} = cowboy_client:request(<<"GET">>,
  599. build_url("/", Config), Client3),
  600. {ok, Client5} = cowboy_client:request(<<"GET">>,
  601. build_url("/", Config), Client4),
  602. {ok, Client6} = cowboy_client:request(<<"GET">>,
  603. build_url("/", Config), [{<<"connection">>, <<"close">>}], Client5),
  604. {ok, 200, _, Client7} = cowboy_client:response(Client6),
  605. {ok, 200, _, Client8} = cowboy_client:response(Client7),
  606. {ok, 200, _, Client9} = cowboy_client:response(Client8),
  607. {ok, 200, _, Client10} = cowboy_client:response(Client9),
  608. {ok, 200, _, Client11} = cowboy_client:response(Client10),
  609. {error, closed} = cowboy_client:response(Client11).
  610. rest_bad_accept(Config) ->
  611. Client = ?config(client, Config),
  612. {ok, Client2} = cowboy_client:request(<<"GET">>,
  613. build_url("/bad_accept", Config),
  614. [{<<"accept">>, <<"1">>}],
  615. Client),
  616. {ok, 400, _, _} = cowboy_client:response(Client2).
  617. rest_keepalive(Config) ->
  618. Client = ?config(client, Config),
  619. URL = build_url("/simple", Config),
  620. ok = rest_keepalive_loop(Client, URL, 10).
  621. rest_keepalive_loop(_, _, 0) ->
  622. ok;
  623. rest_keepalive_loop(Client, URL, N) ->
  624. Headers = [{<<"connection">>, <<"keep-alive">>}],
  625. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  626. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  627. {<<"connection">>, <<"keep-alive">>}
  628. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  629. rest_keepalive_loop(Client3, URL, N - 1).
  630. rest_keepalive_post(Config) ->
  631. Client = ?config(client, Config),
  632. ok = rest_keepalive_post_loop(Config, Client, forbidden_post, 10).
  633. rest_keepalive_post_loop(_, _, _, 0) ->
  634. ok;
  635. rest_keepalive_post_loop(Config, Client, simple_post, N) ->
  636. Headers = [
  637. {<<"connection">>, <<"keep-alive">>},
  638. {<<"content-type">>, <<"text/plain">>}
  639. ],
  640. {ok, Client2} = cowboy_client:request(<<"POST">>,
  641. build_url("/simple_post", Config), Headers, "12345", Client),
  642. {ok, 303, RespHeaders, Client3} = cowboy_client:response(Client2),
  643. {<<"connection">>, <<"keep-alive">>}
  644. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  645. rest_keepalive_post_loop(Config, Client3, forbidden_post, N - 1);
  646. rest_keepalive_post_loop(Config, Client, forbidden_post, N) ->
  647. Headers = [
  648. {<<"connection">>, <<"keep-alive">>},
  649. {<<"content-type">>, <<"text/plain">>}
  650. ],
  651. {ok, Client2} = cowboy_client:request(<<"POST">>,
  652. build_url("/forbidden_post", Config), Headers, "12345", Client),
  653. {ok, 403, RespHeaders, Client3} = cowboy_client:response(Client2),
  654. {<<"connection">>, <<"keep-alive">>}
  655. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  656. rest_keepalive_post_loop(Config, Client3, simple_post, N - 1).
  657. rest_missing_get_callbacks(Config) ->
  658. Client = ?config(client, Config),
  659. {ok, Client2} = cowboy_client:request(<<"GET">>,
  660. build_url("/missing_get_callbacks", Config), Client),
  661. {ok, 500, _, _} = cowboy_client:response(Client2).
  662. rest_missing_put_callbacks(Config) ->
  663. Client = ?config(client, Config),
  664. {ok, Client2} = cowboy_client:request(<<"PUT">>,
  665. build_url("/missing_put_callbacks", Config),
  666. [{<<"content-type">>, <<"application/json">>}],
  667. <<"{}">>, Client),
  668. {ok, 500, _, _} = cowboy_client:response(Client2).
  669. rest_nodelete(Config) ->
  670. Client = ?config(client, Config),
  671. {ok, Client2} = cowboy_client:request(<<"DELETE">>,
  672. build_url("/nodelete", Config), Client),
  673. {ok, 500, _, _} = cowboy_client:response(Client2).
  674. rest_resource_get_etag(Config, Type) ->
  675. rest_resource_get_etag(Config, Type, []).
  676. rest_resource_get_etag(Config, Type, Headers) ->
  677. Client = ?config(client, Config),
  678. {ok, Client2} = cowboy_client:request(<<"GET">>,
  679. build_url("/resetags?type=" ++ Type, Config), Headers, Client),
  680. {ok, Status, RespHeaders, _} = cowboy_client:response(Client2),
  681. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  682. false -> {Status, false};
  683. {<<"etag">>, ETag} -> {Status, ETag}
  684. end.
  685. rest_resource_etags(Config) ->
  686. Tests = [
  687. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  688. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  689. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  690. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  691. {500, false, "binary-strong-unquoted"},
  692. {500, false, "binary-weak-unquoted"}
  693. ],
  694. _ = [{Status, ETag, Type} = begin
  695. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  696. {Ret, RespETag, Type}
  697. end || {Status, ETag, Type} <- Tests].
  698. rest_resource_etags_if_none_match(Config) ->
  699. Tests = [
  700. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  701. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  702. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  703. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  704. ],
  705. _ = [{Status, Type} = begin
  706. {Ret, _} = rest_resource_get_etag(Config, Type,
  707. [{<<"if-none-match">>, ETag}]),
  708. {Ret, Type}
  709. end || {Status, ETag, Type} <- Tests].
  710. set_resp_body(Config) ->
  711. Client = ?config(client, Config),
  712. {ok, Client2} = cowboy_client:request(<<"GET">>,
  713. build_url("/set_resp/body", Config), Client),
  714. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  715. {ok, <<"A flameless dance does not equal a cycle">>, _}
  716. = cowboy_client:response_body(Client3).
  717. set_resp_header(Config) ->
  718. Client = ?config(client, Config),
  719. {ok, Client2} = cowboy_client:request(<<"GET">>,
  720. build_url("/set_resp/header", Config), Client),
  721. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  722. {<<"vary">>, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  723. {<<"set-cookie">>, _} = lists:keyfind(<<"set-cookie">>, 1, Headers).
  724. set_resp_overwrite(Config) ->
  725. Client = ?config(client, Config),
  726. {ok, Client2} = cowboy_client:request(<<"GET">>,
  727. build_url("/set_resp/overwrite", Config), Client),
  728. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  729. {<<"server">>, <<"DesireDrive/1.0">>}
  730. = lists:keyfind(<<"server">>, 1, Headers).
  731. static_attribute_etag(Config) ->
  732. Client = ?config(client, Config),
  733. {ok, Client2} = cowboy_client:request(<<"GET">>,
  734. build_url("/static_attribute_etag/test.html", Config), Client),
  735. {ok, Client3} = cowboy_client:request(<<"GET">>,
  736. build_url("/static_attribute_etag/test.html", Config), Client2),
  737. {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
  738. {ok, 200, Headers2, _} = cowboy_client:response(Client4),
  739. {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  740. {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  741. false = ETag1 =:= undefined,
  742. ETag1 = ETag2.
  743. static_function_etag(Config) ->
  744. Client = ?config(client, Config),
  745. {ok, Client2} = cowboy_client:request(<<"GET">>,
  746. build_url("/static_function_etag/test.html", Config), Client),
  747. {ok, Client3} = cowboy_client:request(<<"GET">>,
  748. build_url("/static_function_etag/test.html", Config), Client2),
  749. {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
  750. {ok, 200, Headers2, _} = cowboy_client:response(Client4),
  751. {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  752. {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  753. false = ETag1 =:= undefined,
  754. ETag1 = ETag2.
  755. %% Callback function for generating the ETag for the above test.
  756. static_function_etag(Arguments, etag_data) ->
  757. {_, Filepath} = lists:keyfind(filepath, 1, Arguments),
  758. {_, _Filesize} = lists:keyfind(filesize, 1, Arguments),
  759. {_, _INode} = lists:keyfind(inode, 1, Arguments),
  760. {_, _Modified} = lists:keyfind(mtime, 1, Arguments),
  761. ChecksumCommand = lists:flatten(io_lib:format("sha1sum ~s", [Filepath])),
  762. [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "),
  763. {strong, iolist_to_binary(Checksum)}.
  764. static_mimetypes_function(Config) ->
  765. Client = ?config(client, Config),
  766. {ok, Client2} = cowboy_client:request(<<"GET">>,
  767. build_url("/static_mimetypes_function/test.html", Config), Client),
  768. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  769. {<<"content-type">>, <<"text/html">>}
  770. = lists:keyfind(<<"content-type">>, 1, Headers).
  771. static_specify_file(Config) ->
  772. Client = ?config(client, Config),
  773. {ok, Client2} = cowboy_client:request(<<"GET">>,
  774. build_url("/static_specify_file", Config), Client),
  775. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  776. {<<"content-type">>, <<"text/css">>}
  777. = lists:keyfind(<<"content-type">>, 1, Headers),
  778. {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
  779. static_specify_file_catchall(Config) ->
  780. Client = ?config(client, Config),
  781. {ok, Client2} = cowboy_client:request(<<"GET">>,
  782. build_url("/static_specify_file/none", Config), Client),
  783. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  784. {<<"content-type">>, <<"text/css">>}
  785. = lists:keyfind(<<"content-type">>, 1, Headers),
  786. {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
  787. static_test_file(Config) ->
  788. Client = ?config(client, Config),
  789. {ok, Client2} = cowboy_client:request(<<"GET">>,
  790. build_url("/static/test_file", Config), Client),
  791. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  792. {<<"content-type">>, <<"application/octet-stream">>}
  793. = lists:keyfind(<<"content-type">>, 1, Headers).
  794. static_test_file_css(Config) ->
  795. Client = ?config(client, Config),
  796. {ok, Client2} = cowboy_client:request(<<"GET">>,
  797. build_url("/static/test_file.css", Config), Client),
  798. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  799. {<<"content-type">>, <<"text/css">>}
  800. = lists:keyfind(<<"content-type">>, 1, Headers).
  801. stream_body_set_resp(Config) ->
  802. Client = ?config(client, Config),
  803. {ok, Client2} = cowboy_client:request(<<"GET">>,
  804. build_url("/stream_body/set_resp", Config), Client),
  805. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  806. {ok, <<"stream_body_set_resp">>, _}
  807. = cowboy_client:response_body(Client3).
  808. te_chunked(Config) ->
  809. Client = ?config(client, Config),
  810. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  811. Chunks = body_to_chunks(50, Body, []),
  812. {ok, Client2} = cowboy_client:request(<<"GET">>,
  813. build_url("/echo/body", Config),
  814. [{<<"transfer-encoding">>, <<"chunked">>}],
  815. Chunks, Client),
  816. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  817. {ok, Body, _} = cowboy_client:response_body(Client3).
  818. te_chunked_delayed(Config) ->
  819. Client = ?config(client, Config),
  820. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  821. Chunks = body_to_chunks(50, Body, []),
  822. {ok, Client2} = cowboy_client:request(<<"GET">>,
  823. build_url("/echo/body", Config),
  824. [{<<"transfer-encoding">>, <<"chunked">>}], Client),
  825. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  826. _ = [begin
  827. ok = Transport:send(Socket, Chunk),
  828. ok = timer:sleep(10)
  829. end || Chunk <- Chunks],
  830. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  831. {ok, Body, _} = cowboy_client:response_body(Client3).
  832. te_identity(Config) ->
  833. Client = ?config(client, Config),
  834. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  835. {ok, Client2} = cowboy_client:request(<<"GET">>,
  836. build_url("/echo/body", Config), [], Body, Client),
  837. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  838. {ok, Body, _} = cowboy_client:response_body(Client3).