NameValueHeaderValue.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. 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. private NameValueHeaderValue ()
  50. {
  51. }
  52. public string Name { get; private 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 TryParseParameters (Lexer lexer, out List<NameValueHeaderValue> result)
  104. {
  105. return TryParseCollection (lexer, out result, Token.Type.SeparatorSemicolon);
  106. }
  107. internal static bool TryParsePragma (string input, out List<NameValueHeaderValue> result)
  108. {
  109. return TryParseCollection (new Lexer (input), out result, Token.Type.SeparatorComma);
  110. }
  111. static bool TryParseCollection (Lexer lexer, out List<NameValueHeaderValue> result, Token.Type separator)
  112. {
  113. var list = new List<NameValueHeaderValue> ();
  114. result = null;
  115. Token t;
  116. do {
  117. var attr = lexer.Scan ();
  118. if (attr != Token.Type.Token)
  119. return false;
  120. string value = null;
  121. t = lexer.Scan ();
  122. if (t == Token.Type.SeparatorEqual) {
  123. t = lexer.Scan ();
  124. if (t != Token.Type.Token && t != Token.Type.QuotedString)
  125. return false;
  126. value = lexer.GetStringValue (t);
  127. t = lexer.Scan ();
  128. }
  129. if (t == separator|| t == Token.Type.End) {
  130. list.Add (new NameValueHeaderValue () {
  131. Name = lexer.GetStringValue (attr),
  132. value = value
  133. });
  134. } else {
  135. return false;
  136. }
  137. } while (t == separator);
  138. result = list;
  139. return true;
  140. }
  141. public override string ToString ()
  142. {
  143. if (string.IsNullOrEmpty (value))
  144. return Name;
  145. return Name + "=" + value;
  146. }
  147. public static bool TryParse (string input, out NameValueHeaderValue parsedValue)
  148. {
  149. parsedValue = null;
  150. var lexer = new Lexer (input);
  151. var t = lexer.Scan ();
  152. if (t != Token.Type.Token && t != Token.Type.QuotedString)
  153. return false;
  154. string v = null;
  155. var token2 = lexer.Scan ();
  156. if (token2 != Token.Type.End) {
  157. if (token2 != Token.Type.SeparatorEqual)
  158. return false;
  159. token2 = lexer.Scan ();
  160. if (token2 == Token.Type.Token || token2 == Token.Type.QuotedString) {
  161. v = lexer.GetStringValue (token2);
  162. token2 = lexer.Scan ();
  163. }
  164. if (token2 != Token.Type.End)
  165. return false;
  166. }
  167. parsedValue = new NameValueHeaderValue () {
  168. Name = lexer.GetStringValue (t),
  169. value = v
  170. };
  171. return true;
  172. }
  173. }
  174. }