http_SUITE.erl 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. %% Copyright (c) 2011-2012, Loïc Hoguin <essen@ninenines.eu>
  2. %% Copyright (c) 2011, Anthony Ramine <nox@dev-extend.eu>
  3. %%
  4. %% Permission to use, copy, modify, and/or distribute this software for any
  5. %% purpose with or without fee is hereby granted, provided that the above
  6. %% copyright notice and this permission notice appear in all copies.
  7. %%
  8. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. -module(http_SUITE).
  16. -include_lib("common_test/include/ct.hrl").
  17. %% ct.
  18. -export([all/0]).
  19. -export([groups/0]).
  20. -export([init_per_suite/1]).
  21. -export([end_per_suite/1]).
  22. -export([init_per_group/2]).
  23. -export([end_per_group/2]).
  24. %% Tests.
  25. -export([check_raw_status/1]).
  26. -export([check_status/1]).
  27. -export([chunked_response/1]).
  28. -export([echo_body/1]).
  29. -export([error_chain_handle_after_reply/1]).
  30. -export([error_chain_handle_before_reply/1]).
  31. -export([error_handle_after_reply/1]).
  32. -export([error_init_after_reply/1]).
  33. -export([error_init_reply_handle_error/1]).
  34. -export([headers_dupe/1]).
  35. -export([http10_chunkless/1]).
  36. -export([http10_hostless/1]).
  37. -export([keepalive_max/1]).
  38. -export([keepalive_nl/1]).
  39. -export([multipart/1]).
  40. -export([nc_rand/1]).
  41. -export([nc_zero/1]).
  42. -export([onrequest/1]).
  43. -export([onrequest_reply/1]).
  44. -export([onresponse_crash/1]).
  45. -export([onresponse_reply/1]).
  46. -export([pipeline/1]).
  47. -export([rest_bad_accept/1]).
  48. -export([rest_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. {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. {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. {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. {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. {505, ResponsePacket},
  348. {408, "GET / HTTP/1.1\r\n"},
  349. {408, "GET / HTTP/1.1\r\nHost: localhost"},
  350. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n"},
  351. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"},
  352. {414, Huge},
  353. {400, "GET / HTTP/1.1\r\n" ++ Huge},
  354. {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"},
  355. {closed, ""},
  356. {closed, "\r\n"},
  357. {closed, "\r\n\r\n"},
  358. {closed, "GET / HTTP/1.1"}
  359. ],
  360. _ = [{Status, Packet} = begin
  361. Ret = quick_raw(Packet, Config),
  362. {Ret, Packet}
  363. end || {Status, Packet} <- Tests].
  364. check_status(Config) ->
  365. Tests = [
  366. {102, "/long_polling"},
  367. {200, "/"},
  368. {200, "/simple"},
  369. {204, "/loop_timeout"},
  370. {400, "/static/%2f"},
  371. {400, "/static/%2e"},
  372. {400, "/static/%2e%2e"},
  373. {403, "/static/test_dir"},
  374. {403, "/static/test_dir/"},
  375. {403, "/static/test_noread"},
  376. {404, "/not/found"},
  377. {404, "/static/not_found"},
  378. {500, "/handler_errors?case=handler_before_reply"},
  379. {500, "/handler_errors?case=init_before_reply"},
  380. {666, "/init_shutdown"}
  381. ],
  382. _ = [{Status, URL} = begin
  383. Ret = quick_get(URL, Config),
  384. {Ret, URL}
  385. end || {Status, URL} <- Tests].
  386. %% @todo Convert to cowboy_client.
  387. chunked_response(Config) ->
  388. {ok, {{"HTTP/1.1", 200, "OK"}, _, "chunked_handler\r\nworks fine!"}}
  389. = httpc:request(binary_to_list(build_url("/chunked_response", Config))).
  390. %% Check if sending requests whose size is around the MTU breaks something.
  391. echo_body(Config) ->
  392. Client = ?config(client, Config),
  393. {ok, [{mtu, MTU}]} = inet:ifget("lo", [mtu]),
  394. [begin
  395. Body = list_to_binary(lists:duplicate(Size, $a)),
  396. {ok, Client2} = cowboy_client:request(<<"POST">>,
  397. build_url("/echo/body", Config),
  398. [{<<"connection">>, <<"close">>}],
  399. Body, Client),
  400. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  401. {ok, Body, _} = cowboy_client:response_body(Client3)
  402. end || Size <- lists:seq(MTU - 500, MTU)].
  403. error_chain_handle_after_reply(Config) ->
  404. Client = ?config(client, Config),
  405. {ok, Client2} = cowboy_client:request(<<"GET">>,
  406. build_url("/", Config), Client),
  407. {ok, Client3} = cowboy_client:request(<<"GET">>,
  408. build_url("/handler_errors?case=handle_after_reply", Config), Client2),
  409. {ok, 200, _, Client4} = cowboy_client:response(Client3),
  410. {ok, 200, _, Client5} = cowboy_client:response(Client4),
  411. {error, closed} = cowboy_client:response(Client5).
  412. error_chain_handle_before_reply(Config) ->
  413. Client = ?config(client, Config),
  414. {ok, Client2} = cowboy_client:request(<<"GET">>,
  415. build_url("/", Config), Client),
  416. {ok, Client3} = cowboy_client:request(<<"GET">>,
  417. build_url("/handler_errors?case=handle_before_reply", Config), Client2),
  418. {ok, 200, _, Client4} = cowboy_client:response(Client3),
  419. {ok, 500, _, Client5} = cowboy_client:response(Client4),
  420. {error, closed} = cowboy_client:response(Client5).
  421. error_handle_after_reply(Config) ->
  422. Client = ?config(client, Config),
  423. {ok, Client2} = cowboy_client:request(<<"GET">>,
  424. build_url("/handler_errors?case=handle_after_reply", Config), Client),
  425. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  426. {error, closed} = cowboy_client:response(Client3).
  427. error_init_after_reply(Config) ->
  428. Client = ?config(client, Config),
  429. {ok, Client2} = cowboy_client:request(<<"GET">>,
  430. build_url("/handler_errors?case=init_after_reply", Config), Client),
  431. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  432. {error, closed} = cowboy_client:response(Client3).
  433. error_init_reply_handle_error(Config) ->
  434. Client = ?config(client, Config),
  435. {ok, Client2} = cowboy_client:request(<<"GET">>,
  436. build_url("/handler_errors?case=init_reply_handle_error", Config), Client),
  437. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  438. {error, closed} = cowboy_client:response(Client3).
  439. headers_dupe(Config) ->
  440. Client = ?config(client, Config),
  441. {ok, Client2} = cowboy_client:request(<<"GET">>,
  442. build_url("/headers/dupe", Config), Client),
  443. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  444. {<<"connection">>, <<"close">>}
  445. = lists:keyfind(<<"connection">>, 1, Headers),
  446. Connections = [H || H = {Name, _} <- Headers, Name =:= <<"connection">>],
  447. 1 = length(Connections),
  448. {error, closed} = cowboy_client:response(Client3).
  449. http10_chunkless(Config) ->
  450. Client = ?config(client, Config),
  451. Transport = ?config(transport, Config),
  452. {ok, Client2} = cowboy_client:connect(
  453. Transport, "localhost", ?config(port, Config), Client),
  454. Data = "GET /chunked_response HTTP/1.0\r\nHost: localhost\r\n\r\n",
  455. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  456. {ok, 200, Headers, Client4} = cowboy_client:response(Client3),
  457. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  458. %% Hack: we just try to get 28 bytes and compare.
  459. {ok, Transport, Socket} = cowboy_client:transport(Client4),
  460. Buffer = element(7, Client4),
  461. Buffer2 = case Transport:recv(Socket, 28 - byte_size(Buffer), 1000) of
  462. {ok, Recv} -> << Buffer/binary, Recv/binary >>;
  463. _ -> Buffer
  464. end,
  465. <<"chunked_handler\r\nworks fine!">> = Buffer2.
  466. http10_hostless(Config) ->
  467. Port10 = ?config(port, Config) + 10,
  468. Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)),
  469. ranch:start_listener(Name, 5,
  470. ?config(transport, Config), ?config(opts, Config) ++ [{port, Port10}],
  471. cowboy_protocol, [
  472. {dispatch, [{'_', [
  473. {[<<"http1.0">>, <<"hostless">>], http_handler, []}]}]},
  474. {max_keepalive, 50},
  475. {timeout, 500}]
  476. ),
  477. 200 = quick_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
  478. [{port, Port10}|Config]),
  479. cowboy:stop_listener(http10).
  480. keepalive_max(Config) ->
  481. Client = ?config(client, Config),
  482. URL = build_url("/", Config),
  483. ok = keepalive_max_loop(Client, URL, 50).
  484. keepalive_max_loop(_, _, 0) ->
  485. ok;
  486. keepalive_max_loop(Client, URL, N) ->
  487. Headers = [{<<"connection">>, <<"keep-alive">>}],
  488. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  489. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  490. Expected = case N of
  491. 1 -> <<"close">>;
  492. N -> <<"keep-alive">>
  493. end,
  494. {<<"connection">>, Expected}
  495. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  496. keepalive_max_loop(Client3, URL, N - 1).
  497. keepalive_nl(Config) ->
  498. Client = ?config(client, Config),
  499. URL = build_url("/", Config),
  500. ok = keepalive_nl_loop(Client, URL, 10).
  501. keepalive_nl_loop(_, _, 0) ->
  502. ok;
  503. keepalive_nl_loop(Client, URL, N) ->
  504. Headers = [{<<"connection">>, <<"keep-alive">>}],
  505. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  506. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  507. {<<"connection">>, <<"keep-alive">>}
  508. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  509. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  510. ok = Transport:send(Socket, <<"\r\n">>), %% empty line
  511. keepalive_nl_loop(Client3, URL, N - 1).
  512. multipart(Config) ->
  513. Client = ?config(client, Config),
  514. Body = <<
  515. "This is a preamble."
  516. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  517. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  518. "\r\n--OHai--"
  519. "This is an epiloque."
  520. >>,
  521. {ok, Client2} = cowboy_client:request(<<"POST">>,
  522. build_url("/multipart", Config),
  523. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
  524. Body, Client),
  525. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  526. {ok, RespBody, _} = cowboy_client:response_body(Client3),
  527. Parts = binary_to_term(RespBody),
  528. Parts = [
  529. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  530. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  531. ].
  532. nc_reqs(Config, Input) ->
  533. Cat = os:find_executable("cat"),
  534. Nc = os:find_executable("nc"),
  535. case {Cat, Nc} of
  536. {false, _} ->
  537. {skip, {notfound, cat}};
  538. {_, false} ->
  539. {skip, {notfound, nc}};
  540. _Good ->
  541. %% Throw garbage at the server then check if it's still up.
  542. {port, Port} = lists:keyfind(port, 1, Config),
  543. StrPort = integer_to_list(Port),
  544. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  545. || _ <- lists:seq(1, 100)],
  546. 200 = quick_get("/", Config)
  547. end.
  548. nc_rand(Config) ->
  549. nc_reqs(Config, "/dev/urandom").
  550. nc_zero(Config) ->
  551. nc_reqs(Config, "/dev/zero").
  552. onrequest(Config) ->
  553. Client = ?config(client, Config),
  554. {ok, Client2} = cowboy_client:request(<<"GET">>,
  555. build_url("/", Config), Client),
  556. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  557. {<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers),
  558. {ok, <<"http_handler">>, _} = cowboy_client:response_body(Client3).
  559. onrequest_reply(Config) ->
  560. Client = ?config(client, Config),
  561. {ok, Client2} = cowboy_client:request(<<"GET">>,
  562. build_url("/?reply=1", Config), Client),
  563. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  564. {<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
  565. {ok, <<"replied!">>, _} = cowboy_client:response_body(Client3).
  566. %% Hook for the above onrequest tests.
  567. onrequest_hook(Req) ->
  568. case cowboy_req:qs_val(<<"reply">>, Req) of
  569. {undefined, Req2} ->
  570. cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req2);
  571. {_, Req2} ->
  572. {ok, Req3} = cowboy_req:reply(
  573. 200, [], <<"replied!">>, Req2),
  574. Req3
  575. end.
  576. onresponse_crash(Config) ->
  577. Client = ?config(client, Config),
  578. {ok, Client2} = cowboy_client:request(<<"GET">>,
  579. build_url("/handler_errors?case=init_before_reply", Config), Client),
  580. {ok, 777, Headers, _} = cowboy_client:response(Client2),
  581. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
  582. onresponse_reply(Config) ->
  583. Client = ?config(client, Config),
  584. {ok, Client2} = cowboy_client:request(<<"GET">>,
  585. build_url("/", Config), Client),
  586. {ok, 777, Headers, Client3} = cowboy_client:response(Client2),
  587. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
  588. %% Make sure we don't get the body initially sent.
  589. {error, closed} = cowboy_client:response_body(Client3).
  590. %% Hook for the above onresponse tests.
  591. onresponse_hook(_, Headers, _, Req) ->
  592. {ok, Req2} = cowboy_req:reply(
  593. <<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req),
  594. Req2.
  595. pipeline(Config) ->
  596. Client = ?config(client, Config),
  597. {ok, Client2} = cowboy_client:request(<<"GET">>,
  598. build_url("/", Config), Client),
  599. {ok, Client3} = cowboy_client:request(<<"GET">>,
  600. build_url("/", Config), Client2),
  601. {ok, Client4} = cowboy_client:request(<<"GET">>,
  602. build_url("/", Config), Client3),
  603. {ok, Client5} = cowboy_client:request(<<"GET">>,
  604. build_url("/", Config), Client4),
  605. {ok, Client6} = cowboy_client:request(<<"GET">>,
  606. build_url("/", Config), [{<<"connection">>, <<"close">>}], Client5),
  607. {ok, 200, _, Client7} = cowboy_client:response(Client6),
  608. {ok, 200, _, Client8} = cowboy_client:response(Client7),
  609. {ok, 200, _, Client9} = cowboy_client:response(Client8),
  610. {ok, 200, _, Client10} = cowboy_client:response(Client9),
  611. {ok, 200, _, Client11} = cowboy_client:response(Client10),
  612. {error, closed} = cowboy_client:response(Client11).
  613. rest_bad_accept(Config) ->
  614. Client = ?config(client, Config),
  615. {ok, Client2} = cowboy_client:request(<<"GET">>,
  616. build_url("/bad_accept", Config),
  617. [{<<"accept">>, <<"1">>}],
  618. Client),
  619. {ok, 400, _, _} = cowboy_client:response(Client2).
  620. rest_expires(Config) ->
  621. Client = ?config(client, Config),
  622. {ok, Client2} = cowboy_client:request(<<"GET">>,
  623. build_url("/rest_expires", Config), Client),
  624. {ok, 200, RespHeaders, _} = cowboy_client:response(Client2),
  625. {_, Expires} = lists:keyfind(<<"expires">>, 1, RespHeaders),
  626. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, RespHeaders),
  627. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  628. ok.
  629. rest_keepalive(Config) ->
  630. Client = ?config(client, Config),
  631. URL = build_url("/simple", Config),
  632. ok = rest_keepalive_loop(Client, URL, 10).
  633. rest_keepalive_loop(_, _, 0) ->
  634. ok;
  635. rest_keepalive_loop(Client, URL, N) ->
  636. Headers = [{<<"connection">>, <<"keep-alive">>}],
  637. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  638. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  639. {<<"connection">>, <<"keep-alive">>}
  640. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  641. rest_keepalive_loop(Client3, URL, N - 1).
  642. rest_keepalive_post(Config) ->
  643. Client = ?config(client, Config),
  644. ok = rest_keepalive_post_loop(Config, Client, forbidden_post, 10).
  645. rest_keepalive_post_loop(_, _, _, 0) ->
  646. ok;
  647. rest_keepalive_post_loop(Config, Client, simple_post, N) ->
  648. Headers = [
  649. {<<"connection">>, <<"keep-alive">>},
  650. {<<"content-type">>, <<"text/plain">>}
  651. ],
  652. {ok, Client2} = cowboy_client:request(<<"POST">>,
  653. build_url("/simple_post", Config), Headers, "12345", Client),
  654. {ok, 303, RespHeaders, Client3} = cowboy_client:response(Client2),
  655. {<<"connection">>, <<"keep-alive">>}
  656. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  657. rest_keepalive_post_loop(Config, Client3, forbidden_post, N - 1);
  658. rest_keepalive_post_loop(Config, Client, forbidden_post, N) ->
  659. Headers = [
  660. {<<"connection">>, <<"keep-alive">>},
  661. {<<"content-type">>, <<"text/plain">>}
  662. ],
  663. {ok, Client2} = cowboy_client:request(<<"POST">>,
  664. build_url("/forbidden_post", Config), Headers, "12345", Client),
  665. {ok, 403, RespHeaders, Client3} = cowboy_client:response(Client2),
  666. {<<"connection">>, <<"keep-alive">>}
  667. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  668. rest_keepalive_post_loop(Config, Client3, simple_post, N - 1).
  669. rest_missing_get_callbacks(Config) ->
  670. Client = ?config(client, Config),
  671. {ok, Client2} = cowboy_client:request(<<"GET">>,
  672. build_url("/missing_get_callbacks", Config), Client),
  673. {ok, 500, _, _} = cowboy_client:response(Client2).
  674. rest_missing_put_callbacks(Config) ->
  675. Client = ?config(client, Config),
  676. {ok, Client2} = cowboy_client:request(<<"PUT">>,
  677. build_url("/missing_put_callbacks", Config),
  678. [{<<"content-type">>, <<"application/json">>}],
  679. <<"{}">>, Client),
  680. {ok, 500, _, _} = cowboy_client:response(Client2).
  681. rest_nodelete(Config) ->
  682. Client = ?config(client, Config),
  683. {ok, Client2} = cowboy_client:request(<<"DELETE">>,
  684. build_url("/nodelete", Config), Client),
  685. {ok, 500, _, _} = cowboy_client:response(Client2).
  686. rest_resource_get_etag(Config, Type) ->
  687. rest_resource_get_etag(Config, Type, []).
  688. rest_resource_get_etag(Config, Type, Headers) ->
  689. Client = ?config(client, Config),
  690. {ok, Client2} = cowboy_client:request(<<"GET">>,
  691. build_url("/resetags?type=" ++ Type, Config), Headers, Client),
  692. {ok, Status, RespHeaders, _} = cowboy_client:response(Client2),
  693. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  694. false -> {Status, false};
  695. {<<"etag">>, ETag} -> {Status, ETag}
  696. end.
  697. rest_resource_etags(Config) ->
  698. Tests = [
  699. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  700. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  701. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  702. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  703. {500, false, "binary-strong-unquoted"},
  704. {500, false, "binary-weak-unquoted"}
  705. ],
  706. _ = [{Status, ETag, Type} = begin
  707. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  708. {Ret, RespETag, Type}
  709. end || {Status, ETag, Type} <- Tests].
  710. rest_resource_etags_if_none_match(Config) ->
  711. Tests = [
  712. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  713. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  714. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  715. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  716. ],
  717. _ = [{Status, Type} = begin
  718. {Ret, _} = rest_resource_get_etag(Config, Type,
  719. [{<<"if-none-match">>, ETag}]),
  720. {Ret, Type}
  721. end || {Status, ETag, Type} <- Tests].
  722. set_resp_body(Config) ->
  723. Client = ?config(client, Config),
  724. {ok, Client2} = cowboy_client:request(<<"GET">>,
  725. build_url("/set_resp/body", Config), Client),
  726. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  727. {ok, <<"A flameless dance does not equal a cycle">>, _}
  728. = cowboy_client:response_body(Client3).
  729. set_resp_header(Config) ->
  730. Client = ?config(client, Config),
  731. {ok, Client2} = cowboy_client:request(<<"GET">>,
  732. build_url("/set_resp/header", Config), Client),
  733. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  734. {<<"vary">>, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  735. {<<"set-cookie">>, _} = lists:keyfind(<<"set-cookie">>, 1, Headers).
  736. set_resp_overwrite(Config) ->
  737. Client = ?config(client, Config),
  738. {ok, Client2} = cowboy_client:request(<<"GET">>,
  739. build_url("/set_resp/overwrite", Config), Client),
  740. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  741. {<<"server">>, <<"DesireDrive/1.0">>}
  742. = lists:keyfind(<<"server">>, 1, Headers).
  743. static_attribute_etag(Config) ->
  744. Client = ?config(client, Config),
  745. {ok, Client2} = cowboy_client:request(<<"GET">>,
  746. build_url("/static_attribute_etag/test.html", Config), Client),
  747. {ok, Client3} = cowboy_client:request(<<"GET">>,
  748. build_url("/static_attribute_etag/test.html", Config), Client2),
  749. {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
  750. {ok, 200, Headers2, _} = cowboy_client:response(Client4),
  751. {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  752. {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  753. false = ETag1 =:= undefined,
  754. ETag1 = ETag2.
  755. static_function_etag(Config) ->
  756. Client = ?config(client, Config),
  757. {ok, Client2} = cowboy_client:request(<<"GET">>,
  758. build_url("/static_function_etag/test.html", Config), Client),
  759. {ok, Client3} = cowboy_client:request(<<"GET">>,
  760. build_url("/static_function_etag/test.html", Config), Client2),
  761. {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
  762. {ok, 200, Headers2, _} = cowboy_client:response(Client4),
  763. {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  764. {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  765. false = ETag1 =:= undefined,
  766. ETag1 = ETag2.
  767. %% Callback function for generating the ETag for the above test.
  768. static_function_etag(Arguments, etag_data) ->
  769. {_, Filepath} = lists:keyfind(filepath, 1, Arguments),
  770. {_, _Filesize} = lists:keyfind(filesize, 1, Arguments),
  771. {_, _INode} = lists:keyfind(inode, 1, Arguments),
  772. {_, _Modified} = lists:keyfind(mtime, 1, Arguments),
  773. ChecksumCommand = lists:flatten(io_lib:format("sha1sum ~s", [Filepath])),
  774. [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "),
  775. {strong, iolist_to_binary(Checksum)}.
  776. static_mimetypes_function(Config) ->
  777. Client = ?config(client, Config),
  778. {ok, Client2} = cowboy_client:request(<<"GET">>,
  779. build_url("/static_mimetypes_function/test.html", Config), Client),
  780. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  781. {<<"content-type">>, <<"text/html">>}
  782. = lists:keyfind(<<"content-type">>, 1, Headers).
  783. static_specify_file(Config) ->
  784. Client = ?config(client, Config),
  785. {ok, Client2} = cowboy_client:request(<<"GET">>,
  786. build_url("/static_specify_file", Config), Client),
  787. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  788. {<<"content-type">>, <<"text/css">>}
  789. = lists:keyfind(<<"content-type">>, 1, Headers),
  790. {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
  791. static_specify_file_catchall(Config) ->
  792. Client = ?config(client, Config),
  793. {ok, Client2} = cowboy_client:request(<<"GET">>,
  794. build_url("/static_specify_file/none", Config), Client),
  795. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  796. {<<"content-type">>, <<"text/css">>}
  797. = lists:keyfind(<<"content-type">>, 1, Headers),
  798. {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
  799. static_test_file(Config) ->
  800. Client = ?config(client, Config),
  801. {ok, Client2} = cowboy_client:request(<<"GET">>,
  802. build_url("/static/test_file", Config), Client),
  803. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  804. {<<"content-type">>, <<"application/octet-stream">>}
  805. = lists:keyfind(<<"content-type">>, 1, Headers).
  806. static_test_file_css(Config) ->
  807. Client = ?config(client, Config),
  808. {ok, Client2} = cowboy_client:request(<<"GET">>,
  809. build_url("/static/test_file.css", Config), Client),
  810. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  811. {<<"content-type">>, <<"text/css">>}
  812. = lists:keyfind(<<"content-type">>, 1, Headers).
  813. stream_body_set_resp(Config) ->
  814. Client = ?config(client, Config),
  815. {ok, Client2} = cowboy_client:request(<<"GET">>,
  816. build_url("/stream_body/set_resp", Config), Client),
  817. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  818. {ok, <<"stream_body_set_resp">>, _}
  819. = cowboy_client:response_body(Client3).
  820. te_chunked(Config) ->
  821. Client = ?config(client, Config),
  822. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  823. Chunks = body_to_chunks(50, Body, []),
  824. {ok, Client2} = cowboy_client:request(<<"GET">>,
  825. build_url("/echo/body", Config),
  826. [{<<"transfer-encoding">>, <<"chunked">>}],
  827. Chunks, Client),
  828. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  829. {ok, Body, _} = cowboy_client:response_body(Client3).
  830. te_chunked_delayed(Config) ->
  831. Client = ?config(client, Config),
  832. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  833. Chunks = body_to_chunks(50, Body, []),
  834. {ok, Client2} = cowboy_client:request(<<"GET">>,
  835. build_url("/echo/body", Config),
  836. [{<<"transfer-encoding">>, <<"chunked">>}], Client),
  837. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  838. _ = [begin
  839. ok = Transport:send(Socket, Chunk),
  840. ok = timer:sleep(10)
  841. end || Chunk <- Chunks],
  842. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  843. {ok, Body, _} = cowboy_client:response_body(Client3).
  844. te_identity(Config) ->
  845. Client = ?config(client, Config),
  846. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  847. {ok, Client2} = cowboy_client:request(<<"GET">>,
  848. build_url("/echo/body", Config), [], Body, Client),
  849. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  850. {ok, Body, _} = cowboy_client:response_body(Client3).