NameValueHeaderValue.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // NameValueHeaderValue.cs
  3. //
  4. // Authors:
  5. // Marek Safar <[email protected]>
  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. using System.Collections.Generic;
  29. namespace System.Net.Http.Headers
  30. {
  31. public class NameValueHeaderValue : ICloneable
  32. {
  33. internal string value;
  34. public NameValueHeaderValue (string name)
  35. : this (name, null)
  36. {
  37. }
  38. public NameValueHeaderValue (string name, string value)
  39. {
  40. Parser.Token.Check (name);
  41. this.Name = name;
  42. this.Value = value;
  43. }
  44. protected internal NameValueHeaderValue (NameValueHeaderValue source)
  45. {
  46. this.Name = source.Name;
  47. this.value = source.value;
  48. }
  49. internal NameValueHeaderValue ()
  50. {
  51. }
  52. public string Name { get; internal set; }
  53. public string Value {
  54. get {
  55. return value;
  56. }
  57. set {
  58. if (!string.IsNullOrEmpty (value)) {
  59. var lexer = new Lexer (value);
  60. var token = lexer.Scan ();
  61. if (lexer.Scan () != Token.Type.End || !(token == Token.Type.Token || token == Token.Type.QuotedString))
  62. throw new FormatException ();
  63. value = lexer.GetStringValue (token);
  64. }
  65. this.value = value;
  66. }
  67. }
  68. internal static NameValueHeaderValue Create (string name, string value)
  69. {
  70. return new NameValueHeaderValue () {
  71. Name = name,
  72. value = value
  73. };
  74. }
  75. object ICloneable.Clone ()
  76. {
  77. return new NameValueHeaderValue (this);
  78. }
  79. public override int GetHashCode ()
  80. {
  81. int hc = Name.ToLowerInvariant ().GetHashCode ();
  82. if (!string.IsNullOrEmpty (value)) {
  83. hc ^= value.ToLowerInvariant ().GetHashCode ();
  84. }
  85. return hc;
  86. }
  87. public override bool Equals (object obj)
  88. {
  89. var source = obj as NameValueHeaderValue;
  90. if (source == null || !string.Equals (source.Name, Name, StringComparison.OrdinalIgnoreCase))
  91. return false;
  92. if (string.IsNullOrEmpty (value))
  93. return string.IsNullOrEmpty (source.value);
  94. return string.Equals (source.value, value, StringComparison.OrdinalIgnoreCase);
  95. }
  96. public static NameValueHeaderValue Parse (string input)
  97. {
  98. NameValueHeaderValue value;
  99. if (TryParse (input, out value))
  100. return value;
  101. throw new FormatException (input);
  102. }
  103. internal static bool TryParsePragma (string input, int minimalCount, out List<NameValueHeaderValue> result)
  104. {
  105. return CollectionParser.TryParse (input, minimalCount, TryParseElement, out result);
  106. }
  107. internal static bool TryParseParameters (Lexer lexer, out List<NameValueHeaderValue> result, out Token t)
  108. {
  109. var list = new List<NameValueHeaderValue> ();
  110. result = null;
  111. while (true) {
  112. var attr = lexer.Scan ();
  113. if (attr != Token.Type.Token) {
  114. t = Token.Empty;
  115. return false;
  116. }
  117. string value = null;
  118. t = lexer.Scan ();
  119. if (t == Token.Type.SeparatorEqual) {
  120. t = lexer.Scan ();
  121. if (t != Token.Type.Token && t != Token.Type.QuotedString)
  122. return false;
  123. value = lexer.GetStringValue (t);
  124. t = lexer.Scan ();
  125. }
  126. list.Add (new NameValueHeaderValue () {
  127. Name = lexer.GetStringValue (attr),
  128. value = value
  129. });
  130. if (t == Token.Type.SeparatorSemicolon)
  131. continue;
  132. result = list;
  133. return true;
  134. }
  135. }
  136. public override string ToString ()
  137. {
  138. if (string.IsNullOrEmpty (value))
  139. return Name;
  140. return Name + "=" + value;
  141. }
  142. public static bool TryParse (string input, out NameValueHeaderValue parsedValue)
  143. {
  144. var lexer = new Lexer (input);
  145. Token token;
  146. if (TryParseElement (lexer, out parsedValue, out token) && token == Token.Type.End)
  147. return true;
  148. parsedValue = null;
  149. return false;
  150. }
  151. static bool TryParseElement (Lexer lexer, out NameValueHeaderValue parsedValue, out Token t)
  152. {
  153. parsedValue = null;
  154. t = lexer.Scan ();
  155. if (t != Token.Type.Token)
  156. return false;
  157. parsedValue = new NameValueHeaderValue () {
  158. Name = lexer.GetStringValue (t),
  159. };
  160. t = lexer.Scan ();
  161. if (t == Token.Type.SeparatorEqual) {
  162. t = lexer.Scan ();
  163. if (t == Token.Type.Token || t == Token.Type.QuotedString) {
  164. parsedValue.value = lexer.GetStringValue (t);
  165. t = lexer.Scan ();
  166. } else {
  167. return false;
  168. }
  169. }
  170. return true;
  171. }
  172. }
  173. }