From 5eef8fe86233897d421f65cb39ba9936f1fbaef9 Mon Sep 17 00:00:00 2001 From: Maxim Lipnin Date: Wed, 14 Oct 2020 19:07:36 +0300 Subject: [PATCH 1/2] Modify System.Net.HttpListener to throw PNSE at assembly level on browser --- .../System.Net.HttpListener/Directory.Build.props | 1 + .../ref/System.Net.HttpListener.cs | 9 +-------- .../System.Net.HttpListener/src/Resources/Strings.resx | 3 +++ .../src/System.Net.HttpListener.csproj | 7 ++++--- .../src/System/Net/HttpListener.cs | 2 -- .../src/System/Net/HttpListenerRequest.cs | 5 ++--- .../System/Net/Managed/HttpListenerRequest.Managed.cs | 3 --- .../System/Net/Managed/HttpListenerResponse.Managed.cs | 4 ---- .../Net/Managed/HttpListenerTimeoutManager.Managed.cs | 2 -- .../System/Net/Windows/HttpListenerRequest.Windows.cs | 3 --- .../System/Net/Windows/HttpListenerResponse.Windows.cs | 4 ---- .../Net/Windows/HttpListenerTimeoutManager.Windows.cs | 1 - .../System.Net.HttpListener/tests/AssemblyInfo.cs | 6 ++++++ .../tests/System.Net.HttpListener.Tests.csproj | 2 ++ 14 files changed, 19 insertions(+), 33 deletions(-) create mode 100644 src/libraries/System.Net.HttpListener/tests/AssemblyInfo.cs diff --git a/src/libraries/System.Net.HttpListener/Directory.Build.props b/src/libraries/System.Net.HttpListener/Directory.Build.props index 1db5968484c1ec..4784967b7f86ac 100644 --- a/src/libraries/System.Net.HttpListener/Directory.Build.props +++ b/src/libraries/System.Net.HttpListener/Directory.Build.props @@ -3,5 +3,6 @@ Open true + browser \ No newline at end of file diff --git a/src/libraries/System.Net.HttpListener/ref/System.Net.HttpListener.cs b/src/libraries/System.Net.HttpListener/ref/System.Net.HttpListener.cs index cdca2d97d4fe9d..b9bf091ebab1ee 100644 --- a/src/libraries/System.Net.HttpListener/ref/System.Net.HttpListener.cs +++ b/src/libraries/System.Net.HttpListener/ref/System.Net.HttpListener.cs @@ -13,7 +13,7 @@ public HttpListener() { } public System.Net.AuthenticationSchemes AuthenticationSchemes { get { throw null; } set { } } public System.Net.AuthenticationSchemeSelector? AuthenticationSchemeSelectorDelegate { get { throw null; } set { } } public System.Security.Authentication.ExtendedProtection.ServiceNameCollection DefaultServiceNames { get { throw null; } } - public System.Security.Authentication.ExtendedProtection.ExtendedProtectionPolicy ExtendedProtectionPolicy { get { throw null; } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] set { } } + public System.Security.Authentication.ExtendedProtection.ExtendedProtectionPolicy ExtendedProtectionPolicy { get { throw null; } set { } } [System.Diagnostics.CodeAnalysis.DisallowNullAttribute] public System.Net.HttpListener.ExtendedProtectionSelector? ExtendedProtectionSelectorDelegate { get { throw null; } set { } } public bool IgnoreWriteExceptions { get { throw null; } set { } } @@ -87,17 +87,14 @@ internal HttpListenerRequest() { } public string HttpMethod { get { throw null; } } public System.IO.Stream InputStream { get { throw null; } } public bool IsAuthenticated { get { throw null; } } - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public bool IsLocal { get { throw null; } } public bool IsSecureConnection { get { throw null; } } public bool IsWebSocketRequest { get { throw null; } } public bool KeepAlive { get { throw null; } } - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public System.Net.IPEndPoint LocalEndPoint { get { throw null; } } public System.Version ProtocolVersion { get { throw null; } } public System.Collections.Specialized.NameValueCollection QueryString { get { throw null; } } public string? RawUrl { get { throw null; } } - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public System.Net.IPEndPoint RemoteEndPoint { get { throw null; } } public System.Guid RequestTraceIdentifier { get { throw null; } } public string? ServiceName { get { throw null; } } @@ -105,7 +102,6 @@ internal HttpListenerRequest() { } public System.Uri? Url { get { throw null; } } public System.Uri? UrlReferrer { get { throw null; } } public string UserAgent { get { throw null; } } - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public string UserHostAddress { get { throw null; } } public string UserHostName { get { throw null; } } public string[]? UserLanguages { get { throw null; } } @@ -129,14 +125,11 @@ internal HttpListenerResponse() { } public bool SendChunked { get { throw null; } set { } } public int StatusCode { get { throw null; } set { } } public string StatusDescription { get { throw null; } set { } } - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public void Abort() { } public void AddHeader(string name, string value) { } public void AppendCookie(System.Net.Cookie cookie) { } public void AppendHeader(string name, string value) { } - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public void Close() { } - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public void Close(byte[] responseEntity, bool willBlock) { } public void CopyFrom(System.Net.HttpListenerResponse templateResponse) { } public void Redirect(string url) { } diff --git a/src/libraries/System.Net.HttpListener/src/Resources/Strings.resx b/src/libraries/System.Net.HttpListener/src/Resources/Strings.resx index f9747a646f6a33..c9c146f360c39e 100644 --- a/src/libraries/System.Net.HttpListener/src/Resources/Strings.resx +++ b/src/libraries/System.Net.HttpListener/src/Resources/Strings.resx @@ -389,4 +389,7 @@ The WebSocket is in an invalid state ('{0}') for this operation. Valid states are: '{1}' + + System.Net.HttpListener is not supported on this platform. + \ No newline at end of file diff --git a/src/libraries/System.Net.HttpListener/src/System.Net.HttpListener.csproj b/src/libraries/System.Net.HttpListener/src/System.Net.HttpListener.csproj index 476f5987eb069a..26703597e17070 100644 --- a/src/libraries/System.Net.HttpListener/src/System.Net.HttpListener.csproj +++ b/src/libraries/System.Net.HttpListener/src/System.Net.HttpListener.csproj @@ -4,8 +4,9 @@ false $(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser enable + SR.SystemNetHttpListener_PlatformNotSupported - + @@ -33,7 +34,7 @@ - + @@ -247,7 +248,7 @@ - + diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/HttpListener.cs b/src/libraries/System.Net.HttpListener/src/System/Net/HttpListener.cs index d40826ba07710e..b391474791591f 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/HttpListener.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/HttpListener.cs @@ -3,7 +3,6 @@ using System.Collections; using System.Diagnostics.CodeAnalysis; -using System.Runtime.Versioning; using System.Security.Authentication.ExtendedProtection; using System.Text; using System.Threading.Tasks; @@ -82,7 +81,6 @@ public AuthenticationSchemes AuthenticationSchemes public ExtendedProtectionPolicy ExtendedProtectionPolicy { get => _extendedProtectionPolicy; - [UnsupportedOSPlatform("browser")] set { CheckDisposed(); diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerRequest.cs b/src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerRequest.cs index a431ed61e3fc63..1bcab0238d2a1e 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerRequest.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerRequest.cs @@ -7,7 +7,6 @@ using System.Globalization; using System.Net.WebSockets; using System.Reflection; -using System.Runtime.Versioning; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; @@ -111,7 +110,7 @@ public Encoding ContentEncoding public string? ContentType => Headers[HttpKnownHeaderNames.ContentType]; - [UnsupportedOSPlatform("browser")] + public bool IsLocal => LocalEndPoint!.Address.Equals(RemoteEndPoint!.Address); public bool IsWebSocketRequest @@ -208,7 +207,7 @@ public NameValueCollection QueryString public string UserAgent => Headers[HttpKnownHeaderNames.UserAgent]!; - [UnsupportedOSPlatform("browser")] + public string UserHostAddress => LocalEndPoint!.ToString(); public string UserHostName => Headers[HttpKnownHeaderNames.Host]!; diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerRequest.Managed.cs b/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerRequest.Managed.cs index a29021d572f659..cdcda5884bd819 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerRequest.Managed.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerRequest.Managed.cs @@ -35,7 +35,6 @@ using System.Diagnostics; using System.Globalization; using System.IO; -using System.Runtime.Versioning; using System.Security.Authentication.ExtendedProtection; using System.Security.Cryptography.X509Certificates; using System.Text; @@ -375,10 +374,8 @@ public Stream InputStream public bool IsSecureConnection => _context.Connection.IsSecure; - [UnsupportedOSPlatform("browser")] public IPEndPoint? LocalEndPoint => _context.Connection.LocalEndPoint; - [UnsupportedOSPlatform("browser")] public IPEndPoint? RemoteEndPoint => _context.Connection.RemoteEndPoint; public Guid RequestTraceIdentifier { get; } = Guid.NewGuid(); diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerResponse.Managed.cs b/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerResponse.Managed.cs index 4de98bf987c458..2a8fc6efd38729 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerResponse.Managed.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerResponse.Managed.cs @@ -31,7 +31,6 @@ using System.Globalization; using System.IO; -using System.Runtime.Versioning; using System.Text; namespace System.Net @@ -94,7 +93,6 @@ public int StatusCode private void Dispose() => Close(true); - [UnsupportedOSPlatform("browser")] public void Close() { if (Disposed) @@ -103,7 +101,6 @@ public void Close() Close(false); } - [UnsupportedOSPlatform("browser")] public void Abort() { if (Disposed) @@ -118,7 +115,6 @@ private void Close(bool force) _httpContext!.Connection.Close(force); } - [UnsupportedOSPlatform("browser")] public void Close(byte[] responseEntity, bool willBlock) { CheckDisposed(); diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerTimeoutManager.Managed.cs b/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerTimeoutManager.Managed.cs index 8dda7441040797..6194cf1b952111 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerTimeoutManager.Managed.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerTimeoutManager.Managed.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Runtime.Versioning; - namespace System.Net { public class HttpListenerTimeoutManager diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerRequest.Windows.cs b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerRequest.Windows.cs index b49eb2c1f1a21c..c3b038a3f083fe 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerRequest.Windows.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerRequest.Windows.cs @@ -7,7 +7,6 @@ using System.IO; using System.Net.WebSockets; using System.Runtime.InteropServices; -using System.Runtime.Versioning; using System.Security; using System.Security.Authentication.ExtendedProtection; using System.Security.Cryptography; @@ -289,7 +288,6 @@ public bool HasEntityBody } } - [UnsupportedOSPlatform("browser")] public IPEndPoint RemoteEndPoint { get @@ -303,7 +301,6 @@ public IPEndPoint RemoteEndPoint } } - [UnsupportedOSPlatform("browser")] public IPEndPoint LocalEndPoint { get diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerResponse.Windows.cs b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerResponse.Windows.cs index 40d72e8af468b8..0c4b36af8efc75 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerResponse.Windows.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerResponse.Windows.cs @@ -8,7 +8,6 @@ using System.Text; using System.ComponentModel; using System.Diagnostics; -using System.Runtime.Versioning; using Microsoft.Win32.SafeHandles; namespace System.Net @@ -96,7 +95,6 @@ public Version ProtocolVersion } } - [UnsupportedOSPlatform("browser")] public void Abort() { if (Disposed) @@ -108,7 +106,6 @@ public void Abort() HttpListenerContext.Abort(); } - [UnsupportedOSPlatform("browser")] public void Close() { try @@ -121,7 +118,6 @@ public void Close() } } - [UnsupportedOSPlatform("browser")] public void Close(byte[] responseEntity, bool willBlock) { CheckDisposed(); diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerTimeoutManager.Windows.cs b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerTimeoutManager.Windows.cs index a1af5b35b71a84..1e51688fea6e79 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerTimeoutManager.Windows.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerTimeoutManager.Windows.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; -using System.Runtime.Versioning; namespace System.Net { diff --git a/src/libraries/System.Net.HttpListener/tests/AssemblyInfo.cs b/src/libraries/System.Net.HttpListener/tests/AssemblyInfo.cs new file mode 100644 index 00000000000000..be691cadde455f --- /dev/null +++ b/src/libraries/System.Net.HttpListener/tests/AssemblyInfo.cs @@ -0,0 +1,6 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Xunit; + +[assembly: SkipOnMono("System.Net.HttpListener is not supported on wasm", TestPlatforms.Browser)] \ No newline at end of file diff --git a/src/libraries/System.Net.HttpListener/tests/System.Net.HttpListener.Tests.csproj b/src/libraries/System.Net.HttpListener/tests/System.Net.HttpListener.Tests.csproj index 3d8ebf1e50be37..2cb8e45108846b 100644 --- a/src/libraries/System.Net.HttpListener/tests/System.Net.HttpListener.Tests.csproj +++ b/src/libraries/System.Net.HttpListener/tests/System.Net.HttpListener.Tests.csproj @@ -3,8 +3,10 @@ true ../src/Resources/Strings.resx $(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser;$(NetCoreAppCurrent)-OSX + true + From fdb3faa83509b4021a9705bcafe949c713c63b77 Mon Sep 17 00:00:00 2001 From: Maxim Lipnin Date: Thu, 15 Oct 2020 11:46:08 +0300 Subject: [PATCH 2/2] Fix build --- .../src/System.Net.HttpListener.csproj | 4 ++-- .../src/System/Net/HttpListenerRequest.cs | 2 -- .../System/Net/Managed/HttpListenerTimeoutManager.Managed.cs | 2 ++ .../System/Net/Windows/HttpListenerTimeoutManager.Windows.cs | 1 + 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Net.HttpListener/src/System.Net.HttpListener.csproj b/src/libraries/System.Net.HttpListener/src/System.Net.HttpListener.csproj index 26703597e17070..c0af10c41b83b5 100644 --- a/src/libraries/System.Net.HttpListener/src/System.Net.HttpListener.csproj +++ b/src/libraries/System.Net.HttpListener/src/System.Net.HttpListener.csproj @@ -6,7 +6,7 @@ enable SR.SystemNetHttpListener_PlatformNotSupported - + @@ -248,7 +248,7 @@ - + diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerRequest.cs b/src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerRequest.cs index 1bcab0238d2a1e..4540239e76b4e9 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerRequest.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerRequest.cs @@ -110,7 +110,6 @@ public Encoding ContentEncoding public string? ContentType => Headers[HttpKnownHeaderNames.ContentType]; - public bool IsLocal => LocalEndPoint!.Address.Equals(RemoteEndPoint!.Address); public bool IsWebSocketRequest @@ -207,7 +206,6 @@ public NameValueCollection QueryString public string UserAgent => Headers[HttpKnownHeaderNames.UserAgent]!; - public string UserHostAddress => LocalEndPoint!.ToString(); public string UserHostName => Headers[HttpKnownHeaderNames.Host]!; diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerTimeoutManager.Managed.cs b/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerTimeoutManager.Managed.cs index 6194cf1b952111..8dda7441040797 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerTimeoutManager.Managed.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerTimeoutManager.Managed.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Runtime.Versioning; + namespace System.Net { public class HttpListenerTimeoutManager diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerTimeoutManager.Windows.cs b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerTimeoutManager.Windows.cs index 1e51688fea6e79..a1af5b35b71a84 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerTimeoutManager.Windows.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerTimeoutManager.Windows.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; +using System.Runtime.Versioning; namespace System.Net {