fix error

This commit is contained in:
rulingcom 2023-04-03 22:37:48 +08:00
parent 42c8b4f45f
commit 2528857aa7
1 changed files with 19 additions and 7 deletions

View File

@ -38,9 +38,16 @@ class Roda
client = NetX::HTTPUnix.new(_sock_url) client = NetX::HTTPUnix.new(_sock_url)
request_class = Net::HTTP.const_get(:"#{env['REQUEST_METHOD'].to_s.downcase.capitalize}") request_class = Net::HTTP.const_get(:"#{env['REQUEST_METHOD'].to_s.downcase.capitalize}")
req = request_class.new(_proxy_url.to_s, _proxy_headers) req = request_class.new(_proxy_url.to_s, _proxy_headers)
env_body = env["rack.input"].read
env_content_type = env['CONTENT_TYPE']
unless env_body.nil?
req.body = env_body
end
unless env_content_type.nil?
req.content_type = env_content_type
end
f_response = client.request(req) f_response = client.request(req)
# p f_response f_response.content_length = nil #prevent duplicate header Content-Length and content-length
_respond(f_response) _respond(f_response)
end end
@ -71,12 +78,15 @@ class Roda
end end
def _proxy_url def _proxy_url
uri = URI("#{roda_class.opts[:proxy_path]}#{env['PATH_INFO'][1..-1]}") uri = URI("#{roda_class.opts[:proxy_path]}#{env['PATH_INFO'][1..-1]}")
if !env['QUERY_STRING'].empty?
uri.query = env['QUERY_STRING'] uri.query = env['QUERY_STRING']
end
uri uri
end end
def _proxy_headers def _proxy_headers
env env_host = env['HTTP_HOST'].to_s
env_new = env
.select { |k, _v| k.start_with? 'HTTP_' } .select { |k, _v| k.start_with? 'HTTP_' }
.reject { |k, _v| k == 'HTTP_HOST' } .reject { |k, _v| k == 'HTTP_HOST' }
.transform_keys do |k| .transform_keys do |k|
@ -85,9 +95,10 @@ class Roda
.map(&:capitalize) .map(&:capitalize)
.join('-') .join('-')
end end
.merge({ if env_host
'Host' => "127.0.0.1" env_new.merge!({'Host' => env_host})
}) end
env_new
end end
def _respond(proxied_response) def _respond(proxied_response)
@ -98,6 +109,7 @@ class Roda
.each { |k, v| response[k] = v } .each { |k, v| response[k] = v }
response.write(proxied_response.body) response.write(proxied_response.body)
end end
end end
end end