ViaHeaderValue.cs 4.6 KB

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