Merge pull request #665 from andreas-auerswald/master
[mono.git] / mcs / class / System.Net.Http / System.Net.Http.Headers / NameValueHeaderValue.cs
1 //
2 // NameValueHeaderValue.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 NameValueHeaderValue : ICloneable
34         {
35                 string value;
36
37                 public NameValueHeaderValue (string name)
38                         : this (name, null)
39                 {
40                 }
41
42                 public NameValueHeaderValue (string name, string value)
43                 {
44                         Parser.Token.Check (name);
45
46                         this.Name = name;
47                         this.Value = value;
48                 }
49
50                 protected internal NameValueHeaderValue (NameValueHeaderValue source)
51                 {
52                         this.Name = source.Name;
53                         this.value = source.value;
54                 }
55
56                 private NameValueHeaderValue ()
57                 {
58                 }
59
60                 public string Name { get; private set; }
61
62                 public string Value {
63                         get {
64                                 return value;
65                         }
66                         set {
67                                 if (!string.IsNullOrEmpty (value)) {
68                                         var lexer = new Lexer (value);
69                                         var token = lexer.Scan ();
70                                         if (lexer.Scan () != Token.Type.End || !(token == Token.Type.Token || token == Token.Type.QuotedString))
71                                                 throw new FormatException ();
72
73                                         value = lexer.GetStringValue (token);
74                                 }
75
76                                 this.value = value;
77                         }
78                 }
79
80                 internal static NameValueHeaderValue Create (string name, string value)
81                 {
82                         return new NameValueHeaderValue () {
83                                 Name = name,
84                                 value = value
85                         };
86                 }
87
88                 object ICloneable.Clone ()
89                 {
90                         return new NameValueHeaderValue (this);
91                 }
92
93                 public override int GetHashCode ()
94                 {
95                         int hc = Name.ToLowerInvariant ().GetHashCode ();
96                         if (!string.IsNullOrEmpty (value)) {
97                                 hc ^= value.ToLowerInvariant ().GetHashCode ();
98                         }
99
100                         return hc;
101                 }
102
103                 public override bool Equals (object obj)
104                 {
105                         var source = obj as NameValueHeaderValue;
106                         if (source == null || !string.Equals (source.Name, Name, StringComparison.OrdinalIgnoreCase))
107                                 return false;
108
109                         if (string.IsNullOrEmpty (value))
110                                 return string.IsNullOrEmpty (source.value);
111
112                         return string.Equals (source.value, value, StringComparison.OrdinalIgnoreCase);
113                 }
114
115                 public static NameValueHeaderValue Parse (string input)
116                 {
117                         NameValueHeaderValue value;
118                         if (TryParse (input, out value))
119                                 return value;
120
121                         throw new FormatException (input);
122                 }
123
124                 internal static bool TryParseParameters (Lexer lexer, out List<NameValueHeaderValue> result)
125                 {
126                         return TryParseCollection (lexer, out result, Token.Type.SeparatorSemicolon);
127                 }
128
129                 internal static bool TryParsePragma (string input, out List<NameValueHeaderValue> result)
130                 {                       
131                         return TryParseCollection (new Lexer (input), out result, Token.Type.SeparatorComma);
132                 }
133
134                 static bool TryParseCollection (Lexer lexer, out List<NameValueHeaderValue> result, Token.Type separator)
135                 {               
136                         var list = new List<NameValueHeaderValue> ();
137                         result = null;
138
139                         Token t;
140
141                         do {
142                                 var attr = lexer.Scan ();
143                                 if (attr != Token.Type.Token)
144                                         return false;
145
146                                 string value = null;
147
148                                 t = lexer.Scan ();
149                                 if (t == Token.Type.SeparatorEqual) {
150                                         t = lexer.Scan ();
151                                         if (t != Token.Type.Token && t != Token.Type.QuotedString)
152                                                 return false;
153
154                                         value = lexer.GetStringValue (t);
155
156                                         t = lexer.Scan ();
157                                 }
158
159                                 if (t == separator|| t == Token.Type.End) {
160                                         list.Add (new NameValueHeaderValue () {
161                                                 Name = lexer.GetStringValue (attr),
162                                                 value = value
163                                         });
164                                 } else {
165                                         return false;
166                                 }
167
168                         } while (t == separator);
169
170                         result = list;
171                         return true;
172                 }
173
174                 public override string ToString ()
175                 {
176                         if (string.IsNullOrEmpty (value))
177                                 return Name;
178
179                         return Name + "=" + value;
180                 }
181
182                 public static bool TryParse (string input, out NameValueHeaderValue parsedValue)
183                 {
184                         parsedValue = null;
185
186                         var lexer = new Lexer (input);
187                         var t = lexer.Scan ();
188                         if (t != Token.Type.Token && t != Token.Type.QuotedString)
189                                 return false;
190
191                         string v = null;
192                         var token2 = lexer.Scan ();
193                         if (token2 != Token.Type.End) {
194                                 if (token2 != Token.Type.SeparatorEqual)
195                                         return false;
196
197                                 token2 = lexer.Scan ();
198
199                                 if (token2 == Token.Type.Token || token2 == Token.Type.QuotedString) {
200                                         v = lexer.GetStringValue (token2);
201                                         token2 = lexer.Scan ();
202                                 }
203
204                                 if (token2 != Token.Type.End)
205                                         return false;
206                         }
207
208                         parsedValue = new NameValueHeaderValue () {
209                                 Name = lexer.GetStringValue (t),
210                                 value = v
211                         };
212
213                         return true;
214                 }
215         }
216 }