Ejemplo 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);
        }
Ejemplo n.º 2
0
        private static bool TryReadAgent(string input, ref int current, [NotNullWhen(true)] out string?agent)
        {
            agent = null;

            int agentLength = HttpRuleParser.GetHostLength(input, current, true);

            if (agentLength == 0)
            {
                return(false);
            }

            agent   = input.Substring(current, agentLength);
            current = current + agentLength;

            int whitespaceLength = HttpRuleParser.GetWhitespaceLength(input, current);

            current = current + whitespaceLength;

            // At least one whitespace required after <agent>. Also make sure we have characters left for <text>
            if ((whitespaceLength == 0) || (current == input.Length))
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 3
0
        private static int ParseHost(string value, int startIndex, out object?parsedValue)
        {
            int hostLength = HttpRuleParser.GetHostLength(value, startIndex, false, out string?host);

            parsedValue = host;
            return(hostLength);
        }
Ejemplo n.º 4
0
        private static void AssertGetHostLength(string input, int startIndex, int expectedLength, bool allowToken,
                                                string expectedResult)
        {
            string result = null;

            Assert.Equal(expectedLength, HttpRuleParser.GetHostLength(input, startIndex, allowToken, out result));
            Assert.Equal(expectedResult, result);
        }
Ejemplo n.º 5
0
        private static void AssertGetHostLength(string input, int startIndex, int expectedLength, bool allowToken,
                                                string expectedResult)
        {
            int length = HttpRuleParser.GetHostLength(input, startIndex, allowToken);

            Assert.Equal(expectedLength, length);

            if (length != 0)
            {
                string result = input.Substring(startIndex, length);
                Assert.Equal(expectedResult, result);
            }
        }
Ejemplo n.º 6
0
        private static void CheckAgent(string agent)
        {
            if (string.IsNullOrEmpty(agent))
            {
                throw new ArgumentException(SR.net_http_argument_empty_string, nameof(agent));
            }

            // 'receivedBy' can either be a host or a token. Since a token is a valid host, we only verify if the value
            // is a valid host.
            if (HttpRuleParser.GetHostLength(agent, 0, true, out string?host) != agent.Length)
            {
                throw new FormatException(SR.Format(System.Globalization.CultureInfo.InvariantCulture, SR.net_http_headers_invalid_value, agent));
            }
        }
Ejemplo n.º 7
0
        private static void CheckReceivedBy(string receivedBy)
        {
            if (string.IsNullOrEmpty(receivedBy))
            {
                throw new ArgumentException(SR.net_http_argument_empty_string, "receivedBy");
            }

            // 'receivedBy' can either be a host or a token. Since a token is a valid host, we only verify if the value
            // is a valid host.
            string host = null;
            if (HttpRuleParser.GetHostLength(receivedBy, 0, true, out host) != receivedBy.Length)
            {
                throw new FormatException(string.Format(System.Globalization.CultureInfo.InvariantCulture, SR.net_http_headers_invalid_value, receivedBy));
            }
        }
Ejemplo n.º 8
0
        private static void CheckAgent(string agent)
        {
            if (string.IsNullOrEmpty(agent))
            {
                throw new ArgumentException("The value cannot be null or empty.", "agent");
            }

            // 'receivedBy' can either be a host or a token. Since a token is a valid host, we only verify if the value
            // is a valid host.
            string host = null;

            if (HttpRuleParser.GetHostLength(agent, 0, true, out host) != agent.Length)
            {
                throw new FormatException(string.Format("The format of value '{0}' is invalid.", agent));
            }
        }
Ejemplo n.º 9
0
        private static void CheckReceivedBy(string receivedBy)
        {
            if (string.IsNullOrEmpty(receivedBy))
            {
                throw new ArgumentException("The value cannot be null or empty.", nameof(receivedBy));
            }

            // 'receivedBy' can either be a host or a token. Since a token is a valid host, we only verify if the value
            // is a valid host.
            string host = null;

            if (HttpRuleParser.GetHostLength(receivedBy, 0, true, out host) != receivedBy.Length)
            {
                throw new FormatException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "The format of value '{0}' is invalid.", receivedBy));
            }
        }