Пример #1
0
        protected void CreateConnection()
        {
            var limits = _serviceContext.ServerOptions.Limits;

            var features = new FeatureCollection();

            features.Set <IQuicCreateStreamFeature>(this);
            features.Set <IQuicStreamListenerFeature>(this);

            var httpConnectionContext = new HttpConnectionContext
            {
                ConnectionContext  = _mockConnectionContext.Object,
                ConnectionFeatures = features,
                ServiceContext     = _serviceContext,
                MemoryPool         = _memoryPool,
                Transport          = null, // Make sure it's null
                TimeoutControl     = _mockTimeoutControl.Object
            };

            _connection = new Http3Connection(httpConnectionContext);
            var httpConnection = new HttpConnection(httpConnectionContext);

            httpConnection.Initialize(_connection);
            _mockTimeoutHandler.Setup(h => h.OnTimeout(It.IsAny <TimeoutReason>()))
            .Callback <TimeoutReason>(r => httpConnection.OnTimeout(r));
        }
Пример #2
0
 public Http3RequestStream(Http3InMemory testBase, Http3Connection connection, TestStreamContext testStreamContext, Http3RequestHeaderHandler headerHandler)
     : base(testStreamContext)
 {
     TestBase            = testBase;
     Connection          = connection;
     _streamId           = testStreamContext.StreamId;
     _testStreamContext  = testStreamContext;
     this._headerHandler = headerHandler;
 }
Пример #3
0
            public Http3RequestStream(Http3TestBase testBase, Http3Connection connection)
            {
                _testBase   = testBase;
                _connection = connection;
                var inputPipeOptions  = GetInputPipeOptions(_testBase._serviceContext, _testBase._memoryPool, PipeScheduler.ThreadPool);
                var outputPipeOptions = GetOutputPipeOptions(_testBase._serviceContext, _testBase._memoryPool, PipeScheduler.ThreadPool);

                _pair = DuplexPipe.CreateConnectionPair(inputPipeOptions, outputPipeOptions);

                StreamContext = new TestStreamContext(canRead: true, canWrite: true, _pair, this);
            }
        public Http3HttpProtocolFeatureCollectionTests()
        {
            var connection = new Http3Connection(TestContextFactory.CreateHttp3ConnectionContext());

            var streamContext = TestContextFactory.CreateHttp3StreamContext(transport: DuplexPipe.CreateConnectionPair(new PipeOptions(), new PipeOptions()).Application);

            var http3Stream = new TestHttp3Stream(connection, streamContext);

            http3Stream.Reset();
            _http3Collection = http3Stream;
        }
Пример #5
0
            public Http3RequestStream(Http3TestBase testBase, Http3Connection connection)
            {
                _testBase   = testBase;
                _connection = connection;
                var inputPipeOptions  = GetInputPipeOptions(_testBase._serviceContext, _testBase._memoryPool, PipeScheduler.ThreadPool);
                var outputPipeOptions = GetOutputPipeOptions(_testBase._serviceContext, _testBase._memoryPool, PipeScheduler.ThreadPool);

                _pair = DuplexPipe.CreateConnectionPair(inputPipeOptions, outputPipeOptions);

                ConnectionContext           = new DefaultConnectionContext();
                ConnectionContext.Transport = _pair.Transport;
                ConnectionContext.Features.Set <IQuicStreamFeature>(this);
            }
Пример #6
0
        public Task OnConnectionAsync(MultiplexedConnectionContext connectionContext)
        {
            var memoryPoolFeature = connectionContext.Features.Get <IMemoryPoolFeature>();

            var http3ConnectionContext = new Http3ConnectionContext(
                connectionContext.ConnectionId,
                connectionContext,
                _serviceContext,
                connectionContext.Features,
                memoryPoolFeature?.MemoryPool ?? System.Buffers.MemoryPool <byte> .Shared,
                connectionContext.LocalEndPoint as IPEndPoint,
                connectionContext.RemoteEndPoint as IPEndPoint);

            var connection = new Http3Connection(http3ConnectionContext);

            return(connection.ProcessStreamsAsync(_application));
        }
Пример #7
0
    public static Http3StreamContext CreateHttp3StreamContext(
        string connectionId = null,
        BaseConnectionContext connectionContext = null,
        ServiceContext serviceContext           = null,
        IFeatureCollection connectionFeatures   = null,
        MemoryPool <byte> memoryPool            = null,
        IPEndPoint localEndPoint       = null,
        IPEndPoint remoteEndPoint      = null,
        IDuplexPipe transport          = null,
        ITimeoutControl timeoutControl = null,
        IHttp3StreamLifetimeHandler streamLifetimeHandler = null)
    {
        var http3ConnectionContext = CreateHttp3ConnectionContext(
            null,
            serviceContext,
            connectionFeatures,
            memoryPool,
            localEndPoint,
            remoteEndPoint,
            timeoutControl);

        var http3Conection = new Http3Connection(http3ConnectionContext)
        {
            _streamLifetimeHandler = streamLifetimeHandler
        };

        return(new Http3StreamContext
               (
                   connectionId: connectionId ?? "TestConnectionId",
                   protocols: HttpProtocols.Http3,
                   altSvcHeader: null,
                   connectionContext: connectionContext,
                   serviceContext: serviceContext ?? CreateServiceContext(new KestrelServerOptions()),
                   connectionFeatures: connectionFeatures ?? new FeatureCollection(),
                   memoryPool: memoryPool ?? MemoryPool <byte> .Shared,
                   localEndPoint: localEndPoint,
                   remoteEndPoint: remoteEndPoint,
                   streamContext: new DefaultConnectionContext(),
                   connection: http3Conection
               )
        {
            TimeoutControl = timeoutControl,
            Transport = transport,
        });
    }
Пример #8
0
        public Task OnConnectionAsync(MultiplexedConnectionContext connectionContext)
        {
            var memoryPoolFeature = connectionContext.Features.Get <IMemoryPoolFeature>();

            var http3ConnectionContext = new Http3ConnectionContext
            {
                ConnectionId       = connectionContext.ConnectionId,
                ConnectionContext  = connectionContext,
                ServiceContext     = _serviceContext,
                ConnectionFeatures = connectionContext.Features,
                MemoryPool         = memoryPoolFeature?.MemoryPool ?? System.Buffers.MemoryPool <byte> .Shared,
                LocalEndPoint      = connectionContext.LocalEndPoint as IPEndPoint,
                RemoteEndPoint     = connectionContext.RemoteEndPoint as IPEndPoint
            };

            var connection = new Http3Connection(http3ConnectionContext);

            return(connection.ProcessRequestsAsync(_application));
        }
Пример #9
0
 public Http3StreamContext(
     string connectionId,
     HttpProtocols protocols,
     AltSvcHeader?altSvcHeader,
     BaseConnectionContext connectionContext,
     ServiceContext serviceContext,
     IFeatureCollection connectionFeatures,
     MemoryPool <byte> memoryPool,
     IPEndPoint?localEndPoint,
     IPEndPoint?remoteEndPoint,
     ConnectionContext streamContext,
     Http3Connection connection) : base(connectionId, protocols, altSvcHeader, connectionContext, serviceContext, connectionFeatures, memoryPool, localEndPoint, remoteEndPoint)
 {
     StreamLifetimeHandler = connection._streamLifetimeHandler;
     StreamContext         = streamContext;
     ClientPeerSettings    = connection._clientSettings;
     ServerPeerSettings    = connection._serverSettings;
     Connection            = connection;
 }
Пример #10
0
        protected void CreateConnection()
        {
            MultiplexedConnectionContext = new TestMultiplexedConnectionContext(this);

            var httpConnectionContext = new Http3ConnectionContext(
                connectionId: "TestConnectionId",
                connectionContext: MultiplexedConnectionContext,
                connectionFeatures: MultiplexedConnectionContext.Features,
                serviceContext: _serviceContext,
                memoryPool: _memoryPool,
                localEndPoint: null,
                remoteEndPoint: null);

            httpConnectionContext.TimeoutControl = _mockTimeoutControl.Object;

            Connection = new Http3Connection(httpConnectionContext);
            _mockTimeoutHandler.Setup(h => h.OnTimeout(It.IsAny <TimeoutReason>()))
            .Callback <TimeoutReason>(r => Connection.OnTimeout(r));
        }
Пример #11
0
        protected void CreateConnection()
        {
            var limits = _serviceContext.ServerOptions.Limits;

            var features = new FeatureCollection();

            _multiplexedContext = new TestMultiplexedConnectionContext(this);

            var httpConnectionContext = new Http3ConnectionContext
            {
                ConnectionContext  = _multiplexedContext,
                ConnectionFeatures = features,
                ServiceContext     = _serviceContext,
                MemoryPool         = _memoryPool,
                TimeoutControl     = _mockTimeoutControl.Object
            };

            _connection = new Http3Connection(httpConnectionContext);
            _mockTimeoutHandler.Setup(h => h.OnTimeout(It.IsAny <TimeoutReason>()))
            .Callback <TimeoutReason>(r => _connection.OnTimeout(r));
        }
Пример #12
0
    public async Task ProcessRequestsAsync <TContext>(IHttpApplication <TContext> httpApplication) where TContext : notnull
    {
        try
        {
            // Ensure TimeoutControl._lastTimestamp is initialized before anything that could set timeouts runs.
            _timeoutControl.Initialize(_systemClock.UtcNowTicks);

            IRequestProcessor?requestProcessor = null;

            switch (SelectProtocol())
            {
            case HttpProtocols.Http1:
                // _http1Connection must be initialized before adding the connection to the connection manager
                requestProcessor        = _http1Connection = new Http1Connection <TContext>((HttpConnectionContext)_context);
                _protocolSelectionState = ProtocolSelectionState.Selected;
                break;

            case HttpProtocols.Http2:
                // _http2Connection must be initialized before yielding control to the transport thread,
                // to prevent a race condition where _http2Connection.Abort() is called just as
                // _http2Connection is about to be initialized.
                requestProcessor        = new Http2Connection((HttpConnectionContext)_context);
                _protocolSelectionState = ProtocolSelectionState.Selected;
                break;

            case HttpProtocols.Http3:
                requestProcessor        = new Http3Connection((HttpMultiplexedConnectionContext)_context);
                _protocolSelectionState = ProtocolSelectionState.Selected;
                break;

            case HttpProtocols.None:
                // An error was already logged in SelectProtocol(), but we should close the connection.
                break;

            default:
                // SelectProtocol() only returns Http1, Http2, Http3 or None.
                throw new NotSupportedException($"{nameof(SelectProtocol)} returned something other than Http1, Http2 or None.");
            }

            _requestProcessor = requestProcessor;

            if (requestProcessor != null)
            {
                var connectionHeartbeatFeature            = _context.ConnectionFeatures.Get <IConnectionHeartbeatFeature>();
                var connectionLifetimeNotificationFeature = _context.ConnectionFeatures.Get <IConnectionLifetimeNotificationFeature>();

                // These features should never be null in Kestrel itself, if this middleware is ever refactored to run outside of kestrel,
                // we'll need to handle these missing.
                Debug.Assert(connectionHeartbeatFeature != null, nameof(IConnectionHeartbeatFeature) + " is missing!");
                Debug.Assert(connectionLifetimeNotificationFeature != null, nameof(IConnectionLifetimeNotificationFeature) + " is missing!");

                // Register the various callbacks once we're going to start processing requests

                // The heart beat for various timeouts
                connectionHeartbeatFeature?.OnHeartbeat(state => ((HttpConnection)state).Tick(), this);

                // Register for graceful shutdown of the server
                using var shutdownRegistration = connectionLifetimeNotificationFeature?.ConnectionClosedRequested.Register(state => ((HttpConnection)state !).StopProcessingNextRequest(), this);

                // Register for connection close
                using var closedRegistration = _context.ConnectionContext.ConnectionClosed.Register(state => ((HttpConnection)state !).OnConnectionClosed(), this);

                await requestProcessor.ProcessRequestsAsync(httpApplication);
            }
        }
        catch (Exception ex)
        {
            Log.LogCritical(0, ex, $"Unexpected exception in {nameof(HttpConnection)}.{nameof(ProcessRequestsAsync)}.");
        }
    }
 public TestHttp3Stream(Http3Connection connection, Http3StreamContext context) : base(connection, context)
 {
 }