http_SUITE.erl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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]). %% http.
  22. -export([http_200/1, http_404/1]). %% http and https.
  23. -export([http_10_hostless/1]). %% misc.
  24. %% ct.
  25. all() ->
  26. [{group, http}, {group, https}, {group, misc}].
  27. groups() ->
  28. BaseTests = [http_200, http_404],
  29. [{http, [], [chunked_response, headers_dupe, headers_huge,
  30. keepalive_nl, nc_rand, nc_zero, pipeline, raw,
  31. ws0, ws8, ws8_single_bytes, ws8_init_shutdown, ws13,
  32. ws_timeout_hibernate] ++ BaseTests},
  33. {https, [], BaseTests}, {misc, [], [http_10_hostless]}].
  34. init_per_suite(Config) ->
  35. application:start(inets),
  36. application:start(cowboy),
  37. Config.
  38. end_per_suite(_Config) ->
  39. application:stop(cowboy),
  40. application:stop(inets),
  41. ok.
  42. init_per_group(http, Config) ->
  43. Port = 33080,
  44. cowboy:start_listener(http, 100,
  45. cowboy_tcp_transport, [{port, Port}],
  46. cowboy_http_protocol, [{dispatch, init_http_dispatch()}]
  47. ),
  48. [{scheme, "http"}, {port, Port}|Config];
  49. init_per_group(https, Config) ->
  50. Port = 33081,
  51. application:start(crypto),
  52. application:start(public_key),
  53. application:start(ssl),
  54. DataDir = ?config(data_dir, Config),
  55. cowboy:start_listener(https, 100,
  56. cowboy_ssl_transport, [
  57. {port, Port}, {certfile, DataDir ++ "cert.pem"},
  58. {keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}],
  59. cowboy_http_protocol, [{dispatch, init_https_dispatch()}]
  60. ),
  61. [{scheme, "https"}, {port, Port}|Config];
  62. init_per_group(misc, Config) ->
  63. Port = 33082,
  64. cowboy:start_listener(misc, 100,
  65. cowboy_tcp_transport, [{port, Port}],
  66. cowboy_http_protocol, [{dispatch, [{'_', [
  67. {[], http_handler, []}
  68. ]}]}]),
  69. [{port, Port}|Config].
  70. end_per_group(https, _Config) ->
  71. cowboy:stop_listener(https),
  72. application:stop(ssl),
  73. application:stop(public_key),
  74. application:stop(crypto),
  75. ok;
  76. end_per_group(Listener, _Config) ->
  77. cowboy:stop_listener(Listener),
  78. ok.
  79. %% Dispatch configuration.
  80. init_http_dispatch() ->
  81. [
  82. {[<<"localhost">>], [
  83. {[<<"chunked_response">>], chunked_handler, []},
  84. {[<<"websocket">>], websocket_handler, []},
  85. {[<<"ws_timeout_hibernate">>], ws_timeout_hibernate_handler, []},
  86. {[<<"ws_init_shutdown">>], websocket_handler_init_shutdown, []},
  87. {[<<"init_shutdown">>], http_handler_init_shutdown, []},
  88. {[<<"long_polling">>], http_handler_long_polling, []},
  89. {[<<"headers">>, <<"dupe">>], http_handler,
  90. [{headers, [{<<"Connection">>, <<"close">>}]}]},
  91. {[], http_handler, []}
  92. ]}
  93. ].
  94. init_https_dispatch() ->
  95. init_http_dispatch().
  96. %% http.
  97. chunked_response(Config) ->
  98. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "chunked_handler\r\nworks fine!"}} =
  99. httpc:request(build_url("/chunked_response", Config)).
  100. headers_dupe(Config) ->
  101. {port, Port} = lists:keyfind(port, 1, Config),
  102. {ok, Socket} = gen_tcp:connect("localhost", Port,
  103. [binary, {active, false}, {packet, raw}]),
  104. ok = gen_tcp:send(Socket, "GET /headers/dupe HTTP/1.1\r\n"
  105. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  106. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  107. {_Start, _Length} = binary:match(Data, <<"Connection: close">>),
  108. nomatch = binary:match(Data, <<"Connection: keep-alive">>),
  109. {error, closed} = gen_tcp:recv(Socket, 0, 1000).
  110. headers_huge(Config) ->
  111. Cookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  112. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _N <- lists:seq(1, 40)]),
  113. {_Packet, 200} = raw_req(["GET / HTTP/1.0\r\nHost: localhost\r\n"
  114. "Set-Cookie: ", Cookie, "\r\n\r\n"], Config).
  115. keepalive_nl(Config) ->
  116. {port, Port} = lists:keyfind(port, 1, Config),
  117. {ok, Socket} = gen_tcp:connect("localhost", Port,
  118. [binary, {active, false}, {packet, raw}]),
  119. ok = keepalive_nl_loop(Socket, 100),
  120. ok = gen_tcp:close(Socket).
  121. keepalive_nl_loop(_Socket, 0) ->
  122. ok;
  123. keepalive_nl_loop(Socket, N) ->
  124. ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
  125. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  126. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  127. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  128. nomatch = binary:match(Data, <<"Connection: close">>),
  129. ok = gen_tcp:send(Socket, "\r\n"), %% extra nl
  130. keepalive_nl_loop(Socket, N - 1).
  131. nc_rand(Config) ->
  132. nc_reqs(Config, "/dev/urandom").
  133. nc_zero(Config) ->
  134. nc_reqs(Config, "/dev/zero").
  135. nc_reqs(Config, Input) ->
  136. Cat = os:find_executable("cat"),
  137. Nc = os:find_executable("nc"),
  138. case {Cat, Nc} of
  139. {false, _} ->
  140. {skip, {notfound, cat}};
  141. {_, false} ->
  142. {skip, {notfound, nc}};
  143. _Good ->
  144. %% Throw garbage at the server then check if it's still up.
  145. {port, Port} = lists:keyfind(port, 1, Config),
  146. [nc_run_req(Port, Input) || _N <- lists:seq(1, 100)],
  147. Packet = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n",
  148. {Packet, 200} = raw_req(Packet, Config)
  149. end.
  150. nc_run_req(Port, Input) ->
  151. os:cmd("cat " ++ Input ++ " | nc localhost " ++ integer_to_list(Port)).
  152. pipeline(Config) ->
  153. {port, Port} = lists:keyfind(port, 1, Config),
  154. {ok, Socket} = gen_tcp:connect("localhost", Port,
  155. [binary, {active, false}, {packet, raw}]),
  156. ok = gen_tcp:send(Socket,
  157. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  158. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  159. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  160. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  161. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"),
  162. Data = pipeline_recv(Socket, <<>>),
  163. Reqs = binary:split(Data, << "\r\n\r\nhttp_handler" >>, [global, trim]),
  164. 5 = length(Reqs),
  165. pipeline_check(Reqs).
  166. pipeline_check([]) ->
  167. ok;
  168. pipeline_check([Req|Tail]) ->
  169. << "HTTP/1.1 200", _Rest/bits >> = Req,
  170. pipeline_check(Tail).
  171. pipeline_recv(Socket, SoFar) ->
  172. case gen_tcp:recv(Socket, 0, 6000) of
  173. {ok, Data} ->
  174. pipeline_recv(Socket, << SoFar/binary, Data/binary >>);
  175. {error, closed} ->
  176. ok = gen_tcp:close(Socket),
  177. SoFar
  178. end.
  179. raw_req(Packet, Config) ->
  180. {port, Port} = lists:keyfind(port, 1, Config),
  181. {ok, Socket} = gen_tcp:connect("localhost", Port,
  182. [binary, {active, false}, {packet, raw}]),
  183. ok = gen_tcp:send(Socket, Packet),
  184. Res = case gen_tcp:recv(Socket, 0, 6000) of
  185. {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>} ->
  186. list_to_integer(binary_to_list(Str));
  187. {error, Reason} ->
  188. Reason
  189. end,
  190. gen_tcp:close(Socket),
  191. {Packet, Res}.
  192. raw(Config) ->
  193. Huge = [$0 || _N <- lists:seq(1, 5000)],
  194. Tests = [
  195. {"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
  196. {"\n", 400},
  197. {"Garbage\r\n\r\n", 400},
  198. {"\r\n\r\n\r\n\r\n\r\n\r\n", 400},
  199. {"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400},
  200. {"", closed},
  201. {"\r\n", closed},
  202. {"\r\n\r\n", closed},
  203. {"GET / HTTP/1.1", closed},
  204. {"GET / HTTP/1.1\r\n", 408},
  205. {"GET / HTTP/1.1\r\nHost: localhost", 408},
  206. {"GET / HTTP/1.1\r\nHost: localhost\r\n", 408},
  207. {"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408},
  208. {"GET http://localhost/ HTTP/1.1\r\n\r\n", 501},
  209. {"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505},
  210. {"GET /init_shutdown HTTP/1.1\r\nHost: localhost\r\n\r\n", 666},
  211. {"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n\r\n", 102},
  212. {Huge, 413},
  213. {"GET / HTTP/1.1\r\n" ++ Huge, 413}
  214. ],
  215. [{Packet, StatusCode} = raw_req(Packet, Config)
  216. || {Packet, StatusCode} <- Tests].
  217. %% This test makes sure the code works even if we wait for a reply
  218. %% before sending the third challenge key in the GET body.
  219. %%
  220. %% This ensures that Cowboy will work fine with proxies on hixie.
  221. ws0(Config) ->
  222. {port, Port} = lists:keyfind(port, 1, Config),
  223. {ok, Socket} = gen_tcp:connect("localhost", Port,
  224. [binary, {active, false}, {packet, raw}]),
  225. ok = gen_tcp:send(Socket,
  226. "GET /websocket HTTP/1.1\r\n"
  227. "Host: localhost\r\n"
  228. "Connection: Upgrade\r\n"
  229. "Upgrade: WebSocket\r\n"
  230. "Origin: http://localhost\r\n"
  231. "Sec-Websocket-Key1: Y\" 4 1Lj!957b8@0H756!i\r\n"
  232. "Sec-Websocket-Key2: 1711 M;4\\74 80<6\r\n"
  233. "\r\n"),
  234. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  235. {ok, {http_response, {1, 1}, 101, "WebSocket Protocol Handshake"}, Rest}
  236. = erlang:decode_packet(http, Handshake, []),
  237. [Headers, <<>>] = websocket_headers(
  238. erlang:decode_packet(httph, Rest, []), []),
  239. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  240. {'Upgrade', "WebSocket"} = lists:keyfind('Upgrade', 1, Headers),
  241. {"sec-websocket-location", "ws://localhost/websocket"}
  242. = lists:keyfind("sec-websocket-location", 1, Headers),
  243. {"sec-websocket-origin", "http://localhost"}
  244. = lists:keyfind("sec-websocket-origin", 1, Headers),
  245. ok = gen_tcp:send(Socket, <<15,245,8,18,2,204,133,33>>),
  246. {ok, Body} = gen_tcp:recv(Socket, 0, 6000),
  247. <<169,244,191,103,146,33,149,59,74,104,67,5,99,118,171,236>> = Body,
  248. ok = gen_tcp:send(Socket, << 0, "client_msg", 255 >>),
  249. {ok, << 0, "client_msg", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  250. {ok, << 0, "websocket_init", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  251. {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  252. {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  253. {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  254. ok = gen_tcp:send(Socket, << 255, 0 >>),
  255. {ok, << 255, 0 >>} = gen_tcp:recv(Socket, 0, 6000),
  256. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  257. ok.
  258. ws8(Config) ->
  259. {port, Port} = lists:keyfind(port, 1, Config),
  260. {ok, Socket} = gen_tcp:connect("localhost", Port,
  261. [binary, {active, false}, {packet, raw}]),
  262. ok = gen_tcp:send(Socket, [
  263. "GET /websocket HTTP/1.1\r\n"
  264. "Host: localhost\r\n"
  265. "Connection: Upgrade\r\n"
  266. "Upgrade: websocket\r\n"
  267. "Sec-WebSocket-Origin: http://localhost\r\n"
  268. "Sec-WebSocket-Version: 8\r\n"
  269. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  270. "\r\n"]),
  271. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  272. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  273. = erlang:decode_packet(http, Handshake, []),
  274. [Headers, <<>>] = websocket_headers(
  275. erlang:decode_packet(httph, Rest, []), []),
  276. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  277. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  278. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  279. = lists:keyfind("sec-websocket-accept", 1, Headers),
  280. ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
  281. 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
  282. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  283. = gen_tcp:recv(Socket, 0, 6000),
  284. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
  285. = gen_tcp:recv(Socket, 0, 6000),
  286. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  287. = gen_tcp:recv(Socket, 0, 6000),
  288. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  289. = gen_tcp:recv(Socket, 0, 6000),
  290. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  291. = gen_tcp:recv(Socket, 0, 6000),
  292. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping
  293. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
  294. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close
  295. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  296. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  297. ok.
  298. ws8_single_bytes(Config) ->
  299. {port, Port} = lists:keyfind(port, 1, Config),
  300. {ok, Socket} = gen_tcp:connect("localhost", Port,
  301. [binary, {active, false}, {packet, raw}]),
  302. ok = gen_tcp:send(Socket, [
  303. "GET /websocket HTTP/1.1\r\n"
  304. "Host: localhost\r\n"
  305. "Connection: Upgrade\r\n"
  306. "Upgrade: websocket\r\n"
  307. "Sec-WebSocket-Origin: http://localhost\r\n"
  308. "Sec-WebSocket-Version: 8\r\n"
  309. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  310. "\r\n"]),
  311. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  312. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  313. = erlang:decode_packet(http, Handshake, []),
  314. [Headers, <<>>] = websocket_headers(
  315. erlang:decode_packet(httph, Rest, []), []),
  316. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  317. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  318. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  319. = lists:keyfind("sec-websocket-accept", 1, Headers),
  320. ok = gen_tcp:send(Socket, << 16#81 >>), %% send one byte
  321. ok = timer:sleep(100), %% sleep for a period
  322. ok = gen_tcp:send(Socket, << 16#85 >>), %% send another and so on
  323. ok = timer:sleep(100),
  324. ok = gen_tcp:send(Socket, << 16#37 >>),
  325. ok = timer:sleep(100),
  326. ok = gen_tcp:send(Socket, << 16#fa >>),
  327. ok = timer:sleep(100),
  328. ok = gen_tcp:send(Socket, << 16#21 >>),
  329. ok = timer:sleep(100),
  330. ok = gen_tcp:send(Socket, << 16#3d >>),
  331. ok = timer:sleep(100),
  332. ok = gen_tcp:send(Socket, << 16#7f >>),
  333. ok = timer:sleep(100),
  334. ok = gen_tcp:send(Socket, << 16#9f >>),
  335. ok = timer:sleep(100),
  336. ok = gen_tcp:send(Socket, << 16#4d >>),
  337. ok = timer:sleep(100),
  338. ok = gen_tcp:send(Socket, << 16#51 >>),
  339. ok = timer:sleep(100),
  340. ok = gen_tcp:send(Socket, << 16#58 >>),
  341. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
  342. = gen_tcp:recv(Socket, 0, 6000),
  343. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  344. = gen_tcp:recv(Socket, 0, 6000),
  345. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  346. = gen_tcp:recv(Socket, 0, 6000),
  347. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  348. = gen_tcp:recv(Socket, 0, 6000),
  349. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  350. = gen_tcp:recv(Socket, 0, 6000),
  351. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping
  352. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
  353. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close
  354. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  355. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  356. ok.
  357. ws_timeout_hibernate(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, [
  362. "GET /ws_timeout_hibernate HTTP/1.1\r\n"
  363. "Host: localhost\r\n"
  364. "Connection: Upgrade\r\n"
  365. "Upgrade: websocket\r\n"
  366. "Sec-WebSocket-Origin: http://localhost\r\n"
  367. "Sec-WebSocket-Version: 8\r\n"
  368. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  369. "\r\n"]),
  370. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  371. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  372. = erlang:decode_packet(http, Handshake, []),
  373. [Headers, <<>>] = websocket_headers(
  374. erlang:decode_packet(httph, Rest, []), []),
  375. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  376. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  377. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  378. = lists:keyfind("sec-websocket-accept", 1, Headers),
  379. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  380. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  381. ok.
  382. ws8_init_shutdown(Config) ->
  383. {port, Port} = lists:keyfind(port, 1, Config),
  384. {ok, Socket} = gen_tcp:connect("localhost", Port,
  385. [binary, {active, false}, {packet, raw}]),
  386. ok = gen_tcp:send(Socket, [
  387. "GET /ws_init_shutdown HTTP/1.1\r\n"
  388. "Host: localhost\r\n"
  389. "Connection: Upgrade\r\n"
  390. "Upgrade: websocket\r\n"
  391. "Sec-WebSocket-Origin: http://localhost\r\n"
  392. "Sec-WebSocket-Version: 8\r\n"
  393. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  394. "\r\n"]),
  395. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  396. {ok, {http_response, {1, 1}, 403, "Forbidden"}, _Rest}
  397. = erlang:decode_packet(http, Handshake, []),
  398. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  399. ok.
  400. ws13(Config) ->
  401. {port, Port} = lists:keyfind(port, 1, Config),
  402. {ok, Socket} = gen_tcp:connect("localhost", Port,
  403. [binary, {active, false}, {packet, raw}]),
  404. ok = gen_tcp:send(Socket, [
  405. "GET /websocket HTTP/1.1\r\n"
  406. "Host: localhost\r\n"
  407. "Connection: Upgrade\r\n"
  408. "Origin: http://localhost\r\n"
  409. "Sec-WebSocket-Version: 13\r\n"
  410. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  411. "Upgrade: websocket\r\n"
  412. "\r\n"]),
  413. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  414. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  415. = erlang:decode_packet(http, Handshake, []),
  416. [Headers, <<>>] = websocket_headers(
  417. erlang:decode_packet(httph, Rest, []), []),
  418. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  419. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  420. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  421. = lists:keyfind("sec-websocket-accept", 1, Headers),
  422. ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
  423. 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
  424. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  425. = gen_tcp:recv(Socket, 0, 6000),
  426. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
  427. = gen_tcp:recv(Socket, 0, 6000),
  428. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  429. = gen_tcp:recv(Socket, 0, 6000),
  430. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  431. = gen_tcp:recv(Socket, 0, 6000),
  432. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  433. = gen_tcp:recv(Socket, 0, 6000),
  434. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping
  435. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
  436. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close
  437. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  438. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  439. ok.
  440. websocket_headers({ok, http_eoh, Rest}, Acc) ->
  441. [Acc, Rest];
  442. websocket_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) ->
  443. F = fun(S) when is_atom(S) -> S; (S) -> string:to_lower(S) end,
  444. websocket_headers(erlang:decode_packet(httph, Rest, []),
  445. [{F(Key), Value}|Acc]).
  446. %% http and https.
  447. build_url(Path, Config) ->
  448. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  449. {port, Port} = lists:keyfind(port, 1, Config),
  450. Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
  451. http_200(Config) ->
  452. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
  453. httpc:request(build_url("/", Config)).
  454. http_404(Config) ->
  455. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  456. httpc:request(build_url("/not/found", Config)).
  457. %% misc.
  458. http_10_hostless(Config) ->
  459. Packet = "GET / HTTP/1.0\r\n\r\n",
  460. {Packet, 200} = raw_req(Packet, Config).