http_SUITE.erl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.eu>
  2. %% Copyright (c) 2011, Anthony Ramine <nox@dev-extend.eu>
  3. %%
  4. %% Permission to use, copy, modify, and/or distribute this software for any
  5. %% purpose with or without fee is hereby granted, provided that the above
  6. %% copyright notice and this permission notice appear in all copies.
  7. %%
  8. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. -module(http_SUITE).
  16. -include_lib("common_test/include/ct.hrl").
  17. -export([all/0, groups/0, init_per_suite/1, end_per_suite/1,
  18. init_per_group/2, end_per_group/2]). %% ct.
  19. -export([chunked_response/1, headers_dupe/1, headers_huge/1,
  20. keepalive_nl/1, max_keepalive/1, nc_rand/1, nc_zero/1,
  21. pipeline/1, raw/1, set_resp_header/1, set_resp_overwrite/1,
  22. set_resp_body/1, stream_body_set_resp/1, response_as_req/1,
  23. static_mimetypes_function/1, static_attribute_etag/1,
  24. static_function_etag/1, multipart/1]). %% http.
  25. -export([http_200/1, http_404/1, handler_errors/1,
  26. file_200/1, file_403/1, dir_403/1, file_404/1,
  27. file_400/1]). %% http and https.
  28. -export([http_10_hostless/1]). %% misc.
  29. -export([rest_simple/1, rest_keepalive/1]). %% rest.
  30. %% ct.
  31. all() ->
  32. [{group, http}, {group, https}, {group, misc}, {group, rest}].
  33. groups() ->
  34. BaseTests = [http_200, http_404, handler_errors,
  35. file_200, file_403, dir_403, file_404, file_400],
  36. [{http, [], [chunked_response, headers_dupe, headers_huge,
  37. keepalive_nl, max_keepalive, nc_rand, nc_zero, pipeline, raw,
  38. set_resp_header, set_resp_overwrite,
  39. set_resp_body, response_as_req, stream_body_set_resp,
  40. static_mimetypes_function, static_attribute_etag,
  41. static_function_etag, multipart] ++ BaseTests},
  42. {https, [], BaseTests},
  43. {misc, [], [http_10_hostless]},
  44. {rest, [], [rest_simple, rest_keepalive]}].
  45. init_per_suite(Config) ->
  46. application:start(inets),
  47. application:start(cowboy),
  48. Config.
  49. end_per_suite(_Config) ->
  50. application:stop(cowboy),
  51. application:stop(inets),
  52. ok.
  53. init_per_group(http, Config) ->
  54. Port = 33080,
  55. Config1 = init_static_dir(Config),
  56. cowboy:start_listener(http, 100,
  57. cowboy_tcp_transport, [{port, Port}],
  58. cowboy_http_protocol, [{max_keepalive, 50},
  59. {dispatch, init_http_dispatch(Config1)}]
  60. ),
  61. [{scheme, "http"}, {port, Port}|Config1];
  62. init_per_group(https, Config) ->
  63. Port = 33081,
  64. Config1 = init_static_dir(Config),
  65. application:start(crypto),
  66. application:start(public_key),
  67. application:start(ssl),
  68. DataDir = ?config(data_dir, Config),
  69. cowboy:start_listener(https, 100,
  70. cowboy_ssl_transport, [
  71. {port, Port}, {certfile, DataDir ++ "cert.pem"},
  72. {keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}],
  73. cowboy_http_protocol, [{dispatch, init_https_dispatch(Config1)}]
  74. ),
  75. [{scheme, "https"}, {port, Port}|Config1];
  76. init_per_group(misc, Config) ->
  77. Port = 33082,
  78. cowboy:start_listener(misc, 100,
  79. cowboy_tcp_transport, [{port, Port}],
  80. cowboy_http_protocol, [{dispatch, [{'_', [
  81. {[], http_handler, []}
  82. ]}]}]),
  83. [{port, Port}|Config];
  84. init_per_group(rest, Config) ->
  85. Port = 33083,
  86. cowboy:start_listener(reset, 100,
  87. cowboy_tcp_transport, [{port, Port}],
  88. cowboy_http_protocol, [{dispatch, [{'_', [
  89. {[<<"simple">>], rest_simple_resource, []}
  90. ]}]}]),
  91. [{port, Port}|Config].
  92. end_per_group(https, Config) ->
  93. cowboy:stop_listener(https),
  94. application:stop(ssl),
  95. application:stop(public_key),
  96. application:stop(crypto),
  97. end_static_dir(Config),
  98. ok;
  99. end_per_group(http, Config) ->
  100. cowboy:stop_listener(http),
  101. end_static_dir(Config);
  102. end_per_group(Listener, _Config) ->
  103. cowboy:stop_listener(Listener),
  104. ok.
  105. %% Dispatch configuration.
  106. init_http_dispatch(Config) ->
  107. [
  108. {[<<"localhost">>], [
  109. {[<<"chunked_response">>], chunked_handler, []},
  110. {[<<"init_shutdown">>], http_handler_init_shutdown, []},
  111. {[<<"long_polling">>], http_handler_long_polling, []},
  112. {[<<"headers">>, <<"dupe">>], http_handler,
  113. [{headers, [{<<"Connection">>, <<"close">>}]}]},
  114. {[<<"set_resp">>, <<"header">>], http_handler_set_resp,
  115. [{headers, [{<<"Vary">>, <<"Accept">>}]}]},
  116. {[<<"set_resp">>, <<"overwrite">>], http_handler_set_resp,
  117. [{headers, [{<<"Server">>, <<"DesireDrive/1.0">>}]}]},
  118. {[<<"set_resp">>, <<"body">>], http_handler_set_resp,
  119. [{body, <<"A flameless dance does not equal a cycle">>}]},
  120. {[<<"stream_body">>, <<"set_resp">>], http_handler_stream_body,
  121. [{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
  122. {[<<"static">>, '...'], cowboy_http_static,
  123. [{directory, ?config(static_dir, Config)},
  124. {mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
  125. {[<<"static_mimetypes_function">>, '...'], cowboy_http_static,
  126. [{directory, ?config(static_dir, Config)},
  127. {mimetypes, {fun(Path, data) when is_binary(Path) ->
  128. [<<"text/html">>] end, data}}]},
  129. {[<<"handler_errors">>], http_handler_errors, []},
  130. {[<<"static_attribute_etag">>, '...'], cowboy_http_static,
  131. [{directory, ?config(static_dir, Config)},
  132. {etag, {attributes, [filepath, filesize, inode, mtime]}}]},
  133. {[<<"static_function_etag">>, '...'], cowboy_http_static,
  134. [{directory, ?config(static_dir, Config)},
  135. {etag, {fun static_function_etag/2, etag_data}}]},
  136. {[<<"multipart">>], http_handler_multipart, []},
  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. multipart(Config) ->
  217. Url = build_url("/multipart", Config),
  218. Body = <<
  219. "This is a preamble."
  220. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  221. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  222. "\r\n--OHai--"
  223. "This is an epiloque."
  224. >>,
  225. Request = {Url, [], "multipart/x-makes-no-sense; boundary=OHai", Body},
  226. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, Response}} =
  227. httpc:request(post, Request, [], [{body_format, binary}]),
  228. Parts = binary_to_term(Response),
  229. Parts = [
  230. {[{<<"X-Name">>, <<"answer">>}], <<"42">>},
  231. {[{'Server', <<"Cowboy">>}], <<"It rocks!\r\n">>}
  232. ].
  233. nc_rand(Config) ->
  234. nc_reqs(Config, "/dev/urandom").
  235. nc_zero(Config) ->
  236. nc_reqs(Config, "/dev/zero").
  237. nc_reqs(Config, Input) ->
  238. Cat = os:find_executable("cat"),
  239. Nc = os:find_executable("nc"),
  240. case {Cat, Nc} of
  241. {false, _} ->
  242. {skip, {notfound, cat}};
  243. {_, false} ->
  244. {skip, {notfound, nc}};
  245. _Good ->
  246. %% Throw garbage at the server then check if it's still up.
  247. {port, Port} = lists:keyfind(port, 1, Config),
  248. [nc_run_req(Port, Input) || _N <- lists:seq(1, 100)],
  249. Packet = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n",
  250. {Packet, 200} = raw_req(Packet, Config)
  251. end.
  252. nc_run_req(Port, Input) ->
  253. os:cmd("cat " ++ Input ++ " | nc localhost " ++ integer_to_list(Port)).
  254. pipeline(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,
  259. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  260. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  261. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  262. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  263. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"),
  264. Data = pipeline_recv(Socket, <<>>),
  265. Reqs = binary:split(Data, << "\r\n\r\nhttp_handler" >>, [global, trim]),
  266. 5 = length(Reqs),
  267. pipeline_check(Reqs).
  268. pipeline_check([]) ->
  269. ok;
  270. pipeline_check([Req|Tail]) ->
  271. << "HTTP/1.1 200", _Rest/bits >> = Req,
  272. pipeline_check(Tail).
  273. pipeline_recv(Socket, SoFar) ->
  274. case gen_tcp:recv(Socket, 0, 6000) of
  275. {ok, Data} ->
  276. pipeline_recv(Socket, << SoFar/binary, Data/binary >>);
  277. {error, closed} ->
  278. ok = gen_tcp:close(Socket),
  279. SoFar
  280. end.
  281. raw_req(Packet, Config) ->
  282. {port, Port} = lists:keyfind(port, 1, Config),
  283. {ok, Socket} = gen_tcp:connect("localhost", Port,
  284. [binary, {active, false}, {packet, raw}]),
  285. ok = gen_tcp:send(Socket, Packet),
  286. Res = case gen_tcp:recv(Socket, 0, 6000) of
  287. {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>} ->
  288. list_to_integer(binary_to_list(Str));
  289. {error, Reason} ->
  290. Reason
  291. end,
  292. gen_tcp:close(Socket),
  293. {Packet, Res}.
  294. %% Send a raw request. Return the response code and the full response.
  295. raw_resp(Request, Config) ->
  296. {port, Port} = lists:keyfind(port, 1, Config),
  297. Transport = case ?config(scheme, Config) of
  298. "http" -> gen_tcp;
  299. "https" -> ssl
  300. end,
  301. {ok, Socket} = Transport:connect("localhost", Port,
  302. [binary, {active, false}, {packet, raw}]),
  303. ok = Transport:send(Socket, Request),
  304. {StatusCode, Response} = case recv_loop(Transport, Socket, <<>>) of
  305. {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >> = Bin} ->
  306. {list_to_integer(binary_to_list(Str)), Bin};
  307. {ok, Bin} ->
  308. {badresp, Bin};
  309. {error, Reason} ->
  310. {Reason, <<>>}
  311. end,
  312. Transport:close(Socket),
  313. {Response, StatusCode}.
  314. recv_loop(Transport, Socket, Acc) ->
  315. case Transport:recv(Socket, 0, 6000) of
  316. {ok, Data} ->
  317. recv_loop(Transport, Socket, <<Acc/binary, Data/binary>>);
  318. {error, closed} ->
  319. ok = Transport:close(Socket),
  320. {ok, Acc};
  321. {error, Reason} ->
  322. {error, Reason}
  323. end.
  324. raw(Config) ->
  325. Huge = [$0 || _N <- lists:seq(1, 5000)],
  326. Tests = [
  327. {"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
  328. {"\n", 400},
  329. {"Garbage\r\n\r\n", 400},
  330. {"\r\n\r\n\r\n\r\n\r\n\r\n", 400},
  331. {"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400},
  332. {"", closed},
  333. {"\r\n", closed},
  334. {"\r\n\r\n", closed},
  335. {"GET / HTTP/1.1", closed},
  336. {"GET / HTTP/1.1\r\n", 408},
  337. {"GET / HTTP/1.1\r\nHost: localhost", 408},
  338. {"GET / HTTP/1.1\r\nHost: localhost\r\n", 408},
  339. {"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408},
  340. {"GET http://localhost/ HTTP/1.1\r\n\r\n", 501},
  341. {"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505},
  342. {"GET /init_shutdown HTTP/1.1\r\nHost: localhost\r\n\r\n", 666},
  343. {"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n\r\n", 102},
  344. {Huge, 413},
  345. {"GET / HTTP/1.1\r\n" ++ Huge, 413}
  346. ],
  347. [{Packet, StatusCode} = raw_req(Packet, Config)
  348. || {Packet, StatusCode} <- Tests].
  349. set_resp_header(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/header 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. {_, _} = binary:match(Data, <<"Vary: Accept">>),
  357. {_, _} = binary:match(Data, <<"Set-Cookie: ">>).
  358. set_resp_overwrite(Config) ->
  359. {port, Port} = lists:keyfind(port, 1, Config),
  360. {ok, Socket} = gen_tcp:connect("localhost", Port,
  361. [binary, {active, false}, {packet, raw}]),
  362. ok = gen_tcp:send(Socket, "GET /set_resp/overwrite HTTP/1.1\r\n"
  363. "Host: localhost\r\nConnection: close\r\n\r\n"),
  364. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  365. {_Start, _Length} = binary:match(Data, <<"Server: DesireDrive/1.0">>).
  366. set_resp_body(Config) ->
  367. {port, Port} = lists:keyfind(port, 1, Config),
  368. {ok, Socket} = gen_tcp:connect("localhost", Port,
  369. [binary, {active, false}, {packet, raw}]),
  370. ok = gen_tcp:send(Socket, "GET /set_resp/body HTTP/1.1\r\n"
  371. "Host: localhost\r\nConnection: close\r\n\r\n"),
  372. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  373. {_Start, _Length} = binary:match(Data, <<"\r\n\r\n"
  374. "A flameless dance does not equal a cycle">>).
  375. response_as_req(Config) ->
  376. Packet =
  377. "HTTP/1.0 302 Found
  378. Location: http://www.google.co.il/
  379. Cache-Control: private
  380. Content-Type: text/html; charset=UTF-8
  381. 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
  382. Date: Sun, 04 Dec 2011 15:55:01 GMT
  383. Server: gws
  384. Content-Length: 221
  385. X-XSS-Protection: 1; mode=block
  386. X-Frame-Options: SAMEORIGIN
  387. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  388. <TITLE>302 Moved</TITLE></HEAD><BODY>
  389. <H1>302 Moved</H1>
  390. The document has moved
  391. <A HREF=\"http://www.google.co.il/\">here</A>.
  392. </BODY></HTML>",
  393. {Packet, 400} = raw_req(Packet, Config).
  394. stream_body_set_resp(Config) ->
  395. {Packet, 200} = raw_resp(
  396. "GET /stream_body/set_resp HTTP/1.1\r\n"
  397. "Host: localhost\r\nConnection: close\r\n\r\n", Config),
  398. {_Start, _Length} = binary:match(Packet, <<"stream_body_set_resp">>).
  399. static_mimetypes_function(Config) ->
  400. TestURL = build_url("/static_mimetypes_function/test.html", Config),
  401. {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} =
  402. httpc:request(TestURL),
  403. "text/html" = ?config("content-type", Headers1).
  404. handler_errors(Config) ->
  405. Request = fun(Case) ->
  406. raw_resp(["GET /handler_errors?case=", Case, " HTTP/1.1\r\n",
  407. "Host: localhost\r\n\r\n"], Config) end,
  408. {_Packet1, 500} = Request("init_before_reply"),
  409. {Packet2, 200} = Request("init_after_reply"),
  410. nomatch = binary:match(Packet2, <<"HTTP/1.1 500">>),
  411. {Packet3, 200} = Request("init_reply_handle_error"),
  412. nomatch = binary:match(Packet3, <<"HTTP/1.1 500">>),
  413. {_Packet4, 500} = Request("handle_before_reply"),
  414. {Packet5, 200} = Request("handle_after_reply"),
  415. nomatch = binary:match(Packet5, <<"HTTP/1.1 500">>),
  416. {Packet6, 200} = raw_resp([
  417. "GET / HTTP/1.1\r\n",
  418. "Host: localhost\r\n",
  419. "Connection: keep-alive\r\n\r\n",
  420. "GET /handler_errors?case=handle_after_reply\r\n",
  421. "Host: localhost\r\n\r\n"], Config),
  422. nomatch = binary:match(Packet6, <<"HTTP/1.1 500">>),
  423. {Packet7, 200} = raw_resp([
  424. "GET / HTTP/1.1\r\n",
  425. "Host: localhost\r\n",
  426. "Connection: keep-alive\r\n\r\n",
  427. "GET /handler_errors?case=handle_before_reply HTTP/1.1\r\n",
  428. "Host: localhost\r\n\r\n"], Config),
  429. {{_, _}, _} = {binary:match(Packet7, <<"HTTP/1.1 500">>), Packet7},
  430. done.
  431. static_attribute_etag(Config) ->
  432. TestURL = build_url("/static_attribute_etag/test.html", Config),
  433. {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} =
  434. httpc:request(TestURL),
  435. false = ?config("etag", Headers1) =:= undefined,
  436. {ok, {{"HTTP/1.1", 200, "OK"}, Headers2, "test.html\n"}} =
  437. httpc:request(TestURL),
  438. true = ?config("etag", Headers1) =:= ?config("etag", Headers2).
  439. static_function_etag(Config) ->
  440. TestURL = build_url("/static_function_etag/test.html", Config),
  441. {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} =
  442. httpc:request(TestURL),
  443. false = ?config("etag", Headers1) =:= undefined,
  444. {ok, {{"HTTP/1.1", 200, "OK"}, Headers2, "test.html\n"}} =
  445. httpc:request(TestURL),
  446. true = ?config("etag", Headers1) =:= ?config("etag", Headers2).
  447. static_function_etag(Arguments, etag_data) ->
  448. {_, Filepath} = lists:keyfind(filepath, 1, Arguments),
  449. {_, _Filesize} = lists:keyfind(filesize, 1, Arguments),
  450. {_, _INode} = lists:keyfind(inode, 1, Arguments),
  451. {_, _Modified} = lists:keyfind(mtime, 1, Arguments),
  452. ChecksumCommand = lists:flatten(io_lib:format("sha1sum ~s", [Filepath])),
  453. [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "),
  454. iolist_to_binary(Checksum).
  455. %% http and https.
  456. build_url(Path, Config) ->
  457. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  458. {port, Port} = lists:keyfind(port, 1, Config),
  459. Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
  460. http_200(Config) ->
  461. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
  462. httpc:request(build_url("/", Config)).
  463. http_404(Config) ->
  464. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  465. httpc:request(build_url("/not/found", Config)).
  466. file_200(Config) ->
  467. {ok, {{"HTTP/1.1", 200, "OK"}, Headers, "test_file\n"}} =
  468. httpc:request(build_url("/static/test_file", Config)),
  469. "application/octet-stream" = ?config("content-type", Headers),
  470. {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test_file.css\n"}} =
  471. httpc:request(build_url("/static/test_file.css", Config)),
  472. "text/css" = ?config("content-type", Headers1).
  473. file_403(Config) ->
  474. {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} =
  475. httpc:request(build_url("/static/test_noread", Config)).
  476. dir_403(Config) ->
  477. {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} =
  478. httpc:request(build_url("/static/test_dir", Config)),
  479. {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} =
  480. httpc:request(build_url("/static/test_dir/", Config)).
  481. file_404(Config) ->
  482. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  483. httpc:request(build_url("/static/not_found", Config)).
  484. file_400(Config) ->
  485. {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers, _Body}} =
  486. httpc:request(build_url("/static/%2f", Config)),
  487. {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers1, _Body1}} =
  488. httpc:request(build_url("/static/%2e", Config)),
  489. {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers2, _Body2}} =
  490. httpc:request(build_url("/static/%2e%2e", Config)).
  491. %% misc.
  492. http_10_hostless(Config) ->
  493. Packet = "GET / HTTP/1.0\r\n\r\n",
  494. {Packet, 200} = raw_req(Packet, Config).
  495. %% rest.
  496. rest_simple(Config) ->
  497. Packet = "GET /simple HTTP/1.1\r\nHost: localhost\r\n\r\n",
  498. {Packet, 200} = raw_req(Packet, Config).
  499. rest_keepalive(Config) ->
  500. {port, Port} = lists:keyfind(port, 1, Config),
  501. {ok, Socket} = gen_tcp:connect("localhost", Port,
  502. [binary, {active, false}, {packet, raw}]),
  503. ok = rest_keepalive_loop(Socket, 100),
  504. ok = gen_tcp:close(Socket).
  505. rest_keepalive_loop(_Socket, 0) ->
  506. ok;
  507. rest_keepalive_loop(Socket, N) ->
  508. ok = gen_tcp:send(Socket, "GET /simple HTTP/1.1\r\n"
  509. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  510. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  511. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  512. nomatch = binary:match(Data, <<"Connection: close">>),
  513. rest_keepalive_loop(Socket, N - 1).