Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / System.Net.Http / System.Net.Http.Headers / ViaHeaderValue.cs
1 //
2 // ViaHeaderValue.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27 //
28
29 using System.Collections.Generic;
30
31 namespace System.Net.Http.Headers
32 {
33         public class ViaHeaderValue : ICloneable
34         {
35                 public ViaHeaderValue (string protocolVersion, string receivedBy)
36                 {
37                         Parser.Token.Check (protocolVersion);
38                         Parser.Uri.Check (receivedBy);
39
40                         ProtocolVersion = protocolVersion;
41                         ReceivedBy = receivedBy;
42                 }
43
44                 public ViaHeaderValue (string protocolVersion, string receivedBy, string protocolName)
45                         : this (protocolVersion, receivedBy)
46                 {
47                         if (!string.IsNullOrEmpty (protocolName)) {
48                                 Parser.Token.Check (protocolName);
49                                 ProtocolName = protocolName;
50                         }
51                 }
52
53                 public ViaHeaderValue (string protocolVersion, string receivedBy, string protocolName, string comment)
54                         : this (protocolVersion, receivedBy, protocolName)
55                 {
56                         if (!string.IsNullOrEmpty (comment)) {
57                                 Parser.Token.CheckComment (comment);
58                                 Comment = comment;
59                         }
60                 }
61
62                 private ViaHeaderValue ()
63                 {
64                 }
65
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; }
70
71                 object ICloneable.Clone ()
72                 {
73                         return MemberwiseClone ();
74                 }
75
76                 public override bool Equals (object obj)
77                 {
78                         var source = obj as ViaHeaderValue;
79                         if (source == null)
80                                 return false;
81
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);
86                 }
87
88                 public override int GetHashCode ()
89                 {
90                         int hc = ProtocolVersion.ToLowerInvariant ().GetHashCode ();
91                         hc ^= ReceivedBy.ToLowerInvariant ().GetHashCode ();
92
93                         if (!string.IsNullOrEmpty (ProtocolName)) {
94                                 hc ^= ProtocolName.ToLowerInvariant ().GetHashCode ();
95                         }
96
97                         if (!string.IsNullOrEmpty (Comment)) {
98                                 hc ^= Comment.GetHashCode ();
99                         }
100
101                         return hc;
102                 }
103
104                 public static ViaHeaderValue Parse (string input)
105                 {
106                         ViaHeaderValue value;
107                         if (TryParse (input, out value))
108                                 return value;
109
110                         throw new FormatException (input);
111                 }
112                 
113                 public static bool TryParse (string input, out ViaHeaderValue parsedValue)
114                 {
115                         var lexer = new Lexer (input);
116                         Token token;
117                         if (TryParseElement (lexer, out parsedValue, out token) && token == Token.Type.End)
118                                 return true;
119
120                         parsedValue = null;
121                         return false;
122                 }
123
124                 internal static bool TryParse (string input, int minimalCount, out List<ViaHeaderValue> result)
125                 {
126                         return CollectionParser.TryParse (input, minimalCount, TryParseElement, out result);
127                 }
128
129                 static bool TryParseElement (Lexer lexer, out ViaHeaderValue parsedValue, out Token t)  
130                 {
131                         parsedValue = null;
132
133                         t = lexer.Scan ();
134                         if (t != Token.Type.Token)
135                                 return false;
136
137                         var next = lexer.Scan ();
138                         ViaHeaderValue value = new ViaHeaderValue ();
139
140                         if (next == Token.Type.SeparatorSlash) {
141                                 next = lexer.Scan ();
142                                 if (next != Token.Type.Token)
143                                         return false;
144
145                                 value.ProtocolName = lexer.GetStringValue (t);
146                                 value.ProtocolVersion = lexer.GetStringValue (next);
147
148                                 next = lexer.Scan ();
149                         } else {
150                                 value.ProtocolVersion = lexer.GetStringValue (t);
151                         }
152
153                         if (next != Token.Type.Token)
154                                 return false;
155
156                         if (lexer.PeekChar () == ':') {
157                                 lexer.EatChar ();
158
159                                 t = lexer.Scan ();
160                                 if (t != Token.Type.Token)
161                                         return false;
162                         } else {
163                                 t = next;
164                         }
165
166                         value.ReceivedBy = lexer.GetStringValue (next, t);
167
168                         string comment;
169                         if (lexer.ScanCommentOptional (out comment, out t)) {
170                                 t = lexer.Scan ();
171                         }
172
173                         value.Comment = comment;
174                         parsedValue = value;
175                         return true;
176                 }
177
178                 public override string ToString ()
179                 {
180                         string s = ProtocolName != null ?
181                                 ProtocolName + "/" + ProtocolVersion + " " + ReceivedBy :
182                                 ProtocolVersion + " " + ReceivedBy;
183
184                         return Comment != null ? s + " " + Comment : s;
185                 }
186         }
187 }