Skip to content

Commit 8cf57c0

Browse files
committed
Remove ExpressUtils
Class contains methods which are not useful for the user, so extract them and delete the class.
1 parent 707dda8 commit 8cf57c0

File tree

3 files changed

+77
-95
lines changed

3 files changed

+77
-95
lines changed

src/express/http/Request.java

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
import com.sun.net.httpserver.Headers;
44
import com.sun.net.httpserver.HttpExchange;
5-
import express.utils.ExpressUtils;
65
import express.http.cookie.Cookie;
76

87
import java.io.IOException;
98
import java.io.InputStream;
109
import java.io.OutputStream;
10+
import java.io.UnsupportedEncodingException;
1111
import java.net.URI;
12+
import java.net.URLDecoder;
1213
import java.util.HashMap;
1314
import java.util.List;
15+
import java.util.regex.Matcher;
16+
import java.util.regex.Pattern;
1417

1518
/**
1619
* @author Simon Reinisch
@@ -34,8 +37,8 @@ public Request(HttpExchange exchange) {
3437
this.BODY = exchange.getRequestBody();
3538

3639
this.params = new HashMap<>();
37-
this.QUERYS = ExpressUtils.parseRawQuery(exchange.getRequestURI());
38-
this.COOKIES = ExpressUtils.parseCookies(exchange.getRequestHeaders());
40+
this.QUERYS = parseRawQuery(exchange.getRequestURI());
41+
this.COOKIES = parseCookies(exchange.getRequestHeaders());
3942
}
4043

4144
/**
@@ -161,4 +164,58 @@ public List<String> getHeader(String header) {
161164
return HEADER.get(header);
162165
}
163166

167+
/**
168+
* Extract the cookies from the 'Cookie' header.
169+
*
170+
* @param headers The Headers
171+
* @return An hashmap with the cookie name as key and the complete cookie as value.
172+
*/
173+
private static HashMap<String, Cookie> parseCookies(Headers headers) {
174+
HashMap<String, Cookie> cookieList = new HashMap<>();
175+
List<String> headerCookies = headers.get("Cookie");
176+
177+
if (headerCookies == null || headerCookies.size() == 0) {
178+
return cookieList;
179+
}
180+
181+
String hcookies = headerCookies.get(0);
182+
183+
String[] cookies = hcookies.split(";");
184+
for (String cookie : cookies) {
185+
String[] split = cookie.split("=");
186+
String name = split[0].trim();
187+
String value = split[1].trim();
188+
cookieList.put(name, new Cookie(name, value));
189+
}
190+
191+
return cookieList;
192+
}
193+
194+
/**
195+
* Method to extract the querys from an url.
196+
*
197+
* @param uri The uri.
198+
* @return An list with key-values which are encoded in UTF8.
199+
*/
200+
private static HashMap<String, String> parseRawQuery(URI uri) {
201+
HashMap<String, String> querys = new HashMap<>();
202+
String rawQuery = uri.getRawQuery();
203+
204+
if (rawQuery == null)
205+
return querys;
206+
207+
Matcher mat = Pattern.compile("(.+?)=(.+?)(&|$)").matcher(rawQuery);
208+
while (mat.find()) {
209+
try {
210+
String key = URLDecoder.decode(mat.group(1), "UTF8");
211+
String val = URLDecoder.decode(mat.group(2), "UTF8");
212+
querys.put(key, val);
213+
} catch (UnsupportedEncodingException e) {
214+
e.printStackTrace();
215+
}
216+
}
217+
218+
return querys;
219+
}
220+
164221
}

src/express/middleware/Static.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package express.middleware;
22

3-
import express.utils.ExpressUtils;
43
import express.events.HttpRequest;
54
import express.http.Request;
65
import express.http.Response;
6+
import express.utils.MIMETypes;
77

88
import java.io.File;
99

@@ -35,8 +35,23 @@ public void handle(Request req, Response res) {
3535

3636
if (reqFile.exists()) {
3737
String extension = reqFile.getAbsolutePath().replaceAll("^(.*\\.|.*\\\\|.+$)", "");
38-
String contentType = ExpressUtils.getContentType(extension);
38+
String contentType = getContentType(extension);
3939
res.send(reqFile, contentType);
4040
}
4141
}
42+
43+
/**
44+
* Returns the MIME-Type of an filename.
45+
*
46+
* @param fileExtension The file extension.
47+
* @return The MIME-Type.
48+
*/
49+
private static String getContentType(String fileExtension) {
50+
String ct = MIMETypes.get().get(fileExtension);
51+
52+
if (ct == null)
53+
ct = "text/plain";
54+
55+
return ct;
56+
}
4257
}

src/express/utils/ExpressUtils.java

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)