Merge pull request #231 from linquize/a853199c497bb0977970974303fac7e42080809d
[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 namespace System.Net.Http.Headers
30 {
31         public class ViaHeaderValue : ICloneable
32         {
33                 public ViaHeaderValue (string protocolVersion, string receivedBy)
34                 {
35                         Parser.Token.Check (protocolVersion);
36                         Parser.Uri.Check (receivedBy);
37
38                         ProtocolVersion = protocolVersion;
39                         ReceivedBy = receivedBy;
40                 }
41
42                 public ViaHeaderValue (string protocolVersion, string receivedBy, string protocolName)
43                         : this (protocolVersion, receivedBy)
44                 {
45                         if (!string.IsNullOrEmpty (protocolName)) {
46                                 Parser.Token.Check (protocolName);
47                                 ProtocolName = protocolName;
48                         }
49                 }
50
51                 public ViaHeaderValue (string protocolVersion, string receivedBy, string protocolName, string comment)
52                         : this (protocolVersion, receivedBy, protocolName)
53                 {
54                         if (!string.IsNullOrEmpty (comment)) {
55                                 Parser.Token.CheckComment (comment);
56                                 Comment = comment;
57                         }
58                 }
59
60                 private ViaHeaderValue ()
61                 {
62                 }
63
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; }
68
69                 object ICloneable.Clone ()
70                 {
71                         return MemberwiseClone ();
72                 }
73
74                 public override bool Equals (object obj)
75                 {
76                         var source = obj as ViaHeaderValue;
77                         if (source == null)
78                                 return false;
79
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);
84                 }
85
86                 public override int GetHashCode ()
87                 {
88                         int hc = ProtocolVersion.ToLowerInvariant ().GetHashCode ();
89                         hc ^= ReceivedBy.ToLowerInvariant ().GetHashCode ();
90
91                         if (!string.IsNullOrEmpty (ProtocolName)) {
92                                 hc ^= ProtocolName.ToLowerInvariant ().GetHashCode ();
93                         }
94
95                         if (!string.IsNullOrEmpty (Comment)) {
96                                 hc ^= Comment.GetHashCode ();
97                         }
98
99                         return hc;
100                 }
101
102                 public static ViaHeaderValue Parse (string input)
103                 {
104                         ViaHeaderValue value;
105                         if (TryParse (input, out value))
106                                 return value;
107
108                         throw new FormatException (input);
109                 }
110                 
111                 public static bool TryParse (string input, out ViaHeaderValue parsedValue)
112                 {
113                         parsedValue = null;
114
115                         var lexer = new Lexer (input);
116
117                         var t = lexer.Scan ();
118                         if (t != Token.Type.Token)
119                                 return false;
120
121                         var next = lexer.Scan ();
122                         ViaHeaderValue value = new ViaHeaderValue ();
123
124                         if (next == Token.Type.SeparatorSlash) {
125                                 next = lexer.Scan ();
126                                 if (next != Token.Type.Token)
127                                         return false;
128
129                                 value.ProtocolName = lexer.GetStringValue (t);
130                                 value.ProtocolVersion = lexer.GetStringValue (next);
131
132                                 next = lexer.Scan ();
133                         } else {
134                                 value.ProtocolVersion = lexer.GetStringValue (t);
135                         }
136
137                         if (next != Token.Type.Token)
138                                 return false;
139
140                         if (lexer.PeekChar () == ':') {
141                                 lexer.EatChar ();
142
143                                 t = lexer.Scan ();
144                                 if (t != Token.Type.Token)
145                                         return false;
146                         } else {
147                                 t = next;
148                         }
149
150                         value.ReceivedBy = lexer.GetStringValue (next, t);
151
152                         string comment;
153                         if (!lexer.ScanCommentOptional (out comment))
154                                 return false;
155
156                         value.Comment = comment;
157                         parsedValue = value;
158                         return true;
159                 }
160
161                 public override string ToString ()
162                 {
163                         string s = ProtocolName != null ?
164                                 ProtocolName + "/" + ProtocolVersion + " " + ReceivedBy :
165                                 ProtocolVersion + " " + ReceivedBy;
166
167                         return Comment != null ? s + " " + Comment : s;
168                 }
169         }
170 }