2 # httpproxy.rb -- HTTPProxy Class
4 # Author: IPR -- Internet Programming with Ruby -- writers
5 # Copyright (c) 2002 GOTO Kentaro
6 # Copyright (c) 2002 Internet Programming with Ruby writers. All rights
9 # $IPR: httpproxy.rb,v 1.18 2003/03/08 18:58:10 gotoyuzo Exp $
10 # $kNotwork: straw.rb,v 1.3 2002/02/12 15:13:07 gotoken Exp $
12 require "webrick/httpserver"
15 Net::HTTP::version_1_2 if RUBY_VERSION < "1.7"
18 NullReader = Object.new
26 class HTTPProxyServer < HTTPServer
27 def initialize(config)
30 @via = "#{c[:HTTPVersion]} #{c[:ServerName]}:#{c[:Port]}"
34 if req.request_method == "CONNECT"
35 proxy_connect(req, res)
36 elsif req.unparsed_uri =~ %r!^http://!
37 proxy_service(req, res)
43 def proxy_auth(req, res)
44 if proc = @config[:ProxyAuthProc]
47 req.header.delete("proxy-authorization")
50 # Some header fields shuold not be transfered.
51 HopByHop = %w( connection keep-alive proxy-authenticate upgrade
52 proxy-authorization te trailers transfer-encoding )
53 ShouldNotTransfer = %w( set-cookie proxy-connection )
54 def split_field(f) f ? f.split(/,\s+/).collect{|i| i.downcase } : [] end
56 def choose_header(src, dst)
57 connections = split_field(src['connection'])
60 if HopByHop.member?(key) || # RFC2616: 13.5.1
61 connections.member?(key) || # RFC2616: 14.10
62 ShouldNotTransfer.member?(key) # pragmatics
63 @logger.debug("choose_header: `#{key}: #{value}'")
70 # Net::HTTP is stupid about the multiple header fields.
72 def set_cookie(src, dst)
73 if str = src['set-cookie']
75 str.split(/,\s*/).each{|token|
76 if /^[^=]+;/o =~ token
77 cookies[-1] << ", " << token
81 cookies[-1] << ", " << token
84 dst.cookies.replace(cookies)
91 h['via'] << ", " << @via
98 def proxy_uri(req, res)
102 def proxy_service(req, res)
103 # Proxy Authentication
106 # Create Request-URI to send to the origin server
107 uri = req.request_uri
109 path << "?" << uri.query if uri.query
111 # Choose header fields to transfer
113 choose_header(req, header)
116 # select upstream proxy server
117 if proxy = proxy_uri(req, res)
118 proxy_host = proxy.host
119 proxy_port = proxy.port
121 credentials = "Basic " + [proxy.userinfo].pack("m*")
123 header['proxy-authorization'] = credentials
129 http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port)
131 if @config[:ProxyTimeout]
132 ################################## these issues are
133 http.open_timeout = 30 # secs # necessary (maybe bacause
134 http.read_timeout = 60 # secs # Ruby's bug, but why?)
135 ##################################
137 case req.request_method
138 when "GET" then response = http.get(path, header)
139 when "POST" then response = http.post(path, req.body || "", header)
140 when "HEAD" then response = http.head(path, header)
142 raise HTTPStatus::MethodNotAllowed,
143 "unsupported method `#{req.request_method}'."
147 logger.debug("#{err.class}: #{err.message}")
148 raise HTTPStatus::ServiceUnavailable, err.message
151 # Persistent connction requirements are mysterious for me.
152 # So I will close the connection in every response.
153 res['proxy-connection'] = "close"
154 res['connection'] = "close"
156 # Convert Net::HTTP::HTTPResponse to WEBrick::HTTPProxy
157 res.status = response.code.to_i
158 choose_header(response, res)
159 set_cookie(response, res)
161 res.body = response.body
164 if handler = @config[:ProxyContentHandler]
165 handler.call(req, res)
169 def proxy_connect(req, res)
170 # Proxy Authentication
173 ua = Thread.current[:WEBrickSocket] # User-Agent
174 raise HTTPStatus::InternalServerError,
175 "[BUG] cannot get socket" unless ua
177 host, port = req.unparsed_uri.split(":", 2)
178 # Proxy authentication for upstream proxy server
179 if proxy = proxy_uri(req, res)
180 proxy_request_line = "CONNECT #{host}:#{port} HTTP/1.0"
182 credentials = "Basic " + [proxy.userinfo].pack("m*")
185 host, port = proxy.host, proxy.port
189 @logger.debug("CONNECT: upstream proxy is `#{host}:#{port}'.")
190 os = TCPSocket.new(host, port) # origin server
193 @logger.debug("CONNECT: sending a Request-Line")
194 os << proxy_request_line << CRLF
195 @logger.debug("CONNECT: > #{proxy_request_line}")
197 @logger.debug("CONNECT: sending a credentials")
198 os << "Proxy-Authorization: " << credentials << CRLF
201 proxy_status_line = os.gets(LF)
202 @logger.debug("CONNECT: read a Status-Line form the upstream server")
203 @logger.debug("CONNECT: < #{proxy_status_line}")
204 if %r{^HTTP/\d+\.\d+\s+200\s*} =~ proxy_status_line
205 while line = os.gets(LF)
206 break if /\A(#{CRLF}|#{LF})\z/om =~ line
209 raise HTTPStatus::BadGateway
212 @logger.debug("CONNECT #{host}:#{port}: succeeded")
213 res.status = HTTPStatus::RC_OK
215 @logger.debug("CONNECT #{host}:#{port}: failed `#{ex.message}'")
217 raise HTTPStatus::EOFError
219 if handler = @config[:ProxyContentHandler]
220 handler.call(req, res)
222 res.send_response(ua)
223 access_log(@config, req, res)
225 # Should clear request-line not to send the sesponse twice.
226 # see: HTTPServer#run
227 req.parse(NullReader) rescue nil
231 while fds = IO::select([ua, os])
232 if fds[0].member?(ua)
233 buf = ua.sysread(1024);
234 @logger.debug("CONNECT: #{buf.size} byte from User-Agent")
236 elsif fds[0].member?(os)
237 buf = os.sysread(1024);
238 @logger.debug("CONNECT: #{buf.size} byte from #{host}:#{port}")
244 @logger.debug("CONNECT #{host}:#{port}: closed")
247 raise HTTPStatus::EOFError
250 def do_OPTIONS(req, res)
251 res['allow'] = "GET,HEAD,POST,OPTIONS,CONNECT"