# frozen_string_literal: true# Licensed to the Software Freedom Conservancy (SFC) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The SFC licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.require'ipaddr'moduleSeleniummoduleWebDrivermoduleRemotemoduleHttp# @api privateclassDefault<Commonattr_writer:proxyattr_accessor:open_timeout,:read_timeout# 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.definitialize(open_timeout: nil,read_timeout: nil)@open_timeout=open_timeout@read_timeout=read_timeoutsuper()enddefclose@http&.finishendprivatedefhttp@http||=beginhttp=new_http_clientifserver_url.scheme=='https'http.use_ssl=truehttp.verify_mode=OpenSSL::SSL::VERIFY_NONEendhttp.open_timeout=open_timeoutifopen_timeouthttp.read_timeout=read_timeoutifread_timeoutstart(http)httpendenddefstart(http)http.startendMAX_RETRIES=3defrequest(verb,url,headers,payload,redirects=0)retries=0beginrequest=new_request_for(verb,url,headers,payload)response=response_for(request)rescueErrno::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.aspxraiseifretries>=MAX_RETRIESretries+=1sleep2retryrescueErrno::ECONNREFUSED=>eraisee.class,"using proxy: #{proxy.http}"ifuse_proxy?raiseendifresponse.is_a?Net::HTTPRedirectionWebDriver.logger.debug("Redirect to #{response['Location']}; times: #{redirects}")raiseError::WebDriverError,'too many redirects'ifredirects>=MAX_REDIRECTSrequest(:get,URI.parse(response['Location']),DEFAULT_HEADERS.dup,nil,redirects+1)elseWebDriver.logger.debug(" <<< #{response.instance_variable_get(:@header).inspect}",id: :header)create_responseresponse.code,response.body,response.content_typeendenddefnew_request_for(verb,url,headers,payload)req=Net::HTTP.const_get(verb.to_s.capitalize).new(url.path,headers)req.basic_authserver_url.user,server_url.passwordifserver_url.userinforeq.body=payloadifpayloadreqenddefresponse_for(request)http.requestrequestenddefnew_http_clientifuse_proxy?url=@proxy.httpunlessproxy.respond_to?(:http)&&urlraiseError::WebDriverError,"expected HTTP proxy, got #{@proxy.inspect}"endproxy=URI.parse(url)Net::HTTP.new(server_url.host,server_url.port,proxy.host,proxy.port,proxy.user,proxy.password)elseNet::HTTP.newserver_url.host,server_url.portendenddefproxy@proxy||=beginproxy=ENV.fetch('http_proxy',nil)||ENV.fetch('HTTP_PROXY',nil)no_proxy=ENV.fetch('no_proxy',nil)||ENV.fetch('NO_PROXY',nil)ifproxyproxy="http://#{proxy}"unlessproxy.start_with?('http://')Proxy.new(http: proxy,no_proxy: no_proxy)endendenddefuse_proxy?returnfalseifproxy.nil?ifproxy.no_proxyignored=proxy.no_proxy.split(',').any?do|host|host=='*'||host==server_url.host||(beginIPAddr.new(host).include?(server_url.host)rescueArgumentErrorfalseend)end!ignoredelsetrueendendend# Defaultend# Httpend# Remoteend# WebDriverend# Selenium