"Fossies" - the Fresh Open Source Software Archive

Member "selenium-selenium-4.8.1/rb/lib/selenium/webdriver/remote/http/common.rb" (17 Feb 2023, 3417 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # frozen_string_literal: true
    2 
    3 # Licensed to the Software Freedom Conservancy (SFC) under one
    4 # or more contributor license agreements.  See the NOTICE file
    5 # distributed with this work for additional information
    6 # regarding copyright ownership.  The SFC licenses this file
    7 # to you under the Apache License, Version 2.0 (the
    8 # "License"); you may not use this file except in compliance
    9 # with the License.  You may obtain a copy of the License at
   10 #
   11 #   http://www.apache.org/licenses/LICENSE-2.0
   12 #
   13 # Unless required by applicable law or agreed to in writing,
   14 # software distributed under the License is distributed on an
   15 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   16 # KIND, either express or implied.  See the License for the
   17 # specific language governing permissions and limitations
   18 # under the License.
   19 
   20 module Selenium
   21   module WebDriver
   22     module Remote
   23       module Http
   24         class Common
   25           MAX_REDIRECTS   = 20 # same as chromium/gecko
   26           CONTENT_TYPE    = 'application/json'
   27           DEFAULT_HEADERS = {
   28             'Accept' => CONTENT_TYPE,
   29             'Content-Type' => "#{CONTENT_TYPE}; charset=UTF-8",
   30             'User-Agent' => "selenium/#{WebDriver::VERSION} (ruby #{Platform.os})"
   31           }.freeze
   32 
   33           attr_writer :server_url
   34 
   35           def quit_errors
   36             [IOError]
   37           end
   38 
   39           def close
   40             # hook for subclasses - will be called on Driver#quit
   41           end
   42 
   43           def call(verb, url, command_hash)
   44             url      = server_url.merge(url) unless url.is_a?(URI)
   45             headers  = DEFAULT_HEADERS.dup
   46             headers['Cache-Control'] = 'no-cache' if verb == :get
   47 
   48             if command_hash
   49               payload                   = JSON.generate(command_hash)
   50               headers['Content-Length'] = payload.bytesize.to_s if %i[post put].include?(verb)
   51 
   52               WebDriver.logger.info("   >>> #{url} | #{payload}")
   53               WebDriver.logger.debug("     > #{headers.inspect}")
   54             elsif verb == :post
   55               payload = '{}'
   56               headers['Content-Length'] = '2'
   57             end
   58 
   59             request verb, url, headers, payload
   60           end
   61 
   62           private
   63 
   64           def server_url
   65             return @server_url if @server_url
   66 
   67             raise Error::WebDriverError, 'server_url not set'
   68           end
   69 
   70           def request(*)
   71             raise NotImplementedError, 'subclass responsibility'
   72           end
   73 
   74           def create_response(code, body, content_type)
   75             code = code.to_i
   76             body = body.to_s.strip
   77             content_type = content_type.to_s
   78             WebDriver.logger.info("<- #{body}")
   79 
   80             if content_type.include? CONTENT_TYPE
   81               raise Error::WebDriverError, "empty body: #{content_type.inspect} (#{code})\n#{body}" if body.empty?
   82 
   83               Response.new(code, JSON.parse(body))
   84             elsif code == 204
   85               Response.new(code)
   86             else
   87               msg = if body.empty?
   88                       "unexpected response, code=#{code}, content-type=#{content_type.inspect}"
   89                     else
   90                       "unexpected response, code=#{code}, content-type=#{content_type.inspect}\n#{body}"
   91                     end
   92 
   93               raise Error::WebDriverError, msg
   94             end
   95           end
   96         end # Common
   97       end # Http
   98     end # Remote
   99   end # WebDriver
  100 end # Selenium