Ejemplo n.º 1
0
        private async Task PopulateRequestAsync(Stream stream, IOwinRequest request)
        {
            var lineReader  = new ByLineReader(stream, 1024);
            var requestLine = await lineReader.NextLineAsync().ConfigureAwait(false);

            var firstLine = HttpParser.GetAsciiString(requestLine);
            var parts     = firstLine.Split(' ');

            request.Method   = parts[0];
            request.Protocol = parts[2];
            var uri = new Uri("http://localhost" + parts[1]);

            for (; ;)
            {
                var line = await lineReader.NextLineAsync().ConfigureAwait(false);

                if (line.Count == 0)
                {
                    break;
                }
                (var name, var values) = HttpParser.ParseHeaderNameValue(line);
                request.Headers.Add(name, values.ToArray());
            }
            request.Scheme      = uri.Scheme;
            request.Path        = PathString.FromUriComponent(uri);
            request.QueryString = QueryString.FromUriComponent(uri);
            request.Body        = new StreamWithPrefix(lineReader.Remaining, stream, null);
        }
        async Task <HttpRequestFeature> CreateRequesteAsync(Stream stream)
        {
            var lineReader  = new ByLineReader(stream, 1024);
            var requestLine = await lineReader.NextLineAsync().ConfigureAwait(false);

            var firstLine = HttpParser.GetAsciiString(requestLine);
            var parts     = firstLine.Split(' ');
            var result    = new HttpRequestFeature();

            result.Method = parts[0];
            var uri = new Uri("http://localhost" + parts[1]);

            result.Protocol = parts[2];
            for (; ;)
            {
                var line = await lineReader.NextLineAsync().ConfigureAwait(false);

                if (line.Count == 0)
                {
                    break;
                }
                (var name, var values) = HttpParser.ParseHeaderNameValue(line);
                result.Headers.Add(name, new Microsoft.Extensions.Primitives.StringValues(values.ToArray()));
            }
            result.Scheme      = uri.Scheme;
            result.Path        = PathString.FromUriComponent(uri);
            result.QueryString = QueryString.FromUriComponent(uri).Value;
            result.Body        = new StreamWithPrefix(lineReader.Remaining, stream, result.Headers.ContentLength);
            return(result);
        }
Ejemplo n.º 3
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            ValidateAndNormalizeRequest(request);
            var stream = await _dial.DialAsync(request, cancellationToken).ConfigureAwait(false);

            request.Properties.Add(UnderlyingStreamProperty, stream);
            var headerWriter = new HttpHeaderWriter(stream, 4096);
            await headerWriter.WriteMethodAndHeadersAsync(request).ConfigureAwait(false);

            await headerWriter.FlushAsync().ConfigureAwait(false);

            if (request.Content != null)
            {
                await request.Content.CopyToAsync(stream).ConfigureAwait(false);
            }
            await stream.FlushAsync().ConfigureAwait(false);

            await((stream as IWithCloseWriteSupport)?.CloseWriteAsync() ?? Task.CompletedTask).ConfigureAwait(false);
            var responseContent = new DialResponseContent();
            var response        = new HttpResponseMessage {
                RequestMessage = request, Content = responseContent
            };
            var lineReader = new ByLineReader(stream, 4096);

            ParseStatusLine(response, await lineReader.NextLineAsync().ConfigureAwait(false));
            for (; ;)
            {
                var line = await lineReader.NextLineAsync().ConfigureAwait(false);

                if (line.Count == 0)
                {
                    break;
                }
                try
                {
                    (var name, var value) = HttpParser.ParseHeaderNameValue(line);
                    if (!response.Headers.TryAddWithoutValidation(name, value))
                    {
                        response.Content.Headers.TryAddWithoutValidation(name, value);
                    }
                }
                catch (FormatException ex)
                {
                    throw new HttpRequestException("Error parsing header", ex);
                }
            }
            responseContent.SetContent(lineReader.Remaining, stream);
            return(response);
        }