http_SUITE.erl 18 KB

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