-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
TreeRouteBuilder.cs
312 lines (285 loc) · 10.1 KB
/
TreeRouteBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
using System.Linq;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Routing.Template;
using Microsoft.Extensions.Logging;
using System.Data;
#if !COMPONENTS
using Microsoft.Extensions.ObjectPool;
#else
using System.Diagnostics.CodeAnalysis;
using static Microsoft.AspNetCore.Internal.LinkerFlags;
using Microsoft.AspNetCore.Routing.Patterns;
using Microsoft.AspNetCore.Components.Routing;
#endif
namespace Microsoft.AspNetCore.Routing.Tree;
#if !COMPONENTS
/// <summary>
/// Builder for <see cref="TreeRouter"/> instances.
/// </summary>
public class TreeRouteBuilder
#else
internal class TreeRouteBuilder
#endif
{
private readonly ILogger _logger;
private readonly ILogger _constraintLogger;
private readonly UrlEncoder _urlEncoder;
#if !COMPONENTS
private readonly ObjectPool<UriBuildingContext> _objectPool;
#endif
private readonly IInlineConstraintResolver _constraintResolver;
#if !COMPONENTS
/// <summary>
/// Initializes a new instance of <see cref="TreeRouteBuilder"/>.
/// </summary>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
/// <param name="objectPool">The <see cref="ObjectPool{UrlBuildingContext}"/>.</param>
/// <param name="constraintResolver">The <see cref="IInlineConstraintResolver"/>.</param>
#endif
internal TreeRouteBuilder(
ILoggerFactory loggerFactory,
#if !COMPONENTS
ObjectPool<UriBuildingContext> objectPool,
#endif
IInlineConstraintResolver constraintResolver)
{
ArgumentNullException.ThrowIfNull(loggerFactory);
#if !COMPONENTS
ArgumentNullException.ThrowIfNull(objectPool);
#endif
ArgumentNullException.ThrowIfNull(constraintResolver);
_urlEncoder = UrlEncoder.Default;
#if !COMPONENTS
_objectPool = objectPool;
#endif
_constraintResolver = constraintResolver;
_logger = loggerFactory.CreateLogger<TreeRouter>();
_constraintLogger = loggerFactory.CreateLogger(typeof(RouteConstraintMatcher).FullName);
}
#if !COMPONENTS
/// <summary>
/// Adds a new inbound route to the <see cref="TreeRouter"/>.
/// </summary>
/// <param name="handler">The <see cref="IRouter"/> for handling the route.</param>
/// <param name="routeTemplate">The <see cref="RouteTemplate"/> of the route.</param>
/// <param name="routeName">The route name.</param>
/// <param name="order">The route order.</param>
/// <returns>The <see cref="InboundRouteEntry"/>.</returns>
#endif
public InboundRouteEntry MapInbound(
#if !COMPONENTS
IRouter handler,
RouteTemplate routeTemplate,
string routeName,
int order)
#else
[DynamicallyAccessedMembers(Component)] Type handler,
RoutePattern routeTemplate,
List<string> unusedParameterNames)
#endif
{
ArgumentNullException.ThrowIfNull(handler);
ArgumentNullException.ThrowIfNull(routeTemplate);
var entry = new InboundRouteEntry()
{
Handler = handler,
#if !COMPONENTS
Order = order,
#endif
Precedence = RoutePrecedence.ComputeInbound(routeTemplate),
#if !COMPONENTS
RouteName = routeName,
RouteTemplate = routeTemplate,
#else
RoutePattern = routeTemplate,
UnusedRouteParameterNames = unusedParameterNames
#endif
};
#if !COMPONENTS
var constraintBuilder = new RouteConstraintBuilder(_constraintResolver, routeTemplate.TemplateText);
#else
var constraintBuilder = new RouteConstraintBuilder(_constraintResolver, routeTemplate.RawText);
#endif
foreach (var parameter in routeTemplate.Parameters)
{
#if !COMPONENTS
if (parameter.InlineConstraints != null)
{
if (parameter.IsOptional)
{
constraintBuilder.SetOptional(parameter.Name);
}
foreach (var constraint in parameter.InlineConstraints)
{
constraintBuilder.AddResolvedConstraint(parameter.Name, constraint.Constraint);
}
}
#else
if (parameter.ParameterPolicies != null)
{
if (parameter.IsOptional)
{
constraintBuilder.SetOptional(parameter.Name);
}
foreach (var policy in parameter.ParameterPolicies)
{
constraintBuilder.AddResolvedConstraint(parameter.Name, policy.Content);
}
}
#endif
}
entry.Constraints = constraintBuilder.Build();
entry.Defaults = new RouteValueDictionary();
#if !COMPONENTS
foreach (var parameter in entry.RouteTemplate.Parameters)
{
if (parameter.DefaultValue != null)
{
entry.Defaults.Add(parameter.Name, parameter.DefaultValue);
}
}
#else
foreach (var parameter in entry.RoutePattern.Parameters)
{
if (parameter.Default != null)
{
entry.Defaults.Add(parameter.Name, parameter.Default);
}
}
#endif
InboundEntries.Add(entry);
return entry;
}
#if !COMPONENTS
/// <summary>
/// Adds a new outbound route to the <see cref="TreeRouter"/>.
/// </summary>
/// <param name="handler">The <see cref="IRouter"/> for handling the link generation.</param>
/// <param name="routeTemplate">The <see cref="RouteTemplate"/> of the route.</param>
/// <param name="requiredLinkValues">The <see cref="RouteValueDictionary"/> containing the route values.</param>
/// <param name="routeName">The route name.</param>
/// <param name="order">The route order.</param>
/// <returns>The <see cref="OutboundRouteEntry"/>.</returns>
public OutboundRouteEntry MapOutbound(
IRouter handler,
RouteTemplate routeTemplate,
RouteValueDictionary requiredLinkValues,
string routeName,
int order)
{
ArgumentNullException.ThrowIfNull(handler);
ArgumentNullException.ThrowIfNull(routeTemplate);
ArgumentNullException.ThrowIfNull(requiredLinkValues);
var entry = new OutboundRouteEntry()
{
Handler = handler,
Order = order,
Precedence = RoutePrecedence.ComputeOutbound(routeTemplate),
RequiredLinkValues = requiredLinkValues,
RouteName = routeName,
RouteTemplate = routeTemplate,
};
var constraintBuilder = new RouteConstraintBuilder(_constraintResolver, routeTemplate.TemplateText);
foreach (var parameter in routeTemplate.Parameters)
{
if (parameter.InlineConstraints != null)
{
if (parameter.IsOptional)
{
constraintBuilder.SetOptional(parameter.Name);
}
foreach (var constraint in parameter.InlineConstraints)
{
constraintBuilder.AddResolvedConstraint(parameter.Name, constraint.Constraint);
}
}
}
entry.Constraints = constraintBuilder.Build();
entry.Defaults = new RouteValueDictionary();
foreach (var parameter in entry.RouteTemplate.Parameters)
{
if (parameter.DefaultValue != null)
{
entry.Defaults.Add(parameter.Name, parameter.DefaultValue);
}
}
OutboundEntries.Add(entry);
return entry;
}
/// <summary>
/// Gets the list of <see cref="InboundRouteEntry"/>.
/// </summary>
#endif
public IList<InboundRouteEntry> InboundEntries { get; } = new List<InboundRouteEntry>();
#if !COMPONENTS
/// <summary>
/// Gets the list of <see cref="OutboundRouteEntry"/>.
/// </summary>
public IList<OutboundRouteEntry> OutboundEntries { get; } = new List<OutboundRouteEntry>();
/// <summary>
/// Builds a <see cref="TreeRouter"/> with the <see cref="InboundEntries"/>
/// and <see cref="OutboundEntries"/> defined in this <see cref="TreeRouteBuilder"/>.
/// </summary>
/// <returns>The <see cref="TreeRouter"/>.</returns>
#endif
public TreeRouter Build()
{
return Build(version: 0);
}
#if !COMPONENTS
/// <summary>
/// Builds a <see cref="TreeRouter"/> with the <see cref="InboundEntries"/>
/// and <see cref="OutboundEntries"/> defined in this <see cref="TreeRouteBuilder"/>.
/// </summary>
/// <param name="version">The version of the <see cref="TreeRouter"/>.</param>
/// <returns>The <see cref="TreeRouter"/>.</returns>
#endif
public TreeRouter Build(int version)
{
// Tree route builder builds a tree for each of the different route orders defined by
// the user. When a route needs to be matched, the matching algorithm in tree router
// just iterates over the trees in ascending order when it tries to match the route.
var trees = new Dictionary<int, UrlMatchingTree>();
foreach (var entry in InboundEntries)
{
if (!trees.TryGetValue(entry.Order, out var tree))
{
tree = new UrlMatchingTree(entry.Order);
trees.Add(entry.Order, tree);
}
tree.AddEntry(entry);
}
#if !COMPONENTS
return new TreeRouter(
trees.Values.OrderBy(tree => tree.Order).ToArray(),
OutboundEntries,
_urlEncoder,
_objectPool,
_logger,
_constraintLogger,
version);
#else
return new TreeRouter(
trees.Values.OrderBy(tree => tree.Order).ToArray(),
_urlEncoder,
_logger,
version);
#endif
}
#if !COMPONENTS
/// <summary>
/// Removes all <see cref="InboundEntries"/> and <see cref="OutboundEntries"/> from this
/// <see cref="TreeRouteBuilder"/>.
/// </summary>
#endif
public void Clear()
{
InboundEntries.Clear();
#if !COMPONENTS
OutboundEntries.Clear();
#endif
}
}