From c538277463b8b7bcc0f28c336a8c36de7438b01d Mon Sep 17 00:00:00 2001 From: Pavel Savara Date: Mon, 9 Aug 2021 11:50:40 +0200 Subject: [PATCH 1/2] throw PlatformNotSupportedException for ExpectContinue and Expect header fields on Browser --- .../HttpClientHandlerTest.RemoteServer.cs | 2 +- .../System/Net/Http/HttpClientHandlerTest.cs | 6 +++-- .../Net/Http/Headers/HttpRequestHeaders.cs | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs index 9b99ef1d13ff62..9fd29a905f97f2 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs @@ -607,7 +607,7 @@ public static IEnumerable ExpectContinueVersion() [OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))] [Theory] [MemberData(nameof(ExpectContinueVersion))] - [ActiveIssue("https://github.com/dotnet/runtime/issues/53876", TestPlatforms.Browser)] + [SkipOnPlatform(TestPlatforms.Browser, "ExpectContinue not supported on Browser")] public async Task PostAsync_ExpectContinue_Success(bool? expectContinue, Version version) { // Sync API supported only up to HTTP/1.1 diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs index 27a8fc9bed2477..17fcd64ee26d80 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs @@ -429,10 +429,10 @@ await LoopbackServerFactory.CreateClientAndServerAsync(async uri => if (PlatformDetection.IsNotBrowser) { request.Content.Headers.ContentMD5 = MD5.Create().ComputeHash(contentArray); + request.Headers.Expect.Add(new NameValueWithParametersHeaderValue("100-continue")); } request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); request.Headers.Date = DateTimeOffset.Parse("Tue, 15 Nov 1994 08:12:31 GMT"); - request.Headers.Expect.Add(new NameValueWithParametersHeaderValue("100-continue")); request.Headers.Add("Forwarded", "for=192.0.2.60;proto=http;by=203.0.113.43"); request.Headers.Add("From", "User Name "); request.Headers.Host = "en.wikipedia.org:8080"; @@ -1369,7 +1369,7 @@ public async Task GetAsync_UnicodeHostName_SuccessStatusCodeInResponse() #region Post Methods Tests [Fact] - [ActiveIssue("https://github.com/dotnet/runtime/issues/53876", TestPlatforms.Browser)] + [SkipOnPlatform(TestPlatforms.Browser, "ExpectContinue not supported on Browser")] public async Task GetAsync_ExpectContinueTrue_NoContent_StillSendsHeader() { if (IsWinHttpHandler && UseVersion >= HttpVersion20.Value) @@ -1547,6 +1547,7 @@ await server.AcceptConnectionAsync(async connection => } [Fact] + [SkipOnPlatform(TestPlatforms.Browser, "ExpectContinue not supported on Browser")] public async Task SendAsync_MultipleExpected100Responses_ReceivesCorrectResponse() { if (IsWinHttpHandler && UseVersion >= HttpVersion20.Value) @@ -1642,6 +1643,7 @@ await server.AcceptConnectionAsync(async connection => } [Fact] + [SkipOnPlatform(TestPlatforms.Browser, "ExpectContinue not supported on Browser")] public async Task SendAsync_No100ContinueReceived_RequestBodySentEventually() { if (IsWinHttpHandler && UseVersion >= HttpVersion20.Value) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpRequestHeaders.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpRequestHeaders.cs index 1731a954bf0a8d..94cd94ce3ede2a 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpRequestHeaders.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpRequestHeaders.cs @@ -19,8 +19,11 @@ public sealed class HttpRequestHeaders : HttpHeaders private object[]? _specialCollectionsSlots; private HttpGeneralHeaders? _generalHeaders; + +#if !TARGET_BROWSER private HttpHeaderValueCollection? _expect; private bool _expectContinueSet; +#endif #region Request Headers @@ -51,6 +54,21 @@ public AuthenticationHeaderValue? Authorization set { SetOrRemoveParsedValue(KnownHeaders.Authorization.Descriptor, value); } } +#if TARGET_BROWSER + public HttpHeaderValueCollection Expect + { + get => throw new PlatformNotSupportedException(); + } + + public bool? ExpectContinue + { + get => false; + set + { + if (value ?? false) { throw new PlatformNotSupportedException();} + } + } +#else public HttpHeaderValueCollection Expect { get { return ExpectCore; } @@ -89,6 +107,7 @@ public bool? ExpectContinue } } } +#endif public string? From { @@ -189,8 +208,10 @@ public Uri? Referrer public HttpHeaderValueCollection UserAgent => GetSpecializedCollection(UserAgentSlot, static thisRef => new HttpHeaderValueCollection(KnownHeaders.UserAgent.Descriptor, thisRef)); +#if !TARGET_BROWSER private HttpHeaderValueCollection ExpectCore => _expect ??= new HttpHeaderValueCollection(KnownHeaders.Expect.Descriptor, this, HeaderUtilities.ExpectContinue); +#endif #endregion @@ -274,11 +295,13 @@ internal override void AddHeaders(HttpHeaders sourceHeaders) GeneralHeaders.AddSpecialsFrom(sourceRequestHeaders._generalHeaders); } +#if !TARGET_BROWSER bool? expectContinue = ExpectContinue; if (!expectContinue.HasValue) { ExpectContinue = sourceRequestHeaders.ExpectContinue; } +#endif } private HttpGeneralHeaders GeneralHeaders => _generalHeaders ?? (_generalHeaders = new HttpGeneralHeaders(this)); From 04bc52fb87c86b430bee24feea9f9d0df335ffec Mon Sep 17 00:00:00 2001 From: Pavel Savara Date: Mon, 9 Aug 2021 13:17:51 +0200 Subject: [PATCH 2/2] feedback --- .../Net/Http/Headers/HttpRequestHeaders.cs | 23 ------------------- 1 file changed, 23 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpRequestHeaders.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpRequestHeaders.cs index 94cd94ce3ede2a..1731a954bf0a8d 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpRequestHeaders.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpRequestHeaders.cs @@ -19,11 +19,8 @@ public sealed class HttpRequestHeaders : HttpHeaders private object[]? _specialCollectionsSlots; private HttpGeneralHeaders? _generalHeaders; - -#if !TARGET_BROWSER private HttpHeaderValueCollection? _expect; private bool _expectContinueSet; -#endif #region Request Headers @@ -54,21 +51,6 @@ public AuthenticationHeaderValue? Authorization set { SetOrRemoveParsedValue(KnownHeaders.Authorization.Descriptor, value); } } -#if TARGET_BROWSER - public HttpHeaderValueCollection Expect - { - get => throw new PlatformNotSupportedException(); - } - - public bool? ExpectContinue - { - get => false; - set - { - if (value ?? false) { throw new PlatformNotSupportedException();} - } - } -#else public HttpHeaderValueCollection Expect { get { return ExpectCore; } @@ -107,7 +89,6 @@ public bool? ExpectContinue } } } -#endif public string? From { @@ -208,10 +189,8 @@ public Uri? Referrer public HttpHeaderValueCollection UserAgent => GetSpecializedCollection(UserAgentSlot, static thisRef => new HttpHeaderValueCollection(KnownHeaders.UserAgent.Descriptor, thisRef)); -#if !TARGET_BROWSER private HttpHeaderValueCollection ExpectCore => _expect ??= new HttpHeaderValueCollection(KnownHeaders.Expect.Descriptor, this, HeaderUtilities.ExpectContinue); -#endif #endregion @@ -295,13 +274,11 @@ internal override void AddHeaders(HttpHeaders sourceHeaders) GeneralHeaders.AddSpecialsFrom(sourceRequestHeaders._generalHeaders); } -#if !TARGET_BROWSER bool? expectContinue = ExpectContinue; if (!expectContinue.HasValue) { ExpectContinue = sourceRequestHeaders.ExpectContinue; } -#endif } private HttpGeneralHeaders GeneralHeaders => _generalHeaders ?? (_generalHeaders = new HttpGeneralHeaders(this));