http_SUITE.erl 21 KB

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