Skip to content

Commit ccec54d

Browse files
committed
Add query test case, extract static methods
1 parent 50b7380 commit ccec54d

File tree

5 files changed

+100
-31
lines changed

5 files changed

+100
-31
lines changed

src/express/Express.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.net.InetSocketAddress;
99
import java.util.ArrayList;
1010
import java.util.concurrent.ConcurrentHashMap;
11-
import java.util.concurrent.ExecutorService;
1211

1312
public class Express {
1413

src/express/ExpressContextThread.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public ExpressContextThread(HttpExchange httpExchange, Express express) {
2323
this.REQUEST = new Request(httpExchange);
2424
this.RESPONSE = new Response(httpExchange);
2525

26-
this.requestPath = REQUEST.getRequestURI().getRawPath();
27-
this.requestMethod = REQUEST.getRequestMethod();
26+
this.requestPath = REQUEST.getURI().getRawPath();
27+
this.requestMethod = REQUEST.getMethod();
2828
}
2929

3030
@Override

src/express/ExpressUtils.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package express;
2+
3+
import com.sun.net.httpserver.Headers;
4+
import express.cookie.Cookie;
5+
6+
import java.io.UnsupportedEncodingException;
7+
import java.net.URI;
8+
import java.net.URLDecoder;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.regex.Matcher;
12+
import java.util.regex.Pattern;
13+
14+
public class ExpressUtils {
15+
16+
public static HashMap<String, Cookie> parseCookies(Headers headers) {
17+
HashMap<String, Cookie> cookieList = new HashMap<>();
18+
List<String> headerCookies = headers.get("Cookie");
19+
20+
if (headerCookies == null || headerCookies.size() == 0) {
21+
return cookieList;
22+
}
23+
24+
String hcookies = headerCookies.get(0);
25+
26+
String[] cookies = hcookies.split(";");
27+
for (String cookie : cookies) {
28+
String[] split = cookie.split("=");
29+
String name = split[0].trim();
30+
String value = split[1].trim();
31+
cookieList.put(name, new Cookie(name, value));
32+
}
33+
34+
return cookieList;
35+
}
36+
37+
public static HashMap<String, String> parseRawQuery(URI uri) {
38+
HashMap<String, String> querys = new HashMap<>();
39+
String rawQuery = uri.getRawQuery();
40+
41+
Matcher mat = Pattern.compile("(.+?)=(.+?)(&|$)").matcher(rawQuery);
42+
while (mat.find()) {
43+
try {
44+
String key = URLDecoder.decode(mat.group(1), "UTF8");
45+
String val = URLDecoder.decode(mat.group(2), "UTF8");
46+
querys.put(key, val);
47+
} catch (UnsupportedEncodingException e) {
48+
e.printStackTrace();
49+
}
50+
}
51+
52+
return querys;
53+
}
54+
}

src/express/http/Request.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,47 @@
22

33
import com.sun.net.httpserver.Headers;
44
import com.sun.net.httpserver.HttpExchange;
5+
import express.ExpressUtils;
56
import express.cookie.Cookie;
67

8+
import java.io.IOException;
79
import java.io.InputStream;
10+
import java.io.OutputStream;
811
import java.net.URI;
912
import java.util.HashMap;
1013
import java.util.List;
1114

12-
public class Request {
15+
public class Request{
1316

1417
private final HttpExchange HTTP_EXCHANGE;
18+
private final URI URI;
1519
private final InputStream BODY;
1620
private final Headers HEADER;
1721

1822
private final HashMap<String, Cookie> COOKIES;
19-
23+
private final HashMap<String, String> QUERYS;
2024
private HashMap<String, String> params;
2125

2226
public Request(HttpExchange exchange) {
2327
this.HTTP_EXCHANGE = exchange;
28+
this.URI = exchange.getRequestURI();
2429
this.HEADER = exchange.getRequestHeaders();
2530
this.BODY = exchange.getRequestBody();
26-
this.COOKIES = parseCookies(exchange.getRequestHeaders());
31+
32+
this.QUERYS = ExpressUtils.parseRawQuery(exchange.getRequestURI());
33+
this.COOKIES = ExpressUtils.parseCookies(exchange.getRequestHeaders());
34+
}
35+
36+
public InputStream getBody() {
37+
return BODY;
38+
}
39+
40+
public void pipe(OutputStream os, int bufferSize) throws IOException {
41+
byte[] buffer = new byte[bufferSize];
42+
int n;
43+
while ((n = BODY.read(buffer)) != -1)
44+
os.write(buffer, 0, n);
45+
os.close();
2746
}
2847

2948
public Cookie getCookie(String name) {
@@ -43,21 +62,25 @@ public String getHost() {
4362
}
4463

4564
public String getContentType() {
46-
return HEADER.get("Accept").get(0);
65+
return HEADER.get("Content-Type").get(0);
4766
}
4867

49-
public URI getRequestURI() {
50-
return HTTP_EXCHANGE.getRequestURI();
68+
public URI getURI() {
69+
return this.URI;
5170
}
5271

53-
public String getRequestMethod() {
72+
public String getMethod() {
5473
return HTTP_EXCHANGE.getRequestMethod();
5574
}
5675

5776
public String getParam(String key) {
5877
return params.get(key);
5978
}
6079

80+
public String getQuery(String key) {
81+
return QUERYS.get(key);
82+
}
83+
6184
public void setParams(HashMap<String, String> params) {
6285
this.params = params;
6386
}
@@ -66,25 +89,4 @@ public List<String> getHeader(String header) {
6689
return HEADER.get(header);
6790
}
6891

69-
private HashMap<String, Cookie> parseCookies(Headers headers) {
70-
HashMap<String, Cookie> cookieList = new HashMap<>();
71-
List<String> headerCookies = headers.get("Cookie");
72-
73-
if (headerCookies == null || headerCookies.size() == 0) {
74-
return cookieList;
75-
}
76-
77-
String hcookies = headerCookies.get(0);
78-
79-
String[] cookies = hcookies.split(";");
80-
for (String cookie : cookies) {
81-
String[] split = cookie.split("=");
82-
String name = split[0].trim();
83-
String value = split[1].trim();
84-
cookieList.put(name, new Cookie(name, value));
85-
}
86-
87-
return cookieList;
88-
}
89-
9092
}

src/test/Get.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,36 @@ public class Get {
1111
public static void main(String[] args) throws IOException {
1212
Express express = new Express();
1313

14+
// Test case for url
1415
express.get("/", (req, res) -> res.send("Called /"));
1516

17+
// Test case for url
1618
express.get("/user", (req, res) -> res.send("Called /user"));
1719

20+
// Test case for url
1821
express.get("/user/bob", (req, res) -> res.send("Called /user/bob"));
1922

23+
// Test case for url querying
24+
express.get("/getposts", (req, res) -> {
25+
String age = req.getQuery("age");
26+
String from = req.getQuery("from");
27+
res.send("Age: " + age + "\nFrom: " + from);
28+
});
29+
30+
// Test case for param placeholder
2031
express.get("/hello/:username", (req, res) -> {
2132
String username = req.getParam("username");
2233
res.send("User " + username + " sad hello!");
2334
});
2435

36+
// Test case for multiple param placeholder
2537
express.get("/hello/:username/:count", (req, res) -> {
2638
String username = req.getParam("username");
2739
String count = req.getParam("count");
2840
res.send("User " + username + " want to say " + count + " times hello!");
2941
});
3042

43+
// Test case for cookie setting & multiple param placeholder
3144
express.get("/cookie/:name/:val", (req, res) -> {
3245
String name = req.getParam("name");
3346
String val = req.getParam("val");
@@ -36,6 +49,7 @@ public static void main(String[] args) throws IOException {
3649
res.send("ok");
3750
});
3851

52+
// Test case for cookie reading
3953
express.get("/showcookies", (req, res) -> {
4054
HashMap<String, Cookie> cookies = req.getCookies();
4155
StringBuffer buffer = new StringBuffer();

0 commit comments

Comments
 (0)