http_SUITE.erl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(http_SUITE).
  15. -include_lib("common_test/include/ct.hrl").
  16. -export([all/0, groups/0, init_per_suite/1, end_per_suite/1,
  17. init_per_group/2, end_per_group/2]). %% ct.
  18. -export([chunked_response/1, headers_dupe/1, headers_huge/1,
  19. keepalive_nl/1, nc_rand/1, nc_zero/1, pipeline/1, raw/1,
  20. ws0/1, ws8/1, ws8_single_bytes/1, ws8_init_shutdown/1,
  21. ws13/1, ws_timeout_hibernate/1, set_resp_header/1,
  22. set_resp_overwrite/1, set_resp_body/1, response_as_req/1]). %% http.
  23. -export([http_200/1, http_404/1]). %% http and https.
  24. -export([http_10_hostless/1]). %% misc.
  25. -export([rest_simple/1]). %% rest.
  26. %% ct.
  27. all() ->
  28. [{group, http}, {group, https}, {group, misc}, {group, rest}].
  29. groups() ->
  30. BaseTests = [http_200, http_404],
  31. [{http, [], [chunked_response, headers_dupe, headers_huge,
  32. keepalive_nl, nc_rand, nc_zero, pipeline, raw,
  33. ws0, ws8, ws8_single_bytes, ws8_init_shutdown, ws13,
  34. ws_timeout_hibernate, set_resp_header, set_resp_overwrite,
  35. set_resp_body, response_as_req] ++ BaseTests},
  36. {https, [], BaseTests},
  37. {misc, [], [http_10_hostless]},
  38. {rest, [], [rest_simple]}].
  39. init_per_suite(Config) ->
  40. application:start(inets),
  41. application:start(cowboy),
  42. Config.
  43. end_per_suite(_Config) ->
  44. application:stop(cowboy),
  45. application:stop(inets),
  46. ok.
  47. init_per_group(http, Config) ->
  48. Port = 33080,
  49. cowboy:start_listener(http, 100,
  50. cowboy_tcp_transport, [{port, Port}],
  51. cowboy_http_protocol, [{dispatch, init_http_dispatch()}]
  52. ),
  53. [{scheme, "http"}, {port, Port}|Config];
  54. init_per_group(https, Config) ->
  55. Port = 33081,
  56. application:start(crypto),
  57. application:start(public_key),
  58. application:start(ssl),
  59. DataDir = ?config(data_dir, Config),
  60. cowboy:start_listener(https, 100,
  61. cowboy_ssl_transport, [
  62. {port, Port}, {certfile, DataDir ++ "cert.pem"},
  63. {keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}],
  64. cowboy_http_protocol, [{dispatch, init_https_dispatch()}]
  65. ),
  66. [{scheme, "https"}, {port, Port}|Config];
  67. init_per_group(misc, Config) ->
  68. Port = 33082,
  69. cowboy:start_listener(misc, 100,
  70. cowboy_tcp_transport, [{port, Port}],
  71. cowboy_http_protocol, [{dispatch, [{'_', [
  72. {[], http_handler, []}
  73. ]}]}]),
  74. [{port, Port}|Config];
  75. init_per_group(rest, Config) ->
  76. Port = 33083,
  77. cowboy:start_listener(reset, 100,
  78. cowboy_tcp_transport, [{port, Port}],
  79. cowboy_http_protocol, [{dispatch, [{'_', [
  80. {[<<"simple">>], rest_simple_resource, []}
  81. ]}]}]),
  82. [{port, Port}|Config].
  83. end_per_group(https, _Config) ->
  84. cowboy:stop_listener(https),
  85. application:stop(ssl),
  86. application:stop(public_key),
  87. application:stop(crypto),
  88. ok;
  89. end_per_group(Listener, _Config) ->
  90. cowboy:stop_listener(Listener),
  91. ok.
  92. %% Dispatch configuration.
  93. init_http_dispatch() ->
  94. [
  95. {[<<"localhost">>], [
  96. {[<<"chunked_response">>], chunked_handler, []},
  97. {[<<"websocket">>], websocket_handler, []},
  98. {[<<"ws_timeout_hibernate">>], ws_timeout_hibernate_handler, []},
  99. {[<<"ws_init_shutdown">>], websocket_handler_init_shutdown, []},
  100. {[<<"init_shutdown">>], http_handler_init_shutdown, []},
  101. {[<<"long_polling">>], http_handler_long_polling, []},
  102. {[<<"headers">>, <<"dupe">>], http_handler,
  103. [{headers, [{<<"Connection">>, <<"close">>}]}]},
  104. {[<<"set_resp">>, <<"header">>], http_handler_set_resp,
  105. [{headers, [{<<"Vary">>, <<"Accept">>}]}]},
  106. {[<<"set_resp">>, <<"overwrite">>], http_handler_set_resp,
  107. [{headers, [{<<"Server">>, <<"DesireDrive/1.0">>}]}]},
  108. {[<<"set_resp">>, <<"body">>], http_handler_set_resp,
  109. [{body, <<"A flameless dance does not equal a cycle">>}]},
  110. {[], http_handler, []}
  111. ]}
  112. ].
  113. init_https_dispatch() ->
  114. init_http_dispatch().
  115. %% http.
  116. chunked_response(Config) ->
  117. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "chunked_handler\r\nworks fine!"}} =
  118. httpc:request(build_url("/chunked_response", Config)).
  119. headers_dupe(Config) ->
  120. {port, Port} = lists:keyfind(port, 1, Config),
  121. {ok, Socket} = gen_tcp:connect("localhost", Port,
  122. [binary, {active, false}, {packet, raw}]),
  123. ok = gen_tcp:send(Socket, "GET /headers/dupe HTTP/1.1\r\n"
  124. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  125. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  126. {_Start, _Length} = binary:match(Data, <<"Connection: close">>),
  127. nomatch = binary:match(Data, <<"Connection: keep-alive">>),
  128. {error, closed} = gen_tcp:recv(Socket, 0, 1000).
  129. headers_huge(Config) ->
  130. Cookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  131. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _N <- lists:seq(1, 40)]),
  132. {_Packet, 200} = raw_req(["GET / HTTP/1.0\r\nHost: localhost\r\n"
  133. "Set-Cookie: ", Cookie, "\r\n\r\n"], Config).
  134. keepalive_nl(Config) ->
  135. {port, Port} = lists:keyfind(port, 1, Config),
  136. {ok, Socket} = gen_tcp:connect("localhost", Port,
  137. [binary, {active, false}, {packet, raw}]),
  138. ok = keepalive_nl_loop(Socket, 100),
  139. ok = gen_tcp:close(Socket).
  140. keepalive_nl_loop(_Socket, 0) ->
  141. ok;
  142. keepalive_nl_loop(Socket, N) ->
  143. ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
  144. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  145. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  146. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  147. nomatch = binary:match(Data, <<"Connection: close">>),
  148. ok = gen_tcp:send(Socket, "\r\n"), %% extra nl
  149. keepalive_nl_loop(Socket, N - 1).
  150. nc_rand(Config) ->
  151. nc_reqs(Config, "/dev/urandom").
  152. nc_zero(Config) ->
  153. nc_reqs(Config, "/dev/zero").
  154. nc_reqs(Config, Input) ->
  155. Cat = os:find_executable("cat"),
  156. Nc = os:find_executable("nc"),
  157. case {Cat, Nc} of
  158. {false, _} ->
  159. {skip, {notfound, cat}};
  160. {_, false} ->
  161. {skip, {notfound, nc}};
  162. _Good ->
  163. %% Throw garbage at the server then check if it's still up.
  164. {port, Port} = lists:keyfind(port, 1, Config),
  165. [nc_run_req(Port, Input) || _N <- lists:seq(1, 100)],
  166. Packet = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n",
  167. {Packet, 200} = raw_req(Packet, Config)
  168. end.
  169. nc_run_req(Port, Input) ->
  170. os:cmd("cat " ++ Input ++ " | nc localhost " ++ integer_to_list(Port)).
  171. pipeline(Config) ->
  172. {port, Port} = lists:keyfind(port, 1, Config),
  173. {ok, Socket} = gen_tcp:connect("localhost", Port,
  174. [binary, {active, false}, {packet, raw}]),
  175. ok = gen_tcp:send(Socket,
  176. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  177. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  178. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  179. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  180. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"),
  181. Data = pipeline_recv(Socket, <<>>),
  182. Reqs = binary:split(Data, << "\r\n\r\nhttp_handler" >>, [global, trim]),
  183. 5 = length(Reqs),
  184. pipeline_check(Reqs).
  185. pipeline_check([]) ->
  186. ok;
  187. pipeline_check([Req|Tail]) ->
  188. << "HTTP/1.1 200", _Rest/bits >> = Req,
  189. pipeline_check(Tail).
  190. pipeline_recv(Socket, SoFar) ->
  191. case gen_tcp:recv(Socket, 0, 6000) of
  192. {ok, Data} ->
  193. pipeline_recv(Socket, << SoFar/binary, Data/binary >>);
  194. {error, closed} ->
  195. ok = gen_tcp:close(Socket),
  196. SoFar
  197. end.
  198. raw_req(Packet, Config) ->
  199. {port, Port} = lists:keyfind(port, 1, Config),
  200. {ok, Socket} = gen_tcp:connect("localhost", Port,
  201. [binary, {active, false}, {packet, raw}]),
  202. ok = gen_tcp:send(Socket, Packet),
  203. Res = case gen_tcp:recv(Socket, 0, 6000) of
  204. {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>} ->
  205. list_to_integer(binary_to_list(Str));
  206. {error, Reason} ->
  207. Reason
  208. end,
  209. gen_tcp:close(Socket),
  210. {Packet, Res}.
  211. raw(Config) ->
  212. Huge = [$0 || _N <- lists:seq(1, 5000)],
  213. Tests = [
  214. {"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
  215. {"\n", 400},
  216. {"Garbage\r\n\r\n", 400},
  217. {"\r\n\r\n\r\n\r\n\r\n\r\n", 400},
  218. {"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400},
  219. {"", closed},
  220. {"\r\n", closed},
  221. {"\r\n\r\n", closed},
  222. {"GET / HTTP/1.1", closed},
  223. {"GET / HTTP/1.1\r\n", 408},
  224. {"GET / HTTP/1.1\r\nHost: localhost", 408},
  225. {"GET / HTTP/1.1\r\nHost: localhost\r\n", 408},
  226. {"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408},
  227. {"GET http://localhost/ HTTP/1.1\r\n\r\n", 501},
  228. {"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505},
  229. {"GET /init_shutdown HTTP/1.1\r\nHost: localhost\r\n\r\n", 666},
  230. {"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n\r\n", 102},
  231. {Huge, 413},
  232. {"GET / HTTP/1.1\r\n" ++ Huge, 413}
  233. ],
  234. [{Packet, StatusCode} = raw_req(Packet, Config)
  235. || {Packet, StatusCode} <- Tests].
  236. %% This test makes sure the code works even if we wait for a reply
  237. %% before sending the third challenge key in the GET body.
  238. %%
  239. %% This ensures that Cowboy will work fine with proxies on hixie.
  240. ws0(Config) ->
  241. {port, Port} = lists:keyfind(port, 1, Config),
  242. {ok, Socket} = gen_tcp:connect("localhost", Port,
  243. [binary, {active, false}, {packet, raw}]),
  244. ok = gen_tcp:send(Socket,
  245. "GET /websocket HTTP/1.1\r\n"
  246. "Host: localhost\r\n"
  247. "Connection: Upgrade\r\n"
  248. "Upgrade: WebSocket\r\n"
  249. "Origin: http://localhost\r\n"
  250. "Sec-Websocket-Key1: Y\" 4 1Lj!957b8@0H756!i\r\n"
  251. "Sec-Websocket-Key2: 1711 M;4\\74 80<6\r\n"
  252. "\r\n"),
  253. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  254. {ok, {http_response, {1, 1}, 101, "WebSocket Protocol Handshake"}, Rest}
  255. = erlang:decode_packet(http, Handshake, []),
  256. [Headers, <<>>] = websocket_headers(
  257. erlang:decode_packet(httph, Rest, []), []),
  258. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  259. {'Upgrade', "WebSocket"} = lists:keyfind('Upgrade', 1, Headers),
  260. {"sec-websocket-location", "ws://localhost/websocket"}
  261. = lists:keyfind("sec-websocket-location", 1, Headers),
  262. {"sec-websocket-origin", "http://localhost"}
  263. = lists:keyfind("sec-websocket-origin", 1, Headers),
  264. ok = gen_tcp:send(Socket, <<15,245,8,18,2,204,133,33>>),
  265. {ok, Body} = gen_tcp:recv(Socket, 0, 6000),
  266. <<169,244,191,103,146,33,149,59,74,104,67,5,99,118,171,236>> = Body,
  267. ok = gen_tcp:send(Socket, << 0, "client_msg", 255 >>),
  268. {ok, << 0, "client_msg", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  269. {ok, << 0, "websocket_init", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  270. {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  271. {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  272. {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  273. ok = gen_tcp:send(Socket, << 255, 0 >>),
  274. {ok, << 255, 0 >>} = gen_tcp:recv(Socket, 0, 6000),
  275. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  276. ok.
  277. ws8(Config) ->
  278. {port, Port} = lists:keyfind(port, 1, Config),
  279. {ok, Socket} = gen_tcp:connect("localhost", Port,
  280. [binary, {active, false}, {packet, raw}]),
  281. ok = gen_tcp:send(Socket, [
  282. "GET /websocket HTTP/1.1\r\n"
  283. "Host: localhost\r\n"
  284. "Connection: Upgrade\r\n"
  285. "Upgrade: websocket\r\n"
  286. "Sec-WebSocket-Origin: http://localhost\r\n"
  287. "Sec-WebSocket-Version: 8\r\n"
  288. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  289. "\r\n"]),
  290. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  291. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  292. = erlang:decode_packet(http, Handshake, []),
  293. [Headers, <<>>] = websocket_headers(
  294. erlang:decode_packet(httph, Rest, []), []),
  295. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  296. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  297. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  298. = lists:keyfind("sec-websocket-accept", 1, Headers),
  299. ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
  300. 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
  301. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  302. = gen_tcp:recv(Socket, 0, 6000),
  303. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
  304. = gen_tcp:recv(Socket, 0, 6000),
  305. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  306. = gen_tcp:recv(Socket, 0, 6000),
  307. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  308. = gen_tcp:recv(Socket, 0, 6000),
  309. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  310. = gen_tcp:recv(Socket, 0, 6000),
  311. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping
  312. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
  313. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close
  314. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  315. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  316. ok.
  317. ws8_single_bytes(Config) ->
  318. {port, Port} = lists:keyfind(port, 1, Config),
  319. {ok, Socket} = gen_tcp:connect("localhost", Port,
  320. [binary, {active, false}, {packet, raw}]),
  321. ok = gen_tcp:send(Socket, [
  322. "GET /websocket HTTP/1.1\r\n"
  323. "Host: localhost\r\n"
  324. "Connection: Upgrade\r\n"
  325. "Upgrade: websocket\r\n"
  326. "Sec-WebSocket-Origin: http://localhost\r\n"
  327. "Sec-WebSocket-Version: 8\r\n"
  328. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  329. "\r\n"]),
  330. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  331. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  332. = erlang:decode_packet(http, Handshake, []),
  333. [Headers, <<>>] = websocket_headers(
  334. erlang:decode_packet(httph, Rest, []), []),
  335. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  336. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  337. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  338. = lists:keyfind("sec-websocket-accept", 1, Headers),
  339. ok = gen_tcp:send(Socket, << 16#81 >>), %% send one byte
  340. ok = timer:sleep(100), %% sleep for a period
  341. ok = gen_tcp:send(Socket, << 16#85 >>), %% send another and so on
  342. ok = timer:sleep(100),
  343. ok = gen_tcp:send(Socket, << 16#37 >>),
  344. ok = timer:sleep(100),
  345. ok = gen_tcp:send(Socket, << 16#fa >>),
  346. ok = timer:sleep(100),
  347. ok = gen_tcp:send(Socket, << 16#21 >>),
  348. ok = timer:sleep(100),
  349. ok = gen_tcp:send(Socket, << 16#3d >>),
  350. ok = timer:sleep(100),
  351. ok = gen_tcp:send(Socket, << 16#7f >>),
  352. ok = timer:sleep(100),
  353. ok = gen_tcp:send(Socket, << 16#9f >>),
  354. ok = timer:sleep(100),
  355. ok = gen_tcp:send(Socket, << 16#4d >>),
  356. ok = timer:sleep(100),
  357. ok = gen_tcp:send(Socket, << 16#51 >>),
  358. ok = timer:sleep(100),
  359. ok = gen_tcp:send(Socket, << 16#58 >>),
  360. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
  361. = gen_tcp:recv(Socket, 0, 6000),
  362. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  363. = gen_tcp:recv(Socket, 0, 6000),
  364. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  365. = gen_tcp:recv(Socket, 0, 6000),
  366. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  367. = gen_tcp:recv(Socket, 0, 6000),
  368. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  369. = gen_tcp:recv(Socket, 0, 6000),
  370. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping
  371. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
  372. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close
  373. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  374. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  375. ok.
  376. ws_timeout_hibernate(Config) ->
  377. {port, Port} = lists:keyfind(port, 1, Config),
  378. {ok, Socket} = gen_tcp:connect("localhost", Port,
  379. [binary, {active, false}, {packet, raw}]),
  380. ok = gen_tcp:send(Socket, [
  381. "GET /ws_timeout_hibernate HTTP/1.1\r\n"
  382. "Host: localhost\r\n"
  383. "Connection: Upgrade\r\n"
  384. "Upgrade: websocket\r\n"
  385. "Sec-WebSocket-Origin: http://localhost\r\n"
  386. "Sec-WebSocket-Version: 8\r\n"
  387. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  388. "\r\n"]),
  389. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  390. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  391. = erlang:decode_packet(http, Handshake, []),
  392. [Headers, <<>>] = websocket_headers(
  393. erlang:decode_packet(httph, Rest, []), []),
  394. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  395. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  396. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  397. = lists:keyfind("sec-websocket-accept", 1, Headers),
  398. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  399. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  400. ok.
  401. ws8_init_shutdown(Config) ->
  402. {port, Port} = lists:keyfind(port, 1, Config),
  403. {ok, Socket} = gen_tcp:connect("localhost", Port,
  404. [binary, {active, false}, {packet, raw}]),
  405. ok = gen_tcp:send(Socket, [
  406. "GET /ws_init_shutdown HTTP/1.1\r\n"
  407. "Host: localhost\r\n"
  408. "Connection: Upgrade\r\n"
  409. "Upgrade: websocket\r\n"
  410. "Sec-WebSocket-Origin: http://localhost\r\n"
  411. "Sec-WebSocket-Version: 8\r\n"
  412. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  413. "\r\n"]),
  414. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  415. {ok, {http_response, {1, 1}, 403, "Forbidden"}, _Rest}
  416. = erlang:decode_packet(http, Handshake, []),
  417. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  418. ok.
  419. ws13(Config) ->
  420. {port, Port} = lists:keyfind(port, 1, Config),
  421. {ok, Socket} = gen_tcp:connect("localhost", Port,
  422. [binary, {active, false}, {packet, raw}]),
  423. ok = gen_tcp:send(Socket, [
  424. "GET /websocket HTTP/1.1\r\n"
  425. "Host: localhost\r\n"
  426. "Connection: Upgrade\r\n"
  427. "Origin: http://localhost\r\n"
  428. "Sec-WebSocket-Version: 13\r\n"
  429. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  430. "Upgrade: websocket\r\n"
  431. "\r\n"]),
  432. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  433. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  434. = erlang:decode_packet(http, Handshake, []),
  435. [Headers, <<>>] = websocket_headers(
  436. erlang:decode_packet(httph, Rest, []), []),
  437. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  438. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  439. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  440. = lists:keyfind("sec-websocket-accept", 1, Headers),
  441. ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
  442. 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
  443. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  444. = gen_tcp:recv(Socket, 0, 6000),
  445. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
  446. = gen_tcp:recv(Socket, 0, 6000),
  447. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  448. = gen_tcp:recv(Socket, 0, 6000),
  449. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  450. = gen_tcp:recv(Socket, 0, 6000),
  451. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  452. = gen_tcp:recv(Socket, 0, 6000),
  453. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping
  454. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
  455. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close
  456. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  457. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  458. ok.
  459. websocket_headers({ok, http_eoh, Rest}, Acc) ->
  460. [Acc, Rest];
  461. websocket_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) ->
  462. F = fun(S) when is_atom(S) -> S; (S) -> string:to_lower(S) end,
  463. websocket_headers(erlang:decode_packet(httph, Rest, []),
  464. [{F(Key), Value}|Acc]).
  465. set_resp_header(Config) ->
  466. {port, Port} = lists:keyfind(port, 1, Config),
  467. {ok, Socket} = gen_tcp:connect("localhost", Port,
  468. [binary, {active, false}, {packet, raw}]),
  469. ok = gen_tcp:send(Socket, "GET /set_resp/header HTTP/1.1\r\n"
  470. "Host: localhost\r\nConnection: close\r\n\r\n"),
  471. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  472. {_Start, _Length} = binary:match(Data, <<"Vary: Accept">>).
  473. set_resp_overwrite(Config) ->
  474. {port, Port} = lists:keyfind(port, 1, Config),
  475. {ok, Socket} = gen_tcp:connect("localhost", Port,
  476. [binary, {active, false}, {packet, raw}]),
  477. ok = gen_tcp:send(Socket, "GET /set_resp/overwrite HTTP/1.1\r\n"
  478. "Host: localhost\r\nConnection: close\r\n\r\n"),
  479. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  480. {_Start, _Length} = binary:match(Data, <<"Server: DesireDrive/1.0">>).
  481. set_resp_body(Config) ->
  482. {port, Port} = lists:keyfind(port, 1, Config),
  483. {ok, Socket} = gen_tcp:connect("localhost", Port,
  484. [binary, {active, false}, {packet, raw}]),
  485. ok = gen_tcp:send(Socket, "GET /set_resp/body HTTP/1.1\r\n"
  486. "Host: localhost\r\nConnection: close\r\n\r\n"),
  487. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  488. {_Start, _Length} = binary:match(Data, <<"\r\n\r\n"
  489. "A flameless dance does not equal a cycle">>).
  490. response_as_req(Config) ->
  491. Packet =
  492. "HTTP/1.0 302 Found
  493. Location: http://www.google.co.il/
  494. Cache-Control: private
  495. Content-Type: text/html; charset=UTF-8
  496. 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
  497. Date: Sun, 04 Dec 2011 15:55:01 GMT
  498. Server: gws
  499. Content-Length: 221
  500. X-XSS-Protection: 1; mode=block
  501. X-Frame-Options: SAMEORIGIN
  502. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  503. <TITLE>302 Moved</TITLE></HEAD><BODY>
  504. <H1>302 Moved</H1>
  505. The document has moved
  506. <A HREF=\"http://www.google.co.il/\">here</A>.
  507. </BODY></HTML>",
  508. {Packet, 400} = raw_req(Packet, Config).
  509. %% http and https.
  510. build_url(Path, Config) ->
  511. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  512. {port, Port} = lists:keyfind(port, 1, Config),
  513. Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
  514. http_200(Config) ->
  515. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
  516. httpc:request(build_url("/", Config)).
  517. http_404(Config) ->
  518. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  519. httpc:request(build_url("/not/found", Config)).
  520. %% misc.
  521. http_10_hostless(Config) ->
  522. Packet = "GET / HTTP/1.0\r\n\r\n",
  523. {Packet, 200} = raw_req(Packet, Config).
  524. %% rest.
  525. rest_simple(Config) ->
  526. Packet = "GET /simple HTTP/1.1\r\nHost: localhost\r\n\r\n",
  527. {Packet, 200} = raw_req(Packet, Config).