Skip to content

Commit

Permalink
HTTP/3: Update generated transport features (#34757)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Jul 28, 2021
1 parent e52f8e8 commit dd476b1
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17213,4 +17213,4 @@ public bool MoveNext()
}
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Connections.Features;
using Microsoft.AspNetCore.Http.Features;

namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Quic.Internal
{
internal sealed partial class QuicConnectionContext : IProtocolErrorCodeFeature, ITlsConnectionFeature
{
public long Error { get; set; }

// Support accessing client certificate
// https://github.com/dotnet/aspnetcore/issues/34756
public X509Certificate2? ClientCertificate
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}

public Task<X509Certificate2?> GetClientCertificateAsync(CancellationToken cancellationToken)
{
throw new NotSupportedException();
}

private void InitializeFeatures()
{
_currentIProtocolErrorCodeFeature = this;
_currentITlsConnectionFeature = this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Quic.Internal
{
internal class QuicConnectionContext : TransportMultiplexedConnection, IProtocolErrorCodeFeature
internal partial class QuicConnectionContext : TransportMultiplexedConnection
{
// Internal for testing.
internal PooledStreamStack<QuicStreamContext> StreamPool;
Expand All @@ -30,8 +30,6 @@ internal class QuicConnectionContext : TransportMultiplexedConnection, IProtocol

private Task? _closeTask;

public long Error { get; set; }

internal const int InitialStreamPoolSize = 5;
internal const int MaxStreamPoolSize = 100;
internal const long StreamPoolExpiryTicks = TimeSpan.TicksPerSecond * 5;
Expand All @@ -42,10 +40,10 @@ public QuicConnectionContext(QuicConnection connection, QuicTransportContext con
_context = context;
_connection = connection;
ConnectionClosed = _connectionClosedTokenSource.Token;
Features.Set<ITlsConnectionFeature>(new FakeTlsConnectionFeature());
Features.Set<IProtocolErrorCodeFeature>(this);

StreamPool = new PooledStreamStack<QuicStreamContext>(InitialStreamPoolSize);

InitializeFeatures();
}

public override async ValueTask DisposeAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@

namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Quic.Internal
{
internal sealed partial class QuicStreamContext : IPersistentStateFeature
internal sealed partial class QuicStreamContext : IPersistentStateFeature, IStreamDirectionFeature, IProtocolErrorCodeFeature, IStreamIdFeature
{
private IDictionary<object, object?>? _persistentState;

public bool CanRead { get; private set; }
public bool CanWrite { get; private set; }

public long Error { get; set; }

public long StreamId { get; private set; }

IDictionary<object, object?> IPersistentStateFeature.State
{
get
Expand All @@ -23,6 +30,10 @@ internal sealed partial class QuicStreamContext : IPersistentStateFeature
private void InitializeFeatures()
{
_currentIPersistentStateFeature = this;
_currentIStreamDirectionFeature = this;
_currentIProtocolErrorCodeFeature = this;
_currentIStreamIdFeature = this;
_currentITlsConnectionFeature = _connection._currentITlsConnectionFeature;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Quic.Internal
{
internal partial class QuicStreamContext : TransportConnection, IStreamDirectionFeature, IProtocolErrorCodeFeature, IStreamIdFeature, IPooledStream
internal partial class QuicStreamContext : TransportConnection, IPooledStream
{
// Internal for testing.
internal Task _processingTask = Task.CompletedTask;
Expand Down Expand Up @@ -70,10 +70,6 @@ public QuicStreamContext(QuicConnectionContext connection, QuicTransportContext
private PipeWriter Input => Application.Output;
private PipeReader Output => Application.Input;

public bool CanRead { get; private set; }
public bool CanWrite { get; private set; }

public long StreamId { get; private set; }
public bool CanReuse { get; private set; }

public void Initialize(QuicStream stream)
Expand All @@ -89,13 +85,6 @@ public void Initialize(QuicStream stream)

ConnectionClosed = _streamClosedTokenSource.Token;

// TODO - add to generated features
Features.Set<IStreamDirectionFeature>(this);
Features.Set<IProtocolErrorCodeFeature>(this);
Features.Set<IStreamIdFeature>(this);
// TODO populate the ITlsConnectionFeature (requires client certs).
Features.Set<ITlsConnectionFeature>(new FakeTlsConnectionFeature());

InitializeFeatures();

CanRead = _stream.CanRead;
Expand Down Expand Up @@ -134,8 +123,6 @@ public override string ConnectionId
set => _connectionId = value;
}

public long Error { get; set; }

public long PoolExpirationTicks { get; set; }

public void Start()
Expand Down
3 changes: 1 addition & 2 deletions src/Servers/Kestrel/shared/KnownHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1338,8 +1338,7 @@ public bool MoveNext()
}}
")}}}";

// Temporary workaround for https://github.com/dotnet/runtime/issues/55688
return s.Replace("{{", "{").Replace("}}", "}");
return s;
}

private static string GetHeaderLookup()
Expand Down
88 changes: 88 additions & 0 deletions src/Servers/Kestrel/shared/TransportConnection.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ internal partial class TransportConnection : IFeatureCollection,
// Other reserved feature slots
internal protected IPersistentStateFeature? _currentIPersistentStateFeature;
internal protected IConnectionSocketFeature? _currentIConnectionSocketFeature;
internal protected IProtocolErrorCodeFeature? _currentIProtocolErrorCodeFeature;
internal protected IStreamDirectionFeature? _currentIStreamDirectionFeature;
internal protected IStreamIdFeature? _currentIStreamIdFeature;
internal protected ITlsConnectionFeature? _currentITlsConnectionFeature;

private int _featureRevision;

Expand All @@ -45,6 +49,10 @@ private void FastReset()

_currentIPersistentStateFeature = null;
_currentIConnectionSocketFeature = null;
_currentIProtocolErrorCodeFeature = null;
_currentIStreamDirectionFeature = null;
_currentIStreamIdFeature = null;
_currentITlsConnectionFeature = null;
}

// Internal for testing
Expand Down Expand Up @@ -144,6 +152,22 @@ private void ExtraFeatureSet(Type key, object? value)
{
feature = _currentIConnectionSocketFeature;
}
else if (key == typeof(IProtocolErrorCodeFeature))
{
feature = _currentIProtocolErrorCodeFeature;
}
else if (key == typeof(IStreamDirectionFeature))
{
feature = _currentIStreamDirectionFeature;
}
else if (key == typeof(IStreamIdFeature))
{
feature = _currentIStreamIdFeature;
}
else if (key == typeof(ITlsConnectionFeature))
{
feature = _currentITlsConnectionFeature;
}
else if (MaybeExtra != null)
{
feature = ExtraFeatureGet(key);
Expand Down Expand Up @@ -184,6 +208,22 @@ private void ExtraFeatureSet(Type key, object? value)
{
_currentIConnectionSocketFeature = (IConnectionSocketFeature?)value;
}
else if (key == typeof(IProtocolErrorCodeFeature))
{
_currentIProtocolErrorCodeFeature = (IProtocolErrorCodeFeature?)value;
}
else if (key == typeof(IStreamDirectionFeature))
{
_currentIStreamDirectionFeature = (IStreamDirectionFeature?)value;
}
else if (key == typeof(IStreamIdFeature))
{
_currentIStreamIdFeature = (IStreamIdFeature?)value;
}
else if (key == typeof(ITlsConnectionFeature))
{
_currentITlsConnectionFeature = (ITlsConnectionFeature?)value;
}
else
{
ExtraFeatureSet(key, value);
Expand Down Expand Up @@ -226,6 +266,22 @@ private void ExtraFeatureSet(Type key, object? value)
{
feature = Unsafe.As<IConnectionSocketFeature?, TFeature?>(ref _currentIConnectionSocketFeature);
}
else if (typeof(TFeature) == typeof(IProtocolErrorCodeFeature))
{
feature = Unsafe.As<IProtocolErrorCodeFeature?, TFeature?>(ref _currentIProtocolErrorCodeFeature);
}
else if (typeof(TFeature) == typeof(IStreamDirectionFeature))
{
feature = Unsafe.As<IStreamDirectionFeature?, TFeature?>(ref _currentIStreamDirectionFeature);
}
else if (typeof(TFeature) == typeof(IStreamIdFeature))
{
feature = Unsafe.As<IStreamIdFeature?, TFeature?>(ref _currentIStreamIdFeature);
}
else if (typeof(TFeature) == typeof(ITlsConnectionFeature))
{
feature = Unsafe.As<ITlsConnectionFeature?, TFeature?>(ref _currentITlsConnectionFeature);
}
else if (MaybeExtra != null)
{
feature = (TFeature?)(ExtraFeatureGet(typeof(TFeature)));
Expand Down Expand Up @@ -269,6 +325,22 @@ private void ExtraFeatureSet(Type key, object? value)
{
_currentIConnectionSocketFeature = Unsafe.As<TFeature?, IConnectionSocketFeature?>(ref feature);
}
else if (typeof(TFeature) == typeof(IProtocolErrorCodeFeature))
{
_currentIProtocolErrorCodeFeature = Unsafe.As<TFeature?, IProtocolErrorCodeFeature?>(ref feature);
}
else if (typeof(TFeature) == typeof(IStreamDirectionFeature))
{
_currentIStreamDirectionFeature = Unsafe.As<TFeature?, IStreamDirectionFeature?>(ref feature);
}
else if (typeof(TFeature) == typeof(IStreamIdFeature))
{
_currentIStreamIdFeature = Unsafe.As<TFeature?, IStreamIdFeature?>(ref feature);
}
else if (typeof(TFeature) == typeof(ITlsConnectionFeature))
{
_currentITlsConnectionFeature = Unsafe.As<TFeature?, ITlsConnectionFeature?>(ref feature);
}
else
{
ExtraFeatureSet(typeof(TFeature), feature);
Expand Down Expand Up @@ -305,6 +377,22 @@ private IEnumerable<KeyValuePair<Type, object>> FastEnumerable()
{
yield return new KeyValuePair<Type, object>(typeof(IConnectionSocketFeature), _currentIConnectionSocketFeature);
}
if (_currentIProtocolErrorCodeFeature != null)
{
yield return new KeyValuePair<Type, object>(typeof(IProtocolErrorCodeFeature), _currentIProtocolErrorCodeFeature);
}
if (_currentIStreamDirectionFeature != null)
{
yield return new KeyValuePair<Type, object>(typeof(IStreamDirectionFeature), _currentIStreamDirectionFeature);
}
if (_currentIStreamIdFeature != null)
{
yield return new KeyValuePair<Type, object>(typeof(IStreamIdFeature), _currentIStreamIdFeature);
}
if (_currentITlsConnectionFeature != null)
{
yield return new KeyValuePair<Type, object>(typeof(ITlsConnectionFeature), _currentITlsConnectionFeature);
}

if (MaybeExtra != null)
{
Expand Down
Loading

0 comments on commit dd476b1

Please sign in to comment.