http_SUITE.erl 31 KB

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