http_SUITE.erl 27 KB

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