http_SUITE.erl 22 KB

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