http_SUITE.erl 30 KB

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