WarningHeaderValue.cs 4.0 KB

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