5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.Generic;
31 namespace System.Net.Http.Headers
33 public class ViaHeaderValue : ICloneable
35 public ViaHeaderValue (string protocolVersion, string receivedBy)
37 Parser.Token.Check (protocolVersion);
38 Parser.Uri.Check (receivedBy);
40 ProtocolVersion = protocolVersion;
41 ReceivedBy = receivedBy;
44 public ViaHeaderValue (string protocolVersion, string receivedBy, string protocolName)
45 : this (protocolVersion, receivedBy)
47 if (!string.IsNullOrEmpty (protocolName)) {
48 Parser.Token.Check (protocolName);
49 ProtocolName = protocolName;
53 public ViaHeaderValue (string protocolVersion, string receivedBy, string protocolName, string comment)
54 : this (protocolVersion, receivedBy, protocolName)
56 if (!string.IsNullOrEmpty (comment)) {
57 Parser.Token.CheckComment (comment);
62 private ViaHeaderValue ()
66 public string Comment { get; private set; }
67 public string ProtocolName { get; private set; }
68 public string ProtocolVersion { get; private set; }
69 public string ReceivedBy { get; private set; }
71 object ICloneable.Clone ()
73 return MemberwiseClone ();
76 public override bool Equals (object obj)
78 var source = obj as ViaHeaderValue;
82 return string.Equals (source.Comment, Comment, StringComparison.Ordinal) &&
83 string.Equals (source.ProtocolName, ProtocolName, StringComparison.OrdinalIgnoreCase) &&
84 string.Equals (source.ProtocolVersion, ProtocolVersion, StringComparison.OrdinalIgnoreCase) &&
85 string.Equals (source.ReceivedBy, ReceivedBy, StringComparison.OrdinalIgnoreCase);
88 public override int GetHashCode ()
90 int hc = ProtocolVersion.ToLowerInvariant ().GetHashCode ();
91 hc ^= ReceivedBy.ToLowerInvariant ().GetHashCode ();
93 if (!string.IsNullOrEmpty (ProtocolName)) {
94 hc ^= ProtocolName.ToLowerInvariant ().GetHashCode ();
97 if (!string.IsNullOrEmpty (Comment)) {
98 hc ^= Comment.GetHashCode ();
104 public static ViaHeaderValue Parse (string input)
106 ViaHeaderValue value;
107 if (TryParse (input, out value))
110 throw new FormatException (input);
113 public static bool TryParse (string input, out ViaHeaderValue parsedValue)
115 var lexer = new Lexer (input);
117 if (TryParseElement (lexer, out parsedValue, out token) && token == Token.Type.End)
124 internal static bool TryParse (string input, int minimalCount, out List<ViaHeaderValue> result)
126 return CollectionParser.TryParse (input, minimalCount, TryParseElement, out result);
129 static bool TryParseElement (Lexer lexer, out ViaHeaderValue parsedValue, out Token t)
134 if (t != Token.Type.Token)
137 var next = lexer.Scan ();
138 ViaHeaderValue value = new ViaHeaderValue ();
140 if (next == Token.Type.SeparatorSlash) {
141 next = lexer.Scan ();
142 if (next != Token.Type.Token)
145 value.ProtocolName = lexer.GetStringValue (t);
146 value.ProtocolVersion = lexer.GetStringValue (next);
148 next = lexer.Scan ();
150 value.ProtocolVersion = lexer.GetStringValue (t);
153 if (next != Token.Type.Token)
156 if (lexer.PeekChar () == ':') {
160 if (t != Token.Type.Token)
166 value.ReceivedBy = lexer.GetStringValue (next, t);
169 if (lexer.ScanCommentOptional (out comment, out t)) {
173 value.Comment = comment;
178 public override string ToString ()
180 string s = ProtocolName != null ?
181 ProtocolName + "/" + ProtocolVersion + " " + ReceivedBy :
182 ProtocolVersion + " " + ReceivedBy;
184 return Comment != null ? s + " " + Comment : s;