123456789_123456789_123456789_123456789_123456789_

Class: Selenium::WebDriver::Remote::Http::Default Private

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Common
Instance Chain:
self, Common
Inherits: Selenium::WebDriver::Remote::Http::Common
Defined in: rb/lib/selenium/webdriver/remote/http/default.rb

Constant Summary

Common - Inherited

CONTENT_TYPE, DEFAULT_HEADERS, MAX_REDIRECTS

Class Attribute Summary

Class Method Summary

Instance Attribute Summary

Common - Inherited

Instance Method Summary

Common - Inherited

Constructor Details

.new(open_timeout: nil, read_timeout: nil) ⇒ Default

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.

Parameters:

  • open_timeout (Numeric)
    • Open timeout to apply to HTTP client.

  • read_timeout (Numeric)
    • Read timeout (seconds) to apply to HTTP client.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/http/default.rb', line 36

def initialize(open_timeout: nil, read_timeout: nil)
  @open_timeout = open_timeout
  @read_timeout = read_timeout
  super()
end

Instance Attribute Details

#open_timeout (rw)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/http/default.rb', line 29

attr_accessor :open_timeout, :read_timeout

#proxy (rw, private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/http/default.rb', line 137

def proxy
  @proxy ||= begin
    proxy = ENV.fetch('http_proxy', nil) || ENV.fetch('HTTP_PROXY', nil)
    no_proxy = ENV.fetch('no_proxy', nil) || ENV.fetch('NO_PROXY', nil)

    if proxy
      proxy = "http://#{proxy}" unless proxy.start_with?('http://')
      Proxy.new(http: proxy, no_proxy: no_proxy)
    end
  end
end

#proxy=(value) (rw)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/http/default.rb', line 27

attr_writer :proxy

#read_timeout (rw)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/http/default.rb', line 29

attr_accessor :open_timeout, :read_timeout

#use_proxy?Boolean (readonly, private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/http/default.rb', line 149

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

Instance Method Details

#close

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/http/default.rb', line 42

def close
  @http&.finish
end

#http (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/http/default.rb', line 48

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

    http.open_timeout = open_timeout if open_timeout
    http.read_timeout = read_timeout if read_timeout

    start(http)
    http
  end
end

#new_http_client (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/http/default.rb', line 121

def new_http_client
  if use_proxy?
    url = @proxy.http
    unless proxy.respond_to?(:http) && url
      raise Error::WebDriverError,
            "expected HTTP proxy, got #{@proxy.inspect}"
    end

    proxy = URI.parse(url)

    Net::HTTP.new(server_url.host, server_url.port, proxy.host, proxy.port, proxy.user, proxy.password)
  else
    Net::HTTP.new server_url.host, server_url.port
  end
end

#new_request_for(verb, url, headers, payload) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/http/default.rb', line 107

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

#request(verb, url, headers, payload, redirects = 0) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/http/default.rb', line 70

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, Errno::EADDRNOTAVAIL
    # a retry is sometimes needed:
    #   on Windows XP where we may quickly run out of ephemeral ports
    #   when the port becomes temporarily unavailable
    #
    # 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::ECONNREFUSED => e
    raise e.class, "using proxy: #{proxy.http}" if use_proxy?

    raise
  end

  if response.is_a? Net::HTTPRedirection
    WebDriver.logger.debug("Redirect to #{response['Location']}; times: #{redirects}")
    raise Error::WebDriverError, 'too many redirects' if redirects >= MAX_REDIRECTS

    request(:get, URI.parse(response['Location']), DEFAULT_HEADERS.dup, nil, redirects + 1)
  else
    WebDriver.logger.debug("   <<<  #{response.instance_variable_get(:@header).inspect}", id: :header)
    create_response response.code, response.body, response.content_type
  end
end

#response_for(request) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/http/default.rb', line 117

def response_for(request)
  http.request request
end

#start(http) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/http/default.rb', line 64

def start(http)
  http.start
end