http_SUITE.erl 25 KB

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