예제 #1
0
        public static async Task <HttpRequestMessage> CreateNewAsync(this HttpRequestMessage me, Stream requestStream, CancellationToken ctsToken = default)
        {
            // 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(requestStream, ctsToken).ConfigureAwait(false);

            position += startLine.Length;

            var requestLine = RequestLine.CreateNew(startLine);
            var request     = new HttpRequestMessage(requestLine.Method, requestLine.URI);

            string headers = await HttpMessageHelper.ReadHeadersAsync(requestStream, ctsToken).ConfigureAwait(false);

            position += headers.Length + 2;

            var headerSection = HeaderSection.CreateNew(headers);
            var headerStruct  = headerSection.ToHttpRequestHeaders();

            HttpMessageHelper.AssertValidHeaders(headerStruct.RequestHeaders, headerStruct.ContentHeaders);
            request.Content = await HttpMessageHelper.GetContentAsync(requestStream, headerStruct, ctsToken).ConfigureAwait(false);

            HttpMessageHelper.CopyHeaders(headerStruct.RequestHeaders, request.Headers);
            if (request.Content != null)
            {
                HttpMessageHelper.CopyHeaders(headerStruct.ContentHeaders, request.Content.Headers);
            }
            return(request);
        }