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); }
private void ParseStatusLine(HttpResponseMessage response, Span <byte> line) { const int MinStatusLineLength = 12; // "HTTP/1.x 123" if (line.Length < MinStatusLineLength || line[8] != ' ') { throw new HttpRequestException("Invalid response"); } ulong first8Bytes = BinaryPrimitives.ReadUInt64LittleEndian(line); if (first8Bytes != s_http10Bytes) { throw new HttpRequestException("Invalid response"); } response.Version = HttpVersion.Version10; // Set the status code byte status1 = line[9], status2 = line[10], status3 = line[11]; if (!HttpParser.IsDigit(status1) || !HttpParser.IsDigit(status2) || !HttpParser.IsDigit(status3)) { throw new HttpRequestException("Invalid response"); } response.StatusCode = (HttpStatusCode)(100 * (status1 - '0') + 10 * (status2 - '0') + (status3 - '0')); // Parse (optional) reason phrase if (line.Length == MinStatusLineLength) { response.ReasonPhrase = string.Empty; } else if (line[MinStatusLineLength] == ' ') { Span <byte> reasonBytes = line.Slice(MinStatusLineLength + 1); try { response.ReasonPhrase = HttpParser.GetAsciiString(reasonBytes); } catch (FormatException error) { throw new HttpRequestException("Invalid response", error); } } else { throw new HttpRequestException("Invalid response"); } }