From 789b3f7bee26b53f81392e004c5b3cd7132a0b0a Mon Sep 17 00:00:00 2001 From: Joe-noh Date: Tue, 12 Mar 2019 11:04:51 +0900 Subject: [PATCH] pass throught Accept-Encoding copied from https://github.com/axsuul/rails-reverse-proxy/pull/27 --- lib/reverse_proxy/client.rb | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/lib/reverse_proxy/client.rb b/lib/reverse_proxy/client.rb index b08b36a..84d3b4b 100644 --- a/lib/reverse_proxy/client.rb +++ b/lib/reverse_proxy/client.rb @@ -38,12 +38,13 @@ def initialize(url) 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, + reset_accept_encoding: false ) source_request = Rack::Request.new(env) @@ -51,13 +52,11 @@ def request(env, options = {}, &block) # 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] @@ -75,11 +74,11 @@ def request(env, options = {}, &block) # 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 + 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 http_options = {} http_options[:use_ssl] = (uri.scheme == "https") @@ -131,7 +130,7 @@ def request(env, options = {}, &block) def extract_http_request_headers(env) headers = env.reject do |k, v| - !(/^HTTP_[A-Z_]+$/ === k) || k == "HTTP_VERSION" || v.nil? + !(/^HTTP_[A-Z_]+$/ === k) || v.nil? end.map do |k, v| [reconstruct_header_name(k), v] end.inject(Rack::Utils::HeaderHash.new) do |hash, k_v|