http_SUITE.erl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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://proxy/ HTTP/1.1\r\n\r\n", 400},
  344. {"GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
  345. {"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505},
  346. {"GET /init_shutdown HTTP/1.1\r\nHost: localhost\r\n\r\n", 666},
  347. {"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n\r\n", 102},
  348. {Huge, 413},
  349. {"GET / HTTP/1.1\r\n" ++ Huge, 413}
  350. ],
  351. [{Packet, StatusCode} = raw_req(Packet, Config)
  352. || {Packet, StatusCode} <- Tests].
  353. set_resp_header(Config) ->
  354. {port, Port} = lists:keyfind(port, 1, Config),
  355. {ok, Socket} = gen_tcp:connect("localhost", Port,
  356. [binary, {active, false}, {packet, raw}]),
  357. ok = gen_tcp:send(Socket, "GET /set_resp/header HTTP/1.1\r\n"
  358. "Host: localhost\r\nConnection: close\r\n\r\n"),
  359. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  360. {_, _} = binary:match(Data, <<"Vary: Accept">>),
  361. {_, _} = binary:match(Data, <<"Set-Cookie: ">>).
  362. set_resp_overwrite(Config) ->
  363. {port, Port} = lists:keyfind(port, 1, Config),
  364. {ok, Socket} = gen_tcp:connect("localhost", Port,
  365. [binary, {active, false}, {packet, raw}]),
  366. ok = gen_tcp:send(Socket, "GET /set_resp/overwrite HTTP/1.1\r\n"
  367. "Host: localhost\r\nConnection: close\r\n\r\n"),
  368. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  369. {_Start, _Length} = binary:match(Data, <<"Server: DesireDrive/1.0">>).
  370. set_resp_body(Config) ->
  371. {port, Port} = lists:keyfind(port, 1, Config),
  372. {ok, Socket} = gen_tcp:connect("localhost", Port,
  373. [binary, {active, false}, {packet, raw}]),
  374. ok = gen_tcp:send(Socket, "GET /set_resp/body HTTP/1.1\r\n"
  375. "Host: localhost\r\nConnection: close\r\n\r\n"),
  376. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  377. {_Start, _Length} = binary:match(Data, <<"\r\n\r\n"
  378. "A flameless dance does not equal a cycle">>).
  379. response_as_req(Config) ->
  380. Packet =
  381. "HTTP/1.0 302 Found
  382. Location: http://www.google.co.il/
  383. Cache-Control: private
  384. Content-Type: text/html; charset=UTF-8
  385. 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
  386. Date: Sun, 04 Dec 2011 15:55:01 GMT
  387. Server: gws
  388. Content-Length: 221
  389. X-XSS-Protection: 1; mode=block
  390. X-Frame-Options: SAMEORIGIN
  391. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  392. <TITLE>302 Moved</TITLE></HEAD><BODY>
  393. <H1>302 Moved</H1>
  394. The document has moved
  395. <A HREF=\"http://www.google.co.il/\">here</A>.
  396. </BODY></HTML>",
  397. {Packet, 400} = raw_req(Packet, Config).
  398. stream_body_set_resp(Config) ->
  399. {Packet, 200} = raw_resp(
  400. "GET /stream_body/set_resp HTTP/1.1\r\n"
  401. "Host: localhost\r\nConnection: close\r\n\r\n", Config),
  402. {_Start, _Length} = binary:match(Packet, <<"stream_body_set_resp">>).
  403. static_mimetypes_function(Config) ->
  404. TestURL = build_url("/static_mimetypes_function/test.html", Config),
  405. {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} =
  406. httpc:request(TestURL),
  407. "text/html" = ?config("content-type", Headers1).
  408. handler_errors(Config) ->
  409. Request = fun(Case) ->
  410. raw_resp(["GET /handler_errors?case=", Case, " HTTP/1.1\r\n",
  411. "Host: localhost\r\n\r\n"], Config) end,
  412. {_Packet1, 500} = Request("init_before_reply"),
  413. {Packet2, 200} = Request("init_after_reply"),
  414. nomatch = binary:match(Packet2, <<"HTTP/1.1 500">>),
  415. {Packet3, 200} = Request("init_reply_handle_error"),
  416. nomatch = binary:match(Packet3, <<"HTTP/1.1 500">>),
  417. {_Packet4, 500} = Request("handle_before_reply"),
  418. {Packet5, 200} = Request("handle_after_reply"),
  419. nomatch = binary:match(Packet5, <<"HTTP/1.1 500">>),
  420. {Packet6, 200} = raw_resp([
  421. "GET / HTTP/1.1\r\n",
  422. "Host: localhost\r\n",
  423. "Connection: keep-alive\r\n\r\n",
  424. "GET /handler_errors?case=handle_after_reply\r\n",
  425. "Host: localhost\r\n\r\n"], Config),
  426. nomatch = binary:match(Packet6, <<"HTTP/1.1 500">>),
  427. {Packet7, 200} = raw_resp([
  428. "GET / HTTP/1.1\r\n",
  429. "Host: localhost\r\n",
  430. "Connection: keep-alive\r\n\r\n",
  431. "GET /handler_errors?case=handle_before_reply HTTP/1.1\r\n",
  432. "Host: localhost\r\n\r\n"], Config),
  433. {{_, _}, _} = {binary:match(Packet7, <<"HTTP/1.1 500">>), Packet7},
  434. done.
  435. static_attribute_etag(Config) ->
  436. TestURL = build_url("/static_attribute_etag/test.html", Config),
  437. {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} =
  438. httpc:request(TestURL),
  439. false = ?config("etag", Headers1) =:= undefined,
  440. {ok, {{"HTTP/1.1", 200, "OK"}, Headers2, "test.html\n"}} =
  441. httpc:request(TestURL),
  442. true = ?config("etag", Headers1) =:= ?config("etag", Headers2).
  443. static_function_etag(Config) ->
  444. TestURL = build_url("/static_function_etag/test.html", Config),
  445. {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} =
  446. httpc:request(TestURL),
  447. false = ?config("etag", Headers1) =:= undefined,
  448. {ok, {{"HTTP/1.1", 200, "OK"}, Headers2, "test.html\n"}} =
  449. httpc:request(TestURL),
  450. true = ?config("etag", Headers1) =:= ?config("etag", Headers2).
  451. static_function_etag(Arguments, etag_data) ->
  452. {_, Filepath} = lists:keyfind(filepath, 1, Arguments),
  453. {_, _Filesize} = lists:keyfind(filesize, 1, Arguments),
  454. {_, _INode} = lists:keyfind(inode, 1, Arguments),
  455. {_, _Modified} = lists:keyfind(mtime, 1, Arguments),
  456. ChecksumCommand = lists:flatten(io_lib:format("sha1sum ~s", [Filepath])),
  457. [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "),
  458. iolist_to_binary(Checksum).
  459. %% http and https.
  460. build_url(Path, Config) ->
  461. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  462. {port, Port} = lists:keyfind(port, 1, Config),
  463. Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
  464. http_200(Config) ->
  465. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
  466. httpc:request(build_url("/", Config)).
  467. http_404(Config) ->
  468. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  469. httpc:request(build_url("/not/found", Config)).
  470. file_200(Config) ->
  471. {ok, {{"HTTP/1.1", 200, "OK"}, Headers, "test_file\n"}} =
  472. httpc:request(build_url("/static/test_file", Config)),
  473. "application/octet-stream" = ?config("content-type", Headers),
  474. {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test_file.css\n"}} =
  475. httpc:request(build_url("/static/test_file.css", Config)),
  476. "text/css" = ?config("content-type", Headers1).
  477. file_403(Config) ->
  478. {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} =
  479. httpc:request(build_url("/static/test_noread", Config)).
  480. dir_403(Config) ->
  481. {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} =
  482. httpc:request(build_url("/static/test_dir", Config)),
  483. {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} =
  484. httpc:request(build_url("/static/test_dir/", Config)).
  485. file_404(Config) ->
  486. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  487. httpc:request(build_url("/static/not_found", Config)).
  488. file_400(Config) ->
  489. {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers, _Body}} =
  490. httpc:request(build_url("/static/%2f", Config)),
  491. {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers1, _Body1}} =
  492. httpc:request(build_url("/static/%2e", Config)),
  493. {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers2, _Body2}} =
  494. httpc:request(build_url("/static/%2e%2e", Config)).
  495. %% misc.
  496. http_10_hostless(Config) ->
  497. Packet = "GET / HTTP/1.0\r\n\r\n",
  498. {Packet, 200} = raw_req(Packet, Config).
  499. %% rest.
  500. rest_simple(Config) ->
  501. Packet = "GET /simple HTTP/1.1\r\nHost: localhost\r\n\r\n",
  502. {Packet, 200} = raw_req(Packet, Config).
  503. rest_keepalive(Config) ->
  504. {port, Port} = lists:keyfind(port, 1, Config),
  505. {ok, Socket} = gen_tcp:connect("localhost", Port,
  506. [binary, {active, false}, {packet, raw}]),
  507. ok = rest_keepalive_loop(Socket, 100),
  508. ok = gen_tcp:close(Socket).
  509. rest_keepalive_loop(_Socket, 0) ->
  510. ok;
  511. rest_keepalive_loop(Socket, N) ->
  512. ok = gen_tcp:send(Socket, "GET /simple HTTP/1.1\r\n"
  513. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  514. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  515. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  516. nomatch = binary:match(Data, <<"Connection: close">>),
  517. rest_keepalive_loop(Socket, N - 1).
  518. rest_keepalive_post(Config) ->
  519. {port, Port} = lists:keyfind(port, 1, Config),
  520. {ok, Socket} = gen_tcp:connect("localhost", Port,
  521. [binary, {active, false}, {packet, raw}]),
  522. ok = rest_keepalive_post_loop(Socket, 10, forbidden_post),
  523. ok = gen_tcp:close(Socket).
  524. rest_keepalive_post_loop(_Socket, 0, _) ->
  525. ok;
  526. rest_keepalive_post_loop(Socket, N, simple_post) ->
  527. ok = gen_tcp:send(Socket, "POST /simple_post HTTP/1.1\r\n"
  528. "Host: localhost\r\nConnection: keep-alive\r\n"
  529. "Content-Length: 5\r\nContent-Type: text/plain\r\n\r\n12345"),
  530. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  531. {0, 12} = binary:match(Data, <<"HTTP/1.1 303">>),
  532. nomatch = binary:match(Data, <<"Connection: close">>),
  533. rest_keepalive_post_loop(Socket, N - 1, forbidden_post);
  534. rest_keepalive_post_loop(Socket, N, forbidden_post) ->
  535. ok = gen_tcp:send(Socket, "POST /forbidden_post HTTP/1.1\r\n"
  536. "Host: localhost\r\nConnection: keep-alive\r\n"
  537. "Content-Length: 5\r\nContent-Type: text/plain\r\n\r\n12345"),
  538. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  539. {0, 12} = binary:match(Data, <<"HTTP/1.1 403">>),
  540. nomatch = binary:match(Data, <<"Connection: close">>),
  541. rest_keepalive_post_loop(Socket, N - 1, simple_post).
  542. rest_nodelete(Config) ->
  543. {port, Port} = lists:keyfind(port, 1, Config),
  544. {ok, Socket} = gen_tcp:connect("localhost", Port,
  545. [binary, {active, false}, {packet, raw}]),
  546. Request = "DELETE /nodelete HTTP/1.1\r\nHost: localhost\r\n\r\n",
  547. ok = gen_tcp:send(Socket, Request),
  548. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  549. {0, 12} = binary:match(Data, <<"HTTP/1.1 500">>),
  550. ok = gen_tcp:close(Socket).