Merge pull request #27 from kylewlacy/passthrough-compression

Add option to pass through compression
This commit is contained in:
James Hu 2022-02-07 17:49:19 -08:00 committed by GitHub
commit df3184e116
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 16 deletions

View File

@ -37,12 +37,13 @@ module ReverseProxy
def request(env, options = {}, &block) def request(env, options = {}, &block)
options.reverse_merge!( options.reverse_merge!(
headers: {}, headers: {},
http: {}, http: {},
path: nil, path: nil,
username: nil, username: nil,
password: nil, password: nil,
verify_ssl: true verify_ssl: true,
reset_accept_encoding: false
) )
source_request = Rack::Request.new(env) source_request = Rack::Request.new(env)
@ -50,13 +51,11 @@ module ReverseProxy
# We can pass in a custom path # We can pass in a custom path
uri = Addressable::URI.parse("#{url}#{options[:path] || env['ORIGINAL_FULLPATH']}") uri = Addressable::URI.parse("#{url}#{options[:path] || env['ORIGINAL_FULLPATH']}")
# Initialize request # Define headers
target_request = Net::HTTP.const_get(source_request.request_method.capitalize).new(uri.request_uri)
# Setup headers
target_request_headers = extract_http_request_headers(source_request.env).merge(options[: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 # Basic auth
target_request.basic_auth(options[:username], options[:password]) if options[:username] and options[:password] target_request.basic_auth(options[:username], options[:password]) if options[:username] and options[:password]
@ -74,11 +73,11 @@ module ReverseProxy
# Hold the response here # Hold the response here
target_response = nil target_response = nil
# Don't encode response/support compression which was if options[:reset_accept_encoding]
# causing content length not match the actual content # Clear the "Accept-Encoding" header (which will
# length of the response which ended up causing issues # disable compression or other server-side encodings)
# within Varnish (503) target_request['Accept-Encoding'] = nil
target_request['Accept-Encoding'] = nil end
http_options = {} http_options = {}
http_options[:use_ssl] = (uri.scheme == "https") http_options[:use_ssl] = (uri.scheme == "https")