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