PageRenderTime 5830ms CodeModel.GetById 43ms RepoModel.GetById 2ms app.codeStats 0ms

/NET6/Hermod/Hermod/HTTP/General/HTTPVersion.cs

https://github.com/Vanaheimr/Hermod
C# | 378 lines | 156 code | 110 blank | 112 comment | 32 complexity | 86a3bb091f1c0fe9d42a784bf3b9a629 MD5 | raw file
  1. /*
  2. * Copyright (c) 2010-2022 GraphDefined GmbH <achim.friedland@graphdefined.com>
  3. * This file is part of Vanaheimr Hermod <https://www.github.com/Vanaheimr/Hermod>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #region Usings
  18. using System;
  19. using System.Linq;
  20. #endregion
  21. namespace org.GraphDefined.Vanaheimr.Hermod.HTTP
  22. {
  23. /// <summary>
  24. /// A HTTP version identifier.
  25. /// </summary>
  26. public struct HTTPVersion : IEquatable<HTTPVersion>,
  27. IComparable<HTTPVersion>,
  28. IComparable
  29. {
  30. #region Properties
  31. /// <summary>
  32. /// The major of this HTTP version
  33. /// </summary>
  34. public UInt16 Major { get; }
  35. /// <summary>
  36. /// The minor of this HTTP version
  37. /// </summary>
  38. public UInt16 Minor { get; }
  39. #endregion
  40. #region Constructor(s)
  41. /// <summary>
  42. /// Create a new HTTP version identifier.
  43. /// </summary>
  44. /// <param name="Major">The major number.</param>
  45. /// <param name="Minor">The minor number.</param>
  46. public HTTPVersion(UInt16 Major, UInt16 Minor)
  47. {
  48. this.Major = Major;
  49. this.Minor = Minor;
  50. }
  51. #endregion
  52. #region Parse (Text)
  53. /// <summary>
  54. /// Parse the given text representation of a HTTP version, e.g. "HTTP/1.1".
  55. /// </summary>
  56. /// <param name="Text">A text representation of a HTTP version, e.g. "HTTP/1.1".</param>
  57. public static HTTPVersion Parse(String Text)
  58. {
  59. if (TryParse(Text, out HTTPVersion Version))
  60. return Version;
  61. throw new ArgumentException("The given string could not be parsed as a HTTP version!", nameof(Text));
  62. }
  63. #endregion
  64. #region TryParse(Text)
  65. /// <summary>
  66. /// Try to parse the given text representation of a HTTP version, e.g. "HTTP/1.1".
  67. /// </summary>
  68. /// <param name="Text">A text representation of a HTTP version, e.g. "HTTP/1.1".</param>
  69. public static HTTPVersion? TryParse(String Text)
  70. {
  71. if (TryParse(Text, out HTTPVersion Version))
  72. return Version;
  73. return new HTTPVersion?();
  74. }
  75. #endregion
  76. #region TryParse(Text, out Version)
  77. /// <summary>
  78. /// Try to parse the given text representation of a HTTP version, e.g. "HTTP/1.1".
  79. /// </summary>
  80. /// <param name="Text">A text representation of a HTTP version, e.g. "HTTP/1.1".</param>
  81. /// <param name="Version">The parsed HTTP version</param>
  82. public static Boolean TryParse(String Text, out HTTPVersion Version)
  83. {
  84. var MajorMinor = Text.Split(new Char[] { '.' }, StringSplitOptions.None);
  85. if (MajorMinor.Length != 2 ||
  86. !UInt16.TryParse(MajorMinor[0], out UInt16 Major) ||
  87. !UInt16.TryParse(MajorMinor[1], out UInt16 Minor))
  88. {
  89. Version = default(HTTPVersion);
  90. return false;
  91. }
  92. Version = new HTTPVersion(Major, Minor);
  93. return true;
  94. }
  95. #endregion
  96. #region Static HTTP versions
  97. /// <summary>
  98. /// HTTP 1.0
  99. /// </summary>
  100. public static readonly HTTPVersion HTTP_1_0 = new HTTPVersion(1, 0);
  101. /// <summary>
  102. /// HTTP 1.1
  103. /// </summary>
  104. public static readonly HTTPVersion HTTP_1_1 = new HTTPVersion(1, 1);
  105. #endregion
  106. #region Operator overloading
  107. #region Operator == (HTTPVersion1, HTTPVersion2)
  108. /// <summary>
  109. /// Compares two instances of this object.
  110. /// </summary>
  111. /// <param name="HTTPVersion1">A HTTP version.</param>
  112. /// <param name="HTTPVersion2">Another HTTP version.</param>
  113. /// <returns>true|false</returns>
  114. public static Boolean operator == (HTTPVersion HTTPVersion1, HTTPVersion HTTPVersion2)
  115. {
  116. // If both are null, or both are same instance, return true.
  117. if (Object.ReferenceEquals(HTTPVersion1, HTTPVersion2))
  118. return true;
  119. // If one is null, but not both, return false.
  120. if (((Object) HTTPVersion1 == null) || ((Object) HTTPVersion2 == null))
  121. return false;
  122. return HTTPVersion1.Equals(HTTPVersion2);
  123. }
  124. #endregion
  125. #region Operator != (HTTPVersion1, HTTPVersion2)
  126. /// <summary>
  127. /// Compares two instances of this object.
  128. /// </summary>
  129. /// <param name="HTTPVersion1">A HTTP version.</param>
  130. /// <param name="HTTPVersion2">Another HTTP version.</param>
  131. /// <returns>true|false</returns>
  132. public static Boolean operator != (HTTPVersion HTTPVersion1, HTTPVersion HTTPVersion2)
  133. => !(HTTPVersion1 == HTTPVersion2);
  134. #endregion
  135. #region Operator < (HTTPVersion1, HTTPVersion2)
  136. /// <summary>
  137. /// Compares two instances of this object.
  138. /// </summary>
  139. /// <param name="HTTPVersion1">A HTTP version.</param>
  140. /// <param name="HTTPVersion2">Another HTTP version.</param>
  141. /// <returns>true|false</returns>
  142. public static Boolean operator < (HTTPVersion HTTPVersion1, HTTPVersion HTTPVersion2)
  143. {
  144. if ((Object) HTTPVersion1 == null)
  145. throw new ArgumentNullException(nameof(HTTPVersion1), "The given HTTPVersion1 must not be null!");
  146. return HTTPVersion1.CompareTo(HTTPVersion2) < 0;
  147. }
  148. #endregion
  149. #region Operator <= (HTTPVersion1, HTTPVersion2)
  150. /// <summary>
  151. /// Compares two instances of this object.
  152. /// </summary>
  153. /// <param name="HTTPVersion1">A HTTP version.</param>
  154. /// <param name="HTTPVersion2">Another HTTP version.</param>
  155. /// <returns>true|false</returns>
  156. public static Boolean operator <= (HTTPVersion HTTPVersion1, HTTPVersion HTTPVersion2)
  157. => !(HTTPVersion1 > HTTPVersion2);
  158. #endregion
  159. #region Operator > (HTTPVersion1, HTTPVersion2)
  160. /// <summary>
  161. /// Compares two instances of this object.
  162. /// </summary>
  163. /// <param name="HTTPVersion1">A HTTP version.</param>
  164. /// <param name="HTTPVersion2">Another HTTP version.</param>
  165. /// <returns>true|false</returns>
  166. public static Boolean operator > (HTTPVersion HTTPVersion1, HTTPVersion HTTPVersion2)
  167. {
  168. if ((Object) HTTPVersion1 == null)
  169. throw new ArgumentNullException(nameof(HTTPVersion1), "The given HTTPVersion1 must not be null!");
  170. return HTTPVersion1.CompareTo(HTTPVersion2) > 0;
  171. }
  172. #endregion
  173. #region Operator >= (HTTPVersion1, HTTPVersion2)
  174. /// <summary>
  175. /// Compares two instances of this object.
  176. /// </summary>
  177. /// <param name="HTTPVersion1">A HTTP version.</param>
  178. /// <param name="HTTPVersion2">Another HTTP version.</param>
  179. /// <returns>true|false</returns>
  180. public static Boolean operator >= (HTTPVersion HTTPVersion1, HTTPVersion HTTPVersion2)
  181. => !(HTTPVersion1 < HTTPVersion2);
  182. #endregion
  183. #endregion
  184. #region IComparable<HTTPVersion> Members
  185. #region CompareTo(Object)
  186. /// <summary>
  187. /// Compares two instances of this object.
  188. /// </summary>
  189. /// <param name="Object">An object to compare with.</param>
  190. public Int32 CompareTo(Object Object)
  191. {
  192. if (Object == null)
  193. throw new ArgumentNullException("The given object must not be null!");
  194. if (!(Object is HTTPVersion))
  195. throw new ArgumentException("The given object is not a HTTP version!");
  196. return CompareTo((HTTPVersion) Object);
  197. }
  198. #endregion
  199. #region CompareTo(HTTPVersion)
  200. /// <summary>
  201. /// Compares two instances of this object.
  202. /// </summary>
  203. /// <param name="HTTPVersion">An object to compare with.</param>
  204. public Int32 CompareTo(HTTPVersion HTTPVersion)
  205. {
  206. if ((Object) HTTPVersion == null)
  207. throw new ArgumentNullException("The given HTTP version must not be null!");
  208. var _MajorCompared = Major.CompareTo(HTTPVersion.Major);
  209. if (_MajorCompared != 0)
  210. return _MajorCompared;
  211. return Minor.CompareTo(HTTPVersion.Minor);
  212. }
  213. #endregion
  214. #endregion
  215. #region IEquatable<HTTPVersion> Members
  216. #region Equals(Object)
  217. /// <summary>
  218. /// Compares two instances of this object.
  219. /// </summary>
  220. /// <param name="Object">An object to compare with.</param>
  221. /// <returns>true|false</returns>
  222. public override Boolean Equals(Object Object)
  223. {
  224. if (Object == null)
  225. return false;
  226. if (!(Object is HTTPVersion))
  227. return false;
  228. return Equals((HTTPVersion) Object);
  229. }
  230. #endregion
  231. #region Equals(HTTPVersion)
  232. /// <summary>
  233. /// Compares two HTTPVersions for equality.
  234. /// </summary>
  235. /// <param name="HTTPVersion">A HTTPVersion to compare with.</param>
  236. /// <returns>True if both match; False otherwise.</returns>
  237. public Boolean Equals(HTTPVersion HTTPVersion)
  238. {
  239. if ((Object) HTTPVersion == null)
  240. return false;
  241. return Major == HTTPVersion.Major &&
  242. Minor == HTTPVersion.Minor;
  243. }
  244. #endregion
  245. #endregion
  246. #region GetHashCode()
  247. /// <summary>
  248. /// Return the HashCode of this object.
  249. /// </summary>
  250. /// <returns>The HashCode of this object.</returns>
  251. public override Int32 GetHashCode()
  252. {
  253. unchecked
  254. {
  255. return Major.GetHashCode() * 3 ^
  256. Minor.GetHashCode();
  257. }
  258. }
  259. #endregion
  260. #region (override) ToString()
  261. /// <summary>
  262. /// Return a text representation of this object.
  263. /// </summary>
  264. public override String ToString()
  265. => String.Concat(Major, ".", Minor);
  266. #endregion
  267. }
  268. }