Add more cache-control headers to integrated webserver

This commit is contained in:
Lukas Rieger (Blue) 2024-02-25 02:15:34 +01:00
parent b02b91d3bb
commit 908789a815
No known key found for this signature in database
GPG Key ID: AA33883B1BBA03E6
3 changed files with 7 additions and 9 deletions

View File

@ -43,11 +43,9 @@
public class FileRequestHandler implements HttpRequestHandler { public class FileRequestHandler implements HttpRequestHandler {
private final Path webRoot; private final Path webRoot;
private final File emptyTileFile;
public FileRequestHandler(Path webRoot) { public FileRequestHandler(Path webRoot) {
this.webRoot = webRoot.normalize(); this.webRoot = webRoot.normalize();
this.emptyTileFile = webRoot.resolve("assets").resolve("emptyTile.json").toFile();
} }
@Override @Override
@ -90,11 +88,6 @@ private HttpResponse generateResponse(HttpRequest request) {
file = new File(filePath + "/index.html"); file = new File(filePath + "/index.html");
} }
// send empty tile-file if tile not exists
if (!file.exists() && file.toPath().startsWith(webRoot.resolve("maps"))){
file = emptyTileFile;
}
if (!file.exists() || file.isDirectory()) { if (!file.exists() || file.isDirectory()) {
return new HttpResponse(HttpStatusCode.NOT_FOUND); return new HttpResponse(HttpStatusCode.NOT_FOUND);
} }
@ -135,7 +128,7 @@ private HttpResponse generateResponse(HttpRequest request) {
response.addHeader("ETag", eTag); response.addHeader("ETag", eTag);
if (lastModified > 0) response.addHeader("Last-Modified", timestampToString(lastModified)); if (lastModified > 0) response.addHeader("Last-Modified", timestampToString(lastModified));
response.addHeader("Cache-Control", "public"); response.addHeader("Cache-Control", "public");
response.addHeader("Cache-Control", "max-age=" + TimeUnit.HOURS.toSeconds(1)); response.addHeader("Cache-Control", "max-age=" + TimeUnit.DAYS.toSeconds(1));
//add content type header //add content type header
String filetype = file.getName(); String filetype = file.getName();

View File

@ -39,6 +39,7 @@
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -109,6 +110,9 @@ public HttpResponse handle(HttpRequest request) {
if (lastModified > 0) if (lastModified > 0)
response.addHeader("Last-Modified", timestampToString(lastModified)); response.addHeader("Last-Modified", timestampToString(lastModified));
response.addHeader("Cache-Control", "public");
response.addHeader("Cache-Control", "max-age=" + TimeUnit.DAYS.toSeconds(1));
if (lod == 0) response.addHeader("Content-Type", "application/octet-stream"); if (lod == 0) response.addHeader("Content-Type", "application/octet-stream");
else response.addHeader("Content-Type", "image/png"); else response.addHeader("Content-Type", "image/png");
@ -122,6 +126,8 @@ public HttpResponse handle(HttpRequest request) {
if (optIn.isPresent()) { if (optIn.isPresent()) {
CompressedInputStream compressedIn = new CompressedInputStream(optIn.get(), Compression.NONE); CompressedInputStream compressedIn = new CompressedInputStream(optIn.get(), Compression.NONE);
HttpResponse response = new HttpResponse(HttpStatusCode.OK); HttpResponse response = new HttpResponse(HttpStatusCode.OK);
response.addHeader("Cache-Control", "public");
response.addHeader("Cache-Control", "max-age=" + TimeUnit.DAYS.toSeconds(1));
response.addHeader("Content-Type", ContentTypeRegistry.fromFileName(path)); response.addHeader("Content-Type", ContentTypeRegistry.fromFileName(path));
writeToResponse(compressedIn, response, request); writeToResponse(compressedIn, response, request);
return response; return response;