http_SUITE.erl 20 KB

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