TransferCodingHeaderValue.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // TransferCodingHeaderValue.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 TransferCodingHeaderValue : ICloneable
  32. {
  33. string value;
  34. internal List<NameValueHeaderValue> parameters;
  35. public TransferCodingHeaderValue (string value)
  36. {
  37. Parser.Token.Check (value);
  38. this.value = value;
  39. }
  40. protected TransferCodingHeaderValue (TransferCodingHeaderValue source)
  41. {
  42. this.value = source.value;
  43. if (source.parameters != null) {
  44. foreach (var p in source.parameters) {
  45. Parameters.Add (new NameValueHeaderValue (p));
  46. }
  47. }
  48. }
  49. internal TransferCodingHeaderValue ()
  50. {
  51. }
  52. public ICollection<NameValueHeaderValue> Parameters {
  53. get {
  54. return parameters ?? (parameters = new List<NameValueHeaderValue> ());
  55. }
  56. }
  57. public string Value {
  58. get {
  59. return value;
  60. }
  61. }
  62. object ICloneable.Clone ()
  63. {
  64. return new TransferCodingHeaderValue (this);
  65. }
  66. public override bool Equals (object obj)
  67. {
  68. var fchv = obj as TransferCodingHeaderValue;
  69. return fchv != null &&
  70. string.Equals (value, fchv.value, StringComparison.OrdinalIgnoreCase) &&
  71. parameters.SequenceEqual (fchv.parameters);
  72. }
  73. public override int GetHashCode ()
  74. {
  75. var hc = value.ToLowerInvariant ().GetHashCode ();
  76. if (parameters != null)
  77. hc ^= HashCodeCalculator.Calculate (parameters);
  78. return hc;
  79. }
  80. public static TransferCodingHeaderValue Parse (string input)
  81. {
  82. TransferCodingHeaderValue value;
  83. if (TryParse (input, out value))
  84. return value;
  85. throw new FormatException (input);
  86. }
  87. public override string ToString ()
  88. {
  89. return value + CollectionExtensions.ToString (parameters);
  90. }
  91. public static bool TryParse (string input, out TransferCodingHeaderValue parsedValue)
  92. {
  93. return TryParse (input, out parsedValue, () => new TransferCodingHeaderValue ());
  94. }
  95. internal static bool TryParse<T> (string input, out T parsedValue, Func<T> factory) where T : TransferCodingHeaderValue
  96. {
  97. parsedValue = null;
  98. var lexer = new Lexer (input);
  99. var t = lexer.Scan ();
  100. if (t != Token.Type.Token)
  101. return false;
  102. var result = factory ();
  103. result.value = lexer.GetStringValue (t);
  104. t = lexer.Scan ();
  105. // Parameters parsing
  106. if (t == Token.Type.SeparatorSemicolon) {
  107. if (!NameValueHeaderValue.ParseParameters (lexer, out result.parameters))
  108. return false;
  109. } else if (t != Token.Type.End) {
  110. return false;
  111. }
  112. parsedValue = result;
  113. return true;
  114. }
  115. }
  116. }