-
Notifications
You must be signed in to change notification settings - Fork 5.1k
WinHttpHandler: Read HTTP/2 trailing headers (replacement PR) #48704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
antonfirsov
merged 28 commits into
dotnet:main
from
antonfirsov:winhttp/trailing-headers
Mar 10, 2021
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
df5dcd1
WinHttpHandler: Read trailing headers
JamesNK d943c1e
Merge remote-tracking branch 'JamesNK/jamesnk/winhttp-trailingheaders…
antonfirsov 3e5f1cf
Merge branch 'master' into winhttp/trailing-headers
antonfirsov 08d2f03
Merge branch 'master' into winhttp/trailing-headers
antonfirsov 051d910
add "remote server" test
antonfirsov 6f4449b
PlatformDetection.SupportsAlpn = false on .NET Framework
antonfirsov ba83606
make tests conditional
antonfirsov 4ea3699
WinHttpTrailersHelper
antonfirsov 46df0f1
add OS support check
antonfirsov cc4f74d
refactor step 1
antonfirsov 8285ee6
refactor step 2
antonfirsov c5375e7
refactor step 3
antonfirsov 6594f31
Add comments
antonfirsov 1370e6d
simpify GetResponseHeaderCharBufferLength
antonfirsov 3cceba8
fix build
antonfirsov e41eacb
Remove Http2GetAsyncResponseHeadersReadOption_RemoteServer_TrailingHe…
antonfirsov d48ae0a
fix WinHttpHandler.Unit.Tests
antonfirsov 6635caf
fix unit tests
antonfirsov 575cbd8
add comments on RequestMessagePropertyName
antonfirsov fdf4872
Merge branch 'winhttp/trailing-headers' of https://github.com/antonfi…
antonfirsov 098356a
simplify GetTrailersSupported
antonfirsov 01109e8
improve comments
antonfirsov cf13ba0
make code shorter in GetResponseHeaderCharBufferLength
antonfirsov 29969b4
HttpClientHandlerTestBase.AllowAllCertificates should not be static
antonfirsov 0332b0e
revert previous attempt
antonfirsov 05b85ab
AllowAllCertificates: use parameter instead of static property
antonfirsov a3e2350
Merge branch 'winhttp/fix-HttpClientHandlerTestBase' into winhttp/tra…
antonfirsov 4604e47
Merge branch 'main' into winhttp/trailing-headers
antonfirsov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpTrailersHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Net.Http.Headers; | ||
using System.Runtime.InteropServices; | ||
using SafeWinHttpHandle = Interop.WinHttp.SafeWinHttpHandle; | ||
|
||
namespace System.Net.Http | ||
{ | ||
internal static class WinHttpTrailersHelper | ||
{ | ||
// UNITTEST is true when building against WinHttpHandler.Unit.Tests, which includes the source file. | ||
#if !NETSTANDARD2_1 && !UNITTEST | ||
// Trailer property name was chosen to be descriptive and be unlikely to collide with a user set property. | ||
// Apps and libraries will use this key so it shouldn't change. | ||
private const string RequestMessagePropertyName = "__ResponseTrailers"; | ||
private class HttpResponseTrailers : HttpHeaders | ||
{ | ||
} | ||
#endif | ||
private static Lazy<bool> s_trailersSupported = new Lazy<bool>(GetTrailersSupported); | ||
public static bool OsSupportsTrailers => s_trailersSupported.Value; | ||
|
||
public static HttpHeaders GetResponseTrailers(HttpResponseMessage response) | ||
{ | ||
#if NETSTANDARD2_1 || UNITTEST | ||
return response.TrailingHeaders; | ||
#else | ||
HttpResponseTrailers responseTrailers = new HttpResponseTrailers(); | ||
response.RequestMessage.Properties[RequestMessagePropertyName] = responseTrailers; | ||
return responseTrailers; | ||
#endif | ||
} | ||
|
||
// There is no way to verify if WINHTTP_QUERY_FLAG_TRAILERS is supported by the OS without creating a request. | ||
// Instead, the WinHTTP team recommended to check if WINHTTP_OPTION_STREAM_ERROR_CODE is recognized by the OS. | ||
// Both features were introduced in Manganese and are planned to be backported to older Windows versions together. | ||
private static bool GetTrailersSupported() | ||
{ | ||
using SafeWinHttpHandle sessionHandle = Interop.WinHttp.WinHttpOpen( | ||
IntPtr.Zero, | ||
Interop.WinHttp.WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, | ||
Interop.WinHttp.WINHTTP_NO_PROXY_NAME, | ||
Interop.WinHttp.WINHTTP_NO_PROXY_BYPASS, | ||
(int)Interop.WinHttp.WINHTTP_FLAG_ASYNC); | ||
|
||
if (sessionHandle.IsInvalid) return false; | ||
uint buffer = 0; | ||
uint bufferSize = sizeof(uint); | ||
if (Interop.WinHttp.WinHttpQueryOption(sessionHandle, Interop.WinHttp.WINHTTP_OPTION_STREAM_ERROR_CODE, ref buffer, ref bufferSize)) | ||
{ | ||
Debug.Fail("Querying WINHTTP_OPTION_STREAM_ERROR_CODE on a session handle should never succeed."); | ||
return false; | ||
} | ||
|
||
int lastError = Marshal.GetLastWin32Error(); | ||
|
||
// New Windows builds are expected to fail with ERROR_WINHTTP_INCORRECT_HANDLE_TYPE, | ||
// when querying WINHTTP_OPTION_STREAM_ERROR_CODE on a session handle. | ||
return lastError != Interop.WinHttp.ERROR_INVALID_PARAMETER; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.