http_SUITE.erl 20 KB

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