public Http2Utilities(ConnectionContext clientConnectionContext, ILogger logger, CancellationToken stopToken)
 {
     _hpackDecoder = new HPackDecoder((int)_clientSettings.HeaderTableSize, MaxRequestHeaderFieldSize);
     _pair         = new DuplexPipe.DuplexPipePair(transport: null, application: clientConnectionContext.Transport);
     Logger        = logger;
     StopToken     = stopToken;
 }
        private TestHttp1Connection MakeHttp1Connection()
        {
            var options = new PipeOptions(_memoryPool, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline, useSynchronizationContext: false);
            var pair    = DuplexPipe.CreateConnectionPair(options, options);

            _pair = pair;

            var serviceContext = new ServiceContext
            {
                DateHeaderValueManager = new DateHeaderValueManager(),
                ServerOptions          = new KestrelServerOptions(),
                Log        = new MockTrace(),
                HttpParser = new HttpParser <Http1ParsingHandler>()
            };

            var http1Connection = new TestHttp1Connection(new HttpConnectionContext
            {
                ServiceContext     = serviceContext,
                ConnectionFeatures = new FeatureCollection(),
                MemoryPool         = _memoryPool,
                TimeoutControl     = new TimeoutControl(timeoutHandler: null),
                Transport          = pair.Transport
            });

            http1Connection.Reset();
            http1Connection.InitializeBodyControl(MessageBody.ZeroContentLengthKeepAlive);
            serviceContext.DateHeaderValueManager.OnHeartbeat(DateTimeOffset.UtcNow);

            return(http1Connection);
        }
        public void Setup()
        {
            _memoryPool = KestrelMemoryPool.Create();
            var options = new PipeOptions(_memoryPool, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline, useSynchronizationContext: false);

            _pair = DuplexPipe.CreateConnectionPair(options, options);

            var serviceContext = new ServiceContext
            {
                DateHeaderValueManager = new DateHeaderValueManager(),
                ServerOptions          = new KestrelServerOptions(),
                Log        = new MockTrace(),
                HttpParser = new HttpParser <Http1ParsingHandler>()
            };

            var http1Connection = new TestHttp1Connection(new Http1ConnectionContext
            {
                ServiceContext     = serviceContext,
                ConnectionFeatures = new FeatureCollection(),
                MemoryPool         = _memoryPool,
                TimeoutControl     = new MockTimeoutControl(),
                Application        = _pair.Application,
                Transport          = _pair.Transport
            });

            http1Connection.Reset();

            _http1Connection = http1Connection;
        }
Exemple #4
0
        private TestHttp1Connection MakeHttp1Connection()
        {
            var options = new PipeOptions(_memoryPool, useSynchronizationContext: false);
            var pair    = DuplexPipe.CreateConnectionPair(options, options);

            _pair = pair;

            var serviceContext = TestContextFactory.CreateServiceContext(
                serverOptions: new KestrelServerOptions(),
                httpParser: new HttpParser <Http1ParsingHandler>(),
                dateHeaderValueManager: new DateHeaderValueManager());

            var connectionContext = TestContextFactory.CreateHttpConnectionContext(
                serviceContext: serviceContext,
                connectionContext: null,
                transport: pair.Transport,
                timeoutControl: new TimeoutControl(timeoutHandler: null),
                memoryPool: _memoryPool,
                connectionFeatures: new FeatureCollection());

            var http1Connection = new TestHttp1Connection(connectionContext);

            http1Connection.Reset();
            http1Connection.InitializeBodyControl(MessageBody.ZeroContentLengthKeepAlive);
            serviceContext.DateHeaderValueManager.OnHeartbeat(DateTimeOffset.UtcNow);

            return(http1Connection);
        }
        private TestHttp1Connection MakeHttp1Connection()
        {
            var pair = DuplexPipe.CreateConnectionPair(_memoryPool);

            _pair = pair;

            var serviceContext = new ServiceContext
            {
                DateHeaderValueManager = new DateHeaderValueManager(),
                ServerOptions          = new KestrelServerOptions(),
                Log        = new MockTrace(),
                HttpParser = new HttpParser <Http1ParsingHandler>()
            };

            var http1Connection = new TestHttp1Connection(new Http1ConnectionContext
            {
                ServiceContext     = serviceContext,
                ConnectionFeatures = new FeatureCollection(),
                MemoryPool         = _memoryPool,
                Application        = pair.Application,
                Transport          = pair.Transport
            });

            http1Connection.Reset();
            http1Connection.InitializeStreams(MessageBody.ZeroContentLengthKeepAlive);

            return(http1Connection);
        }
Exemple #6
0
        public virtual void GlobalSetup()
        {
            _memoryPool = SlabMemoryPoolFactory.Create();
            _httpFrame  = new Http2Frame();

            var options = new PipeOptions(_memoryPool, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline, useSynchronizationContext: false);

            _connectionPair = DuplexPipe.CreateConnectionPair(options, options);

            _httpRequestHeaders = new HttpRequestHeaders();
            _httpRequestHeaders.HeaderMethod    = new StringValues("GET");
            _httpRequestHeaders.HeaderPath      = new StringValues("/");
            _httpRequestHeaders.HeaderScheme    = new StringValues("http");
            _httpRequestHeaders.HeaderAuthority = new StringValues("localhost:80");

            _headersBuffer = new byte[1024 * 16];
            _hpackEncoder  = new HPackEncoder();

            var serviceContext = TestContextFactory.CreateServiceContext(
                serverOptions: new KestrelServerOptions(),
                dateHeaderValueManager: new DateHeaderValueManager(),
                systemClock: new MockSystemClock(),
                log: new MockTrace());

            serviceContext.DateHeaderValueManager.OnHeartbeat(default);
Exemple #7
0
        public void GlobalSetup()
        {
            _memoryPool   = SlabMemoryPoolFactory.Create();
            _httpFrame    = new Http2Frame();
            _responseData = new string('!', ResponseDataLength);

            var options = new PipeOptions(_memoryPool, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline, useSynchronizationContext: false);

            _connectionPair = DuplexPipe.CreateConnectionPair(options, options);

            _httpRequestHeaders = new HttpRequestHeaders();
            _httpRequestHeaders.Append(HeaderNames.Method, new StringValues("GET"));
            _httpRequestHeaders.Append(HeaderNames.Path, new StringValues("/"));
            _httpRequestHeaders.Append(HeaderNames.Scheme, new StringValues("http"));
            _httpRequestHeaders.Append(HeaderNames.Authority, new StringValues("localhost:80"));

            _headersBuffer = new byte[1024 * 16];

            var serviceContext = new ServiceContext
            {
                DateHeaderValueManager = new DateHeaderValueManager(),
                ServerOptions          = new KestrelServerOptions(),
                Log         = new KestrelTrace(NullLogger.Instance),
                SystemClock = new MockSystemClock()
            };

            serviceContext.DateHeaderValueManager.OnHeartbeat(default);
Exemple #8
0
 public Http2Utilities(ConnectionContext clientConnectionContext)
 {
     _hpackDecoder = new HPackDecoder((int)_clientSettings.HeaderTableSize, MaxRequestHeaderFieldSize);
     _pair         = new DuplexPipe.DuplexPipePair(transport: null, application: clientConnectionContext.Transport);
 }
        public Http2TestBase()
        {
            // Always dispatch test code back to the ThreadPool. This prevents deadlocks caused by continuing
            // Http2Connection.ProcessRequestsAsync() loop with writer locks acquired. Run product code inline to make
            // it easier to verify request frames are processed correctly immediately after sending the them.
            var inputPipeOptions = new PipeOptions(
                pool: _memoryPool,
                readerScheduler: PipeScheduler.Inline,
                writerScheduler: PipeScheduler.ThreadPool,
                useSynchronizationContext: false
                );
            var outputPipeOptions = new PipeOptions(
                pool: _memoryPool,
                readerScheduler: PipeScheduler.ThreadPool,
                writerScheduler: PipeScheduler.Inline,
                useSynchronizationContext: false
                );

            _pair         = DuplexPipe.CreateConnectionPair(inputPipeOptions, outputPipeOptions);
            _hpackDecoder = new HPackDecoder((int)_clientSettings.HeaderTableSize);

            _noopApplication = context => Task.CompletedTask;

            _readHeadersApplication = context =>
            {
                foreach (var header in context.Request.Headers)
                {
                    _receivedHeaders[header.Key] = header.Value.ToString();
                }

                return(Task.CompletedTask);
            };

            _readTrailersApplication = async context =>
            {
                using (var ms = new MemoryStream())
                {
                    // Consuming the entire request body guarantees trailers will be available
                    await context.Request.Body.CopyToAsync(ms);
                }

                foreach (var header in context.Request.Headers)
                {
                    _receivedHeaders[header.Key] = header.Value.ToString();
                }
            };

            _bufferingApplication = async context =>
            {
                var data     = new List <byte>();
                var buffer   = new byte[1024];
                var received = 0;

                while ((received = await context.Request.Body.ReadAsync(buffer, 0, buffer.Length)) > 0)
                {
                    data.AddRange(new ArraySegment <byte>(buffer, 0, received));
                }

                await context.Response.Body.WriteAsync(data.ToArray(), 0, data.Count);
            };

            _echoApplication = async context =>
            {
                var buffer   = new byte[Http2Limits.MinAllowedMaxFrameSize];
                var received = 0;

                while ((received = await context.Request.Body.ReadAsync(buffer, 0, buffer.Length)) > 0)
                {
                    await context.Response.Body.WriteAsync(buffer, 0, received);
                }
            };

            _echoWaitForAbortApplication = async context =>
            {
                var buffer   = new byte[Http2Limits.MinAllowedMaxFrameSize];
                var received = 0;

                while ((received = await context.Request.Body.ReadAsync(buffer, 0, buffer.Length)) > 0)
                {
                    await context.Response.Body.WriteAsync(buffer, 0, received);
                }

                var sem = new SemaphoreSlim(0);

                context.RequestAborted.Register(() =>
                {
                    sem.Release();
                });

                await sem.WaitAsync().DefaultTimeout();
            };

            _largeHeadersApplication = context =>
            {
                foreach (var name in new[] { "a", "b", "c", "d", "e", "f", "g", "h" })
                {
                    context.Response.Headers[name] = _4kHeaderValue;
                }

                return(Task.CompletedTask);
            };

            _waitForAbortApplication = async context =>
            {
                var streamIdFeature = context.Features.Get <IHttp2StreamIdFeature>();
                var sem             = new SemaphoreSlim(0);

                context.RequestAborted.Register(() =>
                {
                    lock (_abortedStreamIdsLock)
                    {
                        _abortedStreamIds.Add(streamIdFeature.StreamId);
                    }

                    sem.Release();
                });

                await sem.WaitAsync().DefaultTimeout();

                _runningStreams[streamIdFeature.StreamId].TrySetResult(null);
            };

            _waitForAbortFlushingApplication = async context =>
            {
                var streamIdFeature = context.Features.Get <IHttp2StreamIdFeature>();
                var sem             = new SemaphoreSlim(0);

                context.RequestAborted.Register(() =>
                {
                    lock (_abortedStreamIdsLock)
                    {
                        _abortedStreamIds.Add(streamIdFeature.StreamId);
                    }

                    sem.Release();
                });

                await sem.WaitAsync().DefaultTimeout();

                await context.Response.Body.FlushAsync();

                _runningStreams[streamIdFeature.StreamId].TrySetResult(null);
            };

            _waitForAbortWithDataApplication = async context =>
            {
                var streamIdFeature = context.Features.Get <IHttp2StreamIdFeature>();
                var sem             = new SemaphoreSlim(0);

                context.RequestAborted.Register(() =>
                {
                    lock (_abortedStreamIdsLock)
                    {
                        _abortedStreamIds.Add(streamIdFeature.StreamId);
                    }

                    sem.Release();
                });

                await sem.WaitAsync().DefaultTimeout();

                await context.Response.Body.WriteAsync(new byte[10], 0, 10);

                _runningStreams[streamIdFeature.StreamId].TrySetResult(null);
            };

            _echoMethod = context =>
            {
                context.Response.Headers["Method"] = context.Request.Method;

                return(Task.CompletedTask);
            };

            _echoHost = context =>
            {
                context.Response.Headers[HeaderNames.Host] = context.Request.Headers[HeaderNames.Host];

                return(Task.CompletedTask);
            };

            _echoPath = context =>
            {
                context.Response.Headers["path"]      = context.Request.Path.ToString();
                context.Response.Headers["rawtarget"] = context.Features.Get <IHttpRequestFeature>().RawTarget;

                return(Task.CompletedTask);
            };
        }