Exemplo n.º 1
0
        internal static int GetViaLength(string?input, int startIndex, out object?parsedValue)
        {
            Debug.Assert(startIndex >= 0);

            parsedValue = null;

            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
            {
                return(0);
            }

            // Read <protocolName> and <protocolVersion> in '[<protocolName>/]<protocolVersion> <receivedBy> [<comment>]'
            int current = GetProtocolEndIndex(input, startIndex, out string?protocolName, out string?protocolVersion);

            // If we reached the end of the string after reading protocolName/Version we return (we expect at least
            // <receivedBy> to follow). If reading protocolName/Version read 0 bytes, we return.
            if ((current == startIndex) || (current == input.Length))
            {
                return(0);
            }
            Debug.Assert(protocolVersion != null);

            // Read <receivedBy> in '[<protocolName>/]<protocolVersion> <receivedBy> [<comment>]'
            int receivedByLength = HttpRuleParser.GetHostLength(input, current, true, out string?receivedBy);

            if (receivedByLength == 0)
            {
                return(0);
            }

            current = current + receivedByLength;
            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            string?comment = null;

            if ((current < input.Length) && (input[current] == '('))
            {
                // We have a <comment> in '[<protocolName>/]<protocolVersion> <receivedBy> [<comment>]'
                int commentLength = 0;
                if (HttpRuleParser.GetCommentLength(input, current, out commentLength) != HttpParseResult.Parsed)
                {
                    return(0); // We found a '(' character but it wasn't a valid comment. Abort.
                }

                comment = input.Substring(current, commentLength);

                current = current + commentLength;
                current = current + HttpRuleParser.GetWhitespaceLength(input, current);
            }

            ViaHeaderValue result = new ViaHeaderValue();

            result._protocolVersion = protocolVersion;
            result._protocolName    = protocolName;
            result._receivedBy      = receivedBy !;
            result._comment         = comment;

            parsedValue = result;
            return(current - startIndex);
        }
Exemplo n.º 2
0
        private static void AssertGetCommentLength(string input, int startIndex, int expectedLength,
                                                   HttpParseResult expectedResult)
        {
            int             length = 0;
            HttpParseResult result = HttpRuleParser.GetCommentLength(input, startIndex, out length);

            Assert.Equal(expectedResult, result);
            Assert.Equal(expectedLength, length);
        }
Exemplo n.º 3
0
        internal static void CheckValidComment(string value, string parameterName)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentException(SR.net_http_argument_empty_string, parameterName);
            }
            int length = 0;

            if (HttpRuleParser.GetCommentLength(value, 0, out length) != HttpParseResult.Parsed || length != value.Length)
            {
                throw new FormatException(SR.Format((IFormatProvider)CultureInfo.InvariantCulture, SR.net_http_headers_invalid_value, (object)value));
            }
        }
Exemplo n.º 4
0
        internal static int GetProductInfoLength(string?input, int startIndex, out ProductInfoHeaderValue?parsedValue)
        {
            Debug.Assert(startIndex >= 0);

            parsedValue = null;

            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
            {
                return(0);
            }

            int current = startIndex;

            // Caller must remove leading whitespace.
            string?            comment = null;
            ProductHeaderValue?product = null;

            if (input[current] == '(')
            {
                int commentLength = 0;
                if (HttpRuleParser.GetCommentLength(input, current, out commentLength) != HttpParseResult.Parsed)
                {
                    return(0);
                }

                comment = input.Substring(current, commentLength);

                current = current + commentLength;
                current = current + HttpRuleParser.GetWhitespaceLength(input, current);

                parsedValue = new ProductInfoHeaderValue(comment);
            }
            else
            {
                // Trailing whitespace is removed by GetProductLength().
                int productLength = ProductHeaderValue.GetProductLength(input, current, out product);

                if (productLength == 0)
                {
                    return(0);
                }

                current = current + productLength;

                parsedValue = new ProductInfoHeaderValue(product !);
            }

            return(current - startIndex);
        }
Exemplo n.º 5
0
        internal static void CheckValidComment(string value, string parameterName)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentException(SR.net_http_argument_empty_string, parameterName);
            }

            int length;

            if ((HttpRuleParser.GetCommentLength(value, 0, out length) != HttpParseResult.Parsed) ||
                (length != value.Length)) // no trailing spaces allowed
            {
                throw new FormatException(SR.Format(CultureInfo.InvariantCulture, SR.net_http_headers_invalid_value, value));
            }
        }
Exemplo n.º 6
0
        internal static void CheckValidComment(string value, string parameterName)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentException("The value cannot be null or empty.", parameterName);
            }

            int length = 0;

            if ((HttpRuleParser.GetCommentLength(value, 0, out length) != HttpParseResult.Parsed) ||
                (length != value.Length)) // no trailing spaces allowed
            {
                throw new FormatException(string.Format("The format of value '{0}' is invalid.", value));
            }
        }
        internal static int GetProductInfoLength(string input, int startIndex, out ProductInfoHeaderValue parsedValue)
        {
            Contract.Requires(startIndex >= 0);

            parsedValue = null;

            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
            {
                return(0);
            }

            int current = startIndex;

            // Caller must remove leading whitespaces.
            string             comment = null;
            ProductHeaderValue product = null;

            if (input[current] == '(')
            {
                int commentLength = 0;
                if (HttpRuleParser.GetCommentLength(input, current, out commentLength) != HttpParseResult.Parsed)
                {
                    return(0);
                }

                comment = input.Substring(current, commentLength);

                current = current + commentLength;
                current = current + HttpRuleParser.GetWhitespaceLength(input, current);
            }
            else
            {
                // Trailing whitespaces are removed by GetProductLength().
                int productLength = ProductHeaderValue.GetProductLength(input, current, out product);

                if (productLength == 0)
                {
                    return(0);
                }

                current = current + productLength;
            }

            parsedValue          = new ProductInfoHeaderValue();
            parsedValue._product = product;
            parsedValue._comment = comment;
            return(current - startIndex);
        }