Skip to content

Commit c54f6d1

Browse files
committed
Improve ExpressFilterIml
Now the average response time is by ~30ms instead of 300ms!
1 parent 51f0cb1 commit c54f6d1

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ app.listen(); // Port 80 is default
2525
When you create an new Express instance you can add an additional host name for example, your local network:
2626
```java
2727
// Will bind the server to your ip-adress
28-
Express app = new Express(ExpressUtils.getYourIp());
28+
Express app = new Express(Utils.getYourIp());
2929
```
3030
Default is localhost, so you can access, without setting the hostname, only from your local pc.
3131

src/express/expressfilter/ExpressFilterImpl.java

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)