27
27
import java .util .Deque ;
28
28
import java .util .Map ;
29
29
import java .util .Optional ;
30
+ import java .util .regex .Pattern ;
30
31
import java .util .stream .Collectors ;
31
32
32
33
import org .bson .BsonArray ;
@@ -491,15 +492,21 @@ private String unmapUri(String mappedUri) {
491
492
}
492
493
}
493
494
495
+ Pattern SPECIAL_REGEX_CHARS = Pattern .compile ("[{}()\\ [\\ ].+*?^$\\ \\ |]" );
496
+
497
+ String escapeSpecialRegexChars (String str ) {
498
+ return SPECIAL_REGEX_CHARS .matcher (str ).replaceAll ("\\ \\ $0" );
499
+ }
500
+
494
501
private String unmapPathUri (String mappedUri ) {
495
502
var ret = URLUtils .removeTrailingSlashes (mappedUri );
496
503
497
504
if (whatUri .equals ("*" )) {
498
505
if (!this .whereUri .equals (SLASH )) {
499
- ret = ret .replaceFirst ("^" + this .whereUri , "" );
506
+ ret = ret .replaceFirst ("^" + escapeSpecialRegexChars ( this .whereUri ) , "" );
500
507
}
501
508
} else if (!this .whereUri .equals (SLASH )) {
502
- ret = URLUtils .removeTrailingSlashes (ret .replaceFirst ("^" + this .whereUri , this .whatUri ));
509
+ ret = URLUtils .removeTrailingSlashes (ret .replaceFirst ("^" + escapeSpecialRegexChars ( this .whereUri ) , this .whatUri ));
503
510
} else {
504
511
ret = URLUtils .removeTrailingSlashes (URLUtils .removeTrailingSlashes (this .whatUri ) + ret );
505
512
}
@@ -518,10 +525,11 @@ private String unmapPathTemplateUri(String mappedUri) {
518
525
// now replace mappedUri with resolved path template
519
526
if (replacedWhatUri .equals ("*" )) {
520
527
if (!this .whereUri .equals (SLASH )) {
521
- ret = ret .replaceFirst ("^" + rewriteUri , "" );
528
+ ret = ret .replaceFirst ("^" + escapeSpecialRegexChars ( rewriteUri ) , "" );
522
529
}
523
530
} else if (!this .whereUri .equals (SLASH )) {
524
- ret = URLUtils .removeTrailingSlashes (ret .replaceFirst ("^" + rewriteUri , replacedWhatUri ));
531
+ var x = rewriteUri ;
532
+ ret = URLUtils .removeTrailingSlashes (ret .replaceFirst ("^" + escapeSpecialRegexChars (rewriteUri ), replacedWhatUri ));
525
533
} else {
526
534
ret = URLUtils .removeTrailingSlashes (URLUtils .removeTrailingSlashes (replacedWhatUri ) + ret );
527
535
}
@@ -553,7 +561,7 @@ private String mapPathUri(String unmappedUri) {
553
561
return this .whereUri + unmappedUri ;
554
562
}
555
563
} else {
556
- ret = URLUtils .removeTrailingSlashes (ret .replaceFirst ("^" + this .whatUri , this .whereUri ));
564
+ ret = URLUtils .removeTrailingSlashes (ret .replaceFirst ("^" + escapeSpecialRegexChars ( this .whatUri ) , this .whereUri ));
557
565
}
558
566
559
567
if (ret .isEmpty ()) {
@@ -576,7 +584,7 @@ private String mapPathTemplateUri(String unmappedUri) {
576
584
return rewriteUri + unmappedUri ;
577
585
}
578
586
} else {
579
- ret = URLUtils .removeTrailingSlashes (ret .replaceFirst ("^" + replacedWhatUri , rewriteUri ));
587
+ ret = URLUtils .removeTrailingSlashes (ret .replaceFirst ("^" + escapeSpecialRegexChars ( replacedWhatUri ) , rewriteUri ));
580
588
}
581
589
582
590
return ret .isEmpty () ? SLASH : ret ;
0 commit comments