def proxy_service(req, res)
proxy_auth(req, res)
uri = req.request_uri
path = uri.path.dup
path << "?" << uri.query if uri.query
header = Hash.new
choose_header(req, header)
set_via(header)
if proxy = proxy_uri(req, res)
proxy_host = proxy.host
proxy_port = proxy.port
if proxy.userinfo
credentials = "Basic " + [proxy.userinfo].pack("m*")
credentials.chomp!
header['proxy-authorization'] = credentials
end
end
response = nil
begin
http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port)
http.start{
if @config[:ProxyTimeout]
http.open_timeout = 30
http.read_timeout = 60
end
case req.request_method
when "GET" then response = http.get(path, header)
when "POST" then response = http.post(path, req.body || "", header)
when "HEAD" then response = http.head(path, header)
else
raise HTTPStatus::MethodNotAllowed,
"unsupported method `#{req.request_method}'."
end
}
rescue => err
logger.debug("#{err.class}: #{err.message}")
raise HTTPStatus::ServiceUnavailable, err.message
end
res['proxy-connection'] = "close"
res['connection'] = "close"
res.status = response.code.to_i
choose_header(response, res)
set_cookie(response, res)
set_via(res)
res.body = response.body
if handler = @config[:ProxyContentHandler]
handler.call(req, res)
end
end