http_SUITE.erl 11 KB

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