Path: blob/trunk/rb/lib/selenium/webdriver/remote/http/common.rb
1990 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819module Selenium20module WebDriver21module Remote22module Http23class Common24MAX_REDIRECTS = 20 # same as chromium/gecko25CONTENT_TYPE = 'application/json'26DEFAULT_HEADERS = {27'Accept' => CONTENT_TYPE,28'Content-Type' => "#{CONTENT_TYPE}; charset=UTF-8"29}.freeze3031class << self32attr_accessor :extra_headers33attr_writer :user_agent3435def user_agent36@user_agent ||= "selenium/#{WebDriver::VERSION} (ruby #{Platform.os})"37end38end3940attr_writer :server_url4142def quit_errors43[IOError]44end4546def close47# hook for subclasses - will be called on Driver#quit48end4950# steep:ignore:start51def call(verb, url, command_hash)52url = server_url.merge(url) unless url.is_a?(URI)53headers = common_headers.dup54headers['Cache-Control'] = 'no-cache' if verb == :get5556if command_hash57payload = JSON.generate(command_hash)58headers['Content-Length'] = payload.bytesize.to_s if %i[post put].include?(verb)5960WebDriver.logger.debug(" >>> #{url} | #{payload}", id: :command)61WebDriver.logger.debug(" > #{headers.inspect}", id: :header)62elsif verb == :post63payload = '{}'64headers['Content-Length'] = '2'65end6667request verb, url, headers, payload68end69# steep:ignore:end7071private7273def common_headers74@common_headers ||= begin75headers = DEFAULT_HEADERS.dup76headers['User-Agent'] = Common.user_agent77headers = headers.merge(Common.extra_headers || {})7879headers80end81end8283def server_url84return @server_url if @server_url8586raise Error::WebDriverError, 'server_url not set'87end8889def request(*)90raise NotImplementedError, 'subclass responsibility'91end9293def create_response(code, body, content_type)94code = code.to_i95body = body.to_s.strip96content_type = content_type.to_s97WebDriver.logger.debug("<- #{body}", id: :command)9899if content_type.include? CONTENT_TYPE100raise Error::WebDriverError, "empty body: #{content_type.inspect} (#{code})\n#{body}" if body.empty?101102Response.new(code, JSON.parse(body))103elsif code == 204104Response.new(code)105else106msg = if body.empty?107"unexpected response, code=#{code}, content-type=#{content_type.inspect}"108else109"unexpected response, code=#{code}, content-type=#{content_type.inspect}\n#{body}"110end111112raise Error::WebDriverError, msg113end114end115end # Common116end # Http117end # Remote118end # WebDriver119end # Selenium120121122