--- /usr/pkg/lib/ruby/1.8/webrick/httpproxy.rb 2008-06-06 17:05:24.000000000 +0900 +++ httpproxy.rb 2013-09-03 18:20:16.000000000 +0900 @@ -125,9 +125,24 @@ end response = nil - begin - http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port) - http.start{ + is_new_connection = false + http = req.instance_variable_get(:@keeped_http) + while not response + if not http + is_new_connection = true + http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port) + req.instance_variable_set(:@keeped_http, http) unless req.keep_alive? + http.start + end + case req.request_method + when "GET" + when "POST" + when "HEAD" + else + raise HTTPStatus::MethodNotAllowed, + "unsupported method `#{req.request_method}'." + end + begin if @config[:ProxyTimeout] ################################## these issues are http.open_timeout = 30 # secs # necessary (maybe bacause @@ -138,20 +153,22 @@ 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 + http.finish unless req.keep_alive? + rescue => err + req.instance_variable_set(:@keeped_http, nil) + if is_new_connection + logger.debug("#{err.class}: #{err.message}") + raise HTTPStatus::ServiceUnavailable, err.message + end + http = nil + end end - # Persistent connction requirements are mysterious for me. - # So I will close the connection in every response. - res['proxy-connection'] = "close" - res['connection'] = "close" + unless req.keep_alive? + res['proxy-connection'] = "close" + res['connection'] = "close" + end # Convert Net::HTTP::HTTPResponse to WEBrick::HTTPProxy res.status = response.code.to_i