public HttpRequest(HttpMethod method, Uri requestUri, Version version, HttpRequestHeadersCollection headers, byte[] content) { Requires.NotNull(method, "method"); Method = method; Requires.NotNull(requestUri, "requestUri"); RequestUri = requestUri; Requires.NotNull(version, "version"); Version = version; Requires.NotNull(headers, "headers"); Headers = headers; Requires.NotNull(content, "content"); Content = content; }
private async Task<HttpRequest> GetRequestAsync(Stream stream) { Requires.NotNull(stream, "stream"); if (!stream.CanRead) { throw new IOException(); } using (var reader = new StreamReader(stream)) { string requestLine = await reader.ReadLineAsync(); string[] splittedRequestLine = requestLine.Split(' '); var method = new HttpMethod(splittedRequestLine[0]); Uri requestedUri = new Uri(splittedRequestLine[1], UriKind.RelativeOrAbsolute); var version = Version.Parse(splittedRequestLine[2].Remove(0, 5)); var headers = new HttpRequestHeadersCollection(); string headerLine = string.Empty; while ((headerLine = await reader.ReadLineAsync()) != string.Empty) { int index = headerLine.IndexOf(':'); string key = headerLine.Substring(0, index); string value = headerLine.Substring(index + 1).Trim(); headers.Add(key, value); } // todo: read body long contentLength; var content = new byte[0]; if (long.TryParse(headers.ContentLength, out contentLength)) { content = new byte[contentLength]; await stream.ReadAsync(content, 0, content.Length); } return new HttpRequest(method, requestedUri, version, headers, content); } }