Routing 727 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. commit e2bcefc3d0bca1fbe59594cacb10f463dd0e46d4
  2. Author: Jass Bagga <[email protected]>
  3. Date: Wed Nov 15 11:17:59 2017 -0800
  4. Move TreeEnumerator to shared source (#494)
  5. diff --git a/shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources/TreeEnumerator.cs b/shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources/TreeEnumerator.cs
  6. new file mode 100644
  7. index 00000000000..916b154ab0f
  8. --- /dev/null
  9. +++ b/shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources/TreeEnumerator.cs
  10. @@ -0,0 +1,119 @@
  11. +// Copyright (c) .NET Foundation. All rights reserved.
  12. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  13. +
  14. +using System.Collections;
  15. +using System.Collections.Generic;
  16. +using System.Diagnostics;
  17. +using Microsoft.AspNetCore.Dispatcher.Internal;
  18. +
  19. +#if ROUTING
  20. +namespace Microsoft.AspNetCore.Routing.Tree
  21. +#elif DISPATCHER
  22. +namespace Microsoft.AspNetCore.Dispatcher
  23. +#else
  24. +#error
  25. +#endif
  26. +{
  27. + internal struct TreeEnumerator : IEnumerator<UrlMatchingNode>
  28. + {
  29. + private readonly Stack<UrlMatchingNode> _stack;
  30. + private readonly PathTokenizer _tokenizer;
  31. +
  32. + public TreeEnumerator(UrlMatchingNode root, PathTokenizer tokenizer)
  33. + {
  34. + _stack = new Stack<UrlMatchingNode>();
  35. + _tokenizer = tokenizer;
  36. + Current = null;
  37. +
  38. + _stack.Push(root);
  39. + }
  40. +
  41. + public UrlMatchingNode Current { get; private set; }
  42. +
  43. + object IEnumerator.Current => Current;
  44. +
  45. + public void Dispose()
  46. + {
  47. + }
  48. +
  49. + public bool MoveNext()
  50. + {
  51. + if (_stack == null)
  52. + {
  53. + return false;
  54. + }
  55. +
  56. + while (_stack.Count > 0)
  57. + {
  58. + var next = _stack.Pop();
  59. +
  60. + // In case of wild card segment, the request path segment length can be greater
  61. + // Example:
  62. + // Template: a/{*path}
  63. + // Request Url: a/b/c/d
  64. + if (next.IsCatchAll && next.Matches.Count > 0)
  65. + {
  66. + Current = next;
  67. + return true;
  68. + }
  69. +
  70. + // Next template has the same length as the url we are trying to match
  71. + // The only possible matching segments are either our current matches or
  72. + // any catch-all segment after this segment in which the catch all is empty.
  73. + else if (next.Depth >= _tokenizer.Count)
  74. + {
  75. + if (next.Matches.Count > 0)
  76. + {
  77. + Current = next;
  78. + return true;
  79. + }
  80. + else
  81. + {
  82. + // We can stop looking as any other child node from this node will be
  83. + // either a literal, a constrained parameter or a parameter.
  84. + // (Catch alls and constrained catch alls will show up as candidate matches).
  85. + continue;
  86. + }
  87. + }
  88. +
  89. + if (next.CatchAlls != null)
  90. + {
  91. + _stack.Push(next.CatchAlls);
  92. + }
  93. +
  94. + if (next.ConstrainedCatchAlls != null)
  95. + {
  96. + _stack.Push(next.ConstrainedCatchAlls);
  97. + }
  98. +
  99. + if (next.Parameters != null)
  100. + {
  101. + _stack.Push(next.Parameters);
  102. + }
  103. +
  104. + if (next.ConstrainedParameters != null)
  105. + {
  106. + _stack.Push(next.ConstrainedParameters);
  107. + }
  108. +
  109. + if (next.Literals.Count > 0)
  110. + {
  111. + Debug.Assert(next.Depth < _tokenizer.Count);
  112. + if (next.Literals.TryGetValue(_tokenizer[next.Depth].Value, out var node))
  113. + {
  114. + _stack.Push(node);
  115. + }
  116. + }
  117. + }
  118. +
  119. + return false;
  120. + }
  121. +
  122. + public void Reset()
  123. + {
  124. + _stack.Clear();
  125. + Current = null;
  126. + }
  127. + }
  128. +
  129. +}
  130. \ No newline at end of file
  131. diff --git a/src/Microsoft.AspNetCore.Dispatcher/Tree/TreeMatcher.cs b/src/Microsoft.AspNetCore.Dispatcher/Tree/TreeMatcher.cs
  132. index ea96a145093..836ccaf22b2 100644
  133. --- a/src/Microsoft.AspNetCore.Dispatcher/Tree/TreeMatcher.cs
  134. +++ b/src/Microsoft.AspNetCore.Dispatcher/Tree/TreeMatcher.cs
  135. @@ -2,7 +2,6 @@
  136. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  137. using System;
  138. -using System.Collections;
  139. using System.Collections.Generic;
  140. using System.Diagnostics;
  141. using System.Linq;
  142. @@ -52,7 +51,7 @@ namespace Microsoft.AspNetCore.Dispatcher
  143. var tree = cache.Trees[i];
  144. var tokenizer = new PathTokenizer(context.HttpContext.Request.Path);
  145. - var treenumerator = new Treenumerator(tree.Root, tokenizer);
  146. + var treenumerator = new TreeEnumerator(tree.Root, tokenizer);
  147. while (treenumerator.MoveNext())
  148. {
  149. @@ -433,108 +432,6 @@ namespace Microsoft.AspNetCore.Dispatcher
  150. }
  151. }
  152. - private struct Treenumerator : IEnumerator<UrlMatchingNode>
  153. - {
  154. - private readonly Stack<UrlMatchingNode> _stack;
  155. - private readonly PathTokenizer _tokenizer;
  156. -
  157. - public Treenumerator(UrlMatchingNode root, PathTokenizer tokenizer)
  158. - {
  159. - _stack = new Stack<UrlMatchingNode>();
  160. - _tokenizer = tokenizer;
  161. - Current = null;
  162. -
  163. - _stack.Push(root);
  164. - }
  165. -
  166. - public UrlMatchingNode Current { get; private set; }
  167. -
  168. - object IEnumerator.Current => Current;
  169. -
  170. - public void Dispose()
  171. - {
  172. - }
  173. -
  174. - public bool MoveNext()
  175. - {
  176. - if (_stack == null)
  177. - {
  178. - return false;
  179. - }
  180. -
  181. - while (_stack.Count > 0)
  182. - {
  183. - var next = _stack.Pop();
  184. -
  185. - // In case of wild card segment, the request path segment length can be greater
  186. - // Example:
  187. - // Template: a/{*path}
  188. - // Request Url: a/b/c/d
  189. - if (next.IsCatchAll && next.Matches.Count > 0)
  190. - {
  191. - Current = next;
  192. - return true;
  193. - }
  194. -
  195. - // Next template has the same length as the url we are trying to match
  196. - // The only possible matching segments are either our current matches or
  197. - // any catch-all segment after this segment in which the catch all is empty.
  198. - else if (next.Depth >= _tokenizer.Count)
  199. - {
  200. - if (next.Matches.Count > 0)
  201. - {
  202. - Current = next;
  203. - return true;
  204. - }
  205. - else
  206. - {
  207. - // We can stop looking as any other child node from this node will be
  208. - // either a literal, a constrained parameter or a parameter.
  209. - // (Catch alls and constrained catch alls will show up as candidate matches).
  210. - continue;
  211. - }
  212. - }
  213. -
  214. - if (next.CatchAlls != null)
  215. - {
  216. - _stack.Push(next.CatchAlls);
  217. - }
  218. -
  219. - if (next.ConstrainedCatchAlls != null)
  220. - {
  221. - _stack.Push(next.ConstrainedCatchAlls);
  222. - }
  223. -
  224. - if (next.Parameters != null)
  225. - {
  226. - _stack.Push(next.Parameters);
  227. - }
  228. -
  229. - if (next.ConstrainedParameters != null)
  230. - {
  231. - _stack.Push(next.ConstrainedParameters);
  232. - }
  233. -
  234. - if (next.Literals.Count > 0)
  235. - {
  236. - Debug.Assert(next.Depth < _tokenizer.Count);
  237. - if (next.Literals.TryGetValue(_tokenizer[next.Depth].Value, out var node))
  238. - {
  239. - _stack.Push(node);
  240. - }
  241. - }
  242. - }
  243. -
  244. - return false;
  245. - }
  246. -
  247. - public void Reset()
  248. - {
  249. - _stack.Clear();
  250. - Current = null;
  251. - }
  252. - }
  253. -
  254. protected override void InitializeServices(IServiceProvider services)
  255. {
  256. _constraintFactory = services.GetRequiredService<IConstraintFactory>();
  257. diff --git a/src/Microsoft.AspNetCore.Routing/Tree/TreeRouter.cs b/src/Microsoft.AspNetCore.Routing/Tree/TreeRouter.cs
  258. index e7ab54341ee..9152dde3bb2 100644
  259. --- a/src/Microsoft.AspNetCore.Routing/Tree/TreeRouter.cs
  260. +++ b/src/Microsoft.AspNetCore.Routing/Tree/TreeRouter.cs
  261. @@ -2,10 +2,7 @@
  262. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  263. using System;
  264. -using System.Collections;
  265. using System.Collections.Generic;
  266. -using System.Diagnostics;
  267. -using System.Text.Encodings.Web;
  268. using System.Threading.Tasks;
  269. using Microsoft.AspNetCore.Dispatcher;
  270. using Microsoft.AspNetCore.Dispatcher.Internal;
  271. @@ -13,7 +10,6 @@ using Microsoft.AspNetCore.Routing.Internal;
  272. using Microsoft.AspNetCore.Routing.Logging;
  273. using Microsoft.AspNetCore.Routing.Template;
  274. using Microsoft.Extensions.Logging;
  275. -using Microsoft.Extensions.ObjectPool;
  276. namespace Microsoft.AspNetCore.Routing.Tree
  277. {
  278. @@ -217,112 +213,9 @@ namespace Microsoft.AspNetCore.Routing.Tree
  279. }
  280. }
  281. - private struct TreeEnumerator : IEnumerator<UrlMatchingNode>
  282. - {
  283. - private readonly Stack<UrlMatchingNode> _stack;
  284. - private readonly PathTokenizer _tokenizer;
  285. -
  286. - public TreeEnumerator(UrlMatchingNode root, PathTokenizer tokenizer)
  287. - {
  288. - _stack = new Stack<UrlMatchingNode>();
  289. - _tokenizer = tokenizer;
  290. - Current = null;
  291. -
  292. - _stack.Push(root);
  293. - }
  294. -
  295. - public UrlMatchingNode Current { get; private set; }
  296. -
  297. - object IEnumerator.Current => Current;
  298. -
  299. - public void Dispose()
  300. - {
  301. - }
  302. -
  303. - public bool MoveNext()
  304. - {
  305. - if (_stack == null)
  306. - {
  307. - return false;
  308. - }
  309. -
  310. - while (_stack.Count > 0)
  311. - {
  312. - var next = _stack.Pop();
  313. -
  314. - // In case of wild card segment, the request path segment length can be greater
  315. - // Example:
  316. - // Template: a/{*path}
  317. - // Request Url: a/b/c/d
  318. - if (next.IsCatchAll && next.Matches.Count > 0)
  319. - {
  320. - Current = next;
  321. - return true;
  322. - }
  323. - // Next template has the same length as the url we are trying to match
  324. - // The only possible matching segments are either our current matches or
  325. - // any catch-all segment after this segment in which the catch all is empty.
  326. - else if (next.Depth == _tokenizer.Count)
  327. - {
  328. - if (next.Matches.Count > 0)
  329. - {
  330. - Current = next;
  331. - return true;
  332. - }
  333. - else
  334. - {
  335. - // We can stop looking as any other child node from this node will be
  336. - // either a literal, a constrained parameter or a parameter.
  337. - // (Catch alls and constrained catch alls will show up as candidate matches).
  338. - continue;
  339. - }
  340. - }
  341. -
  342. - if (next.CatchAlls != null)
  343. - {
  344. - _stack.Push(next.CatchAlls);
  345. - }
  346. -
  347. - if (next.ConstrainedCatchAlls != null)
  348. - {
  349. - _stack.Push(next.ConstrainedCatchAlls);
  350. - }
  351. -
  352. - if (next.Parameters != null)
  353. - {
  354. - _stack.Push(next.Parameters);
  355. - }
  356. -
  357. - if (next.ConstrainedParameters != null)
  358. - {
  359. - _stack.Push(next.ConstrainedParameters);
  360. - }
  361. -
  362. - if (next.Literals.Count > 0)
  363. - {
  364. - UrlMatchingNode node;
  365. - Debug.Assert(next.Depth < _tokenizer.Count);
  366. - if (next.Literals.TryGetValue(_tokenizer[next.Depth].Value, out node))
  367. - {
  368. - _stack.Push(node);
  369. - }
  370. - }
  371. - }
  372. -
  373. - return false;
  374. - }
  375. -
  376. - public void Reset()
  377. - {
  378. - _stack.Clear();
  379. - Current = null;
  380. - }
  381. - }
  382. -
  383. private VirtualPathData GetVirtualPathForNamedRoute(VirtualPathContext context)
  384. {
  385. - OutboundMatch match;
  386. - if (_namedEntries.TryGetValue(context.RouteName, out match))
  387. + if (_namedEntries.TryGetValue(context.RouteName, out var match))
  388. {
  389. var path = GenerateVirtualPath(context, match.Entry, match.TemplateBinder);
  390. if (path != null)