lib/webrick/httpproxy.rb in webrick-1.3.1 vs lib/webrick/httpproxy.rb in webrick-1.4.0.beta1

- old
+ new

@@ -1,5 +1,6 @@ +# frozen_string_literal: false # # httpproxy.rb -- HTTPProxy Class # # Author: IPR -- Internet Programming with Ruby -- writers # Copyright (c) 2002 GOTO Kentaro @@ -10,33 +11,62 @@ # $kNotwork: straw.rb,v 1.3 2002/02/12 15:13:07 gotoken Exp $ require "webrick/httpserver" require "net/http" -Net::HTTP::version_1_2 if RUBY_VERSION < "1.7" - module WEBrick - NullReader = Object.new - class << NullReader + + NullReader = Object.new # :nodoc: + class << NullReader # :nodoc: def read(*args) nil end alias gets read end - FakeProxyURI = Object.new - class << FakeProxyURI + FakeProxyURI = Object.new # :nodoc: + class << FakeProxyURI # :nodoc: def method_missing(meth, *args) if %w(scheme host port path query userinfo).member?(meth.to_s) return nil end super end end + # :startdoc: + ## # An HTTP Proxy server which proxies GET, HEAD and POST requests. + # + # To create a simple proxy server: + # + # require 'webrick' + # require 'webrick/httpproxy' + # + # proxy = WEBrick::HTTPProxyServer.new Port: 8000 + # + # trap 'INT' do proxy.shutdown end + # trap 'TERM' do proxy.shutdown end + # + # proxy.start + # + # See ::new for proxy-specific configuration items. + # + # == Modifying proxied responses + # + # To modify content the proxy server returns use the +:ProxyContentHandler+ + # option: + # + # handler = proc do |req, res| + # if res['content-type'] == 'text/plain' then + # res.body << "\nThis content was proxied!\n" + # end + # end + # + # proxy = + # WEBrick::HTTPProxyServer.new Port: 8000, ProxyContentHandler: handler class HTTPProxyServer < HTTPServer ## # Proxy server configurations. The proxy server handles the following @@ -44,21 +74,22 @@ # # :ProxyAuthProc:: Called with a request and response to authorize a # request # :ProxyVia:: Appended to the via header # :ProxyURI:: The proxy server's URI - # :ProxyContentHandler:: Called with a request and resopnse and allows + # :ProxyContentHandler:: Called with a request and response and allows # modification of the response # :ProxyTimeout:: Sets the proxy timeouts to 30 seconds for open and 60 # seconds for read operations def initialize(config={}, default=Config::HTTP) super(config, default) c = @config @via = "#{c[:HTTPVersion]} #{c[:ServerName]}:#{c[:Port]}" end + # :stopdoc: def service(req, res) if req.request_method == "CONNECT" do_CONNECT(req, res) elsif req.unparsed_uri =~ %r!^http://! proxy_service(req, res) @@ -110,11 +141,11 @@ host, port = req.unparsed_uri.split(":", 2) # Proxy authentication for upstream proxy server if proxy = proxy_uri(req, res) proxy_request_line = "CONNECT #{host}:#{port} HTTP/1.0" if proxy.userinfo - credentials = "Basic " + [proxy.userinfo].pack("m").delete("\n") + credentials = "Basic " + [proxy.userinfo].pack("m0") end host, port = proxy.host, proxy.port end begin @@ -124,16 +155,16 @@ if proxy @logger.debug("CONNECT: sending a Request-Line") os << proxy_request_line << CRLF @logger.debug("CONNECT: > #{proxy_request_line}") if credentials - @logger.debug("CONNECT: sending a credentials") + @logger.debug("CONNECT: sending credentials") os << "Proxy-Authorization: " << credentials << CRLF end os << CRLF proxy_status_line = os.gets(LF) - @logger.debug("CONNECT: read a Status-Line form the upstream server") + @logger.debug("CONNECT: read Status-Line from the upstream server") @logger.debug("CONNECT: < #{proxy_status_line}") if %r{^HTTP/\d+\.\d+\s+200\s*} =~ proxy_status_line while line = os.gets(LF) break if /\A(#{CRLF}|#{LF})\z/om =~ line end @@ -152,11 +183,11 @@ handler.call(req, res) end res.send_response(ua) access_log(@config, req, res) - # Should clear request-line not to send the sesponse twice. + # Should clear request-line not to send the response twice. # see: HTTPServer#run req.parse(NullReader) rescue nil end begin @@ -169,11 +200,11 @@ buf = os.sysread(1024); @logger.debug("CONNECT: #{buf.bytesize} byte from #{host}:#{port}") ua.syswrite(buf) end end - rescue => ex + rescue os.close @logger.debug("CONNECT #{host}:#{port}: closed") end raise HTTPStatus::EOFError @@ -261,11 +292,11 @@ def setup_upstream_proxy_authentication(req, res, header) if upstream = proxy_uri(req, res) if upstream.userinfo header['proxy-authorization'] = - "Basic " + [upstream.userinfo].pack("m").delete("\n") + "Basic " + [upstream.userinfo].pack("m0") end return upstream end return FakeProxyURI end @@ -280,11 +311,11 @@ http = Net::HTTP.new(uri.host, uri.port, upstream.host, upstream.port) http.start do if @config[:ProxyTimeout] ################################## these issues are - http.open_timeout = 30 # secs # necessary (maybe bacause + http.open_timeout = 30 # secs # necessary (maybe because http.read_timeout = 60 # secs # Ruby's bug, but why?) ################################## end response = yield(http, path, header) end @@ -299,7 +330,9 @@ choose_header(response, res) set_cookie(response, res) set_via(res) res.body = response.body end + + # :stopdoc: end end