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