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