diff --git a/dotnet/src/webdriver/Proxy.cs b/dotnet/src/webdriver/Proxy.cs index 0216c50105ce8..fbbf55a3b9c97 100644 --- a/dotnet/src/webdriver/Proxy.cs +++ b/dotnet/src/webdriver/Proxy.cs @@ -23,6 +23,8 @@ using System.Globalization; using System.Text.Json.Serialization; +#nullable enable + namespace OpenQA.Selenium { /// @@ -72,13 +74,13 @@ public class Proxy { private ProxyKind proxyKind = ProxyKind.Unspecified; private bool isAutoDetect; - private string ftpProxyLocation; - private string httpProxyLocation; - private string proxyAutoConfigUrl; - private string sslProxyLocation; - private string socksProxyLocation; - private string socksUserName; - private string socksPassword; + private string? ftpProxyLocation; + private string? httpProxyLocation; + private string? proxyAutoConfigUrl; + private string? sslProxyLocation; + private string? socksProxyLocation; + private string? socksUserName; + private string? socksPassword; private int? socksVersion; private List noProxyAddresses = new List(); @@ -93,6 +95,8 @@ public Proxy() /// Initializes a new instance of the class with the given proxy settings. /// /// A dictionary of settings to use with the proxy. + /// If is . + /// If The "noProxy" value is a list with a element. public Proxy(Dictionary settings) { if (settings == null) @@ -100,46 +104,44 @@ public Proxy(Dictionary settings) throw new ArgumentNullException(nameof(settings), "settings dictionary cannot be null"); } - if (settings.ContainsKey("proxyType") && settings["proxyType"] != null) + if (settings.TryGetValue("proxyType", out object? proxyTypeObj) && proxyTypeObj?.ToString() is string proxyType) { // Special-case "PAC" since that is the correct serialization. - if (settings["proxyType"].ToString().ToLowerInvariant() == "pac") + if (proxyType.Equals("pac", StringComparison.InvariantCultureIgnoreCase)) { this.Kind = ProxyKind.ProxyAutoConfigure; } else { - ProxyKind rawType = (ProxyKind)Enum.Parse(typeof(ProxyKind), settings["proxyType"].ToString(), true); + ProxyKind rawType = (ProxyKind)Enum.Parse(typeof(ProxyKind), proxyType, ignoreCase: true); this.Kind = rawType; } } - if (settings.ContainsKey("ftpProxy") && settings["ftpProxy"] != null) + if (settings.TryGetValue("ftpProxy", out object? ftpProxyObj) && ftpProxyObj?.ToString() is string ftpProxy) { - this.FtpProxy = settings["ftpProxy"].ToString(); + this.FtpProxy = ftpProxy; } - if (settings.ContainsKey("httpProxy") && settings["httpProxy"] != null) + if (settings.TryGetValue("httpProxy", out object? httpProxyObj) && httpProxyObj?.ToString() is string httpProxy) { - this.HttpProxy = settings["httpProxy"].ToString(); + this.HttpProxy = httpProxy; } - if (settings.ContainsKey("noProxy") && settings["noProxy"] != null) + if (settings.TryGetValue("noProxy", out object? noProxy) && noProxy != null) { List bypassAddresses = new List(); - string addressesAsString = settings["noProxy"] as string; - if (addressesAsString != null) + if (noProxy is string addressesAsString) { bypassAddresses.AddRange(addressesAsString.Split(';')); } else { - object[] addressesAsArray = settings["noProxy"] as object[]; - if (addressesAsArray != null) + if (noProxy is object?[] addressesAsArray) { - foreach (object address in addressesAsArray) + foreach (object? address in addressesAsArray) { - bypassAddresses.Add(address.ToString()); + bypassAddresses.Add(address?.ToString() ?? throw new ArgumentException("Proxy bypass address list \"noProxy\" contained a null element", nameof(settings))); } } } @@ -147,39 +149,39 @@ public Proxy(Dictionary settings) this.AddBypassAddresses(bypassAddresses); } - if (settings.ContainsKey("proxyAutoconfigUrl") && settings["proxyAutoconfigUrl"] != null) + if (settings.TryGetValue("proxyAutoconfigUrl", out object? proxyAutoconfigUrlObj) && proxyAutoconfigUrlObj?.ToString() is string proxyAutoconfigUrl) { - this.ProxyAutoConfigUrl = settings["proxyAutoconfigUrl"].ToString(); + this.ProxyAutoConfigUrl = proxyAutoconfigUrl; } - if (settings.ContainsKey("sslProxy") && settings["sslProxy"] != null) + if (settings.TryGetValue("sslProxy", out object? sslProxyObj) && sslProxyObj?.ToString() is string sslProxy) { - this.SslProxy = settings["sslProxy"].ToString(); + this.SslProxy = sslProxy; } - if (settings.ContainsKey("socksProxy") && settings["socksProxy"] != null) + if (settings.TryGetValue("socksProxy", out object? socksProxyObj) && socksProxyObj?.ToString() is string socksProxy) { - this.SocksProxy = settings["socksProxy"].ToString(); + this.SocksProxy = socksProxy; } - if (settings.ContainsKey("socksUsername") && settings["socksUsername"] != null) + if (settings.TryGetValue("socksUsername", out object? socksUsernameObj) && socksUsernameObj?.ToString() is string socksUsername) { - this.SocksUserName = settings["socksUsername"].ToString(); + this.SocksUserName = socksUsername; } - if (settings.ContainsKey("socksPassword") && settings["socksPassword"] != null) + if (settings.TryGetValue("socksPassword", out object? socksPasswordObj) && socksPasswordObj?.ToString() is string socksPassword) { - this.SocksPassword = settings["socksPassword"].ToString(); + this.SocksPassword = socksPassword; } - if (settings.ContainsKey("socksVersion") && settings["socksVersion"] != null) + if (settings.TryGetValue("socksVersion", out object? socksVersion) && socksVersion != null) { - this.SocksVersion = Convert.ToInt32(settings["socksVersion"]); + this.SocksVersion = Convert.ToInt32(socksVersion); } - if (settings.ContainsKey("autodetect") && settings["autodetect"] != null) + if (settings.TryGetValue("autodetect", out object? autodetect) && autodetect != null) { - this.IsAutoDetect = (bool)settings["autodetect"]; + this.IsAutoDetect = Convert.ToBoolean(autodetect); } } @@ -189,10 +191,7 @@ public Proxy(Dictionary settings) [JsonIgnore] public ProxyKind Kind { - get - { - return this.proxyKind; - } + get => this.proxyKind; set { @@ -224,10 +223,7 @@ public string SerializableProxyKind [JsonIgnore] public bool IsAutoDetect { - get - { - return this.isAutoDetect; - } + get => this.isAutoDetect; set { @@ -247,12 +243,9 @@ public bool IsAutoDetect /// [JsonPropertyName("ftpProxy")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] - public string FtpProxy + public string? FtpProxy { - get - { - return this.ftpProxyLocation; - } + get => this.ftpProxyLocation; set { @@ -267,12 +260,9 @@ public string FtpProxy /// [JsonPropertyName("httpProxy")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] - public string HttpProxy + public string? HttpProxy { - get - { - return this.httpProxyLocation; - } + get => this.httpProxyLocation; set { @@ -287,7 +277,7 @@ public string HttpProxy /// [JsonPropertyName("noProxy")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] - public ReadOnlyCollection BypassProxyAddresses + public ReadOnlyCollection? BypassProxyAddresses { get { @@ -305,12 +295,9 @@ public ReadOnlyCollection BypassProxyAddresses /// [JsonPropertyName("proxyAutoconfigUrl")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] - public string ProxyAutoConfigUrl + public string? ProxyAutoConfigUrl { - get - { - return this.proxyAutoConfigUrl; - } + get => this.proxyAutoConfigUrl; set { @@ -325,12 +312,9 @@ public string ProxyAutoConfigUrl /// [JsonPropertyName("sslProxy")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] - public string SslProxy + public string? SslProxy { - get - { - return this.sslProxyLocation; - } + get => this.sslProxyLocation; set { @@ -345,12 +329,9 @@ public string SslProxy /// [JsonPropertyName("socksProxy")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] - public string SocksProxy + public string? SocksProxy { - get - { - return this.socksProxyLocation; - } + get => this.socksProxyLocation; set { @@ -365,12 +346,9 @@ public string SocksProxy /// [JsonPropertyName("socksUsername")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] - public string SocksUserName + public string? SocksUserName { - get - { - return this.socksUserName; - } + get => this.socksUserName; set { @@ -388,10 +366,7 @@ public string SocksUserName [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public int? SocksVersion { - get - { - return this.socksVersion; - } + get => this.socksVersion; set { @@ -403,7 +378,7 @@ public int? SocksVersion { if (value.Value <= 0) { - throw new ArgumentException("SocksVersion must be a positive integer"); + throw new ArgumentOutOfRangeException(nameof(value), "SocksVersion must be a positive integer"); } this.VerifyProxyTypeCompatilibily(ProxyKind.Manual); @@ -418,12 +393,9 @@ public int? SocksVersion /// [JsonPropertyName("socksPassword")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] - public string SocksPassword + public string? SocksPassword { - get - { - return this.socksPassword; - } + get => this.socksPassword; set { @@ -453,7 +425,7 @@ public void AddBypassAddress(string address) /// An array of addresses to add. public void AddBypassAddresses(params string[] addressesToAdd) { - this.AddBypassAddresses(new List(addressesToAdd)); + this.AddBypassAddresses((IEnumerable)addressesToAdd); } /// @@ -478,7 +450,7 @@ public void AddBypassAddresses(IEnumerable addressesToAdd) /// /// A dictionary suitable for serializing to the W3C Specification /// dialect of the wire protocol. - internal Dictionary ToCapability() + internal Dictionary? ToCapability() { return this.AsDictionary(true); } @@ -489,17 +461,17 @@ internal Dictionary ToCapability() /// /// A dictionary suitable for serializing to the OSS dialect of the /// wire protocol. - internal Dictionary ToLegacyCapability() + internal Dictionary? ToLegacyCapability() { return this.AsDictionary(false); } - private Dictionary AsDictionary(bool isSpecCompliant) + private Dictionary? AsDictionary(bool isSpecCompliant) { - Dictionary serializedDictionary = null; + Dictionary? serializedDictionary = null; if (this.proxyKind != ProxyKind.Unspecified) { - serializedDictionary = new Dictionary(); + serializedDictionary = new Dictionary(); if (this.proxyKind == ProxyKind.ProxyAutoConfigure) { serializedDictionary["proxyType"] = "pac"; @@ -556,17 +528,12 @@ private Dictionary AsDictionary(bool isSpecCompliant) return serializedDictionary; } - private object GetNoProxyAddressList(bool isSpecCompliant) + private object? GetNoProxyAddressList(bool isSpecCompliant) { - object addresses = null; + object? addresses = null; if (isSpecCompliant) { - List addressList = new List(); - foreach (string address in this.noProxyAddresses) - { - addressList.Add(address); - } - + List addressList = [.. this.noProxyAddresses]; addresses = addressList; } else