ViaHeaderValue.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // ViaHeaderValue.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 ViaHeaderValue : ICloneable
  32. {
  33. public ViaHeaderValue (string protocolVersion, string receivedBy)
  34. {
  35. Parser.Token.Check (protocolVersion);
  36. Parser.Uri.Check (receivedBy);
  37. ProtocolVersion = protocolVersion;
  38. ReceivedBy = receivedBy;
  39. }
  40. public ViaHeaderValue (string protocolVersion, string receivedBy, string protocolName)
  41. : this (protocolVersion, receivedBy)
  42. {
  43. if (!string.IsNullOrEmpty (protocolName)) {
  44. Parser.Token.Check (protocolName);
  45. ProtocolName = protocolName;
  46. }
  47. }
  48. public ViaHeaderValue (string protocolVersion, string receivedBy, string protocolName, string comment)
  49. : this (protocolVersion, receivedBy, protocolName)
  50. {
  51. if (!string.IsNullOrEmpty (comment)) {
  52. Parser.Token.CheckComment (comment);
  53. Comment = comment;
  54. }
  55. }
  56. private ViaHeaderValue ()
  57. {
  58. }
  59. public string Comment { get; private set; }
  60. public string ProtocolName { get; private set; }
  61. public string ProtocolVersion { get; private set; }
  62. public string ReceivedBy { get; private set; }
  63. object ICloneable.Clone ()
  64. {
  65. return MemberwiseClone ();
  66. }
  67. public override bool Equals (object obj)
  68. {
  69. var source = obj as ViaHeaderValue;
  70. if (source == null)
  71. return false;
  72. return string.Equals (source.Comment, Comment, StringComparison.Ordinal) &&
  73. string.Equals (source.ProtocolName, ProtocolName, StringComparison.OrdinalIgnoreCase) &&
  74. string.Equals (source.ProtocolVersion, ProtocolVersion, StringComparison.OrdinalIgnoreCase) &&
  75. string.Equals (source.ReceivedBy, ReceivedBy, StringComparison.OrdinalIgnoreCase);
  76. }
  77. public override int GetHashCode ()
  78. {
  79. int hc = ProtocolVersion.ToLowerInvariant ().GetHashCode ();
  80. hc ^= ReceivedBy.ToLowerInvariant ().GetHashCode ();
  81. if (!string.IsNullOrEmpty (ProtocolName)) {
  82. hc ^= ProtocolName.ToLowerInvariant ().GetHashCode ();
  83. }
  84. if (!string.IsNullOrEmpty (Comment)) {
  85. hc ^= Comment.GetHashCode ();
  86. }
  87. return hc;
  88. }
  89. public static ViaHeaderValue Parse (string input)
  90. {
  91. ViaHeaderValue value;
  92. if (TryParse (input, out value))
  93. return value;
  94. throw new FormatException (input);
  95. }
  96. public static bool TryParse (string input, out ViaHeaderValue parsedValue)
  97. {
  98. var lexer = new Lexer (input);
  99. Token token;
  100. if (TryParseElement (lexer, out parsedValue, out token) && token == Token.Type.End)
  101. return true;
  102. parsedValue = null;
  103. return false;
  104. }
  105. internal static bool TryParse (string input, int minimalCount, out List<ViaHeaderValue> result)
  106. {
  107. return CollectionParser.TryParse (input, minimalCount, TryParseElement, out result);
  108. }
  109. static bool TryParseElement (Lexer lexer, out ViaHeaderValue parsedValue, out Token t)
  110. {
  111. parsedValue = null;
  112. t = lexer.Scan ();
  113. if (t != Token.Type.Token)
  114. return false;
  115. var next = lexer.Scan ();
  116. ViaHeaderValue value = new ViaHeaderValue ();
  117. if (next == Token.Type.SeparatorSlash) {
  118. next = lexer.Scan ();
  119. if (next != Token.Type.Token)
  120. return false;
  121. value.ProtocolName = lexer.GetStringValue (t);
  122. value.ProtocolVersion = lexer.GetStringValue (next);
  123. next = lexer.Scan ();
  124. } else {
  125. value.ProtocolVersion = lexer.GetStringValue (t);
  126. }
  127. if (next != Token.Type.Token)
  128. return false;
  129. if (lexer.PeekChar () == ':') {
  130. lexer.EatChar ();
  131. t = lexer.Scan ();
  132. if (t != Token.Type.Token)
  133. return false;
  134. } else {
  135. t = next;
  136. }
  137. value.ReceivedBy = lexer.GetStringValue (next, t);
  138. string comment;
  139. if (lexer.ScanCommentOptional (out comment, out t)) {
  140. t = lexer.Scan ();
  141. }
  142. value.Comment = comment;
  143. parsedValue = value;
  144. return true;
  145. }
  146. public override string ToString ()
  147. {
  148. string s = ProtocolName != null ?
  149. ProtocolName + "/" + ProtocolVersion + " " + ReceivedBy :
  150. ProtocolVersion + " " + ReceivedBy;
  151. return Comment != null ? s + " " + Comment : s;
  152. }
  153. }
  154. }