http_SUITE.erl 33 KB

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