public static async Task <HttpResponseMessage> CreateNewAsync(this HttpResponseMessage me, Stream responseStream, HttpMethod requestMethod, CancellationToken ctsToken = default(CancellationToken)) { // https://tools.ietf.org/html/rfc7230#section-3 // The normal procedure for parsing an HTTP message is to read the // start - line into a structure, read each header field into a hash table // by field name until the empty line, and then use the parsed data to // determine if a message body is expected.If a message body has been // indicated, then it is read as a stream until an amount of octets // equal to the message body length is read or the connection is closed. // https://tools.ietf.org/html/rfc7230#section-3 // All HTTP/ 1.1 messages consist of a start - line followed by a sequence // of octets in a format similar to the Internet Message Format // [RFC5322]: zero or more header fields(collectively referred to as // the "headers" or the "header section"), an empty line indicating the // end of the header section, and an optional message body. // HTTP - message = start - line // * (header - field CRLF ) // CRLF // [message - body] var position = 0; string startLine = await HttpMessageHelper.ReadStartLineAsync(responseStream, ctsToken).ConfigureAwait(false); position += startLine.Length; var statusLine = StatusLine.CreateNew(startLine); var response = new HttpResponseMessage(statusLine.StatusCode); string headers = await HttpMessageHelper.ReadHeadersAsync(responseStream, ctsToken).ConfigureAwait(false); position += headers.Length + 2; var headerSection = HeaderSection.CreateNew(headers); var headerStruct = headerSection.ToHttpResponseHeaders(); HttpMessageHelper.AssertValidHeaders(headerStruct.ResponseHeaders, headerStruct.ContentHeaders); response.Content = await HttpMessageHelper.GetContentAsync(responseStream, headerStruct, requestMethod, statusLine, ctsToken).ConfigureAwait(false); HttpMessageHelper.CopyHeaders(headerStruct.ResponseHeaders, response.Headers); if (response.Content != null) { HttpMessageHelper.CopyHeaders(headerStruct.ContentHeaders, response.Content.Headers); } return(response); }