proxy_header_SUITE.erl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. %% Copyright (c) 2018, Loïc Hoguin <essen@ninenines.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(proxy_header_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(cowboy_test, [raw_send/2]).
  20. -import(cowboy_test, [raw_recv_head/1]).
  21. -import(cowboy_test, [raw_recv/3]).
  22. %% ct.
  23. all() ->
  24. [
  25. {group, http},
  26. {group, https},
  27. {group, h2},
  28. {group, h2c},
  29. {group, h2c_upgrade}
  30. ].
  31. groups() ->
  32. Tests = ct_helper:all(?MODULE),
  33. [{h2c_upgrade, [parallel], Tests}|cowboy_test:common_groups(Tests)].
  34. init_per_group(Name=http, Config) ->
  35. cowboy_test:init_http(Name, #{
  36. env => #{dispatch => init_dispatch()},
  37. proxy_header => true
  38. }, Config);
  39. init_per_group(Name=https, Config) ->
  40. cowboy_test:init_https(Name, #{
  41. env => #{dispatch => init_dispatch()},
  42. proxy_header => true
  43. }, Config);
  44. init_per_group(Name=h2, Config) ->
  45. cowboy_test:init_http2(Name, #{
  46. env => #{dispatch => init_dispatch()},
  47. proxy_header => true
  48. }, Config);
  49. init_per_group(Name=h2c, Config) ->
  50. Config1 = cowboy_test:init_http(Name, #{
  51. env => #{dispatch => init_dispatch()},
  52. proxy_header => true
  53. }, Config),
  54. lists:keyreplace(protocol, 1, Config1, {protocol, http2});
  55. init_per_group(Name=h2c_upgrade, Config) ->
  56. Config1 = cowboy_test:init_http(h2c, #{
  57. env => #{dispatch => init_dispatch()},
  58. proxy_header => true
  59. }, Config),
  60. Config2 = lists:keyreplace(protocol, 1, Config1, {protocol, http2}),
  61. lists:keyreplace(ref, 1, Config2, {ref, Name}).
  62. end_per_group(Name, _) ->
  63. cowboy:stop_listener(Name).
  64. %% Routes.
  65. init_dispatch() ->
  66. cowboy_router:compile([{"[...]", [
  67. {"/direct/:key/[...]", echo_h, []}
  68. ]}]).
  69. %% Tests.
  70. v1_proxy_header(Config) ->
  71. doc("Confirm we can read the proxy header at the start of the connection."),
  72. ProxyInfo = #{
  73. version => 1,
  74. command => proxy,
  75. transport_family => ipv4,
  76. transport_protocol => stream,
  77. src_address => {127, 0, 0, 1},
  78. src_port => 444,
  79. dest_address => {192, 168, 0, 1},
  80. dest_port => 443
  81. },
  82. do_proxy_header(Config, ProxyInfo).
  83. v2_proxy_header(Config) ->
  84. doc("Confirm we can read the proxy header at the start of the connection."),
  85. ProxyInfo = #{
  86. version => 2,
  87. command => proxy,
  88. transport_family => ipv4,
  89. transport_protocol => stream,
  90. src_address => {127, 0, 0, 1},
  91. src_port => 444,
  92. dest_address => {192, 168, 0, 1},
  93. dest_port => 443
  94. },
  95. do_proxy_header(Config, ProxyInfo).
  96. v2_local_header(Config) ->
  97. doc("Confirm we can read the proxy header at the start of the connection."),
  98. ProxyInfo = #{
  99. version => 2,
  100. command => local
  101. },
  102. do_proxy_header(Config, ProxyInfo).
  103. do_proxy_header(Config, ProxyInfo) ->
  104. case config(ref, Config) of
  105. http -> do_proxy_header_http(Config, ProxyInfo);
  106. https -> do_proxy_header_https(Config, ProxyInfo);
  107. h2 -> do_proxy_header_h2(Config, ProxyInfo);
  108. h2c -> do_proxy_header_h2c(Config, ProxyInfo);
  109. h2c_upgrade -> do_proxy_header_h2c_upgrade(Config, ProxyInfo)
  110. end.
  111. do_proxy_header_http(Config, ProxyInfo) ->
  112. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config),
  113. [binary, {active, false}, {packet, raw}]),
  114. ok = gen_tcp:send(Socket, ranch_proxy_header:header(ProxyInfo)),
  115. do_proxy_header_http_common({raw_client, Socket, gen_tcp}, ProxyInfo).
  116. do_proxy_header_https(Config, ProxyInfo) ->
  117. {ok, Socket0} = gen_tcp:connect("localhost", config(port, Config),
  118. [binary, {active, false}, {packet, raw}]),
  119. ok = gen_tcp:send(Socket0, ranch_proxy_header:header(ProxyInfo)),
  120. {ok, Socket} = ssl:connect(Socket0, [], 1000),
  121. do_proxy_header_http_common({raw_client, Socket, ssl}, ProxyInfo).
  122. do_proxy_header_http_common(Client, ProxyInfo) ->
  123. ok = raw_send(Client,
  124. "GET /direct/proxy_header HTTP/1.1\r\n"
  125. "Host: localhost\r\n"
  126. "\r\n"),
  127. {_, 200, _, Rest0} = cow_http:parse_status_line(raw_recv_head(Client)),
  128. {Headers, Body0} = cow_http:parse_headers(Rest0),
  129. {_, LenBin} = lists:keyfind(<<"content-length">>, 1, Headers),
  130. Len = binary_to_integer(LenBin),
  131. Body = if
  132. byte_size(Body0) =:= Len -> Body0;
  133. true ->
  134. {ok, Body1} = raw_recv(Client, Len - byte_size(Body0), 5000),
  135. <<Body0/bits, Body1/bits>>
  136. end,
  137. ProxyInfo = do_parse_term(Body),
  138. ok.
  139. do_proxy_header_h2(Config, ProxyInfo) ->
  140. {ok, Socket0} = gen_tcp:connect("localhost", config(port, Config),
  141. [binary, {active, false}, {packet, raw}]),
  142. ok = gen_tcp:send(Socket0, ranch_proxy_header:header(ProxyInfo)),
  143. {ok, Socket} = ssl:connect(Socket0, [{alpn_advertised_protocols, [<<"h2">>]}], 1000),
  144. do_proxy_header_h2_common({raw_client, Socket, ssl}, ProxyInfo).
  145. do_proxy_header_h2c(Config, ProxyInfo) ->
  146. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config),
  147. [binary, {active, false}, {packet, raw}]),
  148. ok = gen_tcp:send(Socket, ranch_proxy_header:header(ProxyInfo)),
  149. do_proxy_header_h2_common({raw_client, Socket, gen_tcp}, ProxyInfo).
  150. do_proxy_header_h2c_upgrade(Config, ProxyInfo) ->
  151. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config),
  152. [binary, {active, false}, {packet, raw}]),
  153. ok = gen_tcp:send(Socket, ranch_proxy_header:header(ProxyInfo)),
  154. Client = {raw_client, Socket, gen_tcp},
  155. ok = raw_send(Client, [
  156. "GET /direct/proxy_header HTTP/1.1\r\n"
  157. "Host: localhost\r\n"
  158. "Connection: Upgrade, HTTP2-Settings\r\n"
  159. "Upgrade: h2c\r\n"
  160. "HTTP2-Settings: ", base64:encode(iolist_to_binary(cow_http2:settings_payload(#{}))), "\r\n"
  161. "\r\n"]),
  162. ok = do_recv_101(Client),
  163. %% Receive the server preface.
  164. {ok, <<PrefaceLen:24>>} = raw_recv(Client, 3, 1000),
  165. {ok, <<4:8, 0:40, _:PrefaceLen/binary>>} = raw_recv(Client, 6 + PrefaceLen, 1000),
  166. do_proxy_header_h2_response_common(Client, ProxyInfo),
  167. ok.
  168. do_proxy_header_h2_common(Client, ProxyInfo) ->
  169. %% Send a valid preface.
  170. ok = raw_send(Client, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  171. %% Receive the server preface.
  172. {ok, <<PrefaceLen:24>>} = raw_recv(Client, 3, 1000),
  173. {ok, <<4:8, 0:40, _:PrefaceLen/binary>>} = raw_recv(Client, 6 + PrefaceLen, 1000),
  174. %% Send the SETTINGS ack.
  175. ok = raw_send(Client, cow_http2:settings_ack()),
  176. %% Receive the SETTINGS ack.
  177. {ok, <<0:24, 4:8, 1:8, 0:32>>} = raw_recv(Client, 9, 1000),
  178. %% Send a GET request.
  179. {HeadersBlock, _} = cow_hpack:encode([
  180. {<<":method">>, <<"GET">>},
  181. {<<":scheme">>, <<"http">>},
  182. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  183. {<<":path">>, <<"/direct/proxy_header">>}
  184. ]),
  185. Len = iolist_size(HeadersBlock),
  186. ok = raw_send(Client, [
  187. <<Len:24, 1:8,
  188. 0:2, %% Undefined.
  189. 0:1, %% PRIORITY.
  190. 0:1, %% Undefined.
  191. 0:1, %% PADDED.
  192. 1:1, %% END_HEADERS.
  193. 0:1, %% Undefined.
  194. 1:1, %% END_STREAM.
  195. 0:1, 1:31>>,
  196. HeadersBlock
  197. ]),
  198. do_proxy_header_h2_response_common(Client, ProxyInfo).
  199. do_proxy_header_h2_response_common(Client, ProxyInfo) ->
  200. %% Receive a response with the proxy header data.
  201. {ok, <<SkipLen:24, 1:8, _:8, 1:32>>} = raw_recv(Client, 9, 1000),
  202. {ok, _} = raw_recv(Client, SkipLen, 1000),
  203. {ok, <<BodyLen:24, 0:8, 1:8, 1:32>>} = raw_recv(Client, 9, 1000),
  204. {ok, Body} = raw_recv(Client, BodyLen, 1000),
  205. ProxyInfo = do_parse_term(Body),
  206. ok.
  207. do_parse_term(Body) ->
  208. {ok, Tokens, _} = erl_scan:string(binary_to_list(Body) ++ "."),
  209. {ok, Exprs} = erl_parse:parse_exprs(Tokens),
  210. {value, Term, _} = erl_eval:exprs(Exprs, erl_eval:new_bindings()),
  211. Term.
  212. %% Match directly for now.
  213. do_recv_101(Client) ->
  214. {ok, <<
  215. "HTTP/1.1 101 Switching Protocols\r\n"
  216. "connection: Upgrade\r\n"
  217. "upgrade: h2c\r\n"
  218. "\r\n"
  219. >>} = raw_recv(Client, 71, 1000),
  220. ok.