proxy_header_SUITE.erl 7.7 KB

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