http_SUITE.erl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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([headers_dupe/1, nc_rand/1, pipeline/1, raw/1]). %% http.
  19. -export([http_200/1, http_404/1, websocket/1]). %% http and https.
  20. %% ct.
  21. all() ->
  22. [{group, http}, {group, https}].
  23. groups() ->
  24. BaseTests = [http_200, http_404],
  25. [{http, [], [headers_dupe, nc_rand, pipeline, raw, websocket] ++ BaseTests},
  26. {https, [], BaseTests}].
  27. init_per_suite(Config) ->
  28. application:start(inets),
  29. application:start(cowboy),
  30. Config.
  31. end_per_suite(_Config) ->
  32. application:stop(cowboy),
  33. application:stop(inets),
  34. ok.
  35. init_per_group(http, Config) ->
  36. Port = 33080,
  37. cowboy:start_listener(http, 100,
  38. cowboy_tcp_transport, [{port, Port}],
  39. cowboy_http_protocol, [{dispatch, init_http_dispatch()}]
  40. ),
  41. [{scheme, "http"}, {port, Port}|Config];
  42. init_per_group(https, Config) ->
  43. Port = 33081,
  44. application:start(crypto),
  45. application:start(public_key),
  46. application:start(ssl),
  47. DataDir = ?config(data_dir, Config),
  48. cowboy:start_listener(https, 100,
  49. cowboy_ssl_transport, [
  50. {port, Port}, {certfile, DataDir ++ "cert.pem"},
  51. {keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}],
  52. cowboy_http_protocol, [{dispatch, init_https_dispatch()}]
  53. ),
  54. [{scheme, "https"}, {port, Port}|Config].
  55. end_per_group(http, _Config) ->
  56. cowboy:stop_listener(http),
  57. ok;
  58. end_per_group(https, _Config) ->
  59. cowboy:stop_listener(https),
  60. application:stop(ssl),
  61. application:stop(public_key),
  62. application:stop(crypto),
  63. ok.
  64. %% Dispatch configuration.
  65. init_http_dispatch() ->
  66. [
  67. {["localhost"], [
  68. {["websocket"], websocket_handler, []},
  69. {["headers", "dupe"], http_handler,
  70. [{headers, [{"Connection", "close"}]}]},
  71. {[], http_handler, []}
  72. ]}
  73. ].
  74. init_https_dispatch() ->
  75. init_http_dispatch().
  76. %% http.
  77. headers_dupe(Config) ->
  78. {port, Port} = lists:keyfind(port, 1, Config),
  79. {ok, Socket} = gen_tcp:connect("localhost", Port,
  80. [binary, {active, false}, {packet, raw}]),
  81. ok = gen_tcp:send(Socket, "GET /headers/dupe HTTP/1.1\r\n"
  82. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  83. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  84. {_Start, _Length} = binary:match(Data, <<"Connection: close">>),
  85. nomatch = binary:match(Data, <<"Connection: keep-alive">>),
  86. ok = gen_tcp:close(Socket).
  87. nc_rand(Config) ->
  88. Cat = os:find_executable("cat"),
  89. Nc = os:find_executable("nc"),
  90. case {Cat, Nc} of
  91. {false, _} ->
  92. {skip, {notfound, cat}};
  93. {_, false} ->
  94. {skip, {notfound, nc}};
  95. _Good ->
  96. %% Throw garbage at the server then check if it's still up.
  97. {port, Port} = lists:keyfind(port, 1, Config),
  98. [nc_rand_run(Port) || _N <- lists:seq(1, 100)],
  99. Packet = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n",
  100. {Packet, 200} = raw_req(Packet, Config)
  101. end.
  102. nc_rand_run(Port) ->
  103. os:cmd("cat /dev/urandom | nc localhost " ++ integer_to_list(Port)).
  104. pipeline(Config) ->
  105. {port, Port} = lists:keyfind(port, 1, Config),
  106. {ok, Socket} = gen_tcp:connect("localhost", Port,
  107. [binary, {active, false}, {packet, raw}]),
  108. ok = gen_tcp:send(Socket,
  109. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  110. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  111. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  112. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  113. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"),
  114. Data = pipeline_recv(Socket, <<>>),
  115. Reqs = binary:split(Data, << "\r\n\r\nhttp_handler" >>, [global, trim]),
  116. 5 = length(Reqs),
  117. pipeline_check(Reqs).
  118. pipeline_check([]) ->
  119. ok;
  120. pipeline_check([Req|Tail]) ->
  121. << "HTTP/1.1 200", _Rest/bits >> = Req,
  122. pipeline_check(Tail).
  123. pipeline_recv(Socket, SoFar) ->
  124. case gen_tcp:recv(Socket, 0, 6000) of
  125. {ok, Data} ->
  126. pipeline_recv(Socket, << SoFar/binary, Data/binary >>);
  127. {error, closed} ->
  128. ok = gen_tcp:close(Socket),
  129. SoFar
  130. end.
  131. raw_req(Packet, Config) ->
  132. {port, Port} = lists:keyfind(port, 1, Config),
  133. {ok, Socket} = gen_tcp:connect("localhost", Port,
  134. [binary, {active, false}, {packet, raw}]),
  135. ok = gen_tcp:send(Socket, Packet),
  136. {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>}
  137. = gen_tcp:recv(Socket, 0, 6000),
  138. gen_tcp:close(Socket),
  139. {Packet, list_to_integer(binary_to_list(Str))}.
  140. raw(Config) ->
  141. Tests = [
  142. {"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
  143. {"\n", 400},
  144. {"Garbage\r\n\r\n", 400},
  145. {"\r\n\r\n\r\n\r\n\r\n\r\n", 400},
  146. {"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400},
  147. {"", 408},
  148. {"\r\n", 408},
  149. {"\r\n\r\n", 408},
  150. {"GET / HTTP/1.1", 408},
  151. {"GET / HTTP/1.1\r\n", 408},
  152. {"GET / HTTP/1.1\r\nHost: localhost", 408},
  153. {"GET / HTTP/1.1\r\nHost: localhost\r\n", 408},
  154. {"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408},
  155. {"GET http://localhost/ HTTP/1.1\r\n\r\n", 501},
  156. {"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505}
  157. ],
  158. [{Packet, StatusCode} = raw_req(Packet, Config)
  159. || {Packet, StatusCode} <- Tests].
  160. websocket(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 /websocket HTTP/1.1\r\n"
  166. "Host: localhost\r\n"
  167. "Connection: Upgrade\r\n"
  168. "Upgrade: WebSocket\r\n"
  169. "Origin: http://localhost\r\n"
  170. "Sec-Websocket-Key1: Y\" 4 1Lj!957b8@0H756!i\r\n"
  171. "Sec-Websocket-Key2: 1711 M;4\\74 80<6\r\n"
  172. "\r\n", <<15,245,8,18,2,204,133,33>>]),
  173. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  174. {ok, {http_response, {1, 1}, 101, "WebSocket Protocol Handshake"}, Rest}
  175. = erlang:decode_packet(http, Handshake, []),
  176. [Headers, Body] = websocket_headers(erlang:decode_packet(httph, Rest, []), []),
  177. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  178. {'Upgrade', "WebSocket"} = lists:keyfind('Upgrade', 1, Headers),
  179. {"sec-websocket-location", "ws://localhost/websocket"}
  180. = lists:keyfind("sec-websocket-location", 1, Headers),
  181. {"sec-websocket-origin", "http://localhost"}
  182. = lists:keyfind("sec-websocket-origin", 1, Headers),
  183. <<169,244,191,103,146,33,149,59,74,104,67,5,99,118,171,236>> = Body,
  184. ok = gen_tcp:send(Socket, << 0, "client_msg", 255 >>),
  185. {ok, << 0, "client_msg", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  186. {ok, << 0, "websocket_init", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  187. {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  188. {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  189. {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  190. ok = gen_tcp:send(Socket, << 255, 0 >>),
  191. {ok, << 255, 0 >>} = gen_tcp:recv(Socket, 0, 6000),
  192. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  193. ok.
  194. websocket_headers({ok, http_eoh, Rest}, Acc) ->
  195. [Acc, Rest];
  196. websocket_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) ->
  197. F = fun(S) when is_atom(S) -> S; (S) -> string:to_lower(S) end,
  198. websocket_headers(erlang:decode_packet(httph, Rest, []),
  199. [{F(Key), Value}|Acc]).
  200. %% http and https.
  201. build_url(Path, Config) ->
  202. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  203. {port, Port} = lists:keyfind(port, 1, Config),
  204. Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
  205. http_200(Config) ->
  206. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
  207. httpc:request(build_url("/", Config)).
  208. http_404(Config) ->
  209. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  210. httpc:request(build_url("/not/found", Config)).