Skip to content

Commit

Permalink
Improve ExpressFilterIml
Browse files Browse the repository at this point in the history
Now the average response time is by ~30ms instead of 300ms!
  • Loading branch information
simonwep committed Feb 8, 2018
1 parent 51f0cb1 commit c54f6d1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ app.listen(); // Port 80 is default
When you create an new Express instance you can add an additional host name for example, your local network:
```java
// Will bind the server to your ip-adress
Express app = new Express(ExpressUtils.getYourIp());
Express app = new Express(Utils.getYourIp());
```
Default is localhost, so you can access, without setting the hostname, only from your local pc.

Expand Down
57 changes: 39 additions & 18 deletions src/express/expressfilter/ExpressFilterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,12 @@ public class ExpressFilterImpl implements HttpRequest {

private final HttpRequest REQUEST;
private final String REQUEST_METHOD;

private final String CONTEXT;
private final String[] CONTEXT_PARAMS;
private final String CONTEXT_REGEX;
private final Pattern CONTEXT_PATTERN;

public ExpressFilterImpl(String requestMethod, String context, HttpRequest httpRequest) {
this.REQUEST_METHOD = requestMethod;
this.REQUEST = httpRequest;

this.CONTEXT = context;
this.CONTEXT_PARAMS = context.split("(/)(:|[^:]+|)(:|)");
this.CONTEXT_REGEX = "^\\Q" + context.replaceAll(":([^/]+)", "\\\\E([^/]+)\\\\Q") + "\\E$";
this.CONTEXT_PATTERN = Pattern.compile(CONTEXT_REGEX);
}

@Override
Expand All @@ -47,22 +39,51 @@ public void handle(Request req, Response res) {
}

// Parse params
HashMap<String, String> params = matchURL(CONTEXT, requestPath);
if(params == null)
return;

req.setParams(params);
REQUEST.handle(req, res);
}

private static HashMap<String, String> matchURL(String filter, String url) {
HashMap<String, String> params = new HashMap<>();
Matcher matcher = CONTEXT_PATTERN.matcher(requestPath);
StringBuilder key = new StringBuilder();
StringBuilder val = new StringBuilder();
char[] uc = url.toCharArray();
char[] fc = filter.toCharArray();
int ui = 0, fi = 0;


// Match all params
if (matcher.find()) {
for (; fi < fc.length; fi++, ui++) {

for (int i = 1; i <= matcher.groupCount() && i < CONTEXT_PARAMS.length; i++) {
String g = matcher.group(i);
params.put(CONTEXT_PARAMS[i], g);
if (fc[fi] == ':') {
key.setLength(0);
val.setLength(0);

fi++;
while (fi < fc.length && fc[fi] != '/') {
key.append(fc[fi++]);
}

while (ui < uc.length && uc[ui] != '/') {
val.append(uc[ui++]);
}

params.put(key.toString(), val.toString());
} else if (fc[fi] != uc[ui]) {

// Failed
return null;
}
}

} else {
return;
if (ui < url.length() || fi < filter.length()) {
return null;
}

req.setParams(params);
REQUEST.handle(req, res);
return params;
}

}

0 comments on commit c54f6d1

Please sign in to comment.