add support for unix socket

This commit is contained in:
BoHung Chiu 2023-04-20 23:44:22 +08:00
parent b5156fa170
commit c201300f6b
1 changed files with 9 additions and 3 deletions

View File

@ -49,13 +49,18 @@ module ReverseProxy
source_request = Rack::Request.new(env) source_request = Rack::Request.new(env)
# 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(self.url)
prefix_path = uri.request_uri
path = "#{prefix_path}#{options[:path] || env['ORIGINAL_FULLPATH']}"
if path.blank?
path = "/"
end
# Define headers # Define 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])
# Initialize request # Initialize request
target_request = Net::HTTP.const_get(source_request.request_method.capitalize).new(uri.request_uri, target_request_headers) target_request = Net::HTTP.const_get(source_request.request_method.capitalize).new(path, 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]
@ -85,7 +90,8 @@ module ReverseProxy
http_options.merge!(options[:http]) if options[:http] http_options.merge!(options[:http]) if options[:http]
# Make the request # Make the request
Net::HTTP.start(uri.hostname, uri.port, http_options) do |http| hostname = self.url.starts_with?("unix://") ? uri.to_s : uri.hostname
NetX::HTTPUnix.start(hostname, uri.port, http_options) do |http|
callbacks[:on_connect].call(http) callbacks[:on_connect].call(http)
target_response = http.request(target_request) target_response = http.request(target_request)
end end