Exemple #1
0
        public async Task TakeStartLineSetsHttpProtocolProperties(
            string requestLine,
            string expectedMethod,
            string expectedRawTarget,
            // This warns that theory methods should use all of their parameters,
            // but this method is using a shared data collection with HttpParserTests.ParsesRequestLine and others.
#pragma warning disable xUnit1026
            string expectedRawPath,
#pragma warning restore xUnit1026
            string expectedDecodedPath,
            string expectedQueryString,
            string expectedHttpVersion)
        {
            var requestLineBytes = Encoding.ASCII.GetBytes(requestLine);
            await _application.Output.WriteAsync(requestLineBytes);

            var readableBuffer = (await _transport.Input.ReadAsync()).Buffer;

            var returnValue = _http1Connection.TakeStartLine(readableBuffer, out _consumed, out _examined);

            _transport.Input.Advance(_consumed, _examined);

            Assert.True(returnValue);
            Assert.Equal(expectedMethod, _http1Connection.Method);
            Assert.Equal(expectedRawTarget, _http1Connection.RawTarget);
            Assert.Equal(expectedDecodedPath, _http1Connection.Path);
            Assert.Equal(expectedQueryString, _http1Connection.QueryString);
            Assert.Equal(expectedHttpVersion, _http1Connection.HttpVersion);
        }
Exemple #2
0
        public async Task TakeMessageHeadersDoesNotCountAlreadyConsumedBytesTowardsSizeLimit()
        {
            const string startLine = "GET / HTTP/1.1\r\n";

            // This doesn't actually need to be larger than the start line to cause the regression,
            // but doing so gives us a nice HeadersExceedMaxTotalSize error rather than an invalid slice
            // when we do see the regression.
            const string headerLine = "Header: makethislargerthanthestartline\r\n";

            _serviceContext.ServerOptions.Limits.MaxRequestHeadersTotalSize = headerLine.Length;
            _http1Connection.Reset();

            // Don't send header initially because the regression is only caught if TakeMessageHeaders
            // is called multiple times. The first call overcounted the header bytes consumed, and the
            // subsequent calls overslice the buffer due to the overcounting.
            await _application.Output.WriteAsync(Encoding.ASCII.GetBytes($"{startLine}"));

            var readableBuffer = (await _transport.Input.ReadAsync()).Buffer;

            SequencePosition TakeStartLineAndMessageHeaders()
            {
                var reader = new SequenceReader <byte>(readableBuffer);

                Assert.True(_http1Connection.TakeStartLine(ref reader));
                Assert.False(_http1Connection.TakeMessageHeaders(ref reader, trailers: false));
                return(reader.Position);
            }

            _transport.Input.AdvanceTo(TakeStartLineAndMessageHeaders());

            Assert.Equal(0, _http1Connection.RequestHeaders.Count);

            await _application.Output.WriteAsync(Encoding.ASCII.GetBytes($"{headerLine}\r\n"));

            readableBuffer = (await _transport.Input.ReadAsync()).Buffer;

            SequencePosition TakeMessageHeaders()
            {
                var reader = new SequenceReader <byte>(readableBuffer);

                Assert.True(_http1Connection.TakeMessageHeaders(ref reader, trailers: false));
                return(reader.Position);
            }

            _transport.Input.AdvanceTo(TakeMessageHeaders());

            Assert.Equal(1, _http1Connection.RequestHeaders.Count);
            Assert.Equal("makethislargerthanthestartline", _http1Connection.RequestHeaders["Header"]);
        }