Skip to content

Commit 46c9477

Browse files
[release/5.0] Modify System.Net.HttpListener to throw PNSE at assembly level on browser (#43523)
* Modify System.Net.HttpListener to throw PNSE at assembly level on browser * Fix build Co-authored-by: Maxim Lipnin <[email protected]>
1 parent cf258a1 commit 46c9477

12 files changed

+16
-29
lines changed

src/libraries/System.Net.HttpListener/Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<PropertyGroup>
44
<StrongNameKeyId>Open</StrongNameKeyId>
55
<IncludePlatformAttributes>true</IncludePlatformAttributes>
6+
<UnsupportedOSPlatforms>browser</UnsupportedOSPlatforms>
67
</PropertyGroup>
78
</Project>

src/libraries/System.Net.HttpListener/ref/System.Net.HttpListener.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public HttpListener() { }
1313
public System.Net.AuthenticationSchemes AuthenticationSchemes { get { throw null; } set { } }
1414
public System.Net.AuthenticationSchemeSelector? AuthenticationSchemeSelectorDelegate { get { throw null; } set { } }
1515
public System.Security.Authentication.ExtendedProtection.ServiceNameCollection DefaultServiceNames { get { throw null; } }
16-
public System.Security.Authentication.ExtendedProtection.ExtendedProtectionPolicy ExtendedProtectionPolicy { get { throw null; } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] set { } }
16+
public System.Security.Authentication.ExtendedProtection.ExtendedProtectionPolicy ExtendedProtectionPolicy { get { throw null; } set { } }
1717
[System.Diagnostics.CodeAnalysis.DisallowNullAttribute]
1818
public System.Net.HttpListener.ExtendedProtectionSelector? ExtendedProtectionSelectorDelegate { get { throw null; } set { } }
1919
public bool IgnoreWriteExceptions { get { throw null; } set { } }
@@ -87,25 +87,21 @@ internal HttpListenerRequest() { }
8787
public string HttpMethod { get { throw null; } }
8888
public System.IO.Stream InputStream { get { throw null; } }
8989
public bool IsAuthenticated { get { throw null; } }
90-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
9190
public bool IsLocal { get { throw null; } }
9291
public bool IsSecureConnection { get { throw null; } }
9392
public bool IsWebSocketRequest { get { throw null; } }
9493
public bool KeepAlive { get { throw null; } }
95-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
9694
public System.Net.IPEndPoint LocalEndPoint { get { throw null; } }
9795
public System.Version ProtocolVersion { get { throw null; } }
9896
public System.Collections.Specialized.NameValueCollection QueryString { get { throw null; } }
9997
public string? RawUrl { get { throw null; } }
100-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
10198
public System.Net.IPEndPoint RemoteEndPoint { get { throw null; } }
10299
public System.Guid RequestTraceIdentifier { get { throw null; } }
103100
public string? ServiceName { get { throw null; } }
104101
public System.Net.TransportContext TransportContext { get { throw null; } }
105102
public System.Uri? Url { get { throw null; } }
106103
public System.Uri? UrlReferrer { get { throw null; } }
107104
public string UserAgent { get { throw null; } }
108-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
109105
public string UserHostAddress { get { throw null; } }
110106
public string UserHostName { get { throw null; } }
111107
public string[]? UserLanguages { get { throw null; } }
@@ -129,14 +125,11 @@ internal HttpListenerResponse() { }
129125
public bool SendChunked { get { throw null; } set { } }
130126
public int StatusCode { get { throw null; } set { } }
131127
public string StatusDescription { get { throw null; } set { } }
132-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
133128
public void Abort() { }
134129
public void AddHeader(string name, string value) { }
135130
public void AppendCookie(System.Net.Cookie cookie) { }
136131
public void AppendHeader(string name, string value) { }
137-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
138132
public void Close() { }
139-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
140133
public void Close(byte[] responseEntity, bool willBlock) { }
141134
public void CopyFrom(System.Net.HttpListenerResponse templateResponse) { }
142135
public void Redirect(string url) { }

src/libraries/System.Net.HttpListener/src/Resources/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,4 +389,7 @@
389389
<data name="net_WebSockets_InvalidState" xml:space="preserve">
390390
<value>The WebSocket is in an invalid state ('{0}') for this operation. Valid states are: '{1}'</value>
391391
</data>
392+
<data name="SystemNetHttpListener_PlatformNotSupported" xml:space="preserve">
393+
<value>System.Net.HttpListener is not supported on this platform.</value>
394+
</data>
392395
</root>

src/libraries/System.Net.HttpListener/src/System.Net.HttpListener.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<ForceManagedImplementation>false</ForceManagedImplementation>
55
<TargetFrameworks>$(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser</TargetFrameworks>
66
<Nullable>enable</Nullable>
7+
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetsBrowser)' == 'true'">SR.SystemNetHttpListener_PlatformNotSupported</GeneratePlatformNotSupportedAssemblyMessage>
78
</PropertyGroup>
89
<ItemGroup>
910
<ProjectReference Include="$(LibrariesProjectRoot)System.Security.Principal.Windows\src\System.Security.Principal.Windows.csproj" />
@@ -33,7 +34,7 @@
3334
<Reference Include="System.Threading" />
3435
<Reference Include="System.Threading.Overlapped" />
3536
</ItemGroup>
36-
<ItemGroup>
37+
<ItemGroup Condition="'$(TargetsBrowser)' != 'true'">
3738
<Compile Include="System\Net\BoundaryType.cs" />
3839
<Compile Include="System\Net\EntitySendFormat.cs" />
3940
<Compile Include="System\Net\HttpListenerPrefixCollection.cs" />
@@ -247,7 +248,7 @@
247248
<Compile Include="$(CommonPath)Interop\Windows\SspiCli\SSPIWrapper.cs"
248249
Link="Common\Interop\Windows\SspiCli\SSPIWrapper.cs" />
249250
</ItemGroup>
250-
<ItemGroup Condition="'$(TargetsUnix)' == 'true' or '$(TargetsBrowser)' == 'true' or '$(ForceManagedImplementation)' == 'true'">
251+
<ItemGroup Condition="'$(TargetsUnix)' == 'true' or '$(ForceManagedImplementation)' == 'true'">
251252
<Compile Include="System\Net\Managed\HttpEndPointListener.cs" />
252253
<Compile Include="System\Net\Managed\HttpEndPointManager.cs" />
253254
<Compile Include="System\Net\Managed\HttpConnection.cs" />

src/libraries/System.Net.HttpListener/src/System/Net/HttpListener.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Collections;
55
using System.Diagnostics.CodeAnalysis;
6-
using System.Runtime.Versioning;
76
using System.Security.Authentication.ExtendedProtection;
87
using System.Text;
98
using System.Threading.Tasks;
@@ -82,7 +81,6 @@ public AuthenticationSchemes AuthenticationSchemes
8281
public ExtendedProtectionPolicy ExtendedProtectionPolicy
8382
{
8483
get => _extendedProtectionPolicy;
85-
[UnsupportedOSPlatform("browser")]
8684
set
8785
{
8886
CheckDisposed();

src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerRequest.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Globalization;
88
using System.Net.WebSockets;
99
using System.Reflection;
10-
using System.Runtime.Versioning;
1110
using System.Security.Cryptography.X509Certificates;
1211
using System.Text;
1312
using System.Threading.Tasks;
@@ -111,7 +110,6 @@ public Encoding ContentEncoding
111110

112111
public string? ContentType => Headers[HttpKnownHeaderNames.ContentType];
113112

114-
[UnsupportedOSPlatform("browser")]
115113
public bool IsLocal => LocalEndPoint!.Address.Equals(RemoteEndPoint!.Address);
116114

117115
public bool IsWebSocketRequest
@@ -208,7 +206,6 @@ public NameValueCollection QueryString
208206

209207
public string UserAgent => Headers[HttpKnownHeaderNames.UserAgent]!;
210208

211-
[UnsupportedOSPlatform("browser")]
212209
public string UserHostAddress => LocalEndPoint!.ToString();
213210

214211
public string UserHostName => Headers[HttpKnownHeaderNames.Host]!;

src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerRequest.Managed.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
using System.Diagnostics;
3535
using System.Globalization;
3636
using System.IO;
37-
using System.Runtime.Versioning;
3837
using System.Security.Authentication.ExtendedProtection;
3938
using System.Security.Cryptography.X509Certificates;
4039
using System.Text;
@@ -374,10 +373,8 @@ public Stream InputStream
374373

375374
public bool IsSecureConnection => _context.Connection.IsSecure;
376375

377-
[UnsupportedOSPlatform("browser")]
378376
public IPEndPoint? LocalEndPoint => _context.Connection.LocalEndPoint;
379377

380-
[UnsupportedOSPlatform("browser")]
381378
public IPEndPoint? RemoteEndPoint => _context.Connection.RemoteEndPoint;
382379

383380
public Guid RequestTraceIdentifier { get; } = Guid.NewGuid();

src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpListenerResponse.Managed.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
using System.Globalization;
3232
using System.IO;
33-
using System.Runtime.Versioning;
3433
using System.Text;
3534

3635
namespace System.Net
@@ -93,7 +92,6 @@ public int StatusCode
9392

9493
private void Dispose() => Close(true);
9594

96-
[UnsupportedOSPlatform("browser")]
9795
public void Close()
9896
{
9997
if (Disposed)
@@ -102,7 +100,6 @@ public void Close()
102100
Close(false);
103101
}
104102

105-
[UnsupportedOSPlatform("browser")]
106103
public void Abort()
107104
{
108105
if (Disposed)
@@ -117,7 +114,6 @@ private void Close(bool force)
117114
_httpContext!.Connection.Close(force);
118115
}
119116

120-
[UnsupportedOSPlatform("browser")]
121117
public void Close(byte[] responseEntity, bool willBlock)
122118
{
123119
CheckDisposed();

src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerRequest.Windows.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.IO;
88
using System.Net.WebSockets;
99
using System.Runtime.InteropServices;
10-
using System.Runtime.Versioning;
1110
using System.Security;
1211
using System.Security.Authentication.ExtendedProtection;
1312
using System.Security.Cryptography;
@@ -289,7 +288,6 @@ public bool HasEntityBody
289288
}
290289
}
291290

292-
[UnsupportedOSPlatform("browser")]
293291
public IPEndPoint RemoteEndPoint
294292
{
295293
get
@@ -303,7 +301,6 @@ public IPEndPoint RemoteEndPoint
303301
}
304302
}
305303

306-
[UnsupportedOSPlatform("browser")]
307304
public IPEndPoint LocalEndPoint
308305
{
309306
get

src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerResponse.Windows.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Text;
99
using System.ComponentModel;
1010
using System.Diagnostics;
11-
using System.Runtime.Versioning;
1211
using Microsoft.Win32.SafeHandles;
1312

1413
namespace System.Net
@@ -96,7 +95,6 @@ public Version ProtocolVersion
9695
}
9796
}
9897

99-
[UnsupportedOSPlatform("browser")]
10098
public void Abort()
10199
{
102100
if (Disposed)
@@ -108,7 +106,6 @@ public void Abort()
108106
HttpListenerContext.Abort();
109107
}
110108

111-
[UnsupportedOSPlatform("browser")]
112109
public void Close()
113110
{
114111
try
@@ -121,7 +118,6 @@ public void Close()
121118
}
122119
}
123120

124-
[UnsupportedOSPlatform("browser")]
125121
public void Close(byte[] responseEntity, bool willBlock)
126122
{
127123
CheckDisposed();

0 commit comments

Comments
 (0)