WarningHeaderValue.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // WarningHeaderValue.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.Globalization;
  29. using System.Collections.Generic;
  30. namespace System.Net.Http.Headers
  31. {
  32. public class WarningHeaderValue : ICloneable
  33. {
  34. public WarningHeaderValue (int code, string agent, string text)
  35. {
  36. if (!IsCodeValid (code))
  37. throw new ArgumentOutOfRangeException ("code");
  38. Parser.Uri.Check (agent);
  39. Parser.Token.CheckQuotedString (text);
  40. Code = code;
  41. Agent = agent;
  42. Text = text;
  43. }
  44. public WarningHeaderValue (int code, string agent, string text, DateTimeOffset date)
  45. : this (code, agent, text)
  46. {
  47. Date = date;
  48. }
  49. private WarningHeaderValue ()
  50. {
  51. }
  52. public string Agent { get; private set; }
  53. public int Code { get; private set; }
  54. public DateTimeOffset? Date { get; private set; }
  55. public string Text { get; private set; }
  56. static bool IsCodeValid (int code)
  57. {
  58. return code >= 0 && code < 1000;
  59. }
  60. object ICloneable.Clone ()
  61. {
  62. return MemberwiseClone ();
  63. }
  64. public override bool Equals (object obj)
  65. {
  66. var source = obj as WarningHeaderValue;
  67. if (source == null)
  68. return false;
  69. return Code == source.Code &&
  70. string.Equals (source.Agent, Agent, StringComparison.OrdinalIgnoreCase) &&
  71. Text == source.Text &&
  72. Date == source.Date;
  73. }
  74. public override int GetHashCode ()
  75. {
  76. int hc = Code.GetHashCode ();
  77. hc ^= Agent.ToLowerInvariant ().GetHashCode ();
  78. hc ^= Text.GetHashCode ();
  79. hc ^= Date.GetHashCode ();
  80. return hc;
  81. }
  82. public static WarningHeaderValue Parse (string input)
  83. {
  84. WarningHeaderValue value;
  85. if (TryParse (input, out value))
  86. return value;
  87. throw new FormatException (input);
  88. }
  89. public static bool TryParse (string input, out WarningHeaderValue parsedValue)
  90. {
  91. var lexer = new Lexer (input);
  92. Token token;
  93. if (TryParseElement (lexer, out parsedValue, out token) && token == Token.Type.End)
  94. return true;
  95. parsedValue = null;
  96. return false;
  97. }
  98. internal static bool TryParse (string input, int minimalCount, out List<WarningHeaderValue> result)
  99. {
  100. return CollectionParser.TryParse (input, minimalCount, TryParseElement, out result);
  101. }
  102. static bool TryParseElement (Lexer lexer, out WarningHeaderValue parsedValue, out Token t)
  103. {
  104. parsedValue = null;
  105. t = lexer.Scan ();
  106. if (t != Token.Type.Token)
  107. return false;
  108. int code;
  109. if (!lexer.TryGetNumericValue (t, out code) || !IsCodeValid (code))
  110. return false;
  111. t = lexer.Scan ();
  112. if (t != Token.Type.Token)
  113. return false;
  114. var next = t;
  115. if (lexer.PeekChar () == ':') {
  116. lexer.EatChar ();
  117. next = lexer.Scan ();
  118. if (next != Token.Type.Token)
  119. return false;
  120. }
  121. var value = new WarningHeaderValue ();
  122. value.Code = code;
  123. value.Agent = lexer.GetStringValue (t, next);
  124. t = lexer.Scan ();
  125. if (t != Token.Type.QuotedString)
  126. return false;
  127. value.Text = lexer.GetStringValue (t);
  128. t = lexer.Scan ();
  129. if (t == Token.Type.QuotedString) {
  130. DateTimeOffset date;
  131. if (!lexer.TryGetDateValue (t, out date))
  132. return false;
  133. value.Date = date;
  134. t = lexer.Scan ();
  135. }
  136. parsedValue = value;
  137. return true;
  138. }
  139. public override string ToString ()
  140. {
  141. string s = Code.ToString ("000") + " " + Agent + " " + Text;
  142. if (Date.HasValue)
  143. s = s + " \"" + Date.Value.ToString ("r", CultureInfo.InvariantCulture) + "\"";
  144. return s;
  145. }
  146. }
  147. }