http_SUITE.erl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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, file_200/1, file_403/1,
  23. dir_403/1, file_404/1, file_400/1]). %% http and https.
  24. -export([http_10_hostless/1]). %% misc.
  25. -export([rest_simple/1, rest_keepalive/1]). %% rest.
  26. %% ct.
  27. all() ->
  28. [{group, http}, {group, https}, {group, misc}, {group, rest}].
  29. groups() ->
  30. BaseTests = [http_200, http_404, file_200, file_403, dir_403, file_404,
  31. file_400],
  32. [{http, [], [chunked_response, headers_dupe, headers_huge,
  33. keepalive_nl, max_keepalive, nc_rand, nc_zero, pipeline, raw,
  34. set_resp_header, set_resp_overwrite,
  35. set_resp_body, response_as_req, stream_body_set_resp] ++ BaseTests},
  36. {https, [], BaseTests},
  37. {misc, [], [http_10_hostless]},
  38. {rest, [], [rest_simple, rest_keepalive]}].
  39. init_per_suite(Config) ->
  40. application:start(inets),
  41. application:start(cowboy),
  42. Config.
  43. end_per_suite(_Config) ->
  44. application:stop(cowboy),
  45. application:stop(inets),
  46. ok.
  47. init_per_group(http, Config) ->
  48. Port = 33080,
  49. Config1 = init_static_dir(Config),
  50. cowboy:start_listener(http, 100,
  51. cowboy_tcp_transport, [{port, Port}],
  52. cowboy_http_protocol, [{max_keepalive, 50},
  53. {dispatch, init_http_dispatch(Config1)}]
  54. ),
  55. [{scheme, "http"}, {port, Port}|Config1];
  56. init_per_group(https, Config) ->
  57. Port = 33081,
  58. Config1 = init_static_dir(Config),
  59. application:start(crypto),
  60. application:start(public_key),
  61. application:start(ssl),
  62. DataDir = ?config(data_dir, Config),
  63. cowboy:start_listener(https, 100,
  64. cowboy_ssl_transport, [
  65. {port, Port}, {certfile, DataDir ++ "cert.pem"},
  66. {keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}],
  67. cowboy_http_protocol, [{dispatch, init_https_dispatch(Config1)}]
  68. ),
  69. [{scheme, "https"}, {port, Port}|Config1];
  70. init_per_group(misc, Config) ->
  71. Port = 33082,
  72. cowboy:start_listener(misc, 100,
  73. cowboy_tcp_transport, [{port, Port}],
  74. cowboy_http_protocol, [{dispatch, [{'_', [
  75. {[], http_handler, []}
  76. ]}]}]),
  77. [{port, Port}|Config];
  78. init_per_group(rest, Config) ->
  79. Port = 33083,
  80. cowboy:start_listener(reset, 100,
  81. cowboy_tcp_transport, [{port, Port}],
  82. cowboy_http_protocol, [{dispatch, [{'_', [
  83. {[<<"simple">>], rest_simple_resource, []}
  84. ]}]}]),
  85. [{port, Port}|Config].
  86. end_per_group(https, Config) ->
  87. cowboy:stop_listener(https),
  88. application:stop(ssl),
  89. application:stop(public_key),
  90. application:stop(crypto),
  91. end_static_dir(Config),
  92. ok;
  93. end_per_group(Listener, Config) ->
  94. cowboy:stop_listener(Listener),
  95. end_static_dir(Config),
  96. ok.
  97. %% Dispatch configuration.
  98. init_http_dispatch(Config) ->
  99. [
  100. {[<<"localhost">>], [
  101. {[<<"chunked_response">>], chunked_handler, []},
  102. {[<<"init_shutdown">>], http_handler_init_shutdown, []},
  103. {[<<"long_polling">>], http_handler_long_polling, []},
  104. {[<<"headers">>, <<"dupe">>], http_handler,
  105. [{headers, [{<<"Connection">>, <<"close">>}]}]},
  106. {[<<"set_resp">>, <<"header">>], http_handler_set_resp,
  107. [{headers, [{<<"Vary">>, <<"Accept">>}]}]},
  108. {[<<"set_resp">>, <<"overwrite">>], http_handler_set_resp,
  109. [{headers, [{<<"Server">>, <<"DesireDrive/1.0">>}]}]},
  110. {[<<"set_resp">>, <<"body">>], http_handler_set_resp,
  111. [{body, <<"A flameless dance does not equal a cycle">>}]},
  112. {[<<"stream_body">>, <<"set_resp">>], http_handler_stream_body,
  113. [{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
  114. {[<<"static">>, '...'], cowboy_http_static,
  115. [{directory, ?config(static_dir, Config)},
  116. {mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
  117. {[], http_handler, []}
  118. ]}
  119. ].
  120. init_https_dispatch(Config) ->
  121. init_http_dispatch(Config).
  122. init_static_dir(Config) ->
  123. Dir = filename:join(?config(priv_dir, Config), "static"),
  124. Level1 = fun(Name) -> filename:join(Dir, Name) end,
  125. ok = file:make_dir(Dir),
  126. ok = file:write_file(Level1("test_file"), "test_file\n"),
  127. ok = file:write_file(Level1("test_file.css"), "test_file.css\n"),
  128. ok = file:write_file(Level1("test_noread"), "test_noread\n"),
  129. ok = file:change_mode(Level1("test_noread"), 8#0333),
  130. ok = file:make_dir(Level1("test_dir")),
  131. [{static_dir, Dir}|Config].
  132. end_static_dir(Config) ->
  133. Dir = ?config(static_dir, Config),
  134. Level1 = fun(Name) -> filename:join(Dir, Name) end,
  135. ok = file:delete(Level1("test_file")),
  136. ok = file:delete(Level1("test_file.css")),
  137. ok = file:delete(Level1("test_noread")),
  138. ok = file:del_dir(Level1("test_dir")),
  139. ok = file:del_dir(Dir),
  140. Config.
  141. %% http.
  142. chunked_response(Config) ->
  143. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "chunked_handler\r\nworks fine!"}} =
  144. httpc:request(build_url("/chunked_response", Config)).
  145. headers_dupe(Config) ->
  146. {port, Port} = lists:keyfind(port, 1, Config),
  147. {ok, Socket} = gen_tcp:connect("localhost", Port,
  148. [binary, {active, false}, {packet, raw}]),
  149. ok = gen_tcp:send(Socket, "GET /headers/dupe HTTP/1.1\r\n"
  150. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  151. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  152. {_Start, _Length} = binary:match(Data, <<"Connection: close">>),
  153. nomatch = binary:match(Data, <<"Connection: keep-alive">>),
  154. {error, closed} = gen_tcp:recv(Socket, 0, 1000).
  155. headers_huge(Config) ->
  156. Cookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  157. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _N <- lists:seq(1, 40)]),
  158. {_Packet, 200} = raw_req(["GET / HTTP/1.0\r\nHost: localhost\r\n"
  159. "Set-Cookie: ", Cookie, "\r\n\r\n"], Config).
  160. keepalive_nl(Config) ->
  161. {port, Port} = lists:keyfind(port, 1, Config),
  162. {ok, Socket} = gen_tcp:connect("localhost", Port,
  163. [binary, {active, false}, {packet, raw}]),
  164. ok = keepalive_nl_loop(Socket, 10),
  165. ok = gen_tcp:close(Socket).
  166. keepalive_nl_loop(_Socket, 0) ->
  167. ok;
  168. keepalive_nl_loop(Socket, N) ->
  169. ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
  170. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  171. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  172. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  173. nomatch = binary:match(Data, <<"Connection: close">>),
  174. ok = gen_tcp:send(Socket, "\r\n"), %% extra nl
  175. keepalive_nl_loop(Socket, N - 1).
  176. max_keepalive(Config) ->
  177. {port, Port} = lists:keyfind(port, 1, Config),
  178. {ok, Socket} = gen_tcp:connect("localhost", Port,
  179. [binary, {active, false}, {packet, raw}]),
  180. ok = max_keepalive_loop(Socket, 50),
  181. {error, closed} = gen_tcp:recv(Socket, 0, 1000).
  182. max_keepalive_loop(_Socket, 0) ->
  183. ok;
  184. max_keepalive_loop(Socket, N) ->
  185. ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
  186. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  187. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  188. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  189. case N of
  190. 1 -> {_, _} = binary:match(Data, <<"Connection: close">>);
  191. N -> nomatch = binary:match(Data, <<"Connection: close">>)
  192. end,
  193. keepalive_nl_loop(Socket, N - 1).
  194. nc_rand(Config) ->
  195. nc_reqs(Config, "/dev/urandom").
  196. nc_zero(Config) ->
  197. nc_reqs(Config, "/dev/zero").
  198. nc_reqs(Config, Input) ->
  199. Cat = os:find_executable("cat"),
  200. Nc = os:find_executable("nc"),
  201. case {Cat, Nc} of
  202. {false, _} ->
  203. {skip, {notfound, cat}};
  204. {_, false} ->
  205. {skip, {notfound, nc}};
  206. _Good ->
  207. %% Throw garbage at the server then check if it's still up.
  208. {port, Port} = lists:keyfind(port, 1, Config),
  209. [nc_run_req(Port, Input) || _N <- lists:seq(1, 100)],
  210. Packet = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n",
  211. {Packet, 200} = raw_req(Packet, Config)
  212. end.
  213. nc_run_req(Port, Input) ->
  214. os:cmd("cat " ++ Input ++ " | nc localhost " ++ integer_to_list(Port)).
  215. pipeline(Config) ->
  216. {port, Port} = lists:keyfind(port, 1, Config),
  217. {ok, Socket} = gen_tcp:connect("localhost", Port,
  218. [binary, {active, false}, {packet, raw}]),
  219. ok = gen_tcp:send(Socket,
  220. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  221. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  222. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  223. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  224. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"),
  225. Data = pipeline_recv(Socket, <<>>),
  226. Reqs = binary:split(Data, << "\r\n\r\nhttp_handler" >>, [global, trim]),
  227. 5 = length(Reqs),
  228. pipeline_check(Reqs).
  229. pipeline_check([]) ->
  230. ok;
  231. pipeline_check([Req|Tail]) ->
  232. << "HTTP/1.1 200", _Rest/bits >> = Req,
  233. pipeline_check(Tail).
  234. pipeline_recv(Socket, SoFar) ->
  235. case gen_tcp:recv(Socket, 0, 6000) of
  236. {ok, Data} ->
  237. pipeline_recv(Socket, << SoFar/binary, Data/binary >>);
  238. {error, closed} ->
  239. ok = gen_tcp:close(Socket),
  240. SoFar
  241. end.
  242. raw_req(Packet, Config) ->
  243. {port, Port} = lists:keyfind(port, 1, Config),
  244. {ok, Socket} = gen_tcp:connect("localhost", Port,
  245. [binary, {active, false}, {packet, raw}]),
  246. ok = gen_tcp:send(Socket, Packet),
  247. Res = case gen_tcp:recv(Socket, 0, 6000) of
  248. {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>} ->
  249. list_to_integer(binary_to_list(Str));
  250. {error, Reason} ->
  251. Reason
  252. end,
  253. gen_tcp:close(Socket),
  254. {Packet, Res}.
  255. raw(Config) ->
  256. Huge = [$0 || _N <- lists:seq(1, 5000)],
  257. Tests = [
  258. {"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
  259. {"\n", 400},
  260. {"Garbage\r\n\r\n", 400},
  261. {"\r\n\r\n\r\n\r\n\r\n\r\n", 400},
  262. {"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400},
  263. {"", closed},
  264. {"\r\n", closed},
  265. {"\r\n\r\n", closed},
  266. {"GET / HTTP/1.1", closed},
  267. {"GET / HTTP/1.1\r\n", 408},
  268. {"GET / HTTP/1.1\r\nHost: localhost", 408},
  269. {"GET / HTTP/1.1\r\nHost: localhost\r\n", 408},
  270. {"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408},
  271. {"GET http://localhost/ HTTP/1.1\r\n\r\n", 501},
  272. {"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505},
  273. {"GET /init_shutdown HTTP/1.1\r\nHost: localhost\r\n\r\n", 666},
  274. {"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n\r\n", 102},
  275. {Huge, 413},
  276. {"GET / HTTP/1.1\r\n" ++ Huge, 413}
  277. ],
  278. [{Packet, StatusCode} = raw_req(Packet, Config)
  279. || {Packet, StatusCode} <- Tests].
  280. set_resp_header(Config) ->
  281. {port, Port} = lists:keyfind(port, 1, Config),
  282. {ok, Socket} = gen_tcp:connect("localhost", Port,
  283. [binary, {active, false}, {packet, raw}]),
  284. ok = gen_tcp:send(Socket, "GET /set_resp/header HTTP/1.1\r\n"
  285. "Host: localhost\r\nConnection: close\r\n\r\n"),
  286. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  287. {_, _} = binary:match(Data, <<"Vary: Accept">>),
  288. {_, _} = binary:match(Data, <<"Set-Cookie: ">>).
  289. set_resp_overwrite(Config) ->
  290. {port, Port} = lists:keyfind(port, 1, Config),
  291. {ok, Socket} = gen_tcp:connect("localhost", Port,
  292. [binary, {active, false}, {packet, raw}]),
  293. ok = gen_tcp:send(Socket, "GET /set_resp/overwrite HTTP/1.1\r\n"
  294. "Host: localhost\r\nConnection: close\r\n\r\n"),
  295. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  296. {_Start, _Length} = binary:match(Data, <<"Server: DesireDrive/1.0">>).
  297. set_resp_body(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 /set_resp/body 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, <<"\r\n\r\n"
  305. "A flameless dance does not equal a cycle">>).
  306. response_as_req(Config) ->
  307. Packet =
  308. "HTTP/1.0 302 Found
  309. Location: http://www.google.co.il/
  310. Cache-Control: private
  311. Content-Type: text/html; charset=UTF-8
  312. 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
  313. Date: Sun, 04 Dec 2011 15:55:01 GMT
  314. Server: gws
  315. Content-Length: 221
  316. X-XSS-Protection: 1; mode=block
  317. X-Frame-Options: SAMEORIGIN
  318. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  319. <TITLE>302 Moved</TITLE></HEAD><BODY>
  320. <H1>302 Moved</H1>
  321. The document has moved
  322. <A HREF=\"http://www.google.co.il/\">here</A>.
  323. </BODY></HTML>",
  324. {Packet, 400} = raw_req(Packet, Config).
  325. stream_body_set_resp(Config) ->
  326. {port, Port} = lists:keyfind(port, 1, Config),
  327. {ok, Socket} = gen_tcp:connect("localhost", Port,
  328. [binary, {active, false}, {packet, raw}]),
  329. ok = gen_tcp:send(Socket, "GET /stream_body/set_resp HTTP/1.1\r\n"
  330. "Host: localhost\r\nConnection: close\r\n\r\n"),
  331. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  332. {_Start, _Length} = binary:match(Data, <<"stream_body_set_resp">>).
  333. %% http and https.
  334. build_url(Path, Config) ->
  335. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  336. {port, Port} = lists:keyfind(port, 1, Config),
  337. Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
  338. http_200(Config) ->
  339. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
  340. httpc:request(build_url("/", Config)).
  341. http_404(Config) ->
  342. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  343. httpc:request(build_url("/not/found", Config)).
  344. file_200(Config) ->
  345. {ok, {{"HTTP/1.1", 200, "OK"}, Headers, "test_file\n"}} =
  346. httpc:request(build_url("/static/test_file", Config)),
  347. "application/octet-stream" = ?config("content-type", Headers),
  348. {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test_file.css\n"}} =
  349. httpc:request(build_url("/static/test_file.css", Config)),
  350. "text/css" = ?config("content-type", Headers1).
  351. file_403(Config) ->
  352. {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} =
  353. httpc:request(build_url("/static/test_noread", Config)).
  354. dir_403(Config) ->
  355. {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} =
  356. httpc:request(build_url("/static/test_dir", Config)),
  357. {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} =
  358. httpc:request(build_url("/static/test_dir/", Config)).
  359. file_404(Config) ->
  360. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  361. httpc:request(build_url("/static/not_found", Config)).
  362. file_400(Config) ->
  363. {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers, _Body}} =
  364. httpc:request(build_url("/static/%2f", Config)),
  365. {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers1, _Body1}} =
  366. httpc:request(build_url("/static/%2e", Config)),
  367. {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers2, _Body2}} =
  368. httpc:request(build_url("/static/%2e%2e", Config)).
  369. %% misc.
  370. http_10_hostless(Config) ->
  371. Packet = "GET / HTTP/1.0\r\n\r\n",
  372. {Packet, 200} = raw_req(Packet, Config).
  373. %% rest.
  374. rest_simple(Config) ->
  375. Packet = "GET /simple HTTP/1.1\r\nHost: localhost\r\n\r\n",
  376. {Packet, 200} = raw_req(Packet, Config).
  377. rest_keepalive(Config) ->
  378. {port, Port} = lists:keyfind(port, 1, Config),
  379. {ok, Socket} = gen_tcp:connect("localhost", Port,
  380. [binary, {active, false}, {packet, raw}]),
  381. ok = rest_keepalive_loop(Socket, 100),
  382. ok = gen_tcp:close(Socket).
  383. rest_keepalive_loop(_Socket, 0) ->
  384. ok;
  385. rest_keepalive_loop(Socket, N) ->
  386. ok = gen_tcp:send(Socket, "GET /simple HTTP/1.1\r\n"
  387. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  388. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  389. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  390. nomatch = binary:match(Data, <<"Connection: close">>),
  391. rest_keepalive_loop(Socket, N - 1).