-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaticFileHandler.java
77 lines (68 loc) · 2.56 KB
/
StaticFileHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Owen Gregson
// Artificial Intelligence
// TTT Checkpoint #3
// Dec 18, 2024
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
class StaticFileHandler implements HttpHandler {
private static final Map<String, String> MIME_TYPES = new HashMap<>();
static {
MIME_TYPES.put("html", "text/html");
MIME_TYPES.put("js", "application/javascript");
MIME_TYPES.put("css", "text/css");
MIME_TYPES.put("json", "application/json");
MIME_TYPES.put("png", "image/png");
MIME_TYPES.put("jpg", "image/jpeg");
MIME_TYPES.put("jpeg", "image/jpeg");
MIME_TYPES.put("gif", "image/gif");
MIME_TYPES.put("svg", "image/svg+xml");
MIME_TYPES.put("ico", "image/x-icon");
}
@Override
public void handle(HttpExchange t) throws IOException {
String uri = t.getRequestURI().getPath();
if (uri.equals("/")) {
uri = "/visualizer.html";
}
if (uri.contains("..")) {
String response = "403 Forbidden";
t.sendResponseHeaders(403, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
return;
}
Path filePath = Paths.get("." + uri).normalize();
if (Files.exists(filePath) && !Files.isDirectory(filePath)) {
String extension = getFileExtension(filePath.getFileName().toString());
String mimeType = MIME_TYPES.getOrDefault(extension, "application/octet-stream");
byte[] response = Files.readAllBytes(filePath);
t.getResponseHeaders().add("Content-Type", mimeType);
t.sendResponseHeaders(200, response.length);
OutputStream os = t.getResponseBody();
os.write(response);
os.close();
} else {
String response = "404 Not Found: " + uri;
t.sendResponseHeaders(404, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
private String getFileExtension(String fileName) {
int lastDot = fileName.lastIndexOf(".");
if (lastDot >= 0 && lastDot < fileName.length() - 1) {
return fileName.substring(lastDot + 1).toLowerCase();
} else {
return "";
}
}
}