http_SUITE.erl 22 KB

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