http_SUITE.erl 25 KB

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