Skip to content

Commit 799b135

Browse files
committed
Improve cookie-session and query-parser
1 parent f70297e commit 799b135

File tree

4 files changed

+49
-28
lines changed

4 files changed

+49
-28
lines changed

src/express/Express.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private void addMiddleware(String requestMethod, String context, HttpRequest mid
8686
}
8787

8888
/**
89-
* Add an listener for GET request's.
89+
* Add an listener for all request methods.
9090
*
9191
* @param context The context.
9292
* @param request An listener which will be fired if the context matches the requestpath.

src/express/expressfilter/ExpressFilterImpl.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,30 @@
1515
public class ExpressFilterImpl implements HttpRequest {
1616

1717
private final HttpRequest REQUEST;
18-
private final String REQUEST_METHOD;
18+
private final String REQ;
1919
private final String CONTEXT;
20+
private final boolean REQ_ALL;
21+
private final boolean CONTEXT_ALL;
2022

2123
public ExpressFilterImpl(String requestMethod, String context, HttpRequest httpRequest) {
22-
this.REQUEST_METHOD = requestMethod;
24+
this.REQ = requestMethod;
2325
this.REQUEST = httpRequest;
2426
this.CONTEXT = context;
27+
28+
// Save some informations which don't need to be processed again
29+
this.REQ_ALL = requestMethod.equals("*");
30+
this.CONTEXT_ALL = context.equals("*");
2531
}
2632

2733
@Override
2834
public void handle(Request req, Response res) {
2935
String requestMethod = req.getMethod();
3036
String requestPath = req.getRedirect() != null ? req.getRedirect() : req.getURI().getRawPath();
3137

32-
if (!(REQUEST_METHOD.equals("*") || REQUEST_METHOD.equals(requestMethod))) {
38+
// Check if
39+
if (!(REQ_ALL || REQ.equals(requestMethod))) {
3340
return;
34-
} else if (CONTEXT.equals("*")) {
41+
} else if (CONTEXT_ALL) {
3542
REQUEST.handle(req, res);
3643
return;
3744
}
@@ -45,6 +52,9 @@ public void handle(Request req, Response res) {
4552
REQUEST.handle(req, res);
4653
}
4754

55+
/**
56+
* Extract and match the parameter from the url with an filter.
57+
*/
4858
private HashMap<String, String> matchURL(String filter, String url) {
4959
HashMap<String, String> params = new HashMap<>();
5060
StringBuilder key = new StringBuilder();
@@ -53,21 +63,18 @@ private HashMap<String, String> matchURL(String filter, String url) {
5363
char[] fc = filter.toCharArray();
5464
int ui = 0, fi = 0;
5565

56-
5766
for (; fi < fc.length; fi++, ui++) {
5867

5968
if (fc[fi] == ':') {
6069
key.setLength(0);
6170
val.setLength(0);
6271

6372
fi++;
64-
while (fi < fc.length && fc[fi] != '/') {
73+
while (fi < fc.length && fc[fi] != '/')
6574
key.append(fc[fi++]);
66-
}
6775

68-
while (ui < uc.length && uc[ui] != '/') {
76+
while (ui < uc.length && uc[ui] != '/')
6977
val.append(uc[ui++]);
70-
}
7178

7279
params.put(key.toString(), val.toString());
7380
} else if (fc[fi] != uc[ui]) {
@@ -84,4 +91,5 @@ private HashMap<String, String> matchURL(String filter, String url) {
8491
return params;
8592
}
8693

94+
8795
}

src/express/http/request/RequestUtils.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
import com.sun.net.httpserver.Headers;
44
import express.http.Cookie;
55

6-
import java.io.UnsupportedEncodingException;
7-
import java.net.URLDecoder;
86
import java.util.HashMap;
97
import java.util.List;
10-
import java.util.regex.Matcher;
11-
import java.util.regex.Pattern;
128

139
public class RequestUtils {
1410

@@ -51,17 +47,31 @@ protected static HashMap<String, String> parseRawQuery(String rawQuery) {
5147
if (rawQuery == null)
5248
return querys;
5349

54-
Matcher mat = Pattern.compile("(.+?)=(.+?)(&|$)").matcher(rawQuery);
55-
while (mat.find()) {
56-
try {
57-
String key = URLDecoder.decode(mat.group(1), "UTF8");
58-
String val = URLDecoder.decode(mat.group(2), "UTF8");
59-
querys.put(key, val);
60-
} catch (UnsupportedEncodingException e) {
61-
e.printStackTrace();
62-
}
50+
StringBuilder key = new StringBuilder();
51+
StringBuilder val = new StringBuilder();
52+
char[] chars = rawQuery.toCharArray();
53+
boolean keyac = false;
54+
char c = '=';
55+
56+
for (int i = 0; i < chars.length; i++) {
57+
c = chars[i];
58+
59+
if (c == '=')
60+
keyac = true;
61+
else if (c == '&') {
62+
querys.put(key.toString(), val.toString());
63+
key.setLength(0);
64+
val.setLength(0);
65+
keyac = false;
66+
} else if (keyac)
67+
val.append(c);
68+
else
69+
key.append(c);
6370
}
6471

72+
if (c != '=' && c != '&')
73+
querys.put(key.toString(), val.toString());
74+
6575
return querys;
6676
}
6777

src/express/middleware/ExpressCookieSession.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ public void handle(Request req, Response res) {
2929
Cookie cookie = req.getCookie(COOKIE_NAME);
3030

3131
if (cookie != null && COOKIES.containsKey(cookie.getValue())) {
32-
3332
req.addMiddlewareContent(this, COOKIES.get(cookie.getValue()));
3433
} else {
35-
String token = Utils.randomToken(32, 16);
34+
String token;
35+
36+
do {
37+
token = Utils.randomToken(32, 16);
38+
} while (COOKIES.contains(token));
3639

3740
cookie = new Cookie(COOKIE_NAME, token).setMaxAge(MAX_AGE);
3841
res.setCookie(cookie);
@@ -68,9 +71,9 @@ public long getDelay() {
6871
public void onUpdate() {
6972
long current = System.currentTimeMillis();
7073

71-
COOKIES.forEach((s, o) -> {
72-
if (current > o.getCreated() + o.getMaxAge())
73-
COOKIES.remove(s);
74+
COOKIES.forEach((cookieHash, cookie) -> {
75+
if (current > cookie.getCreated() + cookie.getMaxAge())
76+
COOKIES.remove(cookieHash);
7477
});
7578
}
7679

0 commit comments

Comments
 (0)