http_SUITE.erl 12 KB

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