Skip to content

Commit 2f8312b

Browse files
committed
fixat så uri rensas och återanvänds, lade till ny metod för sanering
1 parent f3bb1ac commit 2f8312b

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

src/main/java/org/example/ConnectionHandler.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,6 @@ public void runConnectionHandler() throws IOException {
6969
// Sanitize URI here (clean it)
7070
String sanitizedUri = sanitizeUri(parser.getUri());
7171
String cacheKey = "www:" + sanitizedUri;
72-
73-
// Check cache FIRST
74-
byte[] cachedBytes = FileCache.get(cacheKey);
75-
if (cachedBytes != null) {
76-
System.out.println(" Cache HIT: " + sanitizedUri);
77-
response.setContentTypeFromFilename(sanitizedUri);
78-
response.setBody(cachedBytes);
79-
client.getOutputStream().write(response.build());
80-
client.getOutputStream().flush();
81-
return;
82-
}
83-
84-
// Cache miss - StaticFileHandler reads and caches
85-
System.out.println(" Cache MISS: " + sanitizedUri);
86-
try {
87-
byte[] fileBytes = Files.readAllBytes(new File("www", sanitizedUri).toPath());
88-
FileCache.put(cacheKey, fileBytes); // ← SPARAR I CACHEN HÄR
89-
90-
response.setContentTypeFromFilename(sanitizedUri);
91-
response.setBody(fileBytes);
92-
client.getOutputStream().write(response.build());
93-
client.getOutputStream().flush();
94-
} catch (NoSuchFileException e) {
95-
response.setStatusCode(HttpResponseBuilder.SC_NOT_FOUND);
96-
response.setBody("404 Not Found");
97-
client.getOutputStream().write(response.build());
98-
client.getOutputStream().flush();
99-
}
10072
}
10173

10274
private String sanitizeUri(String uri) {

src/main/java/org/example/StaticFileHandler.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,24 @@ public StaticFileHandler(String webRoot) {
2424
}
2525

2626
public void sendGetRequest(OutputStream outputStream, String uri) throws IOException {
27-
if (isPathTraversal(uri)) {
27+
String sanitizedUri = sanitizeUri(uri);
28+
29+
if (isPathTraversal(sanitizedUri)) {
2830
writeResponse(outputStream, 403, "Forbidden");
2931
return;
3032
}
3133

3234
try {
33-
String cacheKey = "www:" + uri;
35+
String cacheKey = "www:" + sanitizedUri;
3436
byte[] fileBytes = FileCache.get(cacheKey);
3537

3638
if (fileBytes == null) {
37-
fileBytes = Files.readAllBytes(new File(webRoot, uri).toPath());
39+
fileBytes = Files.readAllBytes(new File(webRoot, sanitizedUri).toPath());
3840
FileCache.put(cacheKey, fileBytes);
3941
}
4042

4143
HttpResponseBuilder response = new HttpResponseBuilder();
42-
response.setContentTypeFromFilename(uri);
44+
response.setContentTypeFromFilename(sanitizedUri);
4345
response.setBody(fileBytes);
4446
outputStream.write(response.build());
4547
outputStream.flush();
@@ -51,6 +53,21 @@ public void sendGetRequest(OutputStream outputStream, String uri) throws IOExcep
5153
}
5254
}
5355

56+
57+
private String sanitizeUri(String uri) {
58+
if (uri == null || uri.isEmpty()) return "index.html";
59+
60+
int endIndex = Math.min(
61+
uri.indexOf('?') < 0 ? uri.length() : uri.indexOf('?'),
62+
uri.indexOf('#') < 0 ? uri.length() : uri.indexOf('#')
63+
);
64+
65+
return uri.substring(0, endIndex)
66+
.replace("\0", "")
67+
.replaceAll("^/+", "")
68+
.replaceAll("^$", "index.html");
69+
}
70+
5471
private boolean isPathTraversal(String uri) {
5572
try {
5673
Path webRootPath = Paths.get(webRoot).toAbsolutePath().normalize();

0 commit comments

Comments
 (0)