http_SUITE.erl 31 KB

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