@@ -10,6 +10,11 @@ use crate::{IntoPattern, Resource, ResourcePath};
10
10
11
11
const MAX_DYNAMIC_SEGMENTS : usize = 16 ;
12
12
13
+ /// Regex flags to allow '.' in regex to match '\n'
14
+ ///
15
+ /// See the docs under: https://docs.rs/regex/1.5.4/regex/#grouping-and-flags
16
+ const REGEX_FLAGS : & str = "(?s-m)" ;
17
+
13
18
/// ResourceDef describes an entry in resources table
14
19
///
15
20
/// Resource definition can contain only 16 dynamic segments
@@ -571,7 +576,7 @@ impl ResourceDef {
571
576
) -> ( String , Vec < PatternElement > , bool , usize ) {
572
577
if pattern. find ( '{' ) . is_none ( ) {
573
578
return if let Some ( path) = pattern. strip_suffix ( '*' ) {
574
- let re = String :: from ( "^" ) + path + " (.*)";
579
+ let re = format ! ( "{}^{} (.*)", REGEX_FLAGS , path ) ;
575
580
( re, vec ! [ PatternElement :: Str ( String :: from( path) ) ] , true , 0 )
576
581
} else {
577
582
(
@@ -584,7 +589,7 @@ impl ResourceDef {
584
589
}
585
590
586
591
let mut elements = Vec :: new ( ) ;
587
- let mut re = String :: from ( "^" ) ;
592
+ let mut re = format ! ( "{}^" , REGEX_FLAGS ) ;
588
593
let mut dyn_elements = 0 ;
589
594
590
595
while let Some ( idx) = pattern. find ( '{' ) {
@@ -817,6 +822,32 @@ mod tests {
817
822
assert ! ( re. is_match( "/user/2345/sdg" ) ) ;
818
823
}
819
824
825
+ #[ test]
826
+ fn test_newline ( ) {
827
+ let re = ResourceDef :: new ( "/user/a\n b" ) ;
828
+ assert ! ( re. is_match( "/user/a\n b" ) ) ;
829
+ assert ! ( !re. is_match( "/user/a\n b/profile" ) ) ;
830
+
831
+ let re = ResourceDef :: new ( "/a{x}b/test/a{y}b" ) ;
832
+ let mut path = Path :: new ( "/a\n b/test/a\n b" ) ;
833
+ assert ! ( re. match_path( & mut path) ) ;
834
+ assert_eq ! ( path. get( "x" ) . unwrap( ) , "\n " ) ;
835
+ assert_eq ! ( path. get( "y" ) . unwrap( ) , "\n " ) ;
836
+
837
+ let re = ResourceDef :: new ( "/user/*" ) ;
838
+ assert ! ( re. is_match( "/user/a\n b/" ) ) ;
839
+
840
+ let re = ResourceDef :: new ( "/user/{id}*" ) ;
841
+ let mut path = Path :: new ( "/user/a\n b/a\n b" ) ;
842
+ assert ! ( re. match_path( & mut path) ) ;
843
+ assert_eq ! ( path. get( "id" ) . unwrap( ) , "a\n b/a\n b" ) ;
844
+
845
+ let re = ResourceDef :: new ( "/user/{id:.*}" ) ;
846
+ let mut path = Path :: new ( "/user/a\n b/a\n b" ) ;
847
+ assert ! ( re. match_path( & mut path) ) ;
848
+ assert_eq ! ( path. get( "id" ) . unwrap( ) , "a\n b/a\n b" ) ;
849
+ }
850
+
820
851
#[ cfg( feature = "http" ) ]
821
852
#[ test]
822
853
fn test_parse_urlencoded_param ( ) {
0 commit comments