From c9336a23d31f9578ecb817c5feca4bb3dd27beda Mon Sep 17 00:00:00 2001 From: Kyle Lacy Date: Mon, 29 Jan 2018 15:37:22 -0800 Subject: [PATCH 1/3] Tweak Rack request header initialization --- lib/reverse_proxy/client.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/reverse_proxy/client.rb b/lib/reverse_proxy/client.rb index bb8c72c..b752f64 100644 --- a/lib/reverse_proxy/client.rb +++ b/lib/reverse_proxy/client.rb @@ -51,13 +51,11 @@ module ReverseProxy # We can pass in a custom path uri = Addressable::URI.parse("#{url}#{options[:path] || env['ORIGINAL_FULLPATH']}") - # Initialize request - target_request = Net::HTTP.const_get(source_request.request_method.capitalize).new(uri.request_uri) - - # Setup headers + # Define headers target_request_headers = extract_http_request_headers(source_request.env).merge(options[:headers]) - target_request.initialize_http_header(target_request_headers) + # Initialize request + target_request = Net::HTTP.const_get(source_request.request_method.capitalize).new(uri.request_uri, target_request_headers) # Basic auth target_request.basic_auth(options[:username], options[:password]) if options[:username] and options[:password] From 5662cc2bde38d4424820434e7a992b3402d7a56d Mon Sep 17 00:00:00 2001 From: Kyle Lacy Date: Mon, 29 Jan 2018 15:38:05 -0800 Subject: [PATCH 2/3] Add option to pass through compressed responses --- README.md | 8 ++++++++ lib/reverse_proxy/client.rb | 26 +++++++++++++++----------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index addeadd..f0d6aee 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,14 @@ If you'd like to bypass SSL verification reverse_proxy "http://localhost:8000", verify_ssl: false ``` +If you'd like to allow the proxy to request compressed resources by forwarding the `Accept-Encoding` header + +```ruby +reverse_proxy "http://localhost:8000", compression: :passthrough +# Note that your controller's response will only be compressed +# if the original response from localhost:8000 is compressed! +``` + If you'd like to customize the options passed into the [HTTP session](https://ruby-doc.org/stdlib-2.4.0/libdoc/net/http/rdoc/Net/HTTP.html#start-method) ```ruby diff --git a/lib/reverse_proxy/client.rb b/lib/reverse_proxy/client.rb index b752f64..bcc02b2 100644 --- a/lib/reverse_proxy/client.rb +++ b/lib/reverse_proxy/client.rb @@ -38,12 +38,13 @@ module ReverseProxy def request(env, options = {}, &block) options.reverse_merge!( - headers: {}, - http: {}, - path: nil, - username: nil, - password: nil, - verify_ssl: true + headers: {}, + http: {}, + path: nil, + username: nil, + password: nil, + verify_ssl: true, + compression: :disabled ) source_request = Rack::Request.new(env) @@ -73,11 +74,14 @@ module ReverseProxy # Hold the response here target_response = nil - # Don't encode response/support compression which was - # causing content length not match the actual content - # length of the response which ended up causing issues - # within Varnish (503) - target_request['Accept-Encoding'] = nil + case options[:compression] + when :passthrough + # Pass along the "Accept-Encoding" header from the source request as-is, + # so we don't need to change anything + when :disabled, false, nil + # Remove the "Accept-Encoding" header if compression is disabled + target_request['Accept-Encoding'] = nil + end http_options = {} http_options[:use_ssl] = (uri.scheme == "https") From 46eaa864b3b6afc3e6d9244fa474c2204a054d81 Mon Sep 17 00:00:00 2001 From: Kyle Lacy Date: Wed, 18 Apr 2018 21:24:31 -0700 Subject: [PATCH 3/3] Replace `:compression` option with `:reset_accept_encoding` The default is to now to not clear the `Accept-Encoding` header (this can be changed back to the older behavior by passing `reset_accept_encoding: true`) --- README.md | 8 -------- lib/reverse_proxy/client.rb | 23 ++++++++++------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index f0d6aee..addeadd 100644 --- a/README.md +++ b/README.md @@ -65,14 +65,6 @@ If you'd like to bypass SSL verification reverse_proxy "http://localhost:8000", verify_ssl: false ``` -If you'd like to allow the proxy to request compressed resources by forwarding the `Accept-Encoding` header - -```ruby -reverse_proxy "http://localhost:8000", compression: :passthrough -# Note that your controller's response will only be compressed -# if the original response from localhost:8000 is compressed! -``` - If you'd like to customize the options passed into the [HTTP session](https://ruby-doc.org/stdlib-2.4.0/libdoc/net/http/rdoc/Net/HTTP.html#start-method) ```ruby diff --git a/lib/reverse_proxy/client.rb b/lib/reverse_proxy/client.rb index bcc02b2..84d3b4b 100644 --- a/lib/reverse_proxy/client.rb +++ b/lib/reverse_proxy/client.rb @@ -38,13 +38,13 @@ module ReverseProxy def request(env, options = {}, &block) options.reverse_merge!( - headers: {}, - http: {}, - path: nil, - username: nil, - password: nil, - verify_ssl: true, - compression: :disabled + headers: {}, + http: {}, + path: nil, + username: nil, + password: nil, + verify_ssl: true, + reset_accept_encoding: false ) source_request = Rack::Request.new(env) @@ -74,12 +74,9 @@ module ReverseProxy # Hold the response here target_response = nil - case options[:compression] - when :passthrough - # Pass along the "Accept-Encoding" header from the source request as-is, - # so we don't need to change anything - when :disabled, false, nil - # Remove the "Accept-Encoding" header if compression is disabled + if options[:reset_accept_encoding] + # Clear the "Accept-Encoding" header (which will + # disable compression or other server-side encodings) target_request['Accept-Encoding'] = nil end