http_SUITE.erl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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, pipeline/1, raw/1,
  20. ws0/1, ws8/1, ws_timeout_hibernate/1]). %% http.
  21. -export([http_200/1, http_404/1]). %% http and https.
  22. -export([http_10_hostless/1]). %% misc.
  23. %% ct.
  24. all() ->
  25. [{group, http}, {group, https}, {group, misc}].
  26. groups() ->
  27. BaseTests = [http_200, http_404],
  28. [{http, [], [chunked_response, headers_dupe, headers_huge,
  29. keepalive_nl, nc_rand, pipeline, raw,
  30. ws0, ws8, ws_timeout_hibernate] ++ BaseTests},
  31. {https, [], BaseTests}, {misc, [], [http_10_hostless]}].
  32. init_per_suite(Config) ->
  33. application:start(inets),
  34. application:start(cowboy),
  35. Config.
  36. end_per_suite(_Config) ->
  37. application:stop(cowboy),
  38. application:stop(inets),
  39. ok.
  40. init_per_group(http, Config) ->
  41. Port = 33080,
  42. cowboy:start_listener(http, 100,
  43. cowboy_tcp_transport, [{port, Port}],
  44. cowboy_http_protocol, [{dispatch, init_http_dispatch()}]
  45. ),
  46. [{scheme, "http"}, {port, Port}|Config];
  47. init_per_group(https, Config) ->
  48. Port = 33081,
  49. application:start(crypto),
  50. application:start(public_key),
  51. application:start(ssl),
  52. DataDir = ?config(data_dir, Config),
  53. cowboy:start_listener(https, 100,
  54. cowboy_ssl_transport, [
  55. {port, Port}, {certfile, DataDir ++ "cert.pem"},
  56. {keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}],
  57. cowboy_http_protocol, [{dispatch, init_https_dispatch()}]
  58. ),
  59. [{scheme, "https"}, {port, Port}|Config];
  60. init_per_group(misc, Config) ->
  61. Port = 33082,
  62. cowboy:start_listener(misc, 100,
  63. cowboy_tcp_transport, [{port, Port}],
  64. cowboy_http_protocol, [{dispatch, [{'_', [
  65. {[], http_handler, []}
  66. ]}]}]),
  67. [{port, Port}|Config].
  68. end_per_group(https, _Config) ->
  69. cowboy:stop_listener(https),
  70. application:stop(ssl),
  71. application:stop(public_key),
  72. application:stop(crypto),
  73. ok;
  74. end_per_group(Listener, _Config) ->
  75. cowboy:stop_listener(Listener),
  76. ok.
  77. %% Dispatch configuration.
  78. init_http_dispatch() ->
  79. [
  80. {[<<"localhost">>], [
  81. {[<<"chunked_response">>], chunked_handler, []},
  82. {[<<"websocket">>], websocket_handler, []},
  83. {[<<"ws_timeout_hibernate">>], ws_timeout_hibernate_handler, []},
  84. {[<<"headers">>, <<"dupe">>], http_handler,
  85. [{headers, [{<<"Connection">>, <<"close">>}]}]},
  86. {[], http_handler, []}
  87. ]}
  88. ].
  89. init_https_dispatch() ->
  90. init_http_dispatch().
  91. %% http.
  92. chunked_response(Config) ->
  93. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "chunked_handler\r\nworks fine!"}} =
  94. httpc:request(build_url("/chunked_response", Config)).
  95. headers_dupe(Config) ->
  96. {port, Port} = lists:keyfind(port, 1, Config),
  97. {ok, Socket} = gen_tcp:connect("localhost", Port,
  98. [binary, {active, false}, {packet, raw}]),
  99. ok = gen_tcp:send(Socket, "GET /headers/dupe HTTP/1.1\r\n"
  100. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  101. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  102. {_Start, _Length} = binary:match(Data, <<"Connection: close">>),
  103. nomatch = binary:match(Data, <<"Connection: keep-alive">>),
  104. ok = gen_tcp:close(Socket).
  105. headers_huge(Config) ->
  106. Cookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  107. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _N <- lists:seq(1, 1000)]),
  108. {_Packet, 200} = raw_req(["GET / HTTP/1.0\r\nHost: localhost\r\n"
  109. "Set-Cookie: ", Cookie, "\r\n\r\n"], Config).
  110. keepalive_nl(Config) ->
  111. {port, Port} = lists:keyfind(port, 1, Config),
  112. {ok, Socket} = gen_tcp:connect("localhost", Port,
  113. [binary, {active, false}, {packet, raw}]),
  114. ok = keepalive_nl_loop(Socket, 100),
  115. ok = gen_tcp:close(Socket).
  116. keepalive_nl_loop(_Socket, 0) ->
  117. ok;
  118. keepalive_nl_loop(Socket, N) ->
  119. ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
  120. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  121. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  122. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  123. nomatch = binary:match(Data, <<"Connection: close">>),
  124. ok = gen_tcp:send(Socket, "\r\n"), %% extra nl
  125. keepalive_nl_loop(Socket, N - 1).
  126. nc_rand(Config) ->
  127. Cat = os:find_executable("cat"),
  128. Nc = os:find_executable("nc"),
  129. case {Cat, Nc} of
  130. {false, _} ->
  131. {skip, {notfound, cat}};
  132. {_, false} ->
  133. {skip, {notfound, nc}};
  134. _Good ->
  135. %% Throw garbage at the server then check if it's still up.
  136. {port, Port} = lists:keyfind(port, 1, Config),
  137. [nc_rand_run(Port) || _N <- lists:seq(1, 100)],
  138. Packet = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n",
  139. {Packet, 200} = raw_req(Packet, Config)
  140. end.
  141. nc_rand_run(Port) ->
  142. os:cmd("cat /dev/urandom | nc localhost " ++ integer_to_list(Port)).
  143. pipeline(Config) ->
  144. {port, Port} = lists:keyfind(port, 1, Config),
  145. {ok, Socket} = gen_tcp:connect("localhost", Port,
  146. [binary, {active, false}, {packet, raw}]),
  147. ok = gen_tcp:send(Socket,
  148. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  149. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  150. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  151. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  152. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"),
  153. Data = pipeline_recv(Socket, <<>>),
  154. Reqs = binary:split(Data, << "\r\n\r\nhttp_handler" >>, [global, trim]),
  155. 5 = length(Reqs),
  156. pipeline_check(Reqs).
  157. pipeline_check([]) ->
  158. ok;
  159. pipeline_check([Req|Tail]) ->
  160. << "HTTP/1.1 200", _Rest/bits >> = Req,
  161. pipeline_check(Tail).
  162. pipeline_recv(Socket, SoFar) ->
  163. case gen_tcp:recv(Socket, 0, 6000) of
  164. {ok, Data} ->
  165. pipeline_recv(Socket, << SoFar/binary, Data/binary >>);
  166. {error, closed} ->
  167. ok = gen_tcp:close(Socket),
  168. SoFar
  169. end.
  170. raw_req(Packet, Config) ->
  171. {port, Port} = lists:keyfind(port, 1, Config),
  172. {ok, Socket} = gen_tcp:connect("localhost", Port,
  173. [binary, {active, false}, {packet, raw}]),
  174. ok = gen_tcp:send(Socket, Packet),
  175. {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>}
  176. = gen_tcp:recv(Socket, 0, 6000),
  177. gen_tcp:close(Socket),
  178. {Packet, list_to_integer(binary_to_list(Str))}.
  179. raw(Config) ->
  180. Tests = [
  181. {"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
  182. {"\n", 400},
  183. {"Garbage\r\n\r\n", 400},
  184. {"\r\n\r\n\r\n\r\n\r\n\r\n", 400},
  185. {"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400},
  186. {"", 408},
  187. {"\r\n", 408},
  188. {"\r\n\r\n", 408},
  189. {"GET / HTTP/1.1", 408},
  190. {"GET / HTTP/1.1\r\n", 408},
  191. {"GET / HTTP/1.1\r\nHost: localhost", 408},
  192. {"GET / HTTP/1.1\r\nHost: localhost\r\n", 408},
  193. {"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408},
  194. {"GET http://localhost/ HTTP/1.1\r\n\r\n", 501},
  195. {"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505}
  196. ],
  197. [{Packet, StatusCode} = raw_req(Packet, Config)
  198. || {Packet, StatusCode} <- Tests].
  199. ws0(Config) ->
  200. {port, Port} = lists:keyfind(port, 1, Config),
  201. {ok, Socket} = gen_tcp:connect("localhost", Port,
  202. [binary, {active, false}, {packet, raw}]),
  203. ok = gen_tcp:send(Socket, [
  204. "GET /websocket HTTP/1.1\r\n"
  205. "Host: localhost\r\n"
  206. "Connection: Upgrade\r\n"
  207. "Upgrade: WebSocket\r\n"
  208. "Origin: http://localhost\r\n"
  209. "Sec-Websocket-Key1: Y\" 4 1Lj!957b8@0H756!i\r\n"
  210. "Sec-Websocket-Key2: 1711 M;4\\74 80<6\r\n"
  211. "\r\n", <<15,245,8,18,2,204,133,33>>]),
  212. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  213. {ok, {http_response, {1, 1}, 101, "WebSocket Protocol Handshake"}, Rest}
  214. = erlang:decode_packet(http, Handshake, []),
  215. [Headers, Body] = websocket_headers(
  216. erlang:decode_packet(httph, Rest, []), []),
  217. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  218. {'Upgrade', "WebSocket"} = lists:keyfind('Upgrade', 1, Headers),
  219. {"sec-websocket-location", "ws://localhost/websocket"}
  220. = lists:keyfind("sec-websocket-location", 1, Headers),
  221. {"sec-websocket-origin", "http://localhost"}
  222. = lists:keyfind("sec-websocket-origin", 1, Headers),
  223. <<169,244,191,103,146,33,149,59,74,104,67,5,99,118,171,236>> = Body,
  224. ok = gen_tcp:send(Socket, << 0, "client_msg", 255 >>),
  225. {ok, << 0, "client_msg", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  226. {ok, << 0, "websocket_init", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  227. {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  228. {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  229. {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  230. ok = gen_tcp:send(Socket, << 255, 0 >>),
  231. {ok, << 255, 0 >>} = gen_tcp:recv(Socket, 0, 6000),
  232. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  233. ok.
  234. ws8(Config) ->
  235. {port, Port} = lists:keyfind(port, 1, Config),
  236. {ok, Socket} = gen_tcp:connect("localhost", Port,
  237. [binary, {active, false}, {packet, raw}]),
  238. ok = gen_tcp:send(Socket, [
  239. "GET /websocket HTTP/1.1\r\n"
  240. "Host: localhost\r\n"
  241. "Connection: Upgrade\r\n"
  242. "Upgrade: websocket\r\n"
  243. "Sec-WebSocket-Origin: http://localhost\r\n"
  244. "Sec-WebSocket-Version: 8\r\n"
  245. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  246. "\r\n"]),
  247. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  248. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  249. = erlang:decode_packet(http, Handshake, []),
  250. [Headers, <<>>] = websocket_headers(
  251. erlang:decode_packet(httph, Rest, []), []),
  252. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  253. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  254. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  255. = lists:keyfind("sec-websocket-accept", 1, Headers),
  256. ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
  257. 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
  258. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
  259. = gen_tcp:recv(Socket, 0, 6000),
  260. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
  261. = gen_tcp:recv(Socket, 0, 6000),
  262. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  263. = gen_tcp:recv(Socket, 0, 6000),
  264. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  265. = gen_tcp:recv(Socket, 0, 6000),
  266. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
  267. = gen_tcp:recv(Socket, 0, 6000),
  268. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping
  269. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
  270. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close
  271. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  272. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  273. ok.
  274. ws_timeout_hibernate(Config) ->
  275. {port, Port} = lists:keyfind(port, 1, Config),
  276. {ok, Socket} = gen_tcp:connect("localhost", Port,
  277. [binary, {active, false}, {packet, raw}]),
  278. ok = gen_tcp:send(Socket, [
  279. "GET /ws_timeout_hibernate HTTP/1.1\r\n"
  280. "Host: localhost\r\n"
  281. "Connection: Upgrade\r\n"
  282. "Upgrade: websocket\r\n"
  283. "Sec-WebSocket-Origin: http://localhost\r\n"
  284. "Sec-WebSocket-Version: 8\r\n"
  285. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  286. "\r\n"]),
  287. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  288. {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
  289. = erlang:decode_packet(http, Handshake, []),
  290. [Headers, <<>>] = websocket_headers(
  291. erlang:decode_packet(httph, Rest, []), []),
  292. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  293. {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  294. {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
  295. = lists:keyfind("sec-websocket-accept", 1, Headers),
  296. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  297. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  298. ok.
  299. websocket_headers({ok, http_eoh, Rest}, Acc) ->
  300. [Acc, Rest];
  301. websocket_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) ->
  302. F = fun(S) when is_atom(S) -> S; (S) -> string:to_lower(S) end,
  303. websocket_headers(erlang:decode_packet(httph, Rest, []),
  304. [{F(Key), Value}|Acc]).
  305. %% http and https.
  306. build_url(Path, Config) ->
  307. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  308. {port, Port} = lists:keyfind(port, 1, Config),
  309. Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
  310. http_200(Config) ->
  311. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
  312. httpc:request(build_url("/", Config)).
  313. http_404(Config) ->
  314. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  315. httpc:request(build_url("/not/found", Config)).
  316. %% misc.
  317. http_10_hostless(Config) ->
  318. Packet = "GET / HTTP/1.0\r\n\r\n",
  319. {Packet, 200} = raw_req(Packet, Config).