From b8deb11b596888c8da9c61b375d053c33c05bf37 Mon Sep 17 00:00:00 2001 From: ntl Date: Tue, 17 Apr 2012 15:14:12 -0500 Subject: [PATCH 1/5] Preserve both host and port number when :preserve_host is true --- lib/rack/reverse_proxy.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/rack/reverse_proxy.rb b/lib/rack/reverse_proxy.rb index b7acfcf..9f5dcff 100644 --- a/lib/rack/reverse_proxy.rb +++ b/lib/rack/reverse_proxy.rb @@ -23,8 +23,10 @@ def call(env) headers[$1] = value end } - headers['HOST'] = uri.host if all_opts[:preserve_host] - + if all_opts[:preserve_host] + headers['HOST'] = [uri.host, env['SERVER_PORT']].compact.map(&:to_s).join ':' + end + session = Net::HTTP.new(uri.host, uri.port) session.read_timeout=all_opts[:timeout] if all_opts[:timeout] From 4791d8a5fbd1dbcaac52309c10cd715d7349df04 Mon Sep 17 00:00:00 2001 From: Drew Bisset and Rolen T Le Date: Tue, 5 Aug 2014 13:37:20 -0400 Subject: [PATCH 2/5] Use full url when filtering requests --- lib/rack/reverse_proxy.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rack/reverse_proxy.rb b/lib/rack/reverse_proxy.rb index 9f5dcff..f822e3f 100644 --- a/lib/rack/reverse_proxy.rb +++ b/lib/rack/reverse_proxy.rb @@ -12,7 +12,7 @@ def initialize(app = nil, &b) def call(env) rackreq = Rack::Request.new(env) - matcher = get_matcher rackreq.fullpath + matcher = get_matcher rackreq.url return @app.call(env) if matcher.nil? uri = matcher.get_uri(rackreq.fullpath,env) From f246a8b1b81b4f45331760fbf4629f67e8ca0303 Mon Sep 17 00:00:00 2001 From: Drew Bisset and Rolen T Le Date: Tue, 5 Aug 2014 13:37:55 -0400 Subject: [PATCH 3/5] Set X-Forwarded-Host in Headers to preserve port on the original request --- lib/rack/reverse_proxy.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/rack/reverse_proxy.rb b/lib/rack/reverse_proxy.rb index f822e3f..94c1a66 100644 --- a/lib/rack/reverse_proxy.rb +++ b/lib/rack/reverse_proxy.rb @@ -6,7 +6,7 @@ class ReverseProxy def initialize(app = nil, &b) @app = app || lambda {|env| [404, [], []] } @matchers = [] - @global_options = {:preserve_host => true, :matching => :all, :verify_ssl => true} + @global_options = {:preserve_host => true, :x_forwarded_host => true, :matching => :all, :verify_ssl => true} instance_eval &b if block_given? end @@ -23,9 +23,9 @@ def call(env) headers[$1] = value end } - if all_opts[:preserve_host] - headers['HOST'] = [uri.host, env['SERVER_PORT']].compact.map(&:to_s).join ':' - end + + headers['HOST'] = host_with_port uri, env['SERVER_PORT'] if all_opts[:preserve_host] + headers['X-Forwarded-Host'] = host_with_port uri, env['SERVER_PORT'] if all_opts[:x_forwarded_host] session = Net::HTTP.new(uri.host, uri.port) session.read_timeout=all_opts[:timeout] if all_opts[:timeout] From efbbb5b4362f4d31819e6b1ff81cab70d12b02d8 Mon Sep 17 00:00:00 2001 From: Drew Bisset and Rolen T Le Date: Tue, 5 Aug 2014 16:13:59 -0400 Subject: [PATCH 4/5] Update version and add new method dependency that was missed --- VERSION | 2 +- lib/rack/reverse_proxy.rb | 4 +++- rack-reverse-proxy.gemspec | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/VERSION b/VERSION index 6f2743d..0bfccb0 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.4.4 +0.4.5 diff --git a/lib/rack/reverse_proxy.rb b/lib/rack/reverse_proxy.rb index 94c1a66..f0a8d14 100644 --- a/lib/rack/reverse_proxy.rb +++ b/lib/rack/reverse_proxy.rb @@ -23,7 +23,6 @@ def call(env) headers[$1] = value end } - headers['HOST'] = host_with_port uri, env['SERVER_PORT'] if all_opts[:preserve_host] headers['X-Forwarded-Host'] = host_with_port uri, env['SERVER_PORT'] if all_opts[:x_forwarded_host] @@ -73,6 +72,9 @@ def call(env) end private + def host_with_port uri, port + [uri.host, port].compact.map(&:to_s).join ':' + end def get_matcher path matches = @matchers.select do |matcher| diff --git a/rack-reverse-proxy.gemspec b/rack-reverse-proxy.gemspec index 2fbd435..152b33f 100644 --- a/rack-reverse-proxy.gemspec +++ b/rack-reverse-proxy.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = %q{rack-reverse-proxy} - s.version = "0.4.4" + s.version = "0.4.5" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Jon Swope"] From 9382badcb9845239093175103376eb4705fa3d80 Mon Sep 17 00:00:00 2001 From: Drew Bisset and Rolen T Le Date: Wed, 6 Aug 2014 10:49:45 -0400 Subject: [PATCH 5/5] Delete Content-Length from response headers * To support responses that have been gzipped by Rack::Deflate --- VERSION | 2 +- lib/rack/reverse_proxy.rb | 3 +++ rack-reverse-proxy.gemspec | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 0bfccb0..ef52a64 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.4.5 +0.4.6 diff --git a/lib/rack/reverse_proxy.rb b/lib/rack/reverse_proxy.rb index f0a8d14..64cf35f 100644 --- a/lib/rack/reverse_proxy.rb +++ b/lib/rack/reverse_proxy.rb @@ -97,6 +97,9 @@ def create_response_headers http_response # TODO: figure out how to handle chunked responses response_headers.delete('transfer-encoding') # TODO: Verify Content Length, and required Rack headers + # Workaround: Ignore Content-Length headers to support Rack::Deflate + # gzipped responses + response_headers.delete('Content-Length') response_headers end diff --git a/rack-reverse-proxy.gemspec b/rack-reverse-proxy.gemspec index 152b33f..20fce72 100644 --- a/rack-reverse-proxy.gemspec +++ b/rack-reverse-proxy.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = %q{rack-reverse-proxy} - s.version = "0.4.5" + s.version = "0.4.6" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Jon Swope"]