RpcPath.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using EdjCase.JsonRpc.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace EdjCase.JsonRpc.Router
  6. {
  7. /// <summary>
  8. /// Represents the url path for Rpc routing purposes
  9. /// </summary>
  10. public struct RpcPath : IEquatable<RpcPath>
  11. {
  12. /// <summary>
  13. /// Default/Empty path
  14. /// </summary>
  15. public static RpcPath Default => new RpcPath();
  16. /// <summary>
  17. /// Path components split on forward slashes
  18. /// </summary>
  19. private readonly string[] componentsValue;
  20. private int? hashCodeCache;
  21. /// <param name="components">Uri components for the path</param>
  22. private RpcPath(string[] components = null)
  23. {
  24. this.componentsValue = components ?? new string[0];
  25. this.hashCodeCache = null;
  26. }
  27. public static bool operator ==(RpcPath path1, RpcPath path2)
  28. {
  29. return path1.Equals(path2);
  30. }
  31. public static bool operator !=(RpcPath path1, RpcPath path2)
  32. {
  33. return !path1.Equals(path2);
  34. }
  35. public bool StartsWith(RpcPath other)
  36. {
  37. if ((other.componentsValue?.Length ?? 0) == 0)
  38. {
  39. return true;
  40. }
  41. if ((this.componentsValue?.Length ?? 0) == 0)
  42. {
  43. return false;
  44. }
  45. if (other.componentsValue.Length > this.componentsValue.Length)
  46. {
  47. return false;
  48. }
  49. for (int i = 0; i < other.componentsValue.Length; i++)
  50. {
  51. string component = this.componentsValue[i];
  52. string otherComponent = other.componentsValue[i];
  53. if (!string.Equals(component, otherComponent))
  54. {
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. public bool Equals(RpcPath other)
  61. {
  62. return this.GetHashCode() == other.GetHashCode();
  63. }
  64. public override bool Equals(object obj)
  65. {
  66. if (obj is RpcPath path)
  67. {
  68. return this.Equals(path);
  69. }
  70. return false;
  71. }
  72. public override int GetHashCode()
  73. {
  74. //TODO best way to optimize gethashcode? multithread?
  75. if (this.hashCodeCache == null)
  76. {
  77. int hash;
  78. if (this.componentsValue == null || this.componentsValue.Length == 0)
  79. {
  80. hash = 0;
  81. }
  82. else
  83. {
  84. hash = 1337;
  85. foreach (string component in this.componentsValue)
  86. {
  87. hash = (hash * 7) + component.GetHashCode();
  88. }
  89. }
  90. this.hashCodeCache = hash;
  91. }
  92. return this.hashCodeCache.Value;
  93. }
  94. /// <summary>
  95. /// Creates a <see cref="RpcPath"/> based on the string form of the path
  96. /// </summary>
  97. /// <param name="path">Uri/route path</param>
  98. /// <returns>Rpc path based on the path string</returns>
  99. public static RpcPath Parse(string path)
  100. {
  101. if (!RpcPath.TryParse(path, out RpcPath rpcPath))
  102. {
  103. throw new RpcException(RpcErrorCode.ParseError, $"Rpc path could not be parsed from '{path}'.");
  104. }
  105. return rpcPath;
  106. }
  107. /// <summary>
  108. /// Creates a <see cref="RpcPath"/> based on the string form of the path
  109. /// </summary>
  110. /// <param name="path">Uri/route path</param>
  111. /// <returns>True if the path parses, otherwise false</returns>
  112. public static bool TryParse(string path, out RpcPath rpcPath)
  113. {
  114. if (string.IsNullOrWhiteSpace(path))
  115. {
  116. rpcPath = new RpcPath();
  117. return true;
  118. }
  119. else
  120. {
  121. try
  122. {
  123. string[] pathComponents = path
  124. .ToLowerInvariant()
  125. .Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
  126. rpcPath = new RpcPath(pathComponents);
  127. return true;
  128. }
  129. catch
  130. {
  131. rpcPath = default;
  132. return false;
  133. }
  134. }
  135. }
  136. /// <summary>
  137. /// Removes the base path path from this path
  138. /// </summary>
  139. /// <param name="basePath">Base path to remove</param>
  140. /// <returns>A new path that is the full path without the base path</returns>
  141. public RpcPath RemoveBasePath(RpcPath basePath)
  142. {
  143. if (!this.TryRemoveBasePath(basePath, out RpcPath path))
  144. {
  145. throw new RpcException(RpcErrorCode.ParseError, $"Count not remove path '{basePath}' from path '{this}'.");
  146. }
  147. return path;
  148. }
  149. /// <summary>
  150. /// Tries to remove the base path path from this path
  151. /// </summary>
  152. /// <param name="basePath">Base path to remove</param>
  153. /// <returns>True if removed the base path. Otherwise false</returns>
  154. public bool TryRemoveBasePath(RpcPath basePath, out RpcPath path)
  155. {
  156. if (basePath == default)
  157. {
  158. path = this.Clone();
  159. return true;
  160. }
  161. if (!this.StartsWith(basePath))
  162. {
  163. path = default;
  164. return false;
  165. }
  166. var newComponents = new string[this.componentsValue.Length - basePath.componentsValue.Length];
  167. if (newComponents.Length > 0)
  168. {
  169. Array.Copy(this.componentsValue, basePath.componentsValue.Length, newComponents, 0, newComponents.Length);
  170. }
  171. path = new RpcPath(newComponents);
  172. return true;
  173. }
  174. /// <summary>
  175. /// Merges the two paths to create a new Rpc path that is the combination of the two
  176. /// </summary>
  177. /// <param name="other">Other path to add to the end of the current path</param>
  178. /// <returns>A new path that is the combination of the two paths</returns>
  179. public RpcPath Add(RpcPath other)
  180. {
  181. if (other.componentsValue == null)
  182. {
  183. return this.Clone();
  184. }
  185. if (this.componentsValue == null)
  186. {
  187. return other.Clone();
  188. }
  189. int componentCount = this.componentsValue.Length + other.componentsValue.Length;
  190. string[] newComponents = new string[componentCount];
  191. this.componentsValue.CopyTo(newComponents, 0);
  192. other.componentsValue.CopyTo(newComponents, this.componentsValue.Length);
  193. return new RpcPath(newComponents);
  194. }
  195. public override string ToString()
  196. {
  197. if (this.componentsValue == null)
  198. {
  199. return "/";
  200. }
  201. return "/" + string.Join("/", this.componentsValue);
  202. }
  203. public RpcPath Clone()
  204. {
  205. if (this.componentsValue == null || this.componentsValue.Length == 0)
  206. {
  207. return new RpcPath();
  208. }
  209. int componentCount = this.componentsValue.Length;
  210. string[] newComponents = new string[componentCount];
  211. this.componentsValue.CopyTo(newComponents, 0);
  212. return new RpcPath(newComponents);
  213. }
  214. public static implicit operator string(RpcPath path)
  215. {
  216. return path.ToString();
  217. }
  218. public static implicit operator RpcPath(string s)
  219. {
  220. return RpcPath.Parse(s);
  221. }
  222. }
  223. }