-
Notifications
You must be signed in to change notification settings - Fork 0
Server‑side caching Issue36 #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Boppler12
wants to merge
39
commits into
main
Choose a base branch
from
issue36
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
b9600f6
ska vara klart nu har glömt att commit jobbet inser jag dock
Boppler12 07e47bf
har lagt till i line 20-25 samt line 29
Boppler12 8e0ab50
tester för FileCacheTest
Boppler12 fffdebf
updaterat i StaticFileHandler och skapat CacheFilter
Boppler12 c69b9dd
ska vara klart nu har glömt att commit jobbet inser jag dock
Boppler12 bfa3129
har lagt till i line 20-25 samt line 29
Boppler12 3b34b91
tester för FileCacheTest
Boppler12 b2ef693
updaterat i StaticFileHandler och skapat CacheFilter
Boppler12 515bc8c
Merge remote-tracking branch 'origin/issue36' into Server‑side-cachin…
Boppler12 0a53b7f
vad problem med github
Boppler12 ddd7b40
vad problem med github
Boppler12 0c47d58
nu ska det funka
Boppler12 dd0530a
tog i bort cache git för missar
Boppler12 7f73baf
la till concurrentHashMap i rad 8
Boppler12 0a3c08c
refactored caching by removing FileCache completely; implemented an i…
Boppler12 56cc3e8
removed extra whitespace in MAX_CACHE_BYTES declaration
Boppler12 d20bb6f
removed things not used in code
Boppler12 3231ee1
ändringar som kodrabbit förstog
Boppler12 99f5fd7
ändringar som kodrabbit förstog
Boppler12 04cba97
ändringar som kodrabbit förstog
Boppler12 c158677
refactored StaticFileHandler to improve error handling, enhance sanit…
Boppler12 811ccce
fixa lite och hade glömt att commit vissa saker efter ändringar
Boppler12 1df4286
glömde av att fixa problem i cachefilter
Boppler12 19d0a48
updated CacheFilter to clean dead comments and import Paths/Path; imp…
Boppler12 d4227b0
fixat så problem som kommer up vid commit
Boppler12 3497f8a
Merge branch 'main' into issue36
Boppler12 bb0d51a
kom up problem när github kör ska ha fixat det tror jag
Boppler12 f5b2f44
kom up problem när github kör ska ha fixat det tror jag
Boppler12 704f8d8
Gjort det coderabbit bätt mig om
Boppler12 3f107c6
fixa problem
Boppler12 d87171b
fixat mer
Boppler12 c74b88b
fixat mer från coderabbit
Boppler12 baa3981
daniel har hjälp mig med ändringar
Boppler12 f3bb1ac
daniel har hjälp mig med ändringar
Boppler12 2f8312b
fixat så uri rensas och återanvänds, lade till ny metod för sanering
Boppler12 32cbdd3
Merge branch 'main' into issue36
Boppler12 331615a
fixa compleir issue
Boppler12 1be780f
behövde fixa saker efter en rebase
Boppler12 56ba520
should have made it according to the mall given
Boppler12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package org.example; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class FileCache { | ||
| private final Map<String, byte[]> cache; | ||
|
|
||
| public FileCache(int maxSize) { | ||
| this.cache = Collections.synchronizedMap(new LinkedHashMap<String, byte[]>(maxSize, 0.75f, true) { | ||
| @Override | ||
| protected boolean removeEldestEntry(Map.Entry<String, byte[]> eldest) { | ||
| return size() > maxSize; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public byte[] get(String key) { return cache.get(key); } | ||
| public void put(String key, byte[] content) { cache.put(key, content); } | ||
| public void clear() { cache.clear(); } | ||
| public int size() { return cache.size(); } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,69 +1,58 @@ | ||
|
|
||
| package org.example; | ||
|
|
||
| import org.example.http.HttpResponseBuilder; | ||
| import static org.example.http.HttpResponseBuilder.*; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| public class StaticFileHandler { | ||
| private final String WEB_ROOT; | ||
| private byte[] fileBytes; | ||
| private int statusCode; | ||
| private static final String DEFAULT_WEB_ROOT = "www"; | ||
| private final String webRoot; | ||
| private final FileCache fileCache; | ||
|
|
||
| // Constructor for production | ||
| public StaticFileHandler() { | ||
| WEB_ROOT = "www"; | ||
| public StaticFileHandler(FileCache fileCache) { | ||
| this(DEFAULT_WEB_ROOT, fileCache); | ||
| } | ||
|
|
||
| // Constructor for tests, otherwise the www folder won't be seen | ||
| public StaticFileHandler(String webRoot) { | ||
| WEB_ROOT = webRoot; | ||
| public StaticFileHandler(String webRoot, FileCache fileCache) { | ||
| this.webRoot = webRoot; | ||
| this.fileCache = fileCache; | ||
| } | ||
|
|
||
| private void handleGetRequest(String uri) throws IOException { | ||
| // Sanitize URI | ||
| int q = uri.indexOf('?'); | ||
| if (q >= 0) uri = uri.substring(0, q); | ||
| int h = uri.indexOf('#'); | ||
| if (h >= 0) uri = uri.substring(0, h); | ||
| uri = uri.replace("\0", ""); | ||
| if (uri.startsWith("/")) uri = uri.substring(1); | ||
| public void sendGetRequest(OutputStream outputStream, String uri) throws IOException { | ||
| String sanitizedUri = sanitizeUri(uri); | ||
| Path filePath = Path.of(webRoot, sanitizedUri); | ||
|
|
||
| // Path traversal check | ||
| File root = new File(WEB_ROOT).getCanonicalFile(); | ||
| File file = new File(root, uri).getCanonicalFile(); | ||
| if (!file.toPath().startsWith(root.toPath())) { | ||
| fileBytes = "403 Forbidden".getBytes(java.nio.charset.StandardCharsets.UTF_8); | ||
| statusCode = SC_FORBIDDEN; | ||
| return; | ||
| } | ||
| // Använd fullständig sökväg som cache-nyckel | ||
| String cacheKey = filePath.toString(); | ||
| byte[] fileBytes = fileCache.get(cacheKey); | ||
|
|
||
| // Read file | ||
| if (file.isFile()) { | ||
| fileBytes = Files.readAllBytes(file.toPath()); | ||
| statusCode = SC_OK; | ||
| } else { | ||
| File errorFile = new File(WEB_ROOT, "pageNotFound.html"); | ||
| if (errorFile.isFile()) { | ||
| fileBytes = Files.readAllBytes(errorFile.toPath()); | ||
| } else { | ||
| fileBytes = "404 Not Found".getBytes(java.nio.charset.StandardCharsets.UTF_8); | ||
| } | ||
| statusCode = SC_NOT_FOUND; | ||
| if (fileBytes == null) { | ||
| fileBytes = Files.readAllBytes(filePath); | ||
| fileCache.put(cacheKey, fileBytes); | ||
| } | ||
| } | ||
|
|
||
| public void sendGetRequest(OutputStream outputStream, String uri) throws IOException { | ||
| handleGetRequest(uri); | ||
| HttpResponseBuilder response = new HttpResponseBuilder(); | ||
| response.setStatusCode(statusCode); | ||
| // Use MimeTypeDetector instead of hardcoded text/html | ||
| response.setContentTypeFromFilename(uri); | ||
| response.setContentTypeFromFilename(sanitizedUri); | ||
| response.setBody(fileBytes); | ||
| outputStream.write(response.build()); | ||
| outputStream.flush(); | ||
| } | ||
|
|
||
| private String sanitizeUri(String uri) { | ||
| if (uri == null || uri.isEmpty() || uri.equals("/")) return "index.html"; | ||
|
|
||
| // Ta bort query, anchors, start-snedstreck och null-bytes | ||
| String cleanUri = uri.split("[?#]")[0] | ||
| .replaceAll("^/+", "") | ||
| .replace("\0", ""); | ||
|
|
||
| return cleanUri.isEmpty() ? "index.html" : cleanUri; | ||
| } | ||
|
|
||
| public void clearCache() { | ||
| fileCache.clear(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.