Skip to content

34411 HTTP/2 and HTTP/3: Response trailers collection not reset #37627

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
merged 2 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ protected override void OnReset()
_currentIHttpResponseTrailersFeature = this;
_currentIHttpResetFeature = this;
_currentIPersistentStateFeature = this;
_userTrailers = null;
}

protected override void OnRequestProcessingEnded()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ protected override void OnReset()
_currentIHttpMinRequestBodyDataRateFeature = this;
_currentIHttpResponseTrailersFeature = this;
_currentIHttpResetFeature = this;
_userTrailers = null;
}

protected override void ApplicationAbort() => ApplicationAbort(new ConnectionAbortedException(CoreStrings.ConnectionAbortedByApplication), Http3ErrorCode.InternalError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,74 @@ await ExpectAsync(Http2FrameType.HEADERS,
await StopConnectionAsync(expectedLastStreamId: 3, ignoreNonGoAwayFrames: false);
}

[Fact]
public async Task CustomResponseTrailersCollection_MultipleStreams_Reset()
{
IEnumerable<KeyValuePair<string, string>> requestHeaders = new[]
{
new KeyValuePair<string, string>(HeaderNames.Method, "GET"),
new KeyValuePair<string, string>(HeaderNames.Path, "/hello"),
new KeyValuePair<string, string>(HeaderNames.Scheme, "http"),
new KeyValuePair<string, string>(HeaderNames.Authority, "localhost:80"),
new KeyValuePair<string, string>(HeaderNames.ContentType, "application/json")
};

IHeaderDictionary trailersFirst = null;
IHeaderDictionary trailersSecond = null;
var requestCount = 0;
await InitializeConnectionAsync(context =>
{
requestCount++;

var trailersFeature = context.Features.Get<IHttpResponseTrailersFeature>();
if (requestCount == 1)
{
trailersFirst = new ResponseTrailersWrapper(trailersFeature.Trailers);
trailersFeature.Trailers = trailersFirst;
}
else
{
trailersSecond = trailersFeature.Trailers;
}
return Task.CompletedTask;
});

await StartStreamAsync(1, requestHeaders, endStream: true);

var trailersFrame = await ExpectAsync(Http2FrameType.HEADERS,
withLength: 36,
withFlags: (byte)(Http2HeadersFrameFlags.END_HEADERS | Http2HeadersFrameFlags.END_STREAM),
withStreamId: 1);

// Ping will trigger the stream to be returned to the pool so we can assert it
await SendPingAsync(Http2PingFrameFlags.NONE);
await ExpectAsync(Http2FrameType.PING,
withLength: 8,
withFlags: (byte)Http2PingFrameFlags.ACK,
withStreamId: 0);
await SendPingAsync(Http2PingFrameFlags.NONE);
await ExpectAsync(Http2FrameType.PING,
withLength: 8,
withFlags: (byte)Http2PingFrameFlags.ACK,
withStreamId: 0);

// Stream has been returned to the pool
Assert.Equal(1, _connection.StreamPool.Count);

await StartStreamAsync(3, requestHeaders, endStream: true);

trailersFrame = await ExpectAsync(Http2FrameType.HEADERS,
withLength: 6,
withFlags: (byte)(Http2HeadersFrameFlags.END_HEADERS | Http2HeadersFrameFlags.END_STREAM),
withStreamId: 3);

Assert.NotNull(trailersFirst);
Assert.NotNull(trailersSecond);
Assert.NotSame(trailersFirst, trailersSecond);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this test be merged with ResponseTrailers_MultipleStreams_Reset? It'd also be nice to get coverage in the HTTP/3 tests to verify that the built-in trailers dictionary is cleared if there isn't already.

Copy link
Contributor Author

@ladeak ladeak Oct 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I can merge the two. Not sure what you mean by the HTTP3 part though.
EDIT: I think I know what you mean, ack


await StopConnectionAsync(expectedLastStreamId: 3, ignoreNonGoAwayFrames: false);
}

[Fact]
public async Task StreamPool_SingleStream_ReturnedToPool()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@

using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http3;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
using Xunit;
using Http3SettingType = Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http3.Http3SettingType;
Expand Down Expand Up @@ -393,6 +396,48 @@ public async Task VariableMultipleStreamsInSequence_Success(int count, bool send
}
}

[Fact]
public async Task CustomResponseTrailersCollection_MultipleStreams_Reset()
{
var requestHeaders = new[]
{
new KeyValuePair<string, string>(HeaderNames.Method, "GET"),
new KeyValuePair<string, string>(HeaderNames.Path, "/hello"),
new KeyValuePair<string, string>(HeaderNames.Scheme, "http"),
new KeyValuePair<string, string>(HeaderNames.Authority, "localhost:80"),
new KeyValuePair<string, string>(HeaderNames.ContentType, "application/json")
};

IHeaderDictionary trailersFirst = null;
IHeaderDictionary trailersSecond = null;
var requestCount = 0;
await Http3Api.InitializeConnectionAsync(async context =>
{
requestCount++;

var trailersFeature = context.Features.Get<IHttpResponseTrailersFeature>();
if (requestCount == 1)
{
trailersFirst = new ResponseTrailersWrapper(trailersFeature.Trailers);
trailersFeature.Trailers = trailersFirst;
}
else
{
trailersSecond = trailersFeature.Trailers;
}
await context.Response.Body.WriteAsync(Encoding.UTF8.GetBytes($"Hello world").AsMemory());
});

for (int i = 0; i < 2; i++)
{
var streamContext = await MakeRequestAsync(i, requestHeaders, sendData: false, waitForServerDispose: true);
}

Assert.NotNull(trailersFirst);
Assert.NotNull(trailersSecond);
Assert.NotSame(trailersFirst, trailersSecond);
}

private async Task<ConnectionContext> MakeRequestAsync(int index, KeyValuePair<string, string>[] headers, bool sendData, bool waitForServerDispose)
{
var requestStream = await Http3Api.CreateRequestStream();
Expand Down Expand Up @@ -431,5 +476,35 @@ private async Task<ConnectionContext> MakeRequestAsync(int index, KeyValuePair<s

return streamContext;
}

private class ResponseTrailersWrapper : IHeaderDictionary
{
readonly IHeaderDictionary _innerHeaders;

public ResponseTrailersWrapper(IHeaderDictionary headers)
{
_innerHeaders = headers;
}

public StringValues this[string key] { get => _innerHeaders[key]; set => _innerHeaders[key] = value; }
public long? ContentLength { get => _innerHeaders.ContentLength; set => _innerHeaders.ContentLength = value; }
public ICollection<string> Keys => _innerHeaders.Keys;
public ICollection<StringValues> Values => _innerHeaders.Values;
public int Count => _innerHeaders.Count;
public bool IsReadOnly => _innerHeaders.IsReadOnly;
public void Add(string key, StringValues value) => _innerHeaders.Add(key, value);
public void Add(KeyValuePair<string, StringValues> item) => _innerHeaders.Add(item);
public void Clear() => _innerHeaders.Clear();
public bool Contains(KeyValuePair<string, StringValues> item) => _innerHeaders.Contains(item);
public bool ContainsKey(string key) => _innerHeaders.ContainsKey(key);
public void CopyTo(KeyValuePair<string, StringValues>[] array, int arrayIndex) => _innerHeaders.CopyTo(array, arrayIndex);
public IEnumerator<KeyValuePair<string, StringValues>> GetEnumerator() => _innerHeaders.GetEnumerator();
public bool Remove(string key) => _innerHeaders.Remove(key);
public bool Remove(KeyValuePair<string, StringValues> item) => _innerHeaders.Remove(item);
public bool TryGetValue(string key, out StringValues value) => _innerHeaders.TryGetValue(key, out value);
IEnumerator IEnumerable.GetEnumerator() => _innerHeaders.GetEnumerator();
}


}
}