lib/spidr/session_cache.rb in spidr-0.5.0 vs lib/spidr/session_cache.rb in spidr-0.6.0

- old
+ new

@@ -1,40 +1,56 @@ +require 'spidr/settings/proxy' +require 'spidr/settings/timeouts' require 'spidr/spidr' require 'net/http' +require 'openssl' module Spidr # # Stores active HTTP Sessions organized by scheme, host-name and port. # class SessionCache - # Proxy to use - attr_accessor :proxy + include Settings::Proxy + include Settings::Timeouts # # Creates a new session cache. # - # @param [Hash] proxy (Spidr.proxy) + # @param [Hash] options + # Configuration options. + # + # @option [Hash] :proxy (Spidr.proxy) # Proxy options. # - # @option proxy [String] :host - # The host the proxy is running on. + # @option [Integer] :open_timeout (Spidr.open_timeout) + # Optional open timeout. # - # @option proxy [Integer] :port - # The port the proxy is running on. + # @option [Integer] :ssl_timeout (Spidr.ssl_timeout) + # Optional ssl timeout. # - # @option proxy [String] :user - # The user to authenticate as with the proxy. + # @option [Integer] :read_timeout (Spidr.read_timeout) + # Optional read timeout. # - # @option proxy [String] :password - # The password to authenticate with. + # @option [Integer] :continue_timeout (Spidr.continue_timeout) + # Optional `Continue` timeout. # - # @since 0.2.2 + # @option [Integer] :keep_alive_timeout (Spidr.keep_alive_timeout) + # Optional `Keep-Alive` timeout. # - def initialize(proxy=Spidr.proxy) - @proxy = proxy + # @since 0.6.0 + # + def initialize(options={}) + @proxy = options.fetch(:proxy,Spidr.proxy) + + @open_timeout = options.fetch(:open_timeout,Spidr.open_timeout) + @ssl_timeout = options.fetch(:ssl_timeout,Spidr.ssl_timeout) + @read_timeout = options.fetch(:read_timeout,Spidr.read_timeout) + @continue_timeout = options.fetch(:continue_timeout,Spidr.continue_timeout) + @keep_alive_timeout = options.fetch(:keep_alive_timeout,Spidr.keep_alive_timeout) + @sessions = {} end # # Determines if there is an active HTTP session for a given URL. @@ -50,11 +66,11 @@ def active?(url) # normalize the url url = URI(url.to_s) unless url.kind_of?(URI) # session key - key = [url.scheme, url.host, url.port] + key = key_for(url) return @sessions.has_key?(key) end # @@ -69,23 +85,29 @@ def [](url) # normalize the url url = URI(url.to_s) unless url.kind_of?(URI) # session key - key = [url.scheme, url.host, url.port] + key = key_for(url) unless @sessions[key] session = Net::HTTP::Proxy( - @proxy[:host], - @proxy[:port], - @proxy[:user], - @proxy[:password] + @proxy.host, + @proxy.port, + @proxy.user, + @proxy.password ).new(url.host,url.port) + session.open_timeout = @open_timeout if @open_timeout + session.read_timeout = @read_timeout if @read_timeout + session.continue_timeout = @continue_timeout if @continue_timeout + session.keep_alive_timeout = @keep_alive_timeout if @keep_alive_timeout + if url.scheme == 'https' session.use_ssl = true session.verify_mode = OpenSSL::SSL::VERIFY_NONE + session.ssl_timeout = @ssl_timeout session.start end @sessions[key] = session end @@ -106,11 +128,11 @@ def kill!(url) # normalize the url url = URI(url.to_s) unless url.kind_of?(URI) # session key - key = [url.scheme, url.host, url.port] + key = key_for(url) if (sess = @sessions[key]) begin sess.finish rescue IOError @@ -127,19 +149,33 @@ # The cleared session cache. # # @since 0.2.2 # def clear - @sessions.each_value do |sess| + @sessions.each_value do |session| begin - sess.finish + session.finish rescue IOError - nil end end @sessions.clear return self + end + + private + + # + # Creates a session key based on the URL. + # + # @param [URI::HTTP] url + # The given URL. + # + # @return [Array] + # The session key containing the scheme, host and port. + # + def key_for(url) + [url.scheme, url.host, url.port] end end end