http_SUITE.erl 30 KB

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