NameValueHeaderValue.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. object ICloneable.Clone ()
  69. {
  70. return new NameValueHeaderValue (this);
  71. }
  72. public override int GetHashCode ()
  73. {
  74. int hc = Name.ToLowerInvariant ().GetHashCode ();
  75. if (!string.IsNullOrEmpty (value)) {
  76. hc ^= value.ToLowerInvariant ().GetHashCode ();
  77. }
  78. return hc;
  79. }
  80. public override bool Equals (object obj)
  81. {
  82. var source = obj as NameValueHeaderValue;
  83. if (source == null || !string.Equals (source.Name, Name, StringComparison.OrdinalIgnoreCase))
  84. return false;
  85. if (string.IsNullOrEmpty (value))
  86. return string.IsNullOrEmpty (source.value);
  87. return string.Equals (source.value, value, StringComparison.OrdinalIgnoreCase);
  88. }
  89. public static NameValueHeaderValue Parse (string input)
  90. {
  91. NameValueHeaderValue value;
  92. if (TryParse (input, out value))
  93. return value;
  94. throw new FormatException (input);
  95. }
  96. internal static bool ParseParameters (Lexer lexer, out List<NameValueHeaderValue> result)
  97. {
  98. var list = new List<NameValueHeaderValue> ();
  99. result = null;
  100. Token t;
  101. do {
  102. var attr = lexer.Scan ();
  103. if (attr != Token.Type.Token)
  104. return false;
  105. string value = null;
  106. t = lexer.Scan ();
  107. if (t == Token.Type.SeparatorEqual) {
  108. t = lexer.Scan ();
  109. if (t != Token.Type.Token && t != Token.Type.QuotedString)
  110. return false;
  111. value = lexer.GetStringValue (t);
  112. t = lexer.Scan ();
  113. }
  114. if (t == Token.Type.SeparatorSemicolon || t == Token.Type.End) {
  115. list.Add (new NameValueHeaderValue () {
  116. Name = lexer.GetStringValue (attr),
  117. value = value
  118. });
  119. } else {
  120. return false;
  121. }
  122. } while (t == Token.Type.SeparatorSemicolon);
  123. result = list;
  124. return true;
  125. }
  126. public override string ToString ()
  127. {
  128. if (string.IsNullOrEmpty (value))
  129. return Name;
  130. return Name + "=" + value;
  131. }
  132. public static bool TryParse (string input, out NameValueHeaderValue parsedValue)
  133. {
  134. parsedValue = null;
  135. var lexer = new Lexer (input);
  136. var t = lexer.Scan ();
  137. if (t != Token.Type.Token && t != Token.Type.QuotedString)
  138. return false;
  139. string v = null;
  140. var token2 = lexer.Scan ();
  141. if (token2 != Token.Type.End) {
  142. if (token2 != Token.Type.SeparatorEqual)
  143. return false;
  144. token2 = lexer.Scan ();
  145. if (token2 == Token.Type.Token || token2 == Token.Type.QuotedString) {
  146. v = lexer.GetStringValue (token2);
  147. token2 = lexer.Scan ();
  148. }
  149. if (token2 != Token.Type.End)
  150. return false;
  151. }
  152. parsedValue = new NameValueHeaderValue () {
  153. Name = lexer.GetStringValue (t),
  154. value = v
  155. };
  156. return true;
  157. }
  158. }
  159. }