http_SUITE.erl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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_keepalive/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, rest_keepalive]}].
  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. %% We try to send another HTTP request to make sure
  274. %% the server closed the request.
  275. ok = gen_tcp:send(Socket, [
  276. << 255, 0 >>, %% Close websocket command.
  277. "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n" %% Server should ignore it.
  278. ]),
  279. {ok, << 255, 0 >>} = gen_tcp:recv(Socket, 0, 6000),
  280. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  281. ok.
  282. ws8(Config) ->
  283. {port, Port} = lists:keyfind(port, 1, Config),
  284. {ok, Socket} = gen_tcp:connect("localhost", Port,
  285. [binary, {active, false}, {packet, raw}]),
  286. ok = gen_tcp:send(Socket, [
  287. "GET /websocket HTTP/1.1\r\n"
  288. "Host: localhost\r\n"
  289. "Connection: Upgrade\r\n"
  290. "Upgrade: websocket\r\n"
  291. "Sec-WebSocket-Origin: http://localhost\r\n"
  292. "Sec-WebSocket-Version: 8\r\n"
  293. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  294. "\r\n"]),
  295. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  296. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  297. = erlang:decode_packet(http, Handshake, []),
  298. [Headers, <<>>] = websocket_headers(
  299. erlang:decode_packet(httph, Rest, []), []),
  300. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  301. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  302. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  303. = lists:keyfind("sec-websocket-accept", 1, Headers),
  304. ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
  305. 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
  306. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  307. = gen_tcp:recv(Socket, 0, 6000),
  308. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
  309. = gen_tcp:recv(Socket, 0, 6000),
  310. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  311. = gen_tcp:recv(Socket, 0, 6000),
  312. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  313. = gen_tcp:recv(Socket, 0, 6000),
  314. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  315. = gen_tcp:recv(Socket, 0, 6000),
  316. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping
  317. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
  318. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close
  319. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  320. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  321. ok.
  322. ws8_single_bytes(Config) ->
  323. {port, Port} = lists:keyfind(port, 1, Config),
  324. {ok, Socket} = gen_tcp:connect("localhost", Port,
  325. [binary, {active, false}, {packet, raw}]),
  326. ok = gen_tcp:send(Socket, [
  327. "GET /websocket HTTP/1.1\r\n"
  328. "Host: localhost\r\n"
  329. "Connection: Upgrade\r\n"
  330. "Upgrade: websocket\r\n"
  331. "Sec-WebSocket-Origin: http://localhost\r\n"
  332. "Sec-WebSocket-Version: 8\r\n"
  333. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  334. "\r\n"]),
  335. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  336. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  337. = erlang:decode_packet(http, Handshake, []),
  338. [Headers, <<>>] = websocket_headers(
  339. erlang:decode_packet(httph, Rest, []), []),
  340. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  341. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  342. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  343. = lists:keyfind("sec-websocket-accept", 1, Headers),
  344. ok = gen_tcp:send(Socket, << 16#81 >>), %% send one byte
  345. ok = timer:sleep(100), %% sleep for a period
  346. ok = gen_tcp:send(Socket, << 16#85 >>), %% send another and so on
  347. ok = timer:sleep(100),
  348. ok = gen_tcp:send(Socket, << 16#37 >>),
  349. ok = timer:sleep(100),
  350. ok = gen_tcp:send(Socket, << 16#fa >>),
  351. ok = timer:sleep(100),
  352. ok = gen_tcp:send(Socket, << 16#21 >>),
  353. ok = timer:sleep(100),
  354. ok = gen_tcp:send(Socket, << 16#3d >>),
  355. ok = timer:sleep(100),
  356. ok = gen_tcp:send(Socket, << 16#7f >>),
  357. ok = timer:sleep(100),
  358. ok = gen_tcp:send(Socket, << 16#9f >>),
  359. ok = timer:sleep(100),
  360. ok = gen_tcp:send(Socket, << 16#4d >>),
  361. ok = timer:sleep(100),
  362. ok = gen_tcp:send(Socket, << 16#51 >>),
  363. ok = timer:sleep(100),
  364. ok = gen_tcp:send(Socket, << 16#58 >>),
  365. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
  366. = gen_tcp:recv(Socket, 0, 6000),
  367. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  368. = gen_tcp:recv(Socket, 0, 6000),
  369. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  370. = gen_tcp:recv(Socket, 0, 6000),
  371. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  372. = gen_tcp:recv(Socket, 0, 6000),
  373. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  374. = gen_tcp:recv(Socket, 0, 6000),
  375. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping
  376. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
  377. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close
  378. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  379. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  380. ok.
  381. ws_timeout_hibernate(Config) ->
  382. {port, Port} = lists:keyfind(port, 1, Config),
  383. {ok, Socket} = gen_tcp:connect("localhost", Port,
  384. [binary, {active, false}, {packet, raw}]),
  385. ok = gen_tcp:send(Socket, [
  386. "GET /ws_timeout_hibernate HTTP/1.1\r\n"
  387. "Host: localhost\r\n"
  388. "Connection: Upgrade\r\n"
  389. "Upgrade: websocket\r\n"
  390. "Sec-WebSocket-Origin: http://localhost\r\n"
  391. "Sec-WebSocket-Version: 8\r\n"
  392. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  393. "\r\n"]),
  394. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  395. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  396. = erlang:decode_packet(http, Handshake, []),
  397. [Headers, <<>>] = websocket_headers(
  398. erlang:decode_packet(httph, Rest, []), []),
  399. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  400. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  401. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  402. = lists:keyfind("sec-websocket-accept", 1, Headers),
  403. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  404. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  405. ok.
  406. ws8_init_shutdown(Config) ->
  407. {port, Port} = lists:keyfind(port, 1, Config),
  408. {ok, Socket} = gen_tcp:connect("localhost", Port,
  409. [binary, {active, false}, {packet, raw}]),
  410. ok = gen_tcp:send(Socket, [
  411. "GET /ws_init_shutdown HTTP/1.1\r\n"
  412. "Host: localhost\r\n"
  413. "Connection: Upgrade\r\n"
  414. "Upgrade: websocket\r\n"
  415. "Sec-WebSocket-Origin: http://localhost\r\n"
  416. "Sec-WebSocket-Version: 8\r\n"
  417. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  418. "\r\n"]),
  419. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  420. {ok, {http_response, {1, 1}, 403, "Forbidden"}, _Rest}
  421. = erlang:decode_packet(http, Handshake, []),
  422. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  423. ok.
  424. ws13(Config) ->
  425. {port, Port} = lists:keyfind(port, 1, Config),
  426. {ok, Socket} = gen_tcp:connect("localhost", Port,
  427. [binary, {active, false}, {packet, raw}]),
  428. ok = gen_tcp:send(Socket, [
  429. "GET /websocket HTTP/1.1\r\n"
  430. "Host: localhost\r\n"
  431. "Connection: Upgrade\r\n"
  432. "Origin: http://localhost\r\n"
  433. "Sec-WebSocket-Version: 13\r\n"
  434. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  435. "Upgrade: websocket\r\n"
  436. "\r\n"]),
  437. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  438. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  439. = erlang:decode_packet(http, Handshake, []),
  440. [Headers, <<>>] = websocket_headers(
  441. erlang:decode_packet(httph, Rest, []), []),
  442. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  443. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  444. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  445. = lists:keyfind("sec-websocket-accept", 1, Headers),
  446. ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
  447. 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
  448. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  449. = gen_tcp:recv(Socket, 0, 6000),
  450. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
  451. = gen_tcp:recv(Socket, 0, 6000),
  452. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  453. = gen_tcp:recv(Socket, 0, 6000),
  454. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  455. = gen_tcp:recv(Socket, 0, 6000),
  456. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  457. = gen_tcp:recv(Socket, 0, 6000),
  458. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping
  459. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
  460. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close
  461. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  462. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  463. ok.
  464. websocket_headers({ok, http_eoh, Rest}, Acc) ->
  465. [Acc, Rest];
  466. websocket_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) ->
  467. F = fun(S) when is_atom(S) -> S; (S) -> string:to_lower(S) end,
  468. websocket_headers(erlang:decode_packet(httph, Rest, []),
  469. [{F(Key), Value}|Acc]).
  470. set_resp_header(Config) ->
  471. {port, Port} = lists:keyfind(port, 1, Config),
  472. {ok, Socket} = gen_tcp:connect("localhost", Port,
  473. [binary, {active, false}, {packet, raw}]),
  474. ok = gen_tcp:send(Socket, "GET /set_resp/header HTTP/1.1\r\n"
  475. "Host: localhost\r\nConnection: close\r\n\r\n"),
  476. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  477. {_, _} = binary:match(Data, <<"Vary: Accept">>),
  478. {_, _} = binary:match(Data, <<"Set-Cookie: ">>).
  479. set_resp_overwrite(Config) ->
  480. {port, Port} = lists:keyfind(port, 1, Config),
  481. {ok, Socket} = gen_tcp:connect("localhost", Port,
  482. [binary, {active, false}, {packet, raw}]),
  483. ok = gen_tcp:send(Socket, "GET /set_resp/overwrite HTTP/1.1\r\n"
  484. "Host: localhost\r\nConnection: close\r\n\r\n"),
  485. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  486. {_Start, _Length} = binary:match(Data, <<"Server: DesireDrive/1.0">>).
  487. set_resp_body(Config) ->
  488. {port, Port} = lists:keyfind(port, 1, Config),
  489. {ok, Socket} = gen_tcp:connect("localhost", Port,
  490. [binary, {active, false}, {packet, raw}]),
  491. ok = gen_tcp:send(Socket, "GET /set_resp/body HTTP/1.1\r\n"
  492. "Host: localhost\r\nConnection: close\r\n\r\n"),
  493. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  494. {_Start, _Length} = binary:match(Data, <<"\r\n\r\n"
  495. "A flameless dance does not equal a cycle">>).
  496. response_as_req(Config) ->
  497. Packet =
  498. "HTTP/1.0 302 Found
  499. Location: http://www.google.co.il/
  500. Cache-Control: private
  501. Content-Type: text/html; charset=UTF-8
  502. 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
  503. Date: Sun, 04 Dec 2011 15:55:01 GMT
  504. Server: gws
  505. Content-Length: 221
  506. X-XSS-Protection: 1; mode=block
  507. X-Frame-Options: SAMEORIGIN
  508. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  509. <TITLE>302 Moved</TITLE></HEAD><BODY>
  510. <H1>302 Moved</H1>
  511. The document has moved
  512. <A HREF=\"http://www.google.co.il/\">here</A>.
  513. </BODY></HTML>",
  514. {Packet, 400} = raw_req(Packet, Config).
  515. %% http and https.
  516. build_url(Path, Config) ->
  517. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  518. {port, Port} = lists:keyfind(port, 1, Config),
  519. Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
  520. http_200(Config) ->
  521. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
  522. httpc:request(build_url("/", Config)).
  523. http_404(Config) ->
  524. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  525. httpc:request(build_url("/not/found", Config)).
  526. %% misc.
  527. http_10_hostless(Config) ->
  528. Packet = "GET / HTTP/1.0\r\n\r\n",
  529. {Packet, 200} = raw_req(Packet, Config).
  530. %% rest.
  531. rest_simple(Config) ->
  532. Packet = "GET /simple HTTP/1.1\r\nHost: localhost\r\n\r\n",
  533. {Packet, 200} = raw_req(Packet, Config).
  534. rest_keepalive(Config) ->
  535. {port, Port} = lists:keyfind(port, 1, Config),
  536. {ok, Socket} = gen_tcp:connect("localhost", Port,
  537. [binary, {active, false}, {packet, raw}]),
  538. ok = rest_keepalive_loop(Socket, 100),
  539. ok = gen_tcp:close(Socket).
  540. rest_keepalive_loop(_Socket, 0) ->
  541. ok;
  542. rest_keepalive_loop(Socket, N) ->
  543. ok = gen_tcp:send(Socket, "GET /simple HTTP/1.1\r\n"
  544. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  545. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  546. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  547. nomatch = binary:match(Data, <<"Connection: close">>),
  548. rest_keepalive_loop(Socket, N - 1).