| Class | WWW::Mechanize::Chain::ConnectionResolver |
| In: |
lib/www/mechanize/chain/connection_resolver.rb
|
| Parent: | Object |
# File lib/www/mechanize/chain/connection_resolver.rb, line 7
7: def initialize( connection_cache,
8: keep_alive,
9: proxy_addr,
10: proxy_port,
11: proxy_user,
12: proxy_pass )
13:
14: @connection_cache = connection_cache
15: @keep_alive = keep_alive
16: @proxy_addr = proxy_addr
17: @proxy_port = proxy_port
18: @proxy_user = proxy_user
19: @proxy_pass = proxy_pass
20: end
# File lib/www/mechanize/chain/connection_resolver.rb, line 22
22: def handle(ctx, params)
23: uri = params[:uri]
24: http_obj = nil
25:
26: case uri.scheme.downcase
27: when 'http', 'https'
28: cache_obj = (@connection_cache["#{uri.host}:#{uri.port}"] ||= {
29: :connection => nil,
30: :keep_alive_options => {},
31: })
32: http_obj = cache_obj[:connection]
33: if http_obj.nil? || ! http_obj.started?
34: http_obj = cache_obj[:connection] =
35: Net::HTTP.new( uri.host,
36: uri.port,
37: @proxy_addr,
38: @proxy_port,
39: @proxy_user,
40: @proxy_pass
41: )
42: cache_obj[:keep_alive_options] = {}
43: end
44:
45: # If we're keeping connections alive and the last request time is too
46: # long ago, stop the connection. Or, if the max requests left is 1,
47: # reset the connection.
48: if @keep_alive && http_obj.started?
49: opts = cache_obj[:keep_alive_options]
50: if((opts[:timeout] &&
51: Time.now.to_i - cache_obj[:last_request_time] > opts[:timeout].to_i) ||
52: opts[:max] && opts[:max].to_i == 1)
53:
54: Mechanize.log.debug('Finishing stale connection') if Mechanize.log
55: http_obj.finish
56:
57: end
58: end
59:
60: cache_obj[:last_request_time] = Time.now.to_i
61: when 'file'
62: http_obj = Object.new
63: class << http_obj
64: def started?; true; end
65: def request(request, *args, &block)
66: response = FileResponse.new(request.uri.path)
67: yield response
68: end
69: end
70: end
71:
72: params[:connection] = http_obj
73: super
74: end