Пример #1
0
        public void GetHashCode_UseSameAndDifferentRanges_SameOrDifferentHashCodes()
        {
            ViaHeaderValue via1 = new ViaHeaderValue("x11", "host");
            ViaHeaderValue via2 = new ViaHeaderValue("x11", "HOST");
            ViaHeaderValue via3 = new ViaHeaderValue("X11", "host");
            ViaHeaderValue via4 = new ViaHeaderValue("x11", "host", "HTTP");
            ViaHeaderValue via5 = new ViaHeaderValue("x11", "host", "http");
            ViaHeaderValue via6 = new ViaHeaderValue("x11", "host", null, "(comment)");
            ViaHeaderValue via7 = new ViaHeaderValue("x11", "host", "HTTP", "(comment)");
            ViaHeaderValue via8 = new ViaHeaderValue("x11", "host", "HTTP", "(COMMENT)");
            ViaHeaderValue via9 = new ViaHeaderValue("x12", "host");
            ViaHeaderValue via10 = new ViaHeaderValue("x11", "host2");
            ViaHeaderValue via11 = new ViaHeaderValue("x11", "host", "WS");
            ViaHeaderValue via12 = new ViaHeaderValue("x11", "host", string.Empty, string.Empty);

            Assert.Equal(via1.GetHashCode(), via2.GetHashCode());
            Assert.Equal(via1.GetHashCode(), via3.GetHashCode());
            Assert.NotEqual(via1.GetHashCode(), via4.GetHashCode());
            Assert.NotEqual(via1.GetHashCode(), via6.GetHashCode());
            Assert.NotEqual(via1.GetHashCode(), via7.GetHashCode());
            Assert.NotEqual(via1.GetHashCode(), via9.GetHashCode());
            Assert.NotEqual(via1.GetHashCode(), via10.GetHashCode());
            Assert.NotEqual(via4.GetHashCode(), via11.GetHashCode());
            Assert.Equal(via4.GetHashCode(), via5.GetHashCode());
            Assert.NotEqual(via4.GetHashCode(), via6.GetHashCode());
            Assert.NotEqual(via6.GetHashCode(), via7.GetHashCode());
            Assert.NotEqual(via7.GetHashCode(), via8.GetHashCode());
            Assert.Equal(via1.GetHashCode(), via12.GetHashCode());
        }
Пример #2
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);
        }
Пример #3
0
        private ViaHeaderValue(ViaHeaderValue source)
        {
            Contract.Requires(source != null);

            this.protocolName = source.protocolName;
            this.protocolVersion = source.protocolVersion;
            this.receivedBy = source.receivedBy;
            this.comment = source.comment;
        }
Пример #4
0
        private ViaHeaderValue(ViaHeaderValue source)
        {
            Contract.Requires(source != null);

            _protocolName    = source._protocolName;
            _protocolVersion = source._protocolVersion;
            _receivedBy      = source._receivedBy;
            _comment         = source._comment;
        }
Пример #5
0
        private ViaHeaderValue(ViaHeaderValue source)
        {
            Debug.Assert(source != null);

            _protocolName = source._protocolName;
            _protocolVersion = source._protocolVersion;
            _receivedBy = source._receivedBy;
            _comment = source._comment;
        }
Пример #6
0
        private ViaHeaderValue(ViaHeaderValue source)
        {
            Contract.Requires(source != null);

            _protocolName = source._protocolName;
            _protocolVersion = source._protocolVersion;
            _receivedBy = source._receivedBy;
            _comment = source._comment;
        }
        private ViaHeaderValue(ViaHeaderValue source)
        {
            Contract.Requires(source != null);

            this.protocolName    = source.protocolName;
            this.protocolVersion = source.protocolVersion;
            this.receivedBy      = source.receivedBy;
            this.comment         = source.comment;
        }
Пример #8
0
        private ViaHeaderValue(ViaHeaderValue source)
        {
            Debug.Assert(source != null);

            _protocolName    = source._protocolName;
            _protocolVersion = source._protocolVersion;
            _receivedBy      = source._receivedBy;
            _comment         = source._comment;
        }
Пример #9
0
 private void CheckValidParsedValue(string input, int startIndex, ViaHeaderValue expectedResult,
     int expectedIndex)
 {
     HttpHeaderParser parser = GenericHeaderParser.MultipleValueViaParser;
     object result = null;
     Assert.True(parser.TryParseValue(input, null, ref startIndex, out result),
         string.Format("TryParse returned false: {0}", input));
     Assert.Equal(expectedIndex, startIndex);
     Assert.Equal(result, expectedResult);
 }
Пример #10
0
        public void Ctor_AllParams_AllFieldsInitializedCorrectly()
        {
            ViaHeaderValue via = new ViaHeaderValue("1.1", "host", "HTTP", "(comment)");
            Assert.Equal("1.1", via.ProtocolVersion);
            Assert.Equal("host", via.ReceivedBy);
            Assert.Equal("HTTP", via.ProtocolName);
            Assert.Equal("(comment)", via.Comment);

            Assert.Throws<FormatException>(() => { new ViaHeaderValue("v", "h", "p", "(x"); });
            Assert.Throws<FormatException>(() => { new ViaHeaderValue("v", "h", "p", "x)"); });
        }
Пример #11
0
        public void Ctor_ProtocolVersionReceivedByAndProtocolNameOnlyOverload_CallForwardedToOtherCtor()
        {
            ViaHeaderValue via = new ViaHeaderValue("1.1", "host", "HTTP");
            Assert.Equal("1.1", via.ProtocolVersion);
            Assert.Equal("host", via.ReceivedBy);
            Assert.Equal("HTTP", via.ProtocolName);
            Assert.Null(via.Comment);

            Assert.Throws<FormatException>(() => { new ViaHeaderValue("v", "h", "x y"); });
            Assert.Throws<FormatException>(() => { new ViaHeaderValue("v", "h", "x "); });
            Assert.Throws<FormatException>(() => { new ViaHeaderValue("v", "h", " x"); });
        }
Пример #12
0
		public void Equals ()
		{
			var value = new ViaHeaderValue ("ab", "x");
			Assert.AreEqual (value, new ViaHeaderValue ("ab", "x"), "#1");
			Assert.AreEqual (value, new ViaHeaderValue ("AB", "X"), "#2");
			Assert.AreNotEqual (value, new ViaHeaderValue ("AA", "x"), "#3");

			value = new ViaHeaderValue ("ab", "DD", "cc");
			Assert.AreEqual (value, new ViaHeaderValue ("Ab", "DD", "cC"), "#4");
			Assert.AreNotEqual (value, new ViaHeaderValue ("AB", "DD"), "#5");
			Assert.AreNotEqual (value, new ViaHeaderValue ("Ab", "dd", "cc", "(c)"), "#6");
		}
Пример #13
0
        public static bool TryParse(string input, out ViaHeaderValue parsedValue)
        {
            int index = 0;
            object output;
            parsedValue = null;

            if (GenericHeaderParser.SingleValueViaParser.TryParseValue(input, null, ref index, out output))
            {
                parsedValue = (ViaHeaderValue)output;
                return true;
            }
            return false;
        }
Пример #14
0
        public static bool TryParse(string input, out ViaHeaderValue parsedValue)
        {
            var   lexer = new Lexer(input);
            Token token;

            if (TryParseElement(lexer, out parsedValue, out token) && token == Token.Type.End)
            {
                return(true);
            }

            parsedValue = null;
            return(false);
        }
Пример #15
0
        public void ToString_UseDifferentRanges_AllSerializedCorrectly()
        {
            ViaHeaderValue via = new ViaHeaderValue("1.1", "host:80");
            Assert.Equal("1.1 host:80", via.ToString());

            via = new ViaHeaderValue("1.1", "[::1]", "HTTP");
            Assert.Equal("HTTP/1.1 [::1]", via.ToString());

            via = new ViaHeaderValue("1.0", "www.example.com", "WS", "(comment)");
            Assert.Equal("WS/1.0 www.example.com (comment)", via.ToString());

            via = new ViaHeaderValue("1.0", "www.example.com:80", null, "(comment)");
            Assert.Equal("1.0 www.example.com:80 (comment)", via.ToString());
        }
Пример #16
0
        public override bool Equals(object obj)
        {
            ViaHeaderValue other = obj as ViaHeaderValue;

            if (other == null)
            {
                return(false);
            }

            // Note that for token and host case-insensitive comparison is used. Comments are compared using case-
            // sensitive comparison.
            return(string.Equals(_protocolVersion, other._protocolVersion, StringComparison.OrdinalIgnoreCase) &&
                   string.Equals(_receivedBy, other._receivedBy, StringComparison.OrdinalIgnoreCase) &&
                   string.Equals(_protocolName, other._protocolName, StringComparison.OrdinalIgnoreCase) &&
                   string.Equals(_comment, other._comment, StringComparison.Ordinal));
        }
Пример #17
0
        public void Ctor_ProtocolVersionAndReceivedByOnlyOverload_CallForwardedToOtherCtor()
        {
            ViaHeaderValue via = new ViaHeaderValue("1.1", ".token");
            Assert.Equal("1.1", via.ProtocolVersion);
            Assert.Equal(".token", via.ReceivedBy);
            Assert.Null(via.ProtocolName);
            Assert.Null(via.Comment);

            via = new ViaHeaderValue("x11", "[::1]:1818");
            Assert.Equal("x11", via.ProtocolVersion);
            Assert.Equal("[::1]:1818", via.ReceivedBy);

            Assert.Throws<ArgumentException>(() => { new ViaHeaderValue(null, "host"); });
            Assert.Throws<ArgumentException>(() => { new ViaHeaderValue("", "host"); });
            Assert.Throws<FormatException>(() => { new ViaHeaderValue("x y", "h"); });
            Assert.Throws<FormatException>(() => { new ViaHeaderValue("x ", "h"); });
            Assert.Throws<FormatException>(() => { new ViaHeaderValue(" x", "h"); });
            Assert.Throws<ArgumentException>(() => { new ViaHeaderValue("1.1", null); });
            Assert.Throws<ArgumentException>(() => { new ViaHeaderValue("1.1", ""); });
            Assert.Throws<FormatException>(() => { new ViaHeaderValue("v", "x y"); });
            Assert.Throws<FormatException>(() => { new ViaHeaderValue("v", "x "); });
            Assert.Throws<FormatException>(() => { new ViaHeaderValue("v", " x"); });
        }
Пример #18
0
        public static bool TryParse(string input, out ViaHeaderValue parsedValue)
        {
            int index = 0;
            object output;
            parsedValue = null;

            if (GenericHeaderParser.SingleValueViaParser.TryParseValue(input, null, ref index, out output))
            {
                parsedValue = (ViaHeaderValue)output;
                return true;
            }
            return false;
        }
Пример #19
0
 public static bool TryParse(string input, out ViaHeaderValue parsedValue)
 {
 }
Пример #20
0
		public void Properties ()
		{
			var value = new ViaHeaderValue ("s", "p");
			Assert.IsNull (value.ProtocolName, "#1");
			Assert.AreEqual ("s", value.ProtocolVersion, "#2");
			Assert.AreEqual ("p", value.ReceivedBy, "#3");

			value = new ViaHeaderValue ("s", "rb", "name");
			Assert.AreEqual ("name", value.ProtocolName, "#4");
			Assert.AreEqual ("s", value.ProtocolVersion, "#5");
			Assert.AreEqual ("rb", value.ReceivedBy, "#6");

			value = new ViaHeaderValue ("s", "rb", "name", "(cmt)");
			Assert.AreEqual ("name", value.ProtocolName, "#7");
			Assert.AreEqual ("s", value.ProtocolVersion, "#8");
			Assert.AreEqual ("rb", value.ReceivedBy, "#9");
			Assert.AreEqual ("(cmt)", value.Comment, "#10");
		}
Пример #21
0
		public static bool TryParse (string input, out ViaHeaderValue parsedValue)
		{
			var lexer = new Lexer (input);
			Token token;
			if (TryParseElement (lexer, out parsedValue, out token) && token == Token.Type.End)
				return true;

			parsedValue = null;
			return false;
		}
Пример #22
0
 private static void CheckGetViaLength(string input, int startIndex, int expectedLength,
     ViaHeaderValue expectedResult)
 {
     object result = null;
     Assert.Equal(expectedLength, ViaHeaderValue.GetViaLength(input, startIndex, out result));
     Assert.Equal(expectedResult, result);
 }
Пример #23
0
 private void CheckValidTryParse(string input, ViaHeaderValue expectedResult)
 {
     ViaHeaderValue result = null;
     Assert.True(ViaHeaderValue.TryParse(input, out result));
     Assert.Equal(expectedResult, result);
 }
Пример #24
0
 private void CheckValidParse(string input, ViaHeaderValue expectedResult)
 {
     ViaHeaderValue result = ViaHeaderValue.Parse(input);
     Assert.Equal(expectedResult, result);
 }
Пример #25
0
        public void Clone_Call_CloneFieldsMatchSourceFields()
        {
            ViaHeaderValue source = new ViaHeaderValue("1.1", "host");
            ViaHeaderValue clone = (ViaHeaderValue)((ICloneable)source).Clone();
            Assert.Equal(source.ProtocolVersion, clone.ProtocolVersion);
            Assert.Equal(source.ReceivedBy, clone.ReceivedBy);
            Assert.Equal(source.ProtocolName, clone.ProtocolName);
            Assert.Equal(source.Comment, clone.Comment);

            source = new ViaHeaderValue("1.1", "host", "HTTP");
            clone = (ViaHeaderValue)((ICloneable)source).Clone();
            Assert.Equal(source.ProtocolVersion, clone.ProtocolVersion);
            Assert.Equal(source.ReceivedBy, clone.ReceivedBy);
            Assert.Equal(source.ProtocolName, clone.ProtocolName);
            Assert.Equal(source.Comment, clone.Comment);

            source = new ViaHeaderValue("1.1", "host", "HTTP", "(comment)");
            clone = (ViaHeaderValue)((ICloneable)source).Clone();
            Assert.Equal(source.ProtocolVersion, clone.ProtocolVersion);
            Assert.Equal(source.ReceivedBy, clone.ReceivedBy);
            Assert.Equal(source.ProtocolName, clone.ProtocolName);
            Assert.Equal(source.Comment, clone.Comment);
        }
Пример #26
0
        public void Equals_UseSameAndDifferentRanges_EqualOrNotEqualNoExceptions()
        {
            ViaHeaderValue via1 = new ViaHeaderValue("x11", "host");
            ViaHeaderValue via2 = new ViaHeaderValue("x11", "HOST");
            ViaHeaderValue via3 = new ViaHeaderValue("X11", "host");
            ViaHeaderValue via4 = new ViaHeaderValue("x11", "host", "HTTP");
            ViaHeaderValue via5 = new ViaHeaderValue("x11", "host", "http");
            ViaHeaderValue via6 = new ViaHeaderValue("x11", "host", null, "(comment)");
            ViaHeaderValue via7 = new ViaHeaderValue("x11", "host", "HTTP", "(comment)");
            ViaHeaderValue via8 = new ViaHeaderValue("x11", "host", "HTTP", "(COMMENT)");
            ViaHeaderValue via9 = new ViaHeaderValue("x12", "host");
            ViaHeaderValue via10 = new ViaHeaderValue("x11", "host2");
            ViaHeaderValue via11 = new ViaHeaderValue("x11", "host", "WS");
            ViaHeaderValue via12 = new ViaHeaderValue("x11", "host", string.Empty, string.Empty);

            Assert.False(via1.Equals(null), "x11 host vs. <null>");
            Assert.True(via1.Equals(via2), "x11 host vs. x11 HOST");
            Assert.True(via1.Equals(via3), "x11 host vs. X11 host");
            Assert.False(via1.Equals(via4), "x11 host vs. HTTP/x11 host");
            Assert.False(via4.Equals(via1), "HTTP/x11 host vs. x11 host");
            Assert.False(via1.Equals(via6), "x11 host vs. HTTP/x11 (comment)");
            Assert.False(via6.Equals(via1), "HTTP/x11 (comment) vs. x11 host");
            Assert.False(via1.Equals(via7), "x11 host vs. HTTP/x11 host (comment)");
            Assert.False(via7.Equals(via1), "HTTP/x11 host (comment) vs. x11 host");
            Assert.False(via1.Equals(via9), "x11 host vs. x12 host");
            Assert.False(via1.Equals(via10), "x11 host vs. x11 host2");
            Assert.False(via4.Equals(via11), "HTTP/x11 host vs. WS/x11 host");
            Assert.True(via4.Equals(via5), "HTTP/x11 host vs. http/x11 host");
            Assert.False(via4.Equals(via6), "HTTP/x11 host vs. x11 host (comment)");
            Assert.False(via6.Equals(via4), "x11 host (comment) vs. HTTP/x11 host");
            Assert.False(via6.Equals(via7), "x11 host (comment) vs. HTTP/x11 host (comment)");
            Assert.False(via7.Equals(via6), "HTTP/x11 host (comment) vs. x11 host (comment)");
            Assert.False(via7.Equals(via8), "HTTP/x11 host (comment) vs. HTTP/x11 host (COMMENT)");
            Assert.True(via1.Equals(via12), "x11 host vs. x11 host <empty> <empty>");
        }
Пример #27
0
		static bool TryParseElement (Lexer lexer, out ViaHeaderValue parsedValue, out Token t)	
		{
			parsedValue = null;

			t = lexer.Scan ();
			if (t != Token.Type.Token)
				return false;

			var next = lexer.Scan ();
			ViaHeaderValue value = new ViaHeaderValue ();

			if (next == Token.Type.SeparatorSlash) {
				next = lexer.Scan ();
				if (next != Token.Type.Token)
					return false;

				value.ProtocolName = lexer.GetStringValue (t);
				value.ProtocolVersion = lexer.GetStringValue (next);

				next = lexer.Scan ();
			} else {
				value.ProtocolVersion = lexer.GetStringValue (t);
			}

			if (next != Token.Type.Token)
				return false;

			if (lexer.PeekChar () == ':') {
				lexer.EatChar ();

				t = lexer.Scan ();
				if (t != Token.Type.Token)
					return false;
			} else {
				t = next;
			}

			value.ReceivedBy = lexer.GetStringValue (next, t);

			string comment;
			if (lexer.ScanCommentOptional (out comment, out t)) {
				t = lexer.Scan ();
			}

			value.Comment = comment;
			parsedValue = value;
			return true;
		}
Пример #28
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>]'
            string protocolName = null;
            string protocolVersion = null;
            int current = GetProtocolEndIndex(input, startIndex, out protocolName, out 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>]'
            string receivedBy = null;
            int receivedByLength = HttpRuleParser.GetHostLength(input, current, true, out 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;
        }
Пример #29
0
        static bool TryParseElement(Lexer lexer, out ViaHeaderValue parsedValue, out Token t)
        {
            parsedValue = null;

            t = lexer.Scan();
            if (t != Token.Type.Token)
            {
                return(false);
            }

            var            next  = lexer.Scan();
            ViaHeaderValue value = new ViaHeaderValue();

            if (next == Token.Type.SeparatorSlash)
            {
                next = lexer.Scan();
                if (next != Token.Type.Token)
                {
                    return(false);
                }

                value.ProtocolName    = lexer.GetStringValue(t);
                value.ProtocolVersion = lexer.GetStringValue(next);

                next = lexer.Scan();
            }
            else
            {
                value.ProtocolVersion = lexer.GetStringValue(t);
            }

            if (next != Token.Type.Token)
            {
                return(false);
            }

            if (lexer.PeekChar() == ':')
            {
                lexer.EatChar();

                t = lexer.Scan();
                if (t != Token.Type.Token)
                {
                    return(false);
                }
            }
            else
            {
                t = next;
            }

            value.ReceivedBy = lexer.GetStringValue(next, t);

            string comment;

            if (lexer.ScanCommentOptional(out comment, out t))
            {
                t = lexer.Scan();
            }

            value.Comment = comment;
            parsedValue   = value;
            return(true);
        }