2323using System . Globalization ;
2424using System . Text . Json . Serialization ;
2525
26+ #nullable enable
27+
2628namespace OpenQA . Selenium
2729{
2830 /// <summary>
@@ -72,13 +74,13 @@ public class Proxy
7274 {
7375 private ProxyKind proxyKind = ProxyKind . Unspecified ;
7476 private bool isAutoDetect ;
75- private string ftpProxyLocation ;
76- private string httpProxyLocation ;
77- private string proxyAutoConfigUrl ;
78- private string sslProxyLocation ;
79- private string socksProxyLocation ;
80- private string socksUserName ;
81- private string socksPassword ;
77+ private string ? ftpProxyLocation ;
78+ private string ? httpProxyLocation ;
79+ private string ? proxyAutoConfigUrl ;
80+ private string ? sslProxyLocation ;
81+ private string ? socksProxyLocation ;
82+ private string ? socksUserName ;
83+ private string ? socksPassword ;
8284 private int ? socksVersion ;
8385 private List < string > noProxyAddresses = new List < string > ( ) ;
8486
@@ -93,93 +95,90 @@ public Proxy()
9395 /// Initializes a new instance of the <see cref="Proxy"/> class with the given proxy settings.
9496 /// </summary>
9597 /// <param name="settings">A dictionary of settings to use with the proxy.</param>
98+ /// <exception cref="ArgumentNullException">If <paramref name="settings"/> is <see langword="null"/>.</exception>
99+ /// <exception cref="ArgumentException">If The "noProxy" value is a list with a <see langword="null"/> element.</exception>
96100 public Proxy ( Dictionary < string , object > settings )
97101 {
98102 if ( settings == null )
99103 {
100104 throw new ArgumentNullException ( nameof ( settings ) , "settings dictionary cannot be null" ) ;
101105 }
102106
103- if ( settings . ContainsKey ( "proxyType" ) && settings [ "proxyType" ] != null )
107+ if ( settings . TryGetValue ( "proxyType" , out object ? proxyTypeObj ) && proxyTypeObj ? . ToString ( ) is string proxyType )
104108 {
105109 // Special-case "PAC" since that is the correct serialization.
106- if ( settings [ " proxyType" ] . ToString ( ) . ToLowerInvariant ( ) == "pac" )
110+ if ( proxyType . Equals ( "pac" , StringComparison . InvariantCultureIgnoreCase ) )
107111 {
108112 this . Kind = ProxyKind . ProxyAutoConfigure ;
109113 }
110114 else
111115 {
112- ProxyKind rawType = ( ProxyKind ) Enum . Parse ( typeof ( ProxyKind ) , settings [ "proxyType" ] . ToString ( ) , true ) ;
116+ ProxyKind rawType = Enum . Parse < ProxyKind > ( proxyType , ignoreCase : true ) ;
113117 this . Kind = rawType ;
114118 }
115119 }
116120
117- if ( settings . ContainsKey ( "ftpProxy" ) && settings [ "ftpProxy" ] != null )
121+ if ( settings . TryGetValue ( "ftpProxy" , out object ? ftpProxyObj ) && ftpProxyObj ? . ToString ( ) is string ftpProxy )
118122 {
119- this . FtpProxy = settings [ " ftpProxy" ] . ToString ( ) ;
123+ this . FtpProxy = ftpProxy ;
120124 }
121125
122- if ( settings . ContainsKey ( "httpProxy" ) && settings [ "httpProxy" ] != null )
126+ if ( settings . TryGetValue ( "httpProxy" , out object ? httpProxyObj ) && httpProxyObj ? . ToString ( ) is string httpProxy )
123127 {
124- this . HttpProxy = settings [ " httpProxy" ] . ToString ( ) ;
128+ this . HttpProxy = httpProxy ;
125129 }
126130
127- if ( settings . ContainsKey ( "noProxy" ) && settings [ " noProxy" ] != null )
131+ if ( settings . TryGetValue ( "noProxy" , out object ? noProxy ) && noProxy != null )
128132 {
129133 List < string > bypassAddresses = new List < string > ( ) ;
130- string addressesAsString = settings [ "noProxy" ] as string ;
131- if ( addressesAsString != null )
134+ if ( noProxy is string addressesAsString )
132135 {
133136 bypassAddresses . AddRange ( addressesAsString . Split ( ';' ) ) ;
134137 }
135- else
138+ else if ( noProxy is object ? [ ] addressesAsArray )
136139 {
137- object [ ] addressesAsArray = settings [ "noProxy" ] as object [ ] ;
138- if ( addressesAsArray != null )
140+ foreach ( object ? address in addressesAsArray )
139141 {
140- foreach ( object address in addressesAsArray )
141- {
142- bypassAddresses . Add ( address . ToString ( ) ) ;
143- }
142+ bypassAddresses . Add ( address ? . ToString ( ) ?? throw new ArgumentException ( "noProxy list contained null element" , nameof ( settings ) ) ) ;
144143 }
145144 }
146145
147146 this . AddBypassAddresses ( bypassAddresses ) ;
148147 }
149148
150- if ( settings . ContainsKey ( "proxyAutoconfigUrl" ) && settings [ "proxyAutoconfigUrl" ] != null )
149+ if ( settings . TryGetValue ( "proxyAutoconfigUrl" , out object ? proxyAutoconfigUrlObj ) && proxyAutoconfigUrlObj ? . ToString ( ) is string proxyAutoconfigUrl )
151150 {
152- this . ProxyAutoConfigUrl = settings [ " proxyAutoconfigUrl" ] . ToString ( ) ;
151+ this . ProxyAutoConfigUrl = proxyAutoconfigUrl ;
153152 }
154153
155- if ( settings . ContainsKey ( "sslProxy" ) && settings [ "sslProxy" ] != null )
154+ if ( settings . TryGetValue ( "sslProxy" , out object ? sslProxyObj ) && sslProxyObj ? . ToString ( ) is string sslProxy )
156155 {
157- this . SslProxy = settings [ " sslProxy" ] . ToString ( ) ;
156+ this . SslProxy = sslProxy ;
158157 }
159158
160- if ( settings . ContainsKey ( "socksProxy" ) && settings [ "socksProxy" ] != null )
159+ if ( settings . TryGetValue ( "socksProxy" , out object ? socksProxyObj ) && socksProxyObj ? . ToString ( ) is string socksProxy )
161160 {
162- this . SocksProxy = settings [ " socksProxy" ] . ToString ( ) ;
161+ this . SocksProxy = socksProxy ;
163162 }
164163
165- if ( settings . ContainsKey ( "socksUsername" ) && settings [ "socksUsername" ] != null )
164+ if ( settings . TryGetValue ( "socksUsername" , out object ? socksUsernameObj ) && socksUsernameObj ? . ToString ( ) is string socksUsername )
166165 {
167- this . SocksUserName = settings [ " socksUsername" ] . ToString ( ) ;
166+ this . SocksUserName = socksUsername ;
168167 }
169168
170- if ( settings . ContainsKey ( "socksPassword" ) && settings [ "socksPassword" ] != null )
169+ if ( settings . TryGetValue ( "socksPassword" , out object ? socksPasswordObj ) && socksPasswordObj ? . ToString ( ) is string socksPassword )
171170 {
172- this . SocksPassword = settings [ " socksPassword" ] . ToString ( ) ;
171+ this . SocksPassword = socksPassword ;
173172 }
174173
175- if ( settings . ContainsKey ( "socksVersion" ) && settings [ " socksVersion" ] != null )
174+ if ( settings . TryGetValue ( "socksVersion" , out object ? socksVersion ) && socksVersion != null )
176175 {
177- this . SocksVersion = Convert . ToInt32 ( settings [ " socksVersion" ] ) ;
176+ this . SocksVersion = Convert . ToInt32 ( socksVersion ) ;
178177 }
179178
180- if ( settings . ContainsKey ( "autodetect" ) && settings [ " autodetect" ] != null )
179+ if ( settings . TryGetValue ( "autodetect" , out object ? autodetect ) && autodetect != null )
181180 {
182- this . IsAutoDetect = ( bool ) settings [ " autodetect" ] ;
181+ this . IsAutoDetect = Convert . ToBoolean ( autodetect ) ;
183182 }
184183 }
185184
@@ -189,10 +188,7 @@ public Proxy(Dictionary<string, object> settings)
189188 [ JsonIgnore ]
190189 public ProxyKind Kind
191190 {
192- get
193- {
194- return this . proxyKind ;
195- }
191+ get => this . proxyKind ;
196192
197193 set
198194 {
@@ -224,10 +220,7 @@ public string SerializableProxyKind
224220 [ JsonIgnore ]
225221 public bool IsAutoDetect
226222 {
227- get
228- {
229- return this . isAutoDetect ;
230- }
223+ get => this . isAutoDetect ;
231224
232225 set
233226 {
@@ -247,12 +240,9 @@ public bool IsAutoDetect
247240 /// </summary>
248241 [ JsonPropertyName ( "ftpProxy" ) ]
249242 [ JsonIgnore ( Condition = JsonIgnoreCondition . WhenWritingDefault ) ]
250- public string FtpProxy
243+ public string ? FtpProxy
251244 {
252- get
253- {
254- return this . ftpProxyLocation ;
255- }
245+ get => this . ftpProxyLocation ;
256246
257247 set
258248 {
@@ -267,12 +257,9 @@ public string FtpProxy
267257 /// </summary>
268258 [ JsonPropertyName ( "httpProxy" ) ]
269259 [ JsonIgnore ( Condition = JsonIgnoreCondition . WhenWritingDefault ) ]
270- public string HttpProxy
260+ public string ? HttpProxy
271261 {
272- get
273- {
274- return this . httpProxyLocation ;
275- }
262+ get => this . httpProxyLocation ;
276263
277264 set
278265 {
@@ -287,7 +274,7 @@ public string HttpProxy
287274 /// </summary>
288275 [ JsonPropertyName ( "noProxy" ) ]
289276 [ JsonIgnore ( Condition = JsonIgnoreCondition . WhenWritingDefault ) ]
290- public ReadOnlyCollection < string > BypassProxyAddresses
277+ public ReadOnlyCollection < string > ? BypassProxyAddresses
291278 {
292279 get
293280 {
@@ -305,12 +292,9 @@ public ReadOnlyCollection<string> BypassProxyAddresses
305292 /// </summary>
306293 [ JsonPropertyName ( "proxyAutoconfigUrl" ) ]
307294 [ JsonIgnore ( Condition = JsonIgnoreCondition . WhenWritingDefault ) ]
308- public string ProxyAutoConfigUrl
295+ public string ? ProxyAutoConfigUrl
309296 {
310- get
311- {
312- return this . proxyAutoConfigUrl ;
313- }
297+ get => this . proxyAutoConfigUrl ;
314298
315299 set
316300 {
@@ -325,12 +309,9 @@ public string ProxyAutoConfigUrl
325309 /// </summary>
326310 [ JsonPropertyName ( "sslProxy" ) ]
327311 [ JsonIgnore ( Condition = JsonIgnoreCondition . WhenWritingDefault ) ]
328- public string SslProxy
312+ public string ? SslProxy
329313 {
330- get
331- {
332- return this . sslProxyLocation ;
333- }
314+ get => this . sslProxyLocation ;
334315
335316 set
336317 {
@@ -345,12 +326,9 @@ public string SslProxy
345326 /// </summary>
346327 [ JsonPropertyName ( "socksProxy" ) ]
347328 [ JsonIgnore ( Condition = JsonIgnoreCondition . WhenWritingDefault ) ]
348- public string SocksProxy
329+ public string ? SocksProxy
349330 {
350- get
351- {
352- return this . socksProxyLocation ;
353- }
331+ get => this . socksProxyLocation ;
354332
355333 set
356334 {
@@ -365,12 +343,9 @@ public string SocksProxy
365343 /// </summary>
366344 [ JsonPropertyName ( "socksUsername" ) ]
367345 [ JsonIgnore ( Condition = JsonIgnoreCondition . WhenWritingDefault ) ]
368- public string SocksUserName
346+ public string ? SocksUserName
369347 {
370- get
371- {
372- return this . socksUserName ;
373- }
348+ get => this . socksUserName ;
374349
375350 set
376351 {
@@ -388,10 +363,7 @@ public string SocksUserName
388363 [ JsonIgnore ( Condition = JsonIgnoreCondition . WhenWritingDefault ) ]
389364 public int ? SocksVersion
390365 {
391- get
392- {
393- return this . socksVersion ;
394- }
366+ get => this . socksVersion ;
395367
396368 set
397369 {
@@ -403,7 +375,7 @@ public int? SocksVersion
403375 {
404376 if ( value . Value <= 0 )
405377 {
406- throw new ArgumentException ( "SocksVersion must be a positive integer" ) ;
378+ throw new ArgumentOutOfRangeException ( nameof ( value ) , "SocksVersion must be a positive integer" ) ;
407379 }
408380
409381 this . VerifyProxyTypeCompatilibily ( ProxyKind . Manual ) ;
@@ -418,12 +390,9 @@ public int? SocksVersion
418390 /// </summary>
419391 [ JsonPropertyName ( "socksPassword" ) ]
420392 [ JsonIgnore ( Condition = JsonIgnoreCondition . WhenWritingDefault ) ]
421- public string SocksPassword
393+ public string ? SocksPassword
422394 {
423- get
424- {
425- return this . socksPassword ;
426- }
395+ get => this . socksPassword ;
427396
428397 set
429398 {
@@ -453,7 +422,7 @@ public void AddBypassAddress(string address)
453422 /// <param name="addressesToAdd">An array of addresses to add.</param>
454423 public void AddBypassAddresses ( params string [ ] addressesToAdd )
455424 {
456- this . AddBypassAddresses ( new List < string > ( addressesToAdd ) ) ;
425+ this . AddBypassAddresses ( ( IEnumerable < string > ) addressesToAdd ) ;
457426 }
458427
459428 /// <summary>
@@ -478,7 +447,7 @@ public void AddBypassAddresses(IEnumerable<string> addressesToAdd)
478447 /// </summary>
479448 /// <returns>A dictionary suitable for serializing to the W3C Specification
480449 /// dialect of the wire protocol.</returns>
481- internal Dictionary < string , object > ToCapability ( )
450+ internal Dictionary < string , object ? > ? ToCapability ( )
482451 {
483452 return this . AsDictionary ( true ) ;
484453 }
@@ -489,17 +458,17 @@ internal Dictionary<string, object> ToCapability()
489458 /// </summary>
490459 /// <returns>A dictionary suitable for serializing to the OSS dialect of the
491460 /// wire protocol.</returns>
492- internal Dictionary < string , object > ToLegacyCapability ( )
461+ internal Dictionary < string , object ? > ? ToLegacyCapability ( )
493462 {
494463 return this . AsDictionary ( false ) ;
495464 }
496465
497- private Dictionary < string , object > AsDictionary ( bool isSpecCompliant )
466+ private Dictionary < string , object ? > ? AsDictionary ( bool isSpecCompliant )
498467 {
499- Dictionary < string , object > serializedDictionary = null ;
468+ Dictionary < string , object ? > ? serializedDictionary = null ;
500469 if ( this . proxyKind != ProxyKind . Unspecified )
501470 {
502- serializedDictionary = new Dictionary < string , object > ( ) ;
471+ serializedDictionary = new Dictionary < string , object ? > ( ) ;
503472 if ( this . proxyKind == ProxyKind . ProxyAutoConfigure )
504473 {
505474 serializedDictionary [ "proxyType" ] = "pac" ;
@@ -556,17 +525,12 @@ private Dictionary<string, object> AsDictionary(bool isSpecCompliant)
556525 return serializedDictionary ;
557526 }
558527
559- private object GetNoProxyAddressList ( bool isSpecCompliant )
528+ private object ? GetNoProxyAddressList ( bool isSpecCompliant )
560529 {
561- object addresses = null ;
530+ object ? addresses = null ;
562531 if ( isSpecCompliant )
563532 {
564- List < object > addressList = new List < object > ( ) ;
565- foreach ( string address in this . noProxyAddresses )
566- {
567- addressList . Add ( address ) ;
568- }
569-
533+ List < object > addressList = [ .. this . noProxyAddresses ] ;
570534 addresses = addressList ;
571535 }
572536 else
0 commit comments