File: ParsedRoute.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (672 lines) | stat: -rw-r--r-- 32,860 bytes parent folder | download | duplicates (7)
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
namespace System.Web.Routing {
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;

    internal sealed class ParsedRoute {
        public ParsedRoute(IList<PathSegment> pathSegments) {
            Debug.Assert(pathSegments != null);

            PathSegments = pathSegments;
        }

        private IList<PathSegment> PathSegments {
            get;
            set;
        }

        public BoundUrl Bind(RouteValueDictionary currentValues, RouteValueDictionary values, RouteValueDictionary defaultValues, RouteValueDictionary constraints) {
            if (currentValues == null) {
                currentValues = new RouteValueDictionary();
            }
            if (values == null) {
                values = new RouteValueDictionary();
            }
            if (defaultValues == null) {
                defaultValues = new RouteValueDictionary();
            }


            // The set of values we should be using when generating the URL in this route
            RouteValueDictionary acceptedValues = new RouteValueDictionary();

            // Keep track of which new values have been used
            HashSet<string> unusedNewValues = new HashSet<string>(values.Keys, StringComparer.OrdinalIgnoreCase);


            // Step 1: Get the list of values we're going to try to use to match and generate this URL


            // Find out which entries in the URL are valid for the URL we want to generate.
            // If the URL had ordered parameters a="1", b="2", c="3" and the new values
            // specified that b="9", then we need to invalidate everything after it. The new
            // values should then be a="1", b="9", c=<no value>.
            ForEachParameter(PathSegments, delegate(ParameterSubsegment parameterSubsegment) {
                // If it's a parameter subsegment, examine the current value to see if it matches the new value
                string parameterName = parameterSubsegment.ParameterName;

                object newParameterValue;
                bool hasNewParameterValue = values.TryGetValue(parameterName, out newParameterValue);
                if (hasNewParameterValue) {
                    unusedNewValues.Remove(parameterName);
                }

                object currentParameterValue;
                bool hasCurrentParameterValue = currentValues.TryGetValue(parameterName, out currentParameterValue);

                if (hasNewParameterValue && hasCurrentParameterValue) {
                    if (!RoutePartsEqual(currentParameterValue, newParameterValue)) {
                        // Stop copying current values when we find one that doesn't match
                        return false;
                    }
                }

                // If the parameter is a match, add it to the list of values we will use for URL generation
                if (hasNewParameterValue) {
                    if (IsRoutePartNonEmpty(newParameterValue)) {
                        acceptedValues.Add(parameterName, newParameterValue);
                    }
                }
                else {
                    if (hasCurrentParameterValue) {
                        acceptedValues.Add(parameterName, currentParameterValue);
                    }
                }
                return true;
            });

            // Add all remaining new values to the list of values we will use for URL generation
            foreach (var newValue in values) {
                if (IsRoutePartNonEmpty(newValue.Value)) {
                    if (!acceptedValues.ContainsKey(newValue.Key)) {
                        acceptedValues.Add(newValue.Key, newValue.Value);
                    }
                }
            }

            // Add all current values that aren't in the URL at all
            foreach (var currentValue in currentValues) {
                string parameterName = currentValue.Key;
                if (!acceptedValues.ContainsKey(parameterName)) {
                    ParameterSubsegment parameterSubsegment = GetParameterSubsegment(PathSegments, parameterName);
                    if (parameterSubsegment == null) {
                        acceptedValues.Add(parameterName, currentValue.Value);
                    }
                }
            }

            // Add all remaining default values from the route to the list of values we will use for URL generation
            ForEachParameter(PathSegments, delegate(ParameterSubsegment parameterSubsegment) {
                if (!acceptedValues.ContainsKey(parameterSubsegment.ParameterName)) {
                    object defaultValue;
                    if (!IsParameterRequired(parameterSubsegment, defaultValues, out defaultValue)) {
                        // Add the default value only if there isn't already a new value for it and
                        // only if it actually has a default value, which we determine based on whether
                        // the parameter value is required.
                        acceptedValues.Add(parameterSubsegment.ParameterName, defaultValue);
                    }
                }
                return true;
            });


            // All required parameters in this URL must have values from somewhere (i.e. the accepted values)
            bool hasAllRequiredValues = ForEachParameter(PathSegments, delegate(ParameterSubsegment parameterSubsegment) {
                object defaultValue;
                if (IsParameterRequired(parameterSubsegment, defaultValues, out defaultValue)) {
                    if (!acceptedValues.ContainsKey(parameterSubsegment.ParameterName)) {
                        // If the route parameter value is required that means there's
                        // no default value, so if there wasn't a new value for it
                        // either, this route won't match.
                        return false;
                    }
                }
                return true;
            });
            if (!hasAllRequiredValues) {
                return null;
            }

            // All other default values must match if they are explicitly defined in the new values
            RouteValueDictionary otherDefaultValues = new RouteValueDictionary(defaultValues);
            ForEachParameter(PathSegments, delegate(ParameterSubsegment parameterSubsegment) {
                otherDefaultValues.Remove(parameterSubsegment.ParameterName);
                return true;
            });

            foreach (var defaultValue in otherDefaultValues) {
                object value;
                if (values.TryGetValue(defaultValue.Key, out value)) {
                    unusedNewValues.Remove(defaultValue.Key);
                    if (!RoutePartsEqual(value, defaultValue.Value)) {
                        // If there is a non-parameterized value in the route and there is a
                        // new value for it and it doesn't match, this route won't match.
                        return null;
                    }
                }
            }


            // Step 2: If the route is a match generate the appropriate URL

            StringBuilder url = new StringBuilder();
            StringBuilder pendingParts = new StringBuilder();

            bool pendingPartsAreAllSafe = false;
            bool blockAllUrlAppends = false;

            for (int i = 0; i < PathSegments.Count; i++) {
                PathSegment pathSegment = PathSegments[i]; // parsedRouteUrlPart

                if (pathSegment is SeparatorPathSegment) {
                    if (pendingPartsAreAllSafe) {
                        // Accept
                        if (pendingParts.Length > 0) {
                            if (blockAllUrlAppends) {
                                return null;
                            }

                            // Append any pending literals to the URL
                            url.Append(pendingParts.ToString());
                            pendingParts.Length = 0;
                        }
                    }
                    pendingPartsAreAllSafe = false;

                    // Guard against appending multiple separators for empty segements
                    if (pendingParts.Length > 0 && pendingParts[pendingParts.Length - 1] == '/') {
                        // Dev10 676725: Route should not be matched if that causes mismatched tokens
                        // Dev11 86819: We will allow empty matches if all subsequent segments are null
                        if (blockAllUrlAppends) {
                            return null;
                        }

                        // Append any pending literals to the URL(without the trailing slash) and prevent any future appends
                        url.Append(pendingParts.ToString(0, pendingParts.Length - 1));
                        pendingParts.Length = 0;
                        blockAllUrlAppends = true;
                    }
                    else {
                        pendingParts.Append("/");
                    }
                }
                else {
                    ContentPathSegment contentPathSegment = pathSegment as ContentPathSegment;
                    if (contentPathSegment != null) {
                        // Segments are treated as all-or-none. We should never output a partial segment.
                        // If we add any subsegment of this segment to the generated URL, we have to add
                        // the complete match. For example, if the subsegment is "{p1}-{p2}.xml" and we
                        // used a value for {p1}, we have to output the entire segment up to the next "/".
                        // Otherwise we could end up with the partial segment "v1" instead of the entire
                        // segment "v1-v2.xml".
                        bool addedAnySubsegments = false;

                        foreach (PathSubsegment subsegment in contentPathSegment.Subsegments) {
                            LiteralSubsegment literalSubsegment = subsegment as LiteralSubsegment;
                            if (literalSubsegment != null) {
                                // If it's a literal we hold on to it until we are sure we need to add it
                                pendingPartsAreAllSafe = true;
                                pendingParts.Append(UrlEncode(literalSubsegment.Literal));
                            }
                            else {
                                ParameterSubsegment parameterSubsegment = subsegment as ParameterSubsegment;
                                if (parameterSubsegment != null) {
                                    if (pendingPartsAreAllSafe) {
                                        // Accept
                                        if (pendingParts.Length > 0) {
                                            if (blockAllUrlAppends) {
                                                return null;
                                            }

                                            // Append any pending literals to the URL
                                            url.Append(pendingParts.ToString());
                                            pendingParts.Length = 0;

                                            addedAnySubsegments = true;
                                        }
                                    }
                                    pendingPartsAreAllSafe = false;

                                    // If it's a parameter, get its value
                                    object acceptedParameterValue;
                                    bool hasAcceptedParameterValue = acceptedValues.TryGetValue(parameterSubsegment.ParameterName, out acceptedParameterValue);
                                    if (hasAcceptedParameterValue) {
                                        unusedNewValues.Remove(parameterSubsegment.ParameterName);
                                    }

                                    object defaultParameterValue;
                                    defaultValues.TryGetValue(parameterSubsegment.ParameterName, out defaultParameterValue);

                                    if (RoutePartsEqual(acceptedParameterValue, defaultParameterValue)) {
                                        // If the accepted value is the same as the default value, mark it as pending since
                                        // we won't necessarily add it to the URL we generate.
                                        pendingParts.Append(UrlEncode(Convert.ToString(acceptedParameterValue, CultureInfo.InvariantCulture)));
                                    }
                                    else {
                                        if (blockAllUrlAppends) {
                                            return null;
                                        }

                                        // Add the new part to the URL as well as any pending parts
                                        if (pendingParts.Length > 0) {
                                            // Append any pending literals to the URL
                                            url.Append(pendingParts.ToString());
                                            pendingParts.Length = 0;
                                        }
                                        url.Append(UrlEncode(Convert.ToString(acceptedParameterValue, CultureInfo.InvariantCulture)));

                                        addedAnySubsegments = true;
                                    }
                                }
                                else {
                                    Debug.Fail("Invalid path subsegment type");
                                }
                            }
                        }

                        if (addedAnySubsegments) {
                            // See comment above about why we add the pending parts
                            if (pendingParts.Length > 0) {
                                if (blockAllUrlAppends) {
                                    return null;
                                }

                                // Append any pending literals to the URL
                                url.Append(pendingParts.ToString());
                                pendingParts.Length = 0;
                            }
                        }
                    }
                    else {
                        Debug.Fail("Invalid path segment type");
                    }
                }
            }

            if (pendingPartsAreAllSafe) {
                // Accept
                if (pendingParts.Length > 0) {
                    if (blockAllUrlAppends) {
                        return null;
                    }

                    // Append any pending literals to the URL
                    url.Append(pendingParts.ToString());
                }
            }

            // Process constraints keys
            if (constraints != null) {
                // If there are any constraints, mark all the keys as being used so that we don't
                // generate query string items for custom constraints that don't appear as parameters
                // in the URL format.
                foreach (var constraintsItem in constraints) {
                    unusedNewValues.Remove(constraintsItem.Key);
                }
            }


            // Add remaining new values as query string parameters to the URL
            if (unusedNewValues.Count > 0) {
                // Generate the query string
                bool firstParam = true;
                foreach (string unusedNewValue in unusedNewValues) {
                    object value;
                    if (acceptedValues.TryGetValue(unusedNewValue, out value)) {
                        url.Append(firstParam ? '?' : '&');
                        firstParam = false;
                        url.Append(Uri.EscapeDataString(unusedNewValue));
                        url.Append('=');
                        url.Append(Uri.EscapeDataString(Convert.ToString(value, CultureInfo.InvariantCulture)));
                    }
                }
            }

            return new BoundUrl {
                Url = url.ToString(),
                Values = acceptedValues
            };
        }

        private static string EscapeReservedCharacters(Match m) {
            return "%" + Convert.ToUInt16(m.Value[0]).ToString("x2", CultureInfo.InvariantCulture);
        }

        private static bool ForEachParameter(IList<PathSegment> pathSegments, Func<ParameterSubsegment, bool> action) {
            for (int i = 0; i < pathSegments.Count; i++) {
                PathSegment pathSegment = pathSegments[i];

                if (pathSegment is SeparatorPathSegment) {
                    // We only care about parameter subsegments, so skip this
                    continue;
                }
                else {
                    ContentPathSegment contentPathSegment = pathSegment as ContentPathSegment;
                    if (contentPathSegment != null) {
                        foreach (PathSubsegment subsegment in contentPathSegment.Subsegments) {
                            LiteralSubsegment literalSubsegment = subsegment as LiteralSubsegment;
                            if (literalSubsegment != null) {
                                // We only care about parameter subsegments, so skip this
                                continue;
                            }
                            else {
                                ParameterSubsegment parameterSubsegment = subsegment as ParameterSubsegment;
                                if (parameterSubsegment != null) {
                                    if (!action(parameterSubsegment)) {
                                        return false;
                                    }
                                }
                                else {
                                    Debug.Fail("Invalid path subsegment type");
                                }
                            }
                        }
                    }
                    else {
                        Debug.Fail("Invalid path segment type");
                    }
                }
            }

            return true;
        }

        private static ParameterSubsegment GetParameterSubsegment(IList<PathSegment> pathSegments, string parameterName) {
            ParameterSubsegment foundParameterSubsegment = null;

            bool continueProcessing = ForEachParameter(pathSegments, delegate(ParameterSubsegment parameterSubsegment) {
                if (String.Equals(parameterName, parameterSubsegment.ParameterName, StringComparison.OrdinalIgnoreCase)) {
                    foundParameterSubsegment = parameterSubsegment;
                    return false;
                }
                else {
                    return true;
                }
            });

            return foundParameterSubsegment;
        }

        private static bool IsParameterRequired(ParameterSubsegment parameterSubsegment, RouteValueDictionary defaultValues, out object defaultValue) {
            if (parameterSubsegment.IsCatchAll) {
                defaultValue = null;
                return false;
            }

            return !defaultValues.TryGetValue(parameterSubsegment.ParameterName, out defaultValue);
        }

        private static bool IsRoutePartNonEmpty(object routePart) {
            string routePartString = routePart as string;
            if (routePartString != null) {
                return (routePartString.Length > 0);
            }
            return (routePart != null);
        }

        public RouteValueDictionary Match(string virtualPath, RouteValueDictionary defaultValues) {
            IList<string> requestPathSegments = RouteParser.SplitUrlToPathSegmentStrings(virtualPath);

            if (defaultValues == null) {
                defaultValues = new RouteValueDictionary();
            }

            RouteValueDictionary matchedValues = new RouteValueDictionary();

            // This flag gets set once all the data in the URL has been parsed through, but
            // the route we're trying to match against still has more parts. At this point
            // we'll only continue matching separator characters and parameters that have
            // default values.
            bool ranOutOfStuffToParse = false;

            // This value gets set once we start processing a catchall parameter (if there is one
            // at all). Once we set this value we consume all remaining parts of the URL into its
            // parameter value.
            bool usedCatchAllParameter = false;

            for (int i = 0; i < PathSegments.Count; i++) {
                PathSegment pathSegment = PathSegments[i];

                if (requestPathSegments.Count <= i) {
                    ranOutOfStuffToParse = true;
                }

                string requestPathSegment = ranOutOfStuffToParse ? null : requestPathSegments[i];

                if (pathSegment is SeparatorPathSegment) {
                    if (ranOutOfStuffToParse) {
                        // If we're trying to match a separator in the route but there's no more content, that's OK
                    }
                    else {
                        if (!String.Equals(requestPathSegment, "/", StringComparison.Ordinal)) {
                            return null;
                        }
                    }
                }
                else {
                    ContentPathSegment contentPathSegment = pathSegment as ContentPathSegment;
                    if (contentPathSegment != null) {
                        if (contentPathSegment.IsCatchAll) {
                            Debug.Assert(i == (PathSegments.Count - 1), "If we're processing a catch-all, we should be on the last route segment.");
                            MatchCatchAll(contentPathSegment, requestPathSegments.Skip(i), defaultValues, matchedValues);
                            usedCatchAllParameter = true;
                        }
                        else {
                            if (!MatchContentPathSegment(contentPathSegment, requestPathSegment, defaultValues, matchedValues)) {
                                return null;
                            }
                        }
                    }
                    else {
                        Debug.Fail("Invalid path segment type");
                    }
                }
            }

            if (!usedCatchAllParameter) {
                if (PathSegments.Count < requestPathSegments.Count) {
                    // If we've already gone through all the parts defined in the route but the URL
                    // still contains more content, check that the remaining content is all separators.
                    for (int i = PathSegments.Count; i < requestPathSegments.Count; i++) {
                        if (!RouteParser.IsSeparator(requestPathSegments[i])) {
                            return null;
                        }
                    }
                }
            }

            // Copy all remaining default values to the route data
            if (defaultValues != null) {
                foreach (var defaultValue in defaultValues) {
                    if (!matchedValues.ContainsKey(defaultValue.Key)) {
                        matchedValues.Add(defaultValue.Key, defaultValue.Value);
                    }
                }
            }

            return matchedValues;
        }

        private void MatchCatchAll(ContentPathSegment contentPathSegment, IEnumerable<string> remainingRequestSegments, RouteValueDictionary defaultValues, RouteValueDictionary matchedValues) {
            string remainingRequest = String.Join(String.Empty, remainingRequestSegments.ToArray());

            ParameterSubsegment catchAllSegment = contentPathSegment.Subsegments[0] as ParameterSubsegment;

            object catchAllValue;

            if (remainingRequest.Length > 0) {
                catchAllValue = remainingRequest;
            }
            else {
                defaultValues.TryGetValue(catchAllSegment.ParameterName, out catchAllValue);
            }
            matchedValues.Add(catchAllSegment.ParameterName, catchAllValue);
        }

        private bool MatchContentPathSegment(ContentPathSegment routeSegment, string requestPathSegment, RouteValueDictionary defaultValues, RouteValueDictionary matchedValues) {
            if (String.IsNullOrEmpty(requestPathSegment)) {
                // If there's no data to parse, we must have exactly one parameter segment and no other segments - otherwise no match

                if (routeSegment.Subsegments.Count > 1) {
                    return false;
                }

                ParameterSubsegment parameterSubsegment = routeSegment.Subsegments[0] as ParameterSubsegment;
                if (parameterSubsegment == null) {
                    return false;
                }

                // We must have a default value since there's no value in the request URL
                object parameterValue;
                if (defaultValues.TryGetValue(parameterSubsegment.ParameterName, out parameterValue)) {
                    // If there's a default value for this parameter, use that default value
                    matchedValues.Add(parameterSubsegment.ParameterName, parameterValue);
                    return true;
                }
                else {
                    // If there's no default value, this segment doesn't match
                    return false;
                }
            }


            // Find last literal segment and get its last index in the string

            int lastIndex = requestPathSegment.Length;
            int indexOfLastSegmentUsed = routeSegment.Subsegments.Count - 1;

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

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

                ParameterSubsegment parameterSubsegment = routeSegment.Subsegments[indexOfLastSegmentUsed] as ParameterSubsegment;
                if (parameterSubsegment != null) {
                    // Hold on to the parameter so that we can fill it in when we locate the next literal
                    parameterNeedsValue = parameterSubsegment;
                }
                else {
                    LiteralSubsegment literalSubsegment = routeSegment.Subsegments[indexOfLastSegmentUsed] as LiteralSubsegment;
                    if (literalSubsegment != null) {
                        lastLiteral = literalSubsegment;

                        int startIndex = lastIndex - 1;
                        // 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 = requestPathSegment.LastIndexOf(literalSubsegment.Literal, startIndex, 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 URL.
                        // Without this check if your route had "/Foo/" we'd match the request URL "/somethingFoo/".
                        // This check is related to the check we do at the very end of this function.
                        if (indexOfLastSegmentUsed == (routeSegment.Subsegments.Count - 1)) {
                            if ((indexOfLiteral + literalSubsegment.Literal.Length) != requestPathSegment.Length) {
                                return false;
                            }
                        }

                        newLastIndex = indexOfLiteral;
                    }
                    else {
                        Debug.Fail("Invalid path segment type");
                    }
                }

                if ((parameterNeedsValue != null) && (((lastLiteral != null) && (parameterSubsegment == null)) || (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.Fail("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) && (parameterSubsegment != null)) {
                            parameterStartIndex = 0;
                            parameterTextLength = lastIndex;
                        }
                        else {
                            parameterStartIndex = newLastIndex + lastLiteral.Literal.Length;
                            parameterTextLength = lastIndex - parameterStartIndex;
                        }
                    }

                    string parameterValueString = requestPathSegment.Substring(parameterStartIndex, parameterTextLength);

                    if (String.IsNullOrEmpty(parameterValueString)) {
                        // 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
                        matchedValues.Add(parameterNeedsValue.ParameterName, parameterValueString);
                    }

                    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 URL "somethingFoo". Thus we have to check that we parsed the *entire*
            // request URL in order for it to be a match.
            // This check is related to the check we do earlier in this function for LiteralSubsegments.
            return (lastIndex == 0) || (routeSegment.Subsegments[0] is ParameterSubsegment);
        }

        private static bool RoutePartsEqual(object a, object b) {
            string sa = a as string;
            string sb = b as string;
            if (sa != null && sb != null) {
                // For strings do a case-insensitive comparison
                return String.Equals(sa, sb, StringComparison.OrdinalIgnoreCase);
            }
            else {
                if (a != null && b != null) {
                    // Explicitly call .Equals() in case it is overridden in the type
                    return a.Equals(b);
                }
                else {
                    // At least one of them is null. Return true if they both are
                    return a == b;
                }
            }
        }

        // Dev10 601636 Work around Uri.EscapeUriString not encoding #,&
        private static string UrlEncode(string str) {
            string escape = Uri.EscapeUriString(str);
            return Regex.Replace(escape, "([#;?:@&=+$,])", new MatchEvaluator(EscapeReservedCharacters));
        }
    }
}