http_SUITE.erl 32 KB

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