diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http3RequestStream.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http3RequestStream.cs index a9718781324c8e..e3554b0fdf487d 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http3RequestStream.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http3RequestStream.cs @@ -46,7 +46,7 @@ internal sealed class Http3RequestStream : IHttpStreamHeadersHandler, IAsyncDisp private string[] _headerValues = Array.Empty(); /// Any trailing headers. - private List<(HeaderDescriptor name, string value)>? _trailingHeaders; + private HttpResponseHeaders? _trailingHeaders; // When reading response content, keep track of the number of bytes left in the current data frame. private long _responseDataPayloadRemaining; @@ -632,7 +632,7 @@ private async ValueTask DrainContentLength0Frames(CancellationToken cancellation private async ValueTask ProcessTrailersAsync(long payloadLength, CancellationToken cancellationToken) { - _trailingHeaders = new List<(HeaderDescriptor name, string value)>(); + _trailingHeaders = new HttpResponseHeaders(containsTrailingHeaders: true); await ReadHeadersAsync(payloadLength, cancellationToken).ConfigureAwait(false); // In typical cases, there should be no more frames. Make sure to read the EOS. @@ -651,13 +651,9 @@ private async ValueTask ProcessTrailersAsync(long payloadLength, CancellationTok private void CopyTrailersToResponseMessage(HttpResponseMessage responseMessage) { - if (_trailingHeaders?.Count > 0) + if (_trailingHeaders is not null) { - foreach ((HeaderDescriptor name, string value) in _trailingHeaders) - { - responseMessage.TrailingHeaders.TryAddWithoutValidation(name, value); - } - _trailingHeaders.Clear(); + responseMessage.StoreReceivedTrailingHeaders(_trailingHeaders); } } @@ -1156,7 +1152,7 @@ int ParseStatusCode(int? index, string value) _response!.Headers.TryAddWithoutValidation(descriptor.HeaderType.HasFlag(HttpHeaderType.Request) ? descriptor.AsCustomHeader() : descriptor, headerValue); break; case HeaderState.TrailingHeaders: - _trailingHeaders!.Add((descriptor.HeaderType.HasFlag(HttpHeaderType.Request) ? descriptor.AsCustomHeader() : descriptor, headerValue)); + _trailingHeaders!.TryAddWithoutValidation(descriptor.HeaderType.HasFlag(HttpHeaderType.Request) ? descriptor.AsCustomHeader() : descriptor, headerValue); break; default: Debug.Fail($"Unexpected {nameof(Http3RequestStream)}.{nameof(_headerState)} '{_headerState}'.");