class Selenium::WebDriver::Remote::Http::Default

@api private

Constants

MAX_RETRIES

Attributes

open_timeout[RW]
proxy[W]
read_timeout[RW]

Public Class Methods

new(open_timeout: nil, read_timeout: nil) click to toggle source

Initializes object. Warning: Setting {#open_timeout} to non-nil values will cause a separate thread to spawn. Debuggers that freeze the process will not be able to evaluate any operations if that happens. @param [Numeric] open_timeout - Open timeout to apply to HTTP client. @param [Numeric] read_timeout - Read timeout (seconds) to apply to HTTP client.

# File lib/selenium/webdriver/remote/http/default.rb, line 39
def initialize(open_timeout: nil, read_timeout: nil)
  @open_timeout = open_timeout
  @read_timeout = read_timeout
end

Public Instance Methods

close() click to toggle source
# File lib/selenium/webdriver/remote/http/default.rb, line 53
def close
  @http&.finish
end
timeout=(value) click to toggle source

Maintaining backward compatibility. @param [Numeric] value - Timeout in seconds to apply to both open timeout and read timeouts. @deprecated Please set the specific desired timeout {#read_timeout} or {#open_timeout} directly.

# File lib/selenium/webdriver/remote/http/default.rb, line 47
def timeout=(value)
  WebDriver.logger.deprecate ':timeout=', '#read_timeout= and #open_timeout='
  self.open_timeout = value
  self.read_timeout = value
end

Private Instance Methods

http() click to toggle source
# File lib/selenium/webdriver/remote/http/default.rb, line 59
def http
  @http ||= begin
    http = new_http_client
    if server_url.scheme == 'https'
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end

    # Defaulting open_timeout to nil to be consistent with Ruby 2.2 and earlier.
    http.open_timeout = open_timeout
    http.read_timeout = read_timeout if read_timeout

    http.start
  end
end
new_http_client() click to toggle source
# File lib/selenium/webdriver/remote/http/default.rb, line 132
def new_http_client
  if use_proxy?
    url = @proxy.http
    raise Error::WebDriverError, "expected HTTP proxy, got #{@proxy.inspect}" unless proxy.respond_to?(:http) && url

    proxy = URI.parse(url)

    clazz = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password)
    clazz.new(server_url.host, server_url.port)
  else
    Net::HTTP.new server_url.host, server_url.port
  end
end
new_request_for(verb, url, headers, payload) click to toggle source
# File lib/selenium/webdriver/remote/http/default.rb, line 118
def new_request_for(verb, url, headers, payload)
  req = Net::HTTP.const_get(verb.to_s.capitalize).new(url.path, headers)

  req.basic_auth server_url.user, server_url.password if server_url.userinfo

  req.body = payload if payload

  req
end
proxy() click to toggle source
# File lib/selenium/webdriver/remote/http/default.rb, line 146
def proxy
  @proxy ||= begin
    proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
    no_proxy = ENV['no_proxy'] || ENV['NO_PROXY']

    if proxy
      proxy = "http://#{proxy}" unless proxy.start_with?('http://')
      Proxy.new(http: proxy, no_proxy: no_proxy)
    end
  end
end
request(verb, url, headers, payload, redirects = 0) click to toggle source
# File lib/selenium/webdriver/remote/http/default.rb, line 77
def request(verb, url, headers, payload, redirects = 0)
  retries = 0

  begin
    request = new_request_for(verb, url, headers, payload)
    response = response_for(request)
  rescue Errno::ECONNABORTED, Errno::ECONNRESET, Errno::EADDRINUSE
    # a retry is sometimes needed on Windows XP where we may quickly
    # run out of ephemeral ports
    #
    # A more robust solution is bumping the MaxUserPort setting
    # as described here:
    #
    # http://msdn.microsoft.com/en-us/library/aa560610%28v=bts.20%29.aspx
    raise if retries >= MAX_RETRIES

    retries += 1
    sleep 2
    retry
  rescue Errno::EADDRNOTAVAIL => ex
    # a retry is sometimes needed when the port becomes temporarily unavailable
    raise if retries >= MAX_RETRIES

    retries += 1
    sleep 2
    retry
  rescue Errno::ECONNREFUSED => ex
    raise ex.class, "using proxy: #{proxy.http}" if use_proxy?

    raise
  end

  if response.is_a? Net::HTTPRedirection
    raise Error::WebDriverError, 'too many redirects' if redirects >= MAX_REDIRECTS

    request(:get, URI.parse(response['Location']), DEFAULT_HEADERS.dup, nil, redirects + 1)
  else
    create_response response.code, response.body, response.content_type
  end
end
response_for(request) click to toggle source
# File lib/selenium/webdriver/remote/http/default.rb, line 128
def response_for(request)
  http.request request
end
use_proxy?() click to toggle source
# File lib/selenium/webdriver/remote/http/default.rb, line 158
def use_proxy?
  return false if proxy.nil?

  if proxy.no_proxy
    ignored = proxy.no_proxy.split(',').any? do |host|
      host == '*' ||
        host == server_url.host || (
      begin
        IPAddr.new(host).include?(server_url.host)
      rescue ArgumentError
        false
      end
    )
    end

    !ignored
  else
    true
  end
end