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:
46:
47:
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