Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RoutePatternMatcher.cs « Patterns « src « Routing « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5a8c9c4b039a7fe6e40dfedc8e9db73cc767b135 (plain)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// 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;
using System.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing.Patterns;
using Microsoft.Extensions.Primitives;

namespace Microsoft.AspNetCore.Routing;

internal class RoutePatternMatcher
{
    // Perf: This is a cache to avoid looking things up in 'Defaults' each request.
    private readonly bool[] _hasDefaultValue;
    private readonly object[] _defaultValues;

    public RoutePatternMatcher(
        RoutePattern pattern,
        RouteValueDictionary defaults)
    {
        if (pattern == null)
        {
            throw new ArgumentNullException(nameof(pattern));
        }

        RoutePattern = pattern;
        Defaults = defaults ?? new RouteValueDictionary();

        // Perf: cache the default value for each parameter (other than complex segments).
        _hasDefaultValue = new bool[RoutePattern.PathSegments.Count];
        _defaultValues = new object[RoutePattern.PathSegments.Count];

        for (var i = 0; i < RoutePattern.PathSegments.Count; i++)
        {
            var segment = RoutePattern.PathSegments[i];
            if (!segment.IsSimple)
            {
                continue;
            }

            var part = segment.Parts[0];
            if (!part.IsParameter)
            {
                continue;
            }

            var parameter = (RoutePatternParameterPart)part;
            if (Defaults.TryGetValue(parameter.Name, out var value))
            {
                _hasDefaultValue[i] = true;
                _defaultValues[i] = value;
            }
        }
    }

    public RouteValueDictionary Defaults { get; }

    public RoutePattern RoutePattern { get; }

    public bool TryMatch(PathString path, RouteValueDictionary values)
    {
        if (values == null)
        {
            throw new ArgumentNullException(nameof(values));
        }

        var i = 0;
        var pathTokenizer = new PathTokenizer(path);

        // Perf: We do a traversal of the request-segments + route-segments twice.
        //
        // For most segment-types, we only really need to any work on one of the two passes.
        //
        // On the first pass, we're just looking to see if there's anything that would disqualify us from matching.
        // The most common case would be a literal segment that doesn't match.
        //
        // On the second pass, we're almost certainly going to match the URL, so go ahead and allocate the 'values'
        // and start capturing strings.
        foreach (var stringSegment in pathTokenizer)
        {
            if (stringSegment.Length == 0)
            {
                return false;
            }

            var pathSegment = i >= RoutePattern.PathSegments.Count ? null : RoutePattern.PathSegments[i];
            if (pathSegment == null && stringSegment.Length > 0)
            {
                // If pathSegment is null, then we're out of route segments. All we can match is the empty
                // string.
                return false;
            }
            else if (pathSegment.IsSimple && pathSegment.Parts[0] is RoutePatternParameterPart parameter && parameter.IsCatchAll)
            {
                // Nothing to validate for a catch-all - it can match any string, including the empty string.
                //
                // Also, a catch-all has to be the last part, so we're done.
                break;
            }
            if (!TryMatchLiterals(i++, stringSegment, pathSegment))
            {
                return false;
            }
        }

        for (; i < RoutePattern.PathSegments.Count; i++)
        {
            // We've matched the request path so far, but still have remaining route segments. These need
            // to be all single-part parameter segments with default values or else they won't match.
            var pathSegment = RoutePattern.PathSegments[i];
            Debug.Assert(pathSegment != null);

            if (!pathSegment.IsSimple)
            {
                // If the segment is a complex segment, it MUST contain literals, and we've parsed the full
                // path so far, so it can't match.
                return false;
            }

            var part = pathSegment.Parts[0];
            if (part.IsLiteral || part.IsSeparator)
            {
                // If the segment is a simple literal - which need the URL to provide a value, so we don't match.
                return false;
            }

            var parameter = (RoutePatternParameterPart)part;
            if (parameter.IsCatchAll)
            {
                // Nothing to validate for a catch-all - it can match any string, including the empty string.
                //
                // Also, a catch-all has to be the last part, so we're done.
                break;
            }

            // If we get here, this is a simple segment with a parameter. We need it to be optional, or for the
            // defaults to have a value.
            if (!_hasDefaultValue[i] && !parameter.IsOptional)
            {
                // There's no default for this (non-optional) parameter so it can't match.
                return false;
            }
        }

        // At this point we've very likely got a match, so start capturing values for real.
        i = 0;
        foreach (var requestSegment in pathTokenizer)
        {
            var pathSegment = RoutePattern.PathSegments[i++];
            if (SavePathSegmentsAsValues(i, values, requestSegment, pathSegment))
            {
                break;
            }
            if (!pathSegment.IsSimple)
            {
                if (!MatchComplexSegment(pathSegment, requestSegment.AsSpan(), values))
                {
                    return false;
                }
            }
        }

        for (; i < RoutePattern.PathSegments.Count; i++)
        {
            // We've matched the request path so far, but still have remaining route segments. We already know these
            // are simple parameters that either have a default, or don't need to produce a value.
            var pathSegment = RoutePattern.PathSegments[i];
            Debug.Assert(pathSegment != null);
            Debug.Assert(pathSegment.IsSimple);

            var part = pathSegment.Parts[0];
            Debug.Assert(part.IsParameter);

            // It's ok for a catch-all to produce a null value
            if (part is RoutePatternParameterPart parameter && (parameter.IsCatchAll || _hasDefaultValue[i]))
            {
                // Don't replace an existing value with a null.
                var defaultValue = _defaultValues[i];
                if (defaultValue != null || !values.ContainsKey(parameter.Name))
                {
                    values[parameter.Name] = defaultValue;
                }
            }
        }

        // Copy all remaining default values to the route data
        foreach (var kvp in Defaults)
        {
#if RVD_TryAdd
                values.TryAdd(kvp.Key, kvp.Value);
#else
            if (!values.ContainsKey(kvp.Key))
            {
                values.Add(kvp.Key, kvp.Value);
            }
#endif
        }

        return true;
    }

    private bool TryMatchLiterals(int index, StringSegment stringSegment, RoutePatternPathSegment pathSegment)
    {
        if (pathSegment.IsSimple && !pathSegment.Parts[0].IsParameter)
        {
            // This is a literal segment, so we need to match the text, or the route isn't a match.
            if (pathSegment.Parts[0].IsLiteral)
            {
                var part = (RoutePatternLiteralPart)pathSegment.Parts[0];

                if (!stringSegment.Equals(part.Content, StringComparison.OrdinalIgnoreCase))
                {
                    return false;
                }
            }
            else
            {
                var part = (RoutePatternSeparatorPart)pathSegment.Parts[0];

                if (!stringSegment.Equals(part.Content, StringComparison.OrdinalIgnoreCase))
                {
                    return false;
                }
            }
        }
        else if (pathSegment.IsSimple && pathSegment.Parts[0].IsParameter)
        {
            // For a parameter, validate that it's a has some length, or we have a default, or it's optional.
            var part = (RoutePatternParameterPart)pathSegment.Parts[0];
            if (stringSegment.Length == 0 &&
                !_hasDefaultValue[index] &&
                !part.IsOptional)
            {
                // There's no value for this parameter, the route can't match.
                return false;
            }
        }
        else
        {
            Debug.Assert(!pathSegment.IsSimple);
            // Don't attempt to validate a complex segment at this point other than being non-empty,
            // do it in the second pass.
        }
        return true;
    }

    private bool SavePathSegmentsAsValues(int index, RouteValueDictionary values, StringSegment requestSegment, RoutePatternPathSegment pathSegment)
    {
        if (pathSegment.IsSimple && pathSegment.Parts[0] is RoutePatternParameterPart parameter && parameter.IsCatchAll)
        {
            // A catch-all captures til the end of the string.
            var captured = requestSegment.Buffer.Substring(requestSegment.Offset);
            if (captured.Length > 0)
            {
                values[parameter.Name] = captured;
            }
            else
            {
                // It's ok for a catch-all to produce a null value, so we don't check _hasDefaultValue.
                values[parameter.Name] = _defaultValues[index];
            }

            // A catch-all has to be the last part, so we're done.
            return true;
        }
        else if (pathSegment.IsSimple && pathSegment.Parts[0].IsParameter)
        {
            // A simple parameter captures the whole segment, or a default value if nothing was
            // provided.
            parameter = (RoutePatternParameterPart)pathSegment.Parts[0];
            if (requestSegment.Length > 0)
            {
                values[parameter.Name] = requestSegment.ToString();
            }
            else
            {
                if (_hasDefaultValue[index])
                {
                    values[parameter.Name] = _defaultValues[index];
                }
            }
        }
        return false;
    }

    internal static bool MatchComplexSegment(
        RoutePatternPathSegment routeSegment,
        ReadOnlySpan<char> requestSegment,
        RouteValueDictionary values)
    {
        var indexOfLastSegment = routeSegment.Parts.Count - 1;

        // We match the request to the template starting at the rightmost parameter
        // If the last segment of template is optional, then request can match the
        // template with or without the last parameter. So we start with regular matching,
        // but if it doesn't match, we start with next to last parameter. Example:
        // Template: {p1}/{p2}.{p3?}. If the request is one/two.three it will match right away
        // giving p3 value of three. But if the request is one/two, we start matching from the
        // rightmost giving p3 the value of two, then we end up not matching the segment.
        // In this case we start again from p2 to match the request and we succeed giving
        // the value two to p2
        if (routeSegment.Parts[indexOfLastSegment] is RoutePatternParameterPart parameter && parameter.IsOptional &&
            routeSegment.Parts[indexOfLastSegment - 1].IsSeparator)
        {
            if (MatchComplexSegmentCore(routeSegment, requestSegment, values, indexOfLastSegment))
            {
                return true;
            }
            else
            {
                var separator = (RoutePatternSeparatorPart)routeSegment.Parts[indexOfLastSegment - 1];
                if (requestSegment.EndsWith(
                separator.Content,
                StringComparison.OrdinalIgnoreCase))
                    return false;

                return MatchComplexSegmentCore(
                    routeSegment,
                    requestSegment,
                    values,
                    indexOfLastSegment - 2);
            }
        }
        else
        {
            return MatchComplexSegmentCore(routeSegment, requestSegment, values, indexOfLastSegment);
        }
    }

    private static bool MatchComplexSegmentCore(
        RoutePatternPathSegment routeSegment,
        ReadOnlySpan<char> requestSegment,
        RouteValueDictionary values,
        int indexOfLastSegmentUsed)
    {
        Debug.Assert(routeSegment != null);
        Debug.Assert(routeSegment.Parts.Count > 1);

        // Find last literal segment and get its last index in the string
        var lastIndex = requestSegment.Length;

        RoutePatternParameterPart parameterNeedsValue = null; // Keeps track of a parameter segment that is pending a value
        RoutePatternPart lastLiteral = null; // Keeps track of the left-most literal we've encountered

        var outValues = new RouteValueDictionary();

        while (indexOfLastSegmentUsed >= 0)
        {
            var newLastIndex = lastIndex;

            var part = routeSegment.Parts[indexOfLastSegmentUsed];
            if (part.IsParameter)
            {
                // Hold on to the parameter so that we can fill it in when we locate the next literal
                parameterNeedsValue = (RoutePatternParameterPart)part;
            }
            else
            {
                Debug.Assert(part.IsLiteral || part.IsSeparator);
                lastLiteral = part;

                var startIndex = lastIndex;
                // If we have a pending parameter subsegment, we must leave at least one character for that
                if (parameterNeedsValue != null)
                {
                    startIndex--;
                }

                if (startIndex == 0)
                {
                    return false;
                }

                int indexOfLiteral;
                if (part.IsLiteral)
                {
                    var literal = (RoutePatternLiteralPart)part;
                    indexOfLiteral = requestSegment.Slice(0, startIndex).LastIndexOf(
                    literal.Content,
                    StringComparison.OrdinalIgnoreCase);
                }
                else
                {
                    var literal = (RoutePatternSeparatorPart)part;
                    indexOfLiteral = requestSegment.Slice(0, startIndex).LastIndexOf(
                    literal.Content,
                    StringComparison.OrdinalIgnoreCase);
                }

                if (indexOfLiteral == -1)
                {
                    // If we couldn't find this literal index, this segment cannot match
                    return false;
                }

                // If the first subsegment is a literal, it must match at the right-most extent of the request URI.
                // Without this check if your route had "/Foo/" we'd match the request URI "/somethingFoo/".
                // This check is related to the check we do at the very end of this function.
                if (indexOfLastSegmentUsed == (routeSegment.Parts.Count - 1))
                {
                    if (part is RoutePatternLiteralPart literal && ((indexOfLiteral + literal.Content.Length) != requestSegment.Length))
                    {
                        return false;
                    }
                    else if (part is RoutePatternSeparatorPart separator && ((indexOfLiteral + separator.Content.Length) != requestSegment.Length))
                    {
                        return false;
                    }
                }

                newLastIndex = indexOfLiteral;
            }

            if ((parameterNeedsValue != null) &&
                (((lastLiteral != null) && !part.IsParameter) || (indexOfLastSegmentUsed == 0)))
            {
                // If we have a pending parameter that needs a value, grab that value

                int parameterStartIndex;
                int parameterTextLength;

                if (lastLiteral == null)
                {
                    if (indexOfLastSegmentUsed == 0)
                    {
                        parameterStartIndex = 0;
                    }
                    else
                    {
                        parameterStartIndex = newLastIndex;
                        Debug.Assert(false, "indexOfLastSegementUsed should always be 0 from the check above");
                    }
                    parameterTextLength = lastIndex;
                }
                else
                {
                    // If we're getting a value for a parameter that is somewhere in the middle of the segment
                    if ((indexOfLastSegmentUsed == 0) && (part.IsParameter))
                    {
                        parameterStartIndex = 0;
                        parameterTextLength = lastIndex;
                    }
                    else
                    {
                        if (lastLiteral.IsLiteral)
                        {
                            var literal = (RoutePatternLiteralPart)lastLiteral;
                            parameterStartIndex = newLastIndex + literal.Content.Length;
                        }
                        else
                        {
                            var separator = (RoutePatternSeparatorPart)lastLiteral;
                            parameterStartIndex = newLastIndex + separator.Content.Length;
                        }
                        parameterTextLength = lastIndex - parameterStartIndex;
                    }
                }

                var parameterValueSpan = requestSegment.Slice(parameterStartIndex, parameterTextLength);

                if (parameterValueSpan.Length == 0)
                {
                    // If we're here that means we have a segment that contains multiple sub-segments.
                    // For these segments all parameters must have non-empty values. If the parameter
                    // has an empty value it's not a match.
                    return false;

                }
                else
                {
                    // If there's a value in the segment for this parameter, use the subsegment value
                    outValues.Add(parameterNeedsValue.Name, new string(parameterValueSpan));
                }

                parameterNeedsValue = null;
                lastLiteral = null;
            }

            lastIndex = newLastIndex;
            indexOfLastSegmentUsed--;
        }

        // If the last subsegment is a parameter, it's OK that we didn't parse all the way to the left extent of
        // the string since the parameter will have consumed all the remaining text anyway. If the last subsegment
        // is a literal then we *must* have consumed the entire text in that literal. Otherwise we end up matching
        // the route "Foo" to the request URI "somethingFoo". Thus we have to check that we parsed the *entire*
        // request URI in order for it to be a match.
        // This check is related to the check we do earlier in this function for LiteralSubsegments.
        if (lastIndex == 0 || routeSegment.Parts[0].IsParameter)
        {
            foreach (var item in outValues)
            {
                values[item.Key] = item.Value;
            }

            return true;
        }

        return false;
    }
}