Path: blob/trunk/rb/lib/selenium/webdriver/common/proxy.rb
1865 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 WebDriver21class Proxy22TYPES = {23direct: 'DIRECT', # Direct connection, no proxy (default on Windows).24manual: 'MANUAL', # Manual proxy settings (e.g., for httpProxy).25pac: 'PAC', # Proxy autoconfiguration from URL.26auto_detect: 'AUTODETECT', # Proxy autodetection (presumably with WPAD).27system: 'SYSTEM' # Use system settings (default on Linux).28}.freeze2930ALLOWED = {type: 'proxyType',31ftp: 'ftpProxy',32http: 'httpProxy',33no_proxy: 'noProxy',34pac: 'proxyAutoconfigUrl',35ssl: 'sslProxy',36auto_detect: 'autodetect',37socks: 'socksProxy',38socks_username: 'socksUsername',39socks_password: 'socksPassword',40socks_version: 'socksVersion'}.freeze4142ALLOWED.each_key { |t| attr_reader t }4344def self.json_create(data)45data['proxyType'] = data['proxyType'].downcase.to_sym46return if data['proxyType'] == :unspecified4748proxy = new4950ALLOWED.each do |k, v|51proxy.send(:"#{k}=", data[v]) if data.key?(v)52end5354proxy55end5657def initialize(opts = {})58not_allowed = []5960opts.each do |k, v|61if ALLOWED.key?(k)62send(:"#{k}=", v)63else64not_allowed << k65end66end6768return if not_allowed.empty?6970raise ArgumentError, "unknown option#{'s' if not_allowed.size != 1}: #{not_allowed.inspect}"71end7273def ==(other)74other.is_a?(self.class) && as_json == other.as_json75end76alias eql? ==7778def ftp=(value)79WebDriver.logger.deprecate('FTP proxy support', nil, id: :ftp_proxy)80self.type = :manual81@ftp = value82end8384def http=(value)85self.type = :manual86@http = value87end8889def no_proxy=(value)90self.type = :manual91@no_proxy = value92end9394def ssl=(value)95self.type = :manual96@ssl = value97end9899def pac=(url)100self.type = :pac101@pac = url102end103104def auto_detect=(bool)105self.type = :auto_detect106@auto_detect = bool107end108109def socks=(value)110self.type = :manual111@socks = value112end113114def socks_username=(value)115self.type = :manual116@socks_username = value117end118119def socks_password=(value)120self.type = :manual121@socks_password = value122end123124def socks_version=(value)125self.type = :manual126@socks_version = value127end128129def type=(type)130unless TYPES.key? type131raise ArgumentError,132"invalid proxy type: #{type.inspect}, expected one of #{TYPES.keys.inspect}"133end134135if defined?(@type) && type != @type136raise ArgumentError, "incompatible proxy type #{type.inspect} (already set to #{@type.inspect})"137end138139@type = type140end141142def as_json(*)143json_result = {144'proxyType' => TYPES[type].downcase,145'ftpProxy' => ftp,146'httpProxy' => http,147'noProxy' => no_proxy.is_a?(String) ? no_proxy.split(', ') : no_proxy,148'proxyAutoconfigUrl' => pac,149'sslProxy' => ssl,150'autodetect' => auto_detect,151'socksProxy' => socks,152'socksUsername' => socks_username,153'socksPassword' => socks_password,154'socksVersion' => socks_version155}.compact156157json_result if json_result.length > 1158end159160def to_json(*)161JSON.generate as_json162end163end # Proxy164end # WebDriver165end # Selenium166167168