@@ -18,20 +18,12 @@ public class ExpressFilterImpl implements HttpRequest {
1818
1919 private final HttpRequest REQUEST ;
2020 private final String REQUEST_METHOD ;
21-
2221 private final String CONTEXT ;
23- private final String [] CONTEXT_PARAMS ;
24- private final String CONTEXT_REGEX ;
25- private final Pattern CONTEXT_PATTERN ;
2622
2723 public ExpressFilterImpl (String requestMethod , String context , HttpRequest httpRequest ) {
2824 this .REQUEST_METHOD = requestMethod ;
2925 this .REQUEST = httpRequest ;
30-
3126 this .CONTEXT = context ;
32- this .CONTEXT_PARAMS = context .split ("(/)(:|[^:]+|)(:|)" );
33- this .CONTEXT_REGEX = "^\\ Q" + context .replaceAll (":([^/]+)" , "\\ \\ E([^/]+)\\ \\ Q" ) + "\\ E$" ;
34- this .CONTEXT_PATTERN = Pattern .compile (CONTEXT_REGEX );
3527 }
3628
3729 @ Override
@@ -47,22 +39,51 @@ public void handle(Request req, Response res) {
4739 }
4840
4941 // Parse params
42+ HashMap <String , String > params = matchURL (CONTEXT , requestPath );
43+ if (params == null )
44+ return ;
45+
46+ req .setParams (params );
47+ REQUEST .handle (req , res );
48+ }
49+
50+ private static HashMap <String , String > matchURL (String filter , String url ) {
5051 HashMap <String , String > params = new HashMap <>();
51- Matcher matcher = CONTEXT_PATTERN .matcher (requestPath );
52+ StringBuilder key = new StringBuilder ();
53+ StringBuilder val = new StringBuilder ();
54+ char [] uc = url .toCharArray ();
55+ char [] fc = filter .toCharArray ();
56+ int ui = 0 , fi = 0 ;
57+
5258
53- // Match all params
54- if (matcher .find ()) {
59+ for (; fi < fc .length ; fi ++, ui ++) {
5560
56- for (int i = 1 ; i <= matcher .groupCount () && i < CONTEXT_PARAMS .length ; i ++) {
57- String g = matcher .group (i );
58- params .put (CONTEXT_PARAMS [i ], g );
61+ if (fc [fi ] == ':' ) {
62+ key .setLength (0 );
63+ val .setLength (0 );
64+
65+ fi ++;
66+ while (fi < fc .length && fc [fi ] != '/' ) {
67+ key .append (fc [fi ++]);
68+ }
69+
70+ while (ui < uc .length && uc [ui ] != '/' ) {
71+ val .append (uc [ui ++]);
72+ }
73+
74+ params .put (key .toString (), val .toString ());
75+ } else if (fc [fi ] != uc [ui ]) {
76+
77+ // Failed
78+ return null ;
5979 }
80+ }
6081
61- } else {
62- return ;
82+ if ( ui < url . length () || fi < filter . length ()) {
83+ return null ;
6384 }
6485
65- req .setParams (params );
66- REQUEST .handle (req , res );
86+ return params ;
6787 }
88+
6889}
0 commit comments