class Selenium::WebDriver::Remote::Http::Common

Constants

BINARY_ENCODINGS
CONTENT_TYPE
DEFAULT_HEADERS
MAX_REDIRECTS

Attributes

extra_headers[RW]
user_agent[W]
server_url[W]

Public Class Methods

user_agent() click to toggle source
# File lib/selenium/webdriver/remote/http/common.rb, line 37
def user_agent
  @user_agent ||= "selenium/#{WebDriver::VERSION} (ruby #{Platform.os})"
end

Public Instance Methods

call(verb, url, command_hash) click to toggle source

steep:ignore:start

# File lib/selenium/webdriver/remote/http/common.rb, line 53
def call(verb, url, command_hash)
  url      = server_url.merge(url) unless url.is_a?(URI)
  headers  = common_headers.dup
  headers['Cache-Control'] = 'no-cache' if verb == :get

  if command_hash
    command_hash              = ensure_utf8_encoding(command_hash)
    payload                   = JSON.generate(command_hash)
    headers['Content-Length'] = payload.bytesize.to_s if %i[post put].include?(verb)

    WebDriver.logger.debug("   >>> #{url} | #{payload}", id: :command)
    WebDriver.logger.debug("     > #{headers.inspect}", id: :header)
  elsif verb == :post
    payload = '{}'
    headers['Content-Length'] = '2'
  end

  request verb, url, headers, payload
end
close() click to toggle source
# File lib/selenium/webdriver/remote/http/common.rb, line 48
def close
  # hook for subclasses - will be called on Driver#quit
end
quit_errors() click to toggle source
# File lib/selenium/webdriver/remote/http/common.rb, line 44
def quit_errors
  [IOError]
end

Private Instance Methods

common_headers() click to toggle source

steep:ignore:end

# File lib/selenium/webdriver/remote/http/common.rb, line 76
def common_headers
  @common_headers ||= begin
    headers = DEFAULT_HEADERS.dup
    headers['User-Agent'] = Common.user_agent
    headers = headers.merge(Common.extra_headers || {})

    headers
  end
end
create_response(code, body, content_type) click to toggle source
# File lib/selenium/webdriver/remote/http/common.rb, line 126
def create_response(code, body, content_type)
  code = code.to_i
  body = body.to_s.strip
  content_type = content_type.to_s
  WebDriver.logger.debug("<- #{body}", id: :command)

  if content_type.include? CONTENT_TYPE
    raise Error::WebDriverError, "empty body: #{content_type.inspect} (#{code})\n#{body}" if body.empty?

    Response.new(code, JSON.parse(body))
  elsif code == 204
    Response.new(code)
  else
    msg = if body.empty?
            "unexpected response, code=#{code}, content-type=#{content_type.inspect}"
          else
            "unexpected response, code=#{code}, content-type=#{content_type.inspect}\n#{body}"
          end

    raise Error::WebDriverError, msg
  end
end
encode_string_to_utf8(str) click to toggle source
# File lib/selenium/webdriver/remote/http/common.rb, line 111
def encode_string_to_utf8(str)
  return str if str.encoding == Encoding::UTF_8 && str.valid_encoding?

  if BINARY_ENCODINGS.include?(str.encoding)
    result = str.dup.force_encoding(Encoding::UTF_8)
    return result if result.valid_encoding?
  end

  str.encode(Encoding::UTF_8)
rescue EncodingError => e
  raise Error::WebDriverError,
        "Unable to encode string to UTF-8: #{e.message}. " \
        "String encoding: #{str.encoding}, content: #{str.inspect}"
end
ensure_utf8_encoding(obj) click to toggle source
# File lib/selenium/webdriver/remote/http/common.rb, line 96
def ensure_utf8_encoding(obj)
  case obj
  when String
    encode_string_to_utf8(obj)
  when Array
    obj.map { |item| ensure_utf8_encoding(item) }
  when Hash
    obj.each_with_object({}) do |(key, value), result|
      result[ensure_utf8_encoding(key)] = ensure_utf8_encoding(value)
    end
  else
    obj
  end
end
request(*) click to toggle source
# File lib/selenium/webdriver/remote/http/common.rb, line 92
def request(*)
  raise NotImplementedError, 'subclass responsibility'
end
server_url() click to toggle source
# File lib/selenium/webdriver/remote/http/common.rb, line 86
def server_url
  return @server_url if @server_url

  raise Error::WebDriverError, 'server_url not set'
end