Ejemplo n.º 1
0
        public static async Task <HeaderSection> CreateNewAsync(string headersString)
        {
            headersString = HeaderField.CorrectObsFolding(headersString);

            var hs = new HeaderSection();

            if (headersString.EndsWith(CRLF + CRLF))
            {
                headersString = headersString.TrimEnd(CRLF, StringComparison.Ordinal);
            }

            using var reader = new StringReader(headersString);
            while (true)
            {
                var field = reader.ReadLine(strictCRLF: true);
                if (field is null)
                {
                    break;
                }
                hs.Fields.Add(await HeaderField.CreateNewAsync(field).ConfigureAwait(false));
            }

            ValidateAndCorrectHeaders(hs);

            return(hs);
        }
Ejemplo n.º 2
0
        private static void ValidateAndCorrectHeaders(HeaderSection hs)
        {
            // https://tools.ietf.org/html/rfc7230#section-5.4
            // Since the Host field - value is critical information for handling a
            // request, a user agent SHOULD generate Host as the first header field
            // following the request - line.
            HeaderField hostToCorrect = null;

            foreach (var f in hs.Fields)
            {
                // if we find host
                if (f.Name == "Host")
                {
                    // if host is not first
                    if (hs.Fields.First().Name != "Host")
                    {
                        // then correct host
                        hostToCorrect = f;
                        break;
                    }
                }
            }

            if (hostToCorrect is { })