http_SUITE.erl 13 KB

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