Annotate System.Net.WebProxy for nullable reference types (#32612)
authorStephen Toub <stoub@microsoft.com>
Sun, 23 Feb 2020 02:11:01 +0000 (21:11 -0500)
committerGitHub <noreply@github.com>
Sun, 23 Feb 2020 02:11:01 +0000 (21:11 -0500)
src/libraries/System.Net.Primitives/ref/System.Net.Primitives.cs
src/libraries/System.Net.Primitives/src/System/Net/IWebProxy.cs
src/libraries/System.Net.WebProxy/ref/System.Net.WebProxy.cs
src/libraries/System.Net.WebProxy/ref/System.Net.WebProxy.csproj
src/libraries/System.Net.WebProxy/src/System.Net.WebProxy.csproj
src/libraries/System.Net.WebProxy/src/System/Net/WebProxy.cs

index b763e41..ff1ed56 100644 (file)
@@ -284,7 +284,7 @@ namespace System.Net
     public partial interface IWebProxy
     {
         System.Net.ICredentials? Credentials { get; set; }
-        System.Uri GetProxy(System.Uri destination);
+        System.Uri? GetProxy(System.Uri destination);
         bool IsBypassed(System.Uri host);
     }
     public partial class NetworkCredential : System.Net.ICredentials, System.Net.ICredentialsByHost
index e9bc4d9..388f392 100644 (file)
@@ -12,7 +12,7 @@ namespace System.Net
     /// </devdoc>
     public interface IWebProxy
     {
-        Uri GetProxy(Uri destination);
+        Uri? GetProxy(Uri destination);
         bool IsBypassed(Uri host);
         ICredentials? Credentials { get; set; }
     }
index e9784ec..83c397b 100644 (file)
@@ -17,25 +17,25 @@ namespace System.Net
     {
         public WebProxy() { }
         protected WebProxy(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext) { }
-        public WebProxy(string Address) { }
-        public WebProxy(string Address, bool BypassOnLocal) { }
-        public WebProxy(string Address, bool BypassOnLocal, string[] BypassList) { }
-        public WebProxy(string Address, bool BypassOnLocal, string[] BypassList, System.Net.ICredentials Credentials) { }
+        public WebProxy(string? Address) { }
+        public WebProxy(string? Address, bool BypassOnLocal) { }
+        public WebProxy(string? Address, bool BypassOnLocal, string[]? BypassList) { }
+        public WebProxy(string? Address, bool BypassOnLocal, string[]? BypassList, System.Net.ICredentials? Credentials) { }
         public WebProxy(string Host, int Port) { }
-        public WebProxy(System.Uri Address) { }
-        public WebProxy(System.Uri Address, bool BypassOnLocal) { }
-        public WebProxy(System.Uri Address, bool BypassOnLocal, string[] BypassList) { }
-        public WebProxy(System.Uri Address, bool BypassOnLocal, string[] BypassList, System.Net.ICredentials Credentials) { }
-        public System.Uri Address { get { throw null; } set { } }
+        public WebProxy(System.Uri? Address) { }
+        public WebProxy(System.Uri? Address, bool BypassOnLocal) { }
+        public WebProxy(System.Uri? Address, bool BypassOnLocal, string[]? BypassList) { }
+        public WebProxy(System.Uri? Address, bool BypassOnLocal, string[]? BypassList, System.Net.ICredentials? Credentials) { }
+        public System.Uri? Address { get { throw null; } set { } }
         public System.Collections.ArrayList BypassArrayList { get { throw null; } }
         public string[] BypassList { get { throw null; } set { } }
         public bool BypassProxyOnLocal { get { throw null; } set { } }
-        public System.Net.ICredentials Credentials { get { throw null; } set { } }
+        public System.Net.ICredentials? Credentials { get { throw null; } set { } }
         public bool UseDefaultCredentials { get { throw null; } set { } }
         [System.ObsoleteAttribute("This method has been deprecated. Please use the proxy selected for you by default. https://go.microsoft.com/fwlink/?linkid=14202")]
         public static System.Net.WebProxy GetDefaultProxy() { throw null; }
         protected virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext) { }
-        public System.Uri GetProxy(System.Uri destination) { throw null; }
+        public System.Uri? GetProxy(System.Uri destination) { throw null; }
         public bool IsBypassed(System.Uri host) { throw null; }
         void System.Runtime.Serialization.ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext) { }
     }
index 773047c..477ecb4 100644 (file)
@@ -1,6 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
+    <Nullable>enable</Nullable>
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="System.Net.WebProxy.cs" />
index 04b660d..bd89d8a 100644 (file)
@@ -2,6 +2,7 @@
   <PropertyGroup>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     <TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
+    <Nullable>enable</Nullable>
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="System\Net\IWebProxyScript.cs" />
index 686a1b9..c81d4be 100644 (file)
@@ -12,18 +12,18 @@ namespace System.Net
 {
     public class WebProxy : IWebProxy, ISerializable
     {
-        private ArrayList _bypassList;
-        private Regex[] _regExBypassList;
+        private ArrayList? _bypassList;
+        private Regex[]? _regexBypassList;
 
-        public WebProxy() : this((Uri)null, false, null, null) { }
+        public WebProxy() : this((Uri?)null, false, null, null) { }
 
-        public WebProxy(Uri Address) : this(Address, false, null, null) { }
+        public WebProxy(Uri? Address) : this(Address, false, null, null) { }
 
-        public WebProxy(Uri Address, bool BypassOnLocal) : this(Address, BypassOnLocal, null, null) { }
+        public WebProxy(Uri? Address, bool BypassOnLocal) : this(Address, BypassOnLocal, null, null) { }
 
-        public WebProxy(Uri Address, bool BypassOnLocal, string[] BypassList) : this(Address, BypassOnLocal, BypassList, null) { }
+        public WebProxy(Uri? Address, bool BypassOnLocal, string[]? BypassList) : this(Address, BypassOnLocal, BypassList, null) { }
 
-        public WebProxy(Uri Address, bool BypassOnLocal, string[] BypassList, ICredentials Credentials)
+        public WebProxy(Uri? Address, bool BypassOnLocal, string[]? BypassList, ICredentials? Credentials)
         {
             this.Address = Address;
             this.Credentials = Credentials;
@@ -31,7 +31,7 @@ namespace System.Net
             if (BypassList != null)
             {
                 _bypassList = new ArrayList(BypassList);
-                UpdateRegExList(true);
+                UpdateRegexList(true);
             }
         }
 
@@ -40,27 +40,27 @@ namespace System.Net
         {
         }
 
-        public WebProxy(string Address)
+        public WebProxy(string? Address)
             : this(CreateProxyUri(Address), false, null, null)
         {
         }
 
-        public WebProxy(string Address, bool BypassOnLocal)
+        public WebProxy(string? Address, bool BypassOnLocal)
             : this(CreateProxyUri(Address), BypassOnLocal, null, null)
         {
         }
 
-        public WebProxy(string Address, bool BypassOnLocal, string[] BypassList)
+        public WebProxy(string? Address, bool BypassOnLocal, string[]? BypassList)
             : this(CreateProxyUri(Address), BypassOnLocal, BypassList, null)
         {
         }
 
-        public WebProxy(string Address, bool BypassOnLocal, string[] BypassList, ICredentials Credentials)
+        public WebProxy(string? Address, bool BypassOnLocal, string[]? BypassList, ICredentials? Credentials)
             : this(CreateProxyUri(Address), BypassOnLocal, BypassList, Credentials)
         {
         }
 
-        public Uri Address { get; set; }
+        public Uri? Address { get; set; }
 
         public bool BypassProxyOnLocal { get; set; }
 
@@ -70,13 +70,13 @@ namespace System.Net
             set
             {
                 _bypassList = new ArrayList(value);
-                UpdateRegExList(true);
+                UpdateRegexList(true);
             }
         }
 
         public ArrayList BypassArrayList => _bypassList ?? (_bypassList = new ArrayList());
 
-        public ICredentials Credentials { get; set; }
+        public ICredentials? Credentials { get; set; }
 
         public bool UseDefaultCredentials
         {
@@ -84,7 +84,7 @@ namespace System.Net
             set { Credentials = value ? CredentialCache.DefaultCredentials : null; }
         }
 
-        public Uri GetProxy(Uri destination)
+        public Uri? GetProxy(Uri destination)
         {
             if (destination == null)
             {
@@ -94,23 +94,23 @@ namespace System.Net
             return IsBypassed(destination) ? destination : Address;
         }
 
-        private static Uri CreateProxyUri(string address) =>
+        private static Uri? CreateProxyUri(string? address) =>
             address == null ? null :
             address.IndexOf("://", StringComparison.Ordinal) == -1 ? new Uri("http://" + address) :
             new Uri(address);
 
-        private void UpdateRegExList(bool canThrow)
+        private void UpdateRegexList(bool canThrow)
         {
-            Regex[] regExBypassList = null;
-            ArrayList bypassList = _bypassList;
+            Regex[]? regexBypassList = null;
+            ArrayList? bypassList = _bypassList;
             try
             {
                 if (bypassList != null && bypassList.Count > 0)
                 {
-                    regExBypassList = new Regex[bypassList.Count];
+                    regexBypassList = new Regex[bypassList.Count];
                     for (int i = 0; i < bypassList.Count; i++)
                     {
-                        regExBypassList[i] = new Regex((string)bypassList[i], RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
+                        regexBypassList[i] = new Regex((string)bypassList[i]!, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
                     }
                 }
             }
@@ -118,27 +118,27 @@ namespace System.Net
             {
                 if (!canThrow)
                 {
-                    _regExBypassList = null;
+                    _regexBypassList = null;
                     return;
                 }
                 throw;
             }
 
             // Update field here, as it could throw earlier in the loop
-            _regExBypassList = regExBypassList;
+            _regexBypassList = regexBypassList;
         }
 
         private bool IsMatchInBypassList(Uri input)
         {
-            UpdateRegExList(false);
+            UpdateRegexList(false);
 
-            if (_regExBypassList != null)
+            if (_regexBypassList != null)
             {
                 string matchUriString = input.IsDefaultPort ?
                     input.Scheme + "://" + input.Host :
                     input.Scheme + "://" + input.Host + ":" + input.Port.ToString();
 
-                foreach (Regex r in _regExBypassList)
+                foreach (Regex r in _regexBypassList)
                 {
                     if (r.IsMatch(matchUriString))
                     {
@@ -159,8 +159,7 @@ namespace System.Net
 
             string hostString = host.Host;
 
-            IPAddress hostAddress;
-            if (IPAddress.TryParse(hostString, out hostAddress))
+            if (IPAddress.TryParse(hostString, out IPAddress? hostAddress))
             {
                 return IPAddress.IsLoopback(hostAddress) || IsAddressLocal(hostAddress);
             }