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 namespace System.Net.Http.Headers
31 public class ViaHeaderValue : ICloneable
33 public ViaHeaderValue (string protocolVersion, string receivedBy)
35 Parser.Token.Check (protocolVersion);
36 Parser.Uri.Check (receivedBy);
38 ProtocolVersion = protocolVersion;
39 ReceivedBy = receivedBy;
42 public ViaHeaderValue (string protocolVersion, string receivedBy, string protocolName)
43 : this (protocolVersion, receivedBy)
45 if (!string.IsNullOrEmpty (protocolName)) {
46 Parser.Token.Check (protocolName);
47 ProtocolName = protocolName;
51 public ViaHeaderValue (string protocolVersion, string receivedBy, string protocolName, string comment)
52 : this (protocolVersion, receivedBy, protocolName)
54 if (!string.IsNullOrEmpty (comment)) {
55 Parser.Token.CheckComment (comment);
60 private ViaHeaderValue ()
64 public string Comment { get; private set; }
65 public string ProtocolName { get; private set; }
66 public string ProtocolVersion { get; private set; }
67 public string ReceivedBy { get; private set; }
69 object ICloneable.Clone ()
71 return MemberwiseClone ();
74 public override bool Equals (object obj)
76 var source = obj as ViaHeaderValue;
80 return string.Equals (source.Comment, Comment, StringComparison.Ordinal) &&
81 string.Equals (source.ProtocolName, ProtocolName, StringComparison.OrdinalIgnoreCase) &&
82 string.Equals (source.ProtocolVersion, ProtocolVersion, StringComparison.OrdinalIgnoreCase) &&
83 string.Equals (source.ReceivedBy, ReceivedBy, StringComparison.OrdinalIgnoreCase);
86 public override int GetHashCode ()
88 int hc = ProtocolVersion.ToLowerInvariant ().GetHashCode ();
89 hc ^= ReceivedBy.ToLowerInvariant ().GetHashCode ();
91 if (!string.IsNullOrEmpty (ProtocolName)) {
92 hc ^= ProtocolName.ToLowerInvariant ().GetHashCode ();
95 if (!string.IsNullOrEmpty (Comment)) {
96 hc ^= Comment.GetHashCode ();
102 public static ViaHeaderValue Parse (string input)
104 ViaHeaderValue value;
105 if (TryParse (input, out value))
108 throw new FormatException (input);
111 public static bool TryParse (string input, out ViaHeaderValue parsedValue)
115 var lexer = new Lexer (input);
117 var t = lexer.Scan ();
118 if (t != Token.Type.Token)
121 var next = lexer.Scan ();
122 ViaHeaderValue value = new ViaHeaderValue ();
124 if (next == Token.Type.SeparatorSlash) {
125 next = lexer.Scan ();
126 if (next != Token.Type.Token)
129 value.ProtocolName = lexer.GetStringValue (t);
130 value.ProtocolVersion = lexer.GetStringValue (next);
132 next = lexer.Scan ();
134 value.ProtocolVersion = lexer.GetStringValue (t);
137 if (next != Token.Type.Token)
140 if (lexer.PeekChar () == ':') {
144 if (t != Token.Type.Token)
150 value.ReceivedBy = lexer.GetStringValue (next, t);
153 if (!lexer.ScanCommentOptional (out comment))
156 value.Comment = comment;
161 public override string ToString ()
163 string s = ProtocolName != null ?
164 ProtocolName + "/" + ProtocolVersion + " " + ReceivedBy :
165 ProtocolVersion + " " + ReceivedBy;
167 return Comment != null ? s + " " + Comment : s;