Skip to content

Commit 6212edc

Browse files
committed
Add static file serving
1 parent ccec54d commit 6212edc

File tree

8 files changed

+212
-6
lines changed

8 files changed

+212
-6
lines changed

src/express/Express.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import express.events.Action;
55
import express.events.HttpRequest;
66

7+
import java.io.File;
78
import java.io.IOException;
89
import java.net.InetSocketAddress;
910
import java.util.ArrayList;
@@ -100,4 +101,16 @@ private void launch(Action action, int port) throws IOException {
100101
}).start();
101102
}
102103

104+
public static HttpRequest statics(String path) {
105+
return (req, res) -> {
106+
File reqFile = new File(path + req.getURI().getPath());
107+
108+
if (reqFile.exists()) {
109+
String extension = reqFile.getAbsolutePath().replaceAll("^(.*\\.|.*\\\\|.+$)", "");
110+
String contentType = ExpressUtils.getContentType(extension);
111+
res.send(reqFile, contentType);
112+
}
113+
};
114+
}
115+
103116
}

src/express/ExpressContextThread.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,26 @@ public ExpressContextThread(HttpExchange httpExchange, Express express) {
2929

3030
@Override
3131
public void run() {
32+
ArrayList<ExpressHandler> middlewareAll = EXPRESS.MITTLEWARE.get("*");
33+
ArrayList<ExpressHandler> requestsAll = EXPRESS.REQUEST.get("*");
3234
ArrayList<ExpressHandler> middleware = EXPRESS.MITTLEWARE.get(requestMethod);
3335
ArrayList<ExpressHandler> requests = EXPRESS.REQUEST.get(requestMethod);
3436

37+
if (middleware == null && middlewareAll != null)
38+
middleware = middlewareAll;
39+
else if (middlewareAll != null)
40+
middleware.addAll(middlewareAll);
41+
42+
if (requests == null && requestsAll != null)
43+
requests = requestsAll;
44+
else if (requestsAll != null)
45+
requests.addAll(requestsAll);
46+
3547
if (middleware != null && middleware.size() > 0) {
3648
middleware.forEach(exh -> {
3749

3850
HashMap<String, String> params = exh.parseParams(requestPath);
39-
if (params != null) {
40-
REQUEST.setParams(params);
51+
if (params != null || exh.getContext().equals("*")) {
4152
exh.getRequest().handle(REQUEST, RESPONSE);
4253

4354
if (RESPONSE.isClosed())
@@ -50,7 +61,7 @@ public void run() {
5061
requests.forEach(exh -> {
5162

5263
HashMap<String, String> params = exh.parseParams(requestPath);
53-
if (params != null) {
64+
if (params != null || exh.getContext().equals("*")) {
5465
REQUEST.setParams(params);
5566
exh.getRequest().handle(REQUEST, RESPONSE);
5667

src/express/ExpressUtils.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,77 @@
1313

1414
public class ExpressUtils {
1515

16+
private final static HashMap<String, String> CTS = new HashMap<>();
17+
18+
static {
19+
CTS.put("aac", "audio/aac");
20+
CTS.put("abw", "application/x-abiword");
21+
CTS.put("arc", "application/octet-stream");
22+
CTS.put("avi", "video/x-msvideo");
23+
CTS.put("azw", "application/vnd.amazon.ebook");
24+
CTS.put("bin", "application/octet-stream");
25+
CTS.put("bz", "application/x-bzip");
26+
CTS.put("bz2", "application/x-bzip2");
27+
CTS.put("csh", "application/x-csh");
28+
CTS.put("css", "text/css");
29+
CTS.put("csv", "text/csv");
30+
CTS.put("doc", "application/msword");
31+
CTS.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
32+
CTS.put("eot", "application/vnd.ms-fontobject");
33+
CTS.put("epub", "application/epub+zip");
34+
CTS.put("gif", "image/gif");
35+
CTS.put("htm", "text/html");
36+
CTS.put("html", "text/html");
37+
CTS.put("ico", "image/x-icon");
38+
CTS.put("ics", "text/calendar");
39+
CTS.put("jar", "application/java-archive");
40+
CTS.put("jpeg", "image/jpeg");
41+
CTS.put("jpg", "image/jpeg");
42+
CTS.put("js", "application/javascript");
43+
CTS.put("json", "application/json");
44+
CTS.put("mid", "audio/midi");
45+
CTS.put("midi", "audio/midi");
46+
CTS.put("mpeg", "video/mpeg");
47+
CTS.put("mpkg", "application/vnd.apple.installer+xml");
48+
CTS.put("odp", "application/vnd.oasis.opendocument.presentation");
49+
CTS.put("ods", "application/vnd.oasis.opendocument.spreadsheet");
50+
CTS.put("odt", "application/vnd.oasis.opendocument.text");
51+
CTS.put("oga", "audio/ogg");
52+
CTS.put("ogv", "video/ogg");
53+
CTS.put("ogx", "application/ogg");
54+
CTS.put("otf", "font/otf");
55+
CTS.put("png", "image/png");
56+
CTS.put("pdf", "application/pdf");
57+
CTS.put("ppt", "application/vnd.ms-powerpoint");
58+
CTS.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
59+
CTS.put("rar", "application/x-rar-compressed");
60+
CTS.put("rtf", "application/rtf");
61+
CTS.put("sh", "application/x-sh");
62+
CTS.put("svg", "image/svg+xml");
63+
CTS.put("swf", "application/x-shockwave-flash");
64+
CTS.put("tar", "application/x-tar");
65+
CTS.put("tif", "image/tiff");
66+
CTS.put("tiff", "image/tiff");
67+
CTS.put("ts", "application/typescript");
68+
CTS.put("ttf", "font/ttf");
69+
CTS.put("vsd", "application/vnd.visio");
70+
CTS.put("wav", "audio/x-wav");
71+
CTS.put("weba", "audio/webm");
72+
CTS.put("webm", "video/webm");
73+
CTS.put("webp", "image/webp");
74+
CTS.put("woff", "font/woff");
75+
CTS.put("woff2", "font/woff2");
76+
CTS.put("xhtml", "application/xhtml+xml");
77+
CTS.put("xls", "application/vnd.ms-excel");
78+
CTS.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
79+
CTS.put("xml", "application/xml");
80+
CTS.put("xul", "application/vnd.mozilla.xul+xml");
81+
CTS.put("zip", "application/zip");
82+
CTS.put("3gp", "video/3gpp");
83+
CTS.put("3g2", "video/3gpp2");
84+
CTS.put("7z", "application/x-7z-compressed");
85+
}
86+
1687
public static HashMap<String, Cookie> parseCookies(Headers headers) {
1788
HashMap<String, Cookie> cookieList = new HashMap<>();
1889
List<String> headerCookies = headers.get("Cookie");
@@ -38,6 +109,9 @@ public static HashMap<String, String> parseRawQuery(URI uri) {
38109
HashMap<String, String> querys = new HashMap<>();
39110
String rawQuery = uri.getRawQuery();
40111

112+
if (rawQuery == null)
113+
return querys;
114+
41115
Matcher mat = Pattern.compile("(.+?)=(.+?)(&|$)").matcher(rawQuery);
42116
while (mat.find()) {
43117
try {
@@ -51,4 +125,13 @@ public static HashMap<String, String> parseRawQuery(URI uri) {
51125

52126
return querys;
53127
}
128+
129+
public static String getContentType(String fileExtension) {
130+
String ct = CTS.get(fileExtension);
131+
132+
if (ct == null)
133+
ct = "text/plain";
134+
135+
return ct;
136+
}
54137
}

src/express/http/Response.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class Response {
1616
private final OutputStream BODY;
1717
private final Headers HEADER;
1818

19-
private String contentType = "text";
19+
private String contentType = "text/plain";
2020
private boolean isClose = false;
2121
private long contentLength = 0;
2222
private int status = 200;
@@ -64,7 +64,7 @@ public void send(String s) {
6464
close();
6565
}
6666

67-
public void sendFile(File file, String contentType) {
67+
public void send(File file, String contentType) {
6868
if (checkIfClosed()) return;
6969
this.contentLength += file.length();
7070
this.contentType = contentType;

src/test/Get.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static void main(String[] args) throws IOException {
5757
res.send(buffer.toString());
5858
});
5959

60-
express.listen(() -> System.out.println("Server stardet!"));
60+
express.listen(() -> System.out.println("Express is listening!"));
6161
}
6262

6363
}

src/test/Use.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package test;
2+
3+
import express.Express;
4+
5+
import java.io.IOException;
6+
7+
public class Use {
8+
9+
public static void main(String[] args) throws IOException {
10+
Express express = new Express();
11+
12+
/**
13+
* Test case for using static ressources
14+
* replace 'yourpath' with the path of, for example, the static files
15+
*/
16+
express.use(Express.statics("yourpath\\test_statics"));
17+
18+
express.listen(() -> System.out.println("Express is listening!"));
19+
}
20+
21+
}

src/test_statics/design.css

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
* {
2+
margin: 0;
3+
padding: 0;
4+
}
5+
6+
body {
7+
background: #ffffff;
8+
font-family: "Roboto";
9+
}
10+
11+
main,
12+
header {
13+
min-width: 30em;
14+
max-width: 80em;
15+
margin: 0 auto;
16+
}
17+
18+
h1 {
19+
text-align: center;
20+
padding: 0.2em;
21+
color: #4a4a4a;
22+
margin: 0 auto;
23+
font-weight: 500;
24+
}
25+
26+
section {
27+
margin: 3em;
28+
}
29+
30+
h2 {
31+
padding: 0.2em 0.2em 0.1em 0.2em;
32+
margin-bottom: 0.5em;
33+
border-bottom: 0.05em solid #4898e8;
34+
font-weight: 500;
35+
}
36+
37+
p {
38+
font-weight: 300;
39+
}

src/test_statics/index.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width">
7+
<title>Awesome website</title>
8+
<link rel="stylesheet" href="design.css">
9+
<link href="https://fonts.googleapis.com/css?family=Roboto:300,500" rel="stylesheet">
10+
</head>
11+
12+
<body>
13+
14+
<header>
15+
<h1>My Blog</h1>
16+
17+
</header>
18+
19+
<main>
20+
21+
<section>
22+
<h2>Day 1</h2>
23+
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
24+
</section>
25+
26+
<section>
27+
<h2>Day 2</h2>
28+
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
29+
</section>
30+
</main>
31+
<footer>
32+
33+
34+
</footer>
35+
36+
37+
</body>
38+
39+
</html>

0 commit comments

Comments
 (0)