Skip to content

Commit acd5d9e

Browse files
committed
Some refactoring and reimplementations
1 parent c2f7cf5 commit acd5d9e

File tree

5 files changed

+71
-19
lines changed

5 files changed

+71
-19
lines changed

src/express/Express.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ public void listen(Action onStart, int port) throws IOException {
283283
// Fire worker threads
284284
WORKER.forEach(ExpressFilterWorker::start);
285285

286-
287286
// Create http server
288287
httpServer = HttpServer.create(new InetSocketAddress(this.hostname, port), 0);
289288
httpServer.setExecutor(executor); // Set thread executor

src/express/expressfilter/FilterLayerHandler.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
public class FilterLayerHandler implements HttpHandler {
1212

13-
private ExpressFilterChain[] LEVELS;
13+
private ExpressFilterChain[] LAYER;
1414

1515
{
1616
// Initialize layers
17-
LEVELS = new ExpressFilterChain[2];
17+
LAYER = new ExpressFilterChain[2];
1818

19-
for (int i = 0; i < LEVELS.length; i++) {
20-
LEVELS[i] = new ExpressFilterChain();
19+
for (int i = 0; i < LAYER.length; i++) {
20+
LAYER[i] = new ExpressFilterChain();
2121
}
2222
}
2323

@@ -27,7 +27,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
2727
Response response = new Response(httpExchange);
2828

2929
// First fire all middlewares, then the normal request filter
30-
for (ExpressFilterChain chain : LEVELS) {
30+
for (ExpressFilterChain chain : LAYER) {
3131
chain.filter(request, response);
3232

3333
if (response.isClosed())
@@ -43,12 +43,12 @@ public void handle(HttpExchange httpExchange) throws IOException {
4343
*/
4444
public void add(int level, HttpRequest handler) {
4545

46-
if (level >= LEVELS.length)
47-
throw new IndexOutOfBoundsException("Out of bounds: " + level + " > " + LEVELS.length);
46+
if (level >= LAYER.length)
47+
throw new IndexOutOfBoundsException("Out of bounds: " + level + " > " + LAYER.length);
4848
if (level < 0)
4949
throw new IndexOutOfBoundsException("Cannot be under zero: " + level + " < 0");
5050

51-
LEVELS[level].add(handler);
51+
LAYER[level].add(handler);
5252
}
5353

5454
}

src/express/http/Cookie.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,18 @@ public class Cookie {
2424
* @param value Cookie value
2525
*/
2626
public Cookie(String name, String value) {
27+
name = name.trim();
28+
29+
if (name.length() == 0 || name.charAt(0) == '$')
30+
throw new IllegalArgumentException("Illegal cookie name");
31+
2732
this.name = name;
2833
this.value = value;
2934
}
3035

36+
public Cookie() {
37+
}
38+
3139
/**
3240
* @return The name of the Cookie
3341
*/
@@ -183,8 +191,34 @@ public void setDomain(String domain) {
183191
this.domain = domain;
184192
}
185193

194+
@Override
195+
public boolean equals(Object obj) {
196+
if (obj instanceof Cookie) {
197+
Cookie other = (Cookie) obj;
198+
199+
if (!other.getValue().equals(this.getValue())) return false;
200+
if (!other.getName().equals(this.getName())) return false;
201+
if (!other.getDomain().equals(this.getDomain())) return false;
202+
if (!other.getExpire().equals(this.getExpire())) return false;
203+
if (other.getMaxAge() != this.getMaxAge()) return false;
204+
if (!other.getSameSite().equals(this.getSameSite())) return false;
205+
if (!other.getPath().equals(this.getPath())) return false;
206+
207+
return true;
208+
}
209+
return super.equals(obj);
210+
}
211+
212+
/**
213+
* Build the string to an cookie-string.
214+
*
215+
* @return The cookie as string, null if the name / value is null.
216+
*/
186217
@Override
187218
public String toString() {
219+
if (name == null || value == null)
220+
return null;
221+
188222
StringBuffer b = new StringBuffer();
189223
b.append(name).append("=").append(value);
190224

src/express/http/request/Request.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ public Request(HttpExchange exchange) {
7070

7171
// Parse query and cookies, both returns not null if there is nothing
7272
this.QUERYS = RequestUtils.parseRawQuery(exchange.getRequestURI().getRawQuery());
73-
this.COOKIES = RequestUtils.parseCookies(exchange.getRequestHeaders());
73+
74+
// Parse cookies
75+
this.COOKIES = RequestUtils.parseCookies(HEADERS);
7476
}
7577

7678
/**

src/express/http/request/RequestUtils.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,39 @@ public class RequestUtils {
1616
* @param headers The Headers
1717
* @return An hashmap with the cookie name as key and the complete cookie as value.
1818
*/
19-
protected static HashMap<String, Cookie> parseCookies(Headers headers) {
19+
static HashMap<String, Cookie> parseCookies(Headers headers) {
2020
HashMap<String, Cookie> cookieList = new HashMap<>();
2121
List<String> headerCookies = headers.get("Cookie");
2222

2323
if (headerCookies == null || headerCookies.size() == 0) {
2424
return cookieList;
2525
}
2626

27-
String hcookies = headerCookies.get(0);
27+
char[] chars = headerCookies.get(0).toCharArray();
28+
StringBuilder key = new StringBuilder();
29+
StringBuilder val = new StringBuilder();
30+
boolean swap = false;
31+
32+
for (char c : chars) {
33+
if (c == '=') {
34+
swap = true;
35+
} else if (c == ';') {
36+
String rkey = key.toString().trim();
37+
cookieList.put(rkey, new Cookie(rkey, val.toString()));
38+
39+
key.setLength(0);
40+
val.setLength(0);
41+
swap = false;
42+
} else if (swap) {
43+
val.append(c);
44+
} else {
45+
key.append(c);
46+
}
47+
}
2848

29-
String[] cookies = hcookies.split(";");
30-
for (String cookie : cookies) {
31-
String[] split = cookie.split("=");
32-
String name = split[0].trim();
33-
String value = split[1].trim();
34-
cookieList.put(name, new Cookie(name, value));
49+
if (key.length() > 0 && val.length() > 0) {
50+
String rkey = key.toString().trim();
51+
cookieList.put(rkey, new Cookie(rkey, val.toString()));
3552
}
3653

3754
return cookieList;
@@ -43,7 +60,7 @@ protected static HashMap<String, Cookie> parseCookies(Headers headers) {
4360
* @param rawQuery The raw query
4461
* @return An list with key-values which are encoded in UTF8.
4562
*/
46-
protected static HashMap<String, String> parseRawQuery(String rawQuery) {
63+
static HashMap<String, String> parseRawQuery(String rawQuery) {
4764
HashMap<String, String> querys = new HashMap<>();
4865

4966
if (rawQuery == null)

0 commit comments

Comments
 (0)