http_SUITE.erl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(http_SUITE).
  15. -include_lib("common_test/include/ct.hrl").
  16. -export([all/0, groups/0, init_per_suite/1, end_per_suite/1,
  17. init_per_group/2, end_per_group/2]). %% ct.
  18. -export([chunked_response/1, headers_dupe/1, headers_huge/1,
  19. keepalive_nl/1, max_keepalive/1, nc_rand/1, nc_zero/1,
  20. pipeline/1, raw/1, set_resp_header/1, set_resp_overwrite/1,
  21. set_resp_body/1, response_as_req/1]). %% http.
  22. -export([http_200/1, http_404/1, handler_errors/1]). %% http and https.
  23. -export([http_10_hostless/1]). %% misc.
  24. -export([rest_simple/1, rest_keepalive/1]). %% rest.
  25. %% ct.
  26. all() ->
  27. [{group, http}, {group, https}, {group, misc}, {group, rest}].
  28. groups() ->
  29. BaseTests = [http_200, http_404, handler_errors],
  30. [{http, [], [chunked_response, headers_dupe, headers_huge,
  31. keepalive_nl, max_keepalive, nc_rand, nc_zero, pipeline, raw,
  32. set_resp_header, set_resp_overwrite,
  33. set_resp_body, response_as_req] ++ BaseTests},
  34. {https, [], BaseTests},
  35. {misc, [], [http_10_hostless]},
  36. {rest, [], [rest_simple, rest_keepalive]}].
  37. init_per_suite(Config) ->
  38. application:start(inets),
  39. application:start(cowboy),
  40. Config.
  41. end_per_suite(_Config) ->
  42. application:stop(cowboy),
  43. application:stop(inets),
  44. ok.
  45. init_per_group(http, Config) ->
  46. Port = 33080,
  47. cowboy:start_listener(http, 100,
  48. cowboy_tcp_transport, [{port, Port}],
  49. cowboy_http_protocol, [{max_keepalive, 50},
  50. {dispatch, init_http_dispatch()}]
  51. ),
  52. [{scheme, "http"}, {port, Port}|Config];
  53. init_per_group(https, Config) ->
  54. Port = 33081,
  55. application:start(crypto),
  56. application:start(public_key),
  57. application:start(ssl),
  58. DataDir = ?config(data_dir, Config),
  59. cowboy:start_listener(https, 100,
  60. cowboy_ssl_transport, [
  61. {port, Port}, {certfile, DataDir ++ "cert.pem"},
  62. {keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}],
  63. cowboy_http_protocol, [{dispatch, init_https_dispatch()}]
  64. ),
  65. [{scheme, "https"}, {port, Port}|Config];
  66. init_per_group(misc, Config) ->
  67. Port = 33082,
  68. cowboy:start_listener(misc, 100,
  69. cowboy_tcp_transport, [{port, Port}],
  70. cowboy_http_protocol, [{dispatch, [{'_', [
  71. {[], http_handler, []}
  72. ]}]}]),
  73. [{port, Port}|Config];
  74. init_per_group(rest, Config) ->
  75. Port = 33083,
  76. cowboy:start_listener(reset, 100,
  77. cowboy_tcp_transport, [{port, Port}],
  78. cowboy_http_protocol, [{dispatch, [{'_', [
  79. {[<<"simple">>], rest_simple_resource, []}
  80. ]}]}]),
  81. [{port, Port}|Config].
  82. end_per_group(https, _Config) ->
  83. cowboy:stop_listener(https),
  84. application:stop(ssl),
  85. application:stop(public_key),
  86. application:stop(crypto),
  87. ok;
  88. end_per_group(Listener, _Config) ->
  89. cowboy:stop_listener(Listener),
  90. ok.
  91. %% Dispatch configuration.
  92. init_http_dispatch() ->
  93. [
  94. {[<<"localhost">>], [
  95. {[<<"chunked_response">>], chunked_handler, []},
  96. {[<<"init_shutdown">>], http_handler_init_shutdown, []},
  97. {[<<"long_polling">>], http_handler_long_polling, []},
  98. {[<<"headers">>, <<"dupe">>], http_handler,
  99. [{headers, [{<<"Connection">>, <<"close">>}]}]},
  100. {[<<"set_resp">>, <<"header">>], http_handler_set_resp,
  101. [{headers, [{<<"Vary">>, <<"Accept">>}]}]},
  102. {[<<"set_resp">>, <<"overwrite">>], http_handler_set_resp,
  103. [{headers, [{<<"Server">>, <<"DesireDrive/1.0">>}]}]},
  104. {[<<"set_resp">>, <<"body">>], http_handler_set_resp,
  105. [{body, <<"A flameless dance does not equal a cycle">>}]},
  106. {[<<"handler_errors">>], http_handler_errors, []},
  107. {[], http_handler, []}
  108. ]}
  109. ].
  110. init_https_dispatch() ->
  111. init_http_dispatch().
  112. %% http.
  113. chunked_response(Config) ->
  114. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "chunked_handler\r\nworks fine!"}} =
  115. httpc:request(build_url("/chunked_response", Config)).
  116. headers_dupe(Config) ->
  117. {port, Port} = lists:keyfind(port, 1, Config),
  118. {ok, Socket} = gen_tcp:connect("localhost", Port,
  119. [binary, {active, false}, {packet, raw}]),
  120. ok = gen_tcp:send(Socket, "GET /headers/dupe HTTP/1.1\r\n"
  121. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  122. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  123. {_Start, _Length} = binary:match(Data, <<"Connection: close">>),
  124. nomatch = binary:match(Data, <<"Connection: keep-alive">>),
  125. {error, closed} = gen_tcp:recv(Socket, 0, 1000).
  126. headers_huge(Config) ->
  127. Cookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  128. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _N <- lists:seq(1, 40)]),
  129. {_Packet, 200} = raw_req(["GET / HTTP/1.0\r\nHost: localhost\r\n"
  130. "Set-Cookie: ", Cookie, "\r\n\r\n"], Config).
  131. keepalive_nl(Config) ->
  132. {port, Port} = lists:keyfind(port, 1, Config),
  133. {ok, Socket} = gen_tcp:connect("localhost", Port,
  134. [binary, {active, false}, {packet, raw}]),
  135. ok = keepalive_nl_loop(Socket, 10),
  136. ok = gen_tcp:close(Socket).
  137. keepalive_nl_loop(_Socket, 0) ->
  138. ok;
  139. keepalive_nl_loop(Socket, N) ->
  140. ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
  141. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  142. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  143. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  144. nomatch = binary:match(Data, <<"Connection: close">>),
  145. ok = gen_tcp:send(Socket, "\r\n"), %% extra nl
  146. keepalive_nl_loop(Socket, N - 1).
  147. max_keepalive(Config) ->
  148. {port, Port} = lists:keyfind(port, 1, Config),
  149. {ok, Socket} = gen_tcp:connect("localhost", Port,
  150. [binary, {active, false}, {packet, raw}]),
  151. ok = max_keepalive_loop(Socket, 50),
  152. {error, closed} = gen_tcp:recv(Socket, 0, 1000).
  153. max_keepalive_loop(_Socket, 0) ->
  154. ok;
  155. max_keepalive_loop(Socket, N) ->
  156. ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
  157. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  158. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  159. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  160. case N of
  161. 1 -> {_, _} = binary:match(Data, <<"Connection: close">>);
  162. N -> nomatch = binary:match(Data, <<"Connection: close">>)
  163. end,
  164. keepalive_nl_loop(Socket, N - 1).
  165. nc_rand(Config) ->
  166. nc_reqs(Config, "/dev/urandom").
  167. nc_zero(Config) ->
  168. nc_reqs(Config, "/dev/zero").
  169. nc_reqs(Config, Input) ->
  170. Cat = os:find_executable("cat"),
  171. Nc = os:find_executable("nc"),
  172. case {Cat, Nc} of
  173. {false, _} ->
  174. {skip, {notfound, cat}};
  175. {_, false} ->
  176. {skip, {notfound, nc}};
  177. _Good ->
  178. %% Throw garbage at the server then check if it's still up.
  179. {port, Port} = lists:keyfind(port, 1, Config),
  180. [nc_run_req(Port, Input) || _N <- lists:seq(1, 100)],
  181. Packet = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n",
  182. {Packet, 200} = raw_req(Packet, Config)
  183. end.
  184. nc_run_req(Port, Input) ->
  185. os:cmd("cat " ++ Input ++ " | nc localhost " ++ integer_to_list(Port)).
  186. pipeline(Config) ->
  187. {port, Port} = lists:keyfind(port, 1, Config),
  188. {ok, Socket} = gen_tcp:connect("localhost", Port,
  189. [binary, {active, false}, {packet, raw}]),
  190. ok = gen_tcp:send(Socket,
  191. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  192. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  193. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  194. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  195. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"),
  196. Data = pipeline_recv(Socket, <<>>),
  197. Reqs = binary:split(Data, << "\r\n\r\nhttp_handler" >>, [global, trim]),
  198. 5 = length(Reqs),
  199. pipeline_check(Reqs).
  200. pipeline_check([]) ->
  201. ok;
  202. pipeline_check([Req|Tail]) ->
  203. << "HTTP/1.1 200", _Rest/bits >> = Req,
  204. pipeline_check(Tail).
  205. pipeline_recv(Socket, SoFar) ->
  206. case gen_tcp:recv(Socket, 0, 6000) of
  207. {ok, Data} ->
  208. pipeline_recv(Socket, << SoFar/binary, Data/binary >>);
  209. {error, closed} ->
  210. ok = gen_tcp:close(Socket),
  211. SoFar
  212. end.
  213. raw_req(Packet, Config) ->
  214. {port, Port} = lists:keyfind(port, 1, Config),
  215. {ok, Socket} = gen_tcp:connect("localhost", Port,
  216. [binary, {active, false}, {packet, raw}]),
  217. ok = gen_tcp:send(Socket, Packet),
  218. Res = case gen_tcp:recv(Socket, 0, 6000) of
  219. {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>} ->
  220. list_to_integer(binary_to_list(Str));
  221. {error, Reason} ->
  222. Reason
  223. end,
  224. gen_tcp:close(Socket),
  225. {Packet, Res}.
  226. %% Send a raw request. Return the response code and the full response.
  227. raw_resp(Request, Config) ->
  228. {port, Port} = lists:keyfind(port, 1, Config),
  229. Transport = case ?config(scheme, Config) of
  230. "http" -> gen_tcp;
  231. "https" -> ssl
  232. end,
  233. {ok, Socket} = Transport:connect("localhost", Port,
  234. [binary, {active, false}, {packet, raw}]),
  235. ok = Transport:send(Socket, Request),
  236. {StatusCode, Response} = case recv_loop(Transport, Socket, <<>>) of
  237. {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >> = Bin} ->
  238. {list_to_integer(binary_to_list(Str)), Bin};
  239. {ok, Bin} ->
  240. {badresp, Bin};
  241. {error, Reason} ->
  242. {Reason, <<>>}
  243. end,
  244. Transport:close(Socket),
  245. {Response, StatusCode}.
  246. recv_loop(Transport, Socket, Acc) ->
  247. case Transport:recv(Socket, 0, 6000) of
  248. {ok, Data} ->
  249. recv_loop(Transport, Socket, <<Acc/binary, Data/binary>>);
  250. {error, closed} ->
  251. ok = Transport:close(Socket),
  252. {ok, Acc};
  253. {error, Reason} ->
  254. {error, Reason}
  255. end.
  256. raw(Config) ->
  257. Huge = [$0 || _N <- lists:seq(1, 5000)],
  258. Tests = [
  259. {"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
  260. {"\n", 400},
  261. {"Garbage\r\n\r\n", 400},
  262. {"\r\n\r\n\r\n\r\n\r\n\r\n", 400},
  263. {"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400},
  264. {"", closed},
  265. {"\r\n", closed},
  266. {"\r\n\r\n", closed},
  267. {"GET / HTTP/1.1", closed},
  268. {"GET / HTTP/1.1\r\n", 408},
  269. {"GET / HTTP/1.1\r\nHost: localhost", 408},
  270. {"GET / HTTP/1.1\r\nHost: localhost\r\n", 408},
  271. {"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408},
  272. {"GET http://localhost/ HTTP/1.1\r\n\r\n", 501},
  273. {"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505},
  274. {"GET /init_shutdown HTTP/1.1\r\nHost: localhost\r\n\r\n", 666},
  275. {"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n\r\n", 102},
  276. {Huge, 413},
  277. {"GET / HTTP/1.1\r\n" ++ Huge, 413}
  278. ],
  279. [{Packet, StatusCode} = raw_req(Packet, Config)
  280. || {Packet, StatusCode} <- Tests].
  281. set_resp_header(Config) ->
  282. {port, Port} = lists:keyfind(port, 1, Config),
  283. {ok, Socket} = gen_tcp:connect("localhost", Port,
  284. [binary, {active, false}, {packet, raw}]),
  285. ok = gen_tcp:send(Socket, "GET /set_resp/header HTTP/1.1\r\n"
  286. "Host: localhost\r\nConnection: close\r\n\r\n"),
  287. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  288. {_, _} = binary:match(Data, <<"Vary: Accept">>),
  289. {_, _} = binary:match(Data, <<"Set-Cookie: ">>).
  290. set_resp_overwrite(Config) ->
  291. {port, Port} = lists:keyfind(port, 1, Config),
  292. {ok, Socket} = gen_tcp:connect("localhost", Port,
  293. [binary, {active, false}, {packet, raw}]),
  294. ok = gen_tcp:send(Socket, "GET /set_resp/overwrite HTTP/1.1\r\n"
  295. "Host: localhost\r\nConnection: close\r\n\r\n"),
  296. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  297. {_Start, _Length} = binary:match(Data, <<"Server: DesireDrive/1.0">>).
  298. set_resp_body(Config) ->
  299. {port, Port} = lists:keyfind(port, 1, Config),
  300. {ok, Socket} = gen_tcp:connect("localhost", Port,
  301. [binary, {active, false}, {packet, raw}]),
  302. ok = gen_tcp:send(Socket, "GET /set_resp/body HTTP/1.1\r\n"
  303. "Host: localhost\r\nConnection: close\r\n\r\n"),
  304. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  305. {_Start, _Length} = binary:match(Data, <<"\r\n\r\n"
  306. "A flameless dance does not equal a cycle">>).
  307. response_as_req(Config) ->
  308. Packet =
  309. "HTTP/1.0 302 Found
  310. Location: http://www.google.co.il/
  311. Cache-Control: private
  312. Content-Type: text/html; charset=UTF-8
  313. 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
  314. Date: Sun, 04 Dec 2011 15:55:01 GMT
  315. Server: gws
  316. Content-Length: 221
  317. X-XSS-Protection: 1; mode=block
  318. X-Frame-Options: SAMEORIGIN
  319. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  320. <TITLE>302 Moved</TITLE></HEAD><BODY>
  321. <H1>302 Moved</H1>
  322. The document has moved
  323. <A HREF=\"http://www.google.co.il/\">here</A>.
  324. </BODY></HTML>",
  325. {Packet, 400} = raw_req(Packet, Config).
  326. handler_errors(Config) ->
  327. Request = fun(Case) ->
  328. raw_resp(["GET /handler_errors?case=", Case, " HTTP/1.1\r\n",
  329. "Host: localhost\r\n\r\n"], Config) end,
  330. {_Packet1, 500} = Request("init_before_reply"),
  331. {Packet2, 200} = Request("init_after_reply"),
  332. nomatch = binary:match(Packet2, <<"HTTP/1.1 500">>),
  333. {Packet3, 200} = Request("init_reply_handle_error"),
  334. nomatch = binary:match(Packet3, <<"HTTP/1.1 500">>),
  335. {_Packet4, 500} = Request("handle_before_reply"),
  336. {Packet5, 200} = Request("handle_after_reply"),
  337. nomatch = binary:match(Packet5, <<"HTTP/1.1 500">>),
  338. {Packet6, 200} = raw_resp([
  339. "GET / HTTP/1.1\r\n",
  340. "Host: localhost\r\n",
  341. "Connection: keep-alive\r\n\r\n",
  342. "GET /handler_errors?case=handle_after_reply\r\n",
  343. "Host: localhost\r\n\r\n"], Config),
  344. nomatch = binary:match(Packet6, <<"HTTP/1.1 500">>),
  345. {Packet7, 200} = raw_resp([
  346. "GET / HTTP/1.1\r\n",
  347. "Host: localhost\r\n",
  348. "Connection: keep-alive\r\n\r\n",
  349. "GET /handler_errors?case=handle_before_reply HTTP/1.1\r\n",
  350. "Host: localhost\r\n\r\n"], Config),
  351. {{_, _}, _} = {binary:match(Packet7, <<"HTTP/1.1 500">>), Packet7},
  352. done.
  353. %% http and https.
  354. build_url(Path, Config) ->
  355. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  356. {port, Port} = lists:keyfind(port, 1, Config),
  357. Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
  358. http_200(Config) ->
  359. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
  360. httpc:request(build_url("/", Config)).
  361. http_404(Config) ->
  362. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  363. httpc:request(build_url("/not/found", Config)).
  364. %% misc.
  365. http_10_hostless(Config) ->
  366. Packet = "GET / HTTP/1.0\r\n\r\n",
  367. {Packet, 200} = raw_req(Packet, Config).
  368. %% rest.
  369. rest_simple(Config) ->
  370. Packet = "GET /simple HTTP/1.1\r\nHost: localhost\r\n\r\n",
  371. {Packet, 200} = raw_req(Packet, Config).
  372. rest_keepalive(Config) ->
  373. {port, Port} = lists:keyfind(port, 1, Config),
  374. {ok, Socket} = gen_tcp:connect("localhost", Port,
  375. [binary, {active, false}, {packet, raw}]),
  376. ok = rest_keepalive_loop(Socket, 100),
  377. ok = gen_tcp:close(Socket).
  378. rest_keepalive_loop(_Socket, 0) ->
  379. ok;
  380. rest_keepalive_loop(Socket, N) ->
  381. ok = gen_tcp:send(Socket, "GET /simple HTTP/1.1\r\n"
  382. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  383. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  384. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  385. nomatch = binary:match(Data, <<"Connection: close">>),
  386. rest_keepalive_loop(Socket, N - 1).