From b79149682b9de93c830320c82510edd90881d57e Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Fri, 17 Jan 2020 16:17:33 -0800 Subject: [PATCH 1/4] v1 --- lib/rack_reverse_proxy/rule.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/rack_reverse_proxy/rule.rb b/lib/rack_reverse_proxy/rule.rb index 0635482..f07425f 100644 --- a/lib/rack_reverse_proxy/rule.rb +++ b/lib/rack_reverse_proxy/rule.rb @@ -48,7 +48,7 @@ def matches(path, *args) spec, url, path, - options[:accept_headers], + options, has_custom_url, *args ) @@ -124,14 +124,15 @@ class Matches # rubocop:disable Metrics/ParameterLists # FIXME: eliminate :url, :accept_headers, :has_custom_url - def initialize(spec, url, path, accept_headers, has_custom_url, headers, rackreq, *_) + def initialize(spec, url, path, options, has_custom_url, headers, rackreq, *_) @spec = spec @url = url @path = path @has_custom_url = has_custom_url @rackreq = rackreq - @headers = headers if accept_headers + @headers = headers if options[:accept_headers] + @host_matches = host_matches?(options, headers) @spec_arity = spec.method(spec_match_method_name).arity end @@ -172,7 +173,7 @@ def found def find_matches Array( - spec.send(spec_match_method_name, *spec_params) + spec.send(spec_match_method_name, *spec_params) && @host_matches ) end @@ -201,6 +202,14 @@ def spec_match_method_name return :match if spec.respond_to?(:match) :call end + + def host_matches?(options, headers) + if options[:host].blank? + return true + end + + headers['HOST'] === options[:host] + end end end end From 57091ac2a7aa7e06a5068842abf43fd12781cb53 Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Fri, 17 Jan 2020 16:27:29 -0800 Subject: [PATCH 2/4] V2 featuring constraints --- lib/rack_reverse_proxy/rule.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/rack_reverse_proxy/rule.rb b/lib/rack_reverse_proxy/rule.rb index f07425f..de3123b 100644 --- a/lib/rack_reverse_proxy/rule.rb +++ b/lib/rack_reverse_proxy/rule.rb @@ -132,7 +132,7 @@ def initialize(spec, url, path, options, has_custom_url, headers, rackreq, *_) @rackreq = rackreq @headers = headers if options[:accept_headers] - @host_matches = host_matches?(options, headers) + @constraints = options[:constraints] @spec_arity = spec.method(spec_match_method_name).arity end @@ -165,7 +165,7 @@ def transform(response, request_uri) private - attr_reader :spec, :url, :path, :headers, :rackreq, :spec_arity, :has_custom_url + attr_reader :spec, :url, :path, :headers, :rackreq, :spec_arity, :has_custom_url, :constraints def found @_found ||= find_matches @@ -173,7 +173,7 @@ def found def find_matches Array( - spec.send(spec_match_method_name, *spec_params) && @host_matches + spec.send(spec_match_method_name, *spec_params) && apply_constraints! ) end @@ -203,12 +203,12 @@ def spec_match_method_name :call end - def host_matches?(options, headers) - if options[:host].blank? + def apply_constraints! + if constraints.blank? return true end - headers['HOST'] === options[:host] + constraints.call(rackreq.env) end end end From 312d7ec769ed5d741c62dda65e83ff8da242c465 Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Mon, 20 Jan 2020 09:38:56 -0800 Subject: [PATCH 3/4] De-pluralized constraint; Updated specs --- lib/rack_reverse_proxy/rule.rb | 8 ++++---- spec/rack/reverse_proxy_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/rack_reverse_proxy/rule.rb b/lib/rack_reverse_proxy/rule.rb index de3123b..6842331 100644 --- a/lib/rack_reverse_proxy/rule.rb +++ b/lib/rack_reverse_proxy/rule.rb @@ -132,7 +132,7 @@ def initialize(spec, url, path, options, has_custom_url, headers, rackreq, *_) @rackreq = rackreq @headers = headers if options[:accept_headers] - @constraints = options[:constraints] + @constraint = options[:constraint] @spec_arity = spec.method(spec_match_method_name).arity end @@ -165,7 +165,7 @@ def transform(response, request_uri) private - attr_reader :spec, :url, :path, :headers, :rackreq, :spec_arity, :has_custom_url, :constraints + attr_reader :spec, :url, :path, :headers, :rackreq, :spec_arity, :has_custom_url, :constraint def found @_found ||= find_matches @@ -204,11 +204,11 @@ def spec_match_method_name end def apply_constraints! - if constraints.blank? + if constraint.blank? return true end - constraints.call(rackreq.env) + constraint.call(rackreq.env) end end end diff --git a/spec/rack/reverse_proxy_spec.rb b/spec/rack/reverse_proxy_spec.rb index 4f4cfbb..12578de 100644 --- a/spec/rack/reverse_proxy_spec.rb +++ b/spec/rack/reverse_proxy_spec.rb @@ -440,6 +440,26 @@ def app end end + describe "with a matching constraint" do + def app + Rack::ReverseProxy.new(dummy_app) do + reverse_proxy "/test", "http://example1.com/", constraint: ->() { true } + reverse_proxy "/test2", "http://example2.com/", constraint: ->() { false } + end + end + + it "is considered when the route matches" do + stub_request(:get, "http://example1.com/test").to_return(:body => "Proxied App") + get "/test" + expect(last_response.body).to eq("Proxied App") + end + + it "forwards requests to the calling app when path matches but constraint returns false" do + get "/test2" + expect(last_response.body).to eq("Dummy App") + end + end + describe "with force ssl turned on" do def app Rack::ReverseProxy.new(dummy_app) do From 676ddccadf51736f3839e086271efa2a2a7a1dbe Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Mon, 20 Jan 2020 09:43:02 -0800 Subject: [PATCH 4/4] Updated constraint to be passed full request instead of just ENV --- lib/rack_reverse_proxy/rule.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rack_reverse_proxy/rule.rb b/lib/rack_reverse_proxy/rule.rb index 6842331..34b0bd2 100644 --- a/lib/rack_reverse_proxy/rule.rb +++ b/lib/rack_reverse_proxy/rule.rb @@ -207,8 +207,8 @@ def apply_constraints! if constraint.blank? return true end - - constraint.call(rackreq.env) + + constraint.call(rackreq) end end end