http_SUITE.erl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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]). %% 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],
  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. {[], http_handler, []}
  107. ]}
  108. ].
  109. init_https_dispatch() ->
  110. init_http_dispatch().
  111. %% http.
  112. chunked_response(Config) ->
  113. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "chunked_handler\r\nworks fine!"}} =
  114. httpc:request(build_url("/chunked_response", Config)).
  115. headers_dupe(Config) ->
  116. {port, Port} = lists:keyfind(port, 1, Config),
  117. {ok, Socket} = gen_tcp:connect("localhost", Port,
  118. [binary, {active, false}, {packet, raw}]),
  119. ok = gen_tcp:send(Socket, "GET /headers/dupe HTTP/1.1\r\n"
  120. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  121. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  122. {_Start, _Length} = binary:match(Data, <<"Connection: close">>),
  123. nomatch = binary:match(Data, <<"Connection: keep-alive">>),
  124. {error, closed} = gen_tcp:recv(Socket, 0, 1000).
  125. headers_huge(Config) ->
  126. Cookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  127. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _N <- lists:seq(1, 40)]),
  128. {_Packet, 200} = raw_req(["GET / HTTP/1.0\r\nHost: localhost\r\n"
  129. "Set-Cookie: ", Cookie, "\r\n\r\n"], Config).
  130. keepalive_nl(Config) ->
  131. {port, Port} = lists:keyfind(port, 1, Config),
  132. {ok, Socket} = gen_tcp:connect("localhost", Port,
  133. [binary, {active, false}, {packet, raw}]),
  134. ok = keepalive_nl_loop(Socket, 10),
  135. ok = gen_tcp:close(Socket).
  136. keepalive_nl_loop(_Socket, 0) ->
  137. ok;
  138. keepalive_nl_loop(Socket, N) ->
  139. ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
  140. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  141. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  142. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  143. nomatch = binary:match(Data, <<"Connection: close">>),
  144. ok = gen_tcp:send(Socket, "\r\n"), %% extra nl
  145. keepalive_nl_loop(Socket, N - 1).
  146. max_keepalive(Config) ->
  147. {port, Port} = lists:keyfind(port, 1, Config),
  148. {ok, Socket} = gen_tcp:connect("localhost", Port,
  149. [binary, {active, false}, {packet, raw}]),
  150. ok = max_keepalive_loop(Socket, 50),
  151. {error, closed} = gen_tcp:recv(Socket, 0, 1000).
  152. max_keepalive_loop(_Socket, 0) ->
  153. ok;
  154. max_keepalive_loop(Socket, N) ->
  155. ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
  156. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  157. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  158. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  159. case N of
  160. 1 -> {_, _} = binary:match(Data, <<"Connection: close">>);
  161. N -> nomatch = binary:match(Data, <<"Connection: close">>)
  162. end,
  163. keepalive_nl_loop(Socket, N - 1).
  164. nc_rand(Config) ->
  165. nc_reqs(Config, "/dev/urandom").
  166. nc_zero(Config) ->
  167. nc_reqs(Config, "/dev/zero").
  168. nc_reqs(Config, Input) ->
  169. Cat = os:find_executable("cat"),
  170. Nc = os:find_executable("nc"),
  171. case {Cat, Nc} of
  172. {false, _} ->
  173. {skip, {notfound, cat}};
  174. {_, false} ->
  175. {skip, {notfound, nc}};
  176. _Good ->
  177. %% Throw garbage at the server then check if it's still up.
  178. {port, Port} = lists:keyfind(port, 1, Config),
  179. [nc_run_req(Port, Input) || _N <- lists:seq(1, 100)],
  180. Packet = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n",
  181. {Packet, 200} = raw_req(Packet, Config)
  182. end.
  183. nc_run_req(Port, Input) ->
  184. os:cmd("cat " ++ Input ++ " | nc localhost " ++ integer_to_list(Port)).
  185. pipeline(Config) ->
  186. {port, Port} = lists:keyfind(port, 1, Config),
  187. {ok, Socket} = gen_tcp:connect("localhost", Port,
  188. [binary, {active, false}, {packet, raw}]),
  189. ok = gen_tcp:send(Socket,
  190. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  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: close\r\n\r\n"),
  195. Data = pipeline_recv(Socket, <<>>),
  196. Reqs = binary:split(Data, << "\r\n\r\nhttp_handler" >>, [global, trim]),
  197. 5 = length(Reqs),
  198. pipeline_check(Reqs).
  199. pipeline_check([]) ->
  200. ok;
  201. pipeline_check([Req|Tail]) ->
  202. << "HTTP/1.1 200", _Rest/bits >> = Req,
  203. pipeline_check(Tail).
  204. pipeline_recv(Socket, SoFar) ->
  205. case gen_tcp:recv(Socket, 0, 6000) of
  206. {ok, Data} ->
  207. pipeline_recv(Socket, << SoFar/binary, Data/binary >>);
  208. {error, closed} ->
  209. ok = gen_tcp:close(Socket),
  210. SoFar
  211. end.
  212. raw_req(Packet, Config) ->
  213. {port, Port} = lists:keyfind(port, 1, Config),
  214. {ok, Socket} = gen_tcp:connect("localhost", Port,
  215. [binary, {active, false}, {packet, raw}]),
  216. ok = gen_tcp:send(Socket, Packet),
  217. Res = case gen_tcp:recv(Socket, 0, 6000) of
  218. {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>} ->
  219. list_to_integer(binary_to_list(Str));
  220. {error, Reason} ->
  221. Reason
  222. end,
  223. gen_tcp:close(Socket),
  224. {Packet, Res}.
  225. raw(Config) ->
  226. Huge = [$0 || _N <- lists:seq(1, 5000)],
  227. Tests = [
  228. {"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
  229. {"\n", 400},
  230. {"Garbage\r\n\r\n", 400},
  231. {"\r\n\r\n\r\n\r\n\r\n\r\n", 400},
  232. {"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400},
  233. {"", closed},
  234. {"\r\n", closed},
  235. {"\r\n\r\n", closed},
  236. {"GET / HTTP/1.1", closed},
  237. {"GET / HTTP/1.1\r\n", 408},
  238. {"GET / HTTP/1.1\r\nHost: localhost", 408},
  239. {"GET / HTTP/1.1\r\nHost: localhost\r\n", 408},
  240. {"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408},
  241. {"GET http://localhost/ HTTP/1.1\r\n\r\n", 501},
  242. {"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505},
  243. {"GET /init_shutdown HTTP/1.1\r\nHost: localhost\r\n\r\n", 666},
  244. {"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n\r\n", 102},
  245. {Huge, 413},
  246. {"GET / HTTP/1.1\r\n" ++ Huge, 413}
  247. ],
  248. [{Packet, StatusCode} = raw_req(Packet, Config)
  249. || {Packet, StatusCode} <- Tests].
  250. set_resp_header(Config) ->
  251. {port, Port} = lists:keyfind(port, 1, Config),
  252. {ok, Socket} = gen_tcp:connect("localhost", Port,
  253. [binary, {active, false}, {packet, raw}]),
  254. ok = gen_tcp:send(Socket, "GET /set_resp/header HTTP/1.1\r\n"
  255. "Host: localhost\r\nConnection: close\r\n\r\n"),
  256. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  257. {_, _} = binary:match(Data, <<"Vary: Accept">>),
  258. {_, _} = binary:match(Data, <<"Set-Cookie: ">>).
  259. set_resp_overwrite(Config) ->
  260. {port, Port} = lists:keyfind(port, 1, Config),
  261. {ok, Socket} = gen_tcp:connect("localhost", Port,
  262. [binary, {active, false}, {packet, raw}]),
  263. ok = gen_tcp:send(Socket, "GET /set_resp/overwrite HTTP/1.1\r\n"
  264. "Host: localhost\r\nConnection: close\r\n\r\n"),
  265. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  266. {_Start, _Length} = binary:match(Data, <<"Server: DesireDrive/1.0">>).
  267. set_resp_body(Config) ->
  268. {port, Port} = lists:keyfind(port, 1, Config),
  269. {ok, Socket} = gen_tcp:connect("localhost", Port,
  270. [binary, {active, false}, {packet, raw}]),
  271. ok = gen_tcp:send(Socket, "GET /set_resp/body HTTP/1.1\r\n"
  272. "Host: localhost\r\nConnection: close\r\n\r\n"),
  273. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  274. {_Start, _Length} = binary:match(Data, <<"\r\n\r\n"
  275. "A flameless dance does not equal a cycle">>).
  276. response_as_req(Config) ->
  277. Packet =
  278. "HTTP/1.0 302 Found
  279. Location: http://www.google.co.il/
  280. Cache-Control: private
  281. Content-Type: text/html; charset=UTF-8
  282. 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
  283. Date: Sun, 04 Dec 2011 15:55:01 GMT
  284. Server: gws
  285. Content-Length: 221
  286. X-XSS-Protection: 1; mode=block
  287. X-Frame-Options: SAMEORIGIN
  288. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  289. <TITLE>302 Moved</TITLE></HEAD><BODY>
  290. <H1>302 Moved</H1>
  291. The document has moved
  292. <A HREF=\"http://www.google.co.il/\">here</A>.
  293. </BODY></HTML>",
  294. {Packet, 400} = raw_req(Packet, Config).
  295. %% http and https.
  296. build_url(Path, Config) ->
  297. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  298. {port, Port} = lists:keyfind(port, 1, Config),
  299. Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
  300. http_200(Config) ->
  301. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
  302. httpc:request(build_url("/", Config)).
  303. http_404(Config) ->
  304. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  305. httpc:request(build_url("/not/found", Config)).
  306. %% misc.
  307. http_10_hostless(Config) ->
  308. Packet = "GET / HTTP/1.0\r\n\r\n",
  309. {Packet, 200} = raw_req(Packet, Config).
  310. %% rest.
  311. rest_simple(Config) ->
  312. Packet = "GET /simple HTTP/1.1\r\nHost: localhost\r\n\r\n",
  313. {Packet, 200} = raw_req(Packet, Config).
  314. rest_keepalive(Config) ->
  315. {port, Port} = lists:keyfind(port, 1, Config),
  316. {ok, Socket} = gen_tcp:connect("localhost", Port,
  317. [binary, {active, false}, {packet, raw}]),
  318. ok = rest_keepalive_loop(Socket, 100),
  319. ok = gen_tcp:close(Socket).
  320. rest_keepalive_loop(_Socket, 0) ->
  321. ok;
  322. rest_keepalive_loop(Socket, N) ->
  323. ok = gen_tcp:send(Socket, "GET /simple HTTP/1.1\r\n"
  324. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  325. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  326. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  327. nomatch = binary:match(Data, <<"Connection: close">>),
  328. rest_keepalive_loop(Socket, N - 1).