2 // NameValueHeaderValue.cs
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 NameValueHeaderValue : ICloneable
35 internal string value;
37 public NameValueHeaderValue (string name)
42 public NameValueHeaderValue (string name, string value)
44 Parser.Token.Check (name);
50 protected internal NameValueHeaderValue (NameValueHeaderValue source)
52 this.Name = source.Name;
53 this.value = source.value;
56 internal NameValueHeaderValue ()
60 public string Name { get; internal 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 ();
73 value = lexer.GetStringValue (token);
80 internal static NameValueHeaderValue Create (string name, string value)
82 return new NameValueHeaderValue () {
88 object ICloneable.Clone ()
90 return new NameValueHeaderValue (this);
93 public override int GetHashCode ()
95 int hc = Name.ToLowerInvariant ().GetHashCode ();
96 if (!string.IsNullOrEmpty (value)) {
97 hc ^= value.ToLowerInvariant ().GetHashCode ();
103 public override bool Equals (object obj)
105 var source = obj as NameValueHeaderValue;
106 if (source == null || !string.Equals (source.Name, Name, StringComparison.OrdinalIgnoreCase))
109 if (string.IsNullOrEmpty (value))
110 return string.IsNullOrEmpty (source.value);
112 return string.Equals (source.value, value, StringComparison.OrdinalIgnoreCase);
115 public static NameValueHeaderValue Parse (string input)
117 NameValueHeaderValue value;
118 if (TryParse (input, out value))
121 throw new FormatException (input);
124 internal static bool TryParsePragma (string input, int minimalCount, out List<NameValueHeaderValue> result)
126 return CollectionParser.TryParse (input, minimalCount, TryParseElement, out result);
129 internal static bool TryParseParameters (Lexer lexer, out List<NameValueHeaderValue> result, out Token t)
131 var list = new List<NameValueHeaderValue> ();
135 var attr = lexer.Scan ();
136 if (attr != Token.Type.Token) {
144 if (t == Token.Type.SeparatorEqual) {
146 if (t != Token.Type.Token && t != Token.Type.QuotedString)
149 value = lexer.GetStringValue (t);
154 list.Add (new NameValueHeaderValue () {
155 Name = lexer.GetStringValue (attr),
159 if (t == Token.Type.SeparatorSemicolon)
167 public override string ToString ()
169 if (string.IsNullOrEmpty (value))
172 return Name + "=" + value;
175 public static bool TryParse (string input, out NameValueHeaderValue parsedValue)
177 var lexer = new Lexer (input);
179 if (TryParseElement (lexer, out parsedValue, out token) && token == Token.Type.End)
186 static bool TryParseElement (Lexer lexer, out NameValueHeaderValue parsedValue, out Token t)
191 if (t != Token.Type.Token)
194 parsedValue = new NameValueHeaderValue () {
195 Name = lexer.GetStringValue (t),
199 if (t == Token.Type.SeparatorEqual) {
202 if (t == Token.Type.Token || t == Token.Type.QuotedString) {
203 parsedValue.value = lexer.GetStringValue (t);