Fix webserver stalling

This commit is contained in:
Lukas Rieger (Blue) 2024-06-03 10:52:14 +02:00
parent b5e8bf42ae
commit 4b8245d19d
No known key found for this signature in database
GPG Key ID: AA33883B1BBA03E6
2 changed files with 13 additions and 2 deletions

View File

@ -36,6 +36,7 @@
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.concurrent.TimeUnit;
@ -134,7 +135,10 @@ private HttpResponse generateResponse(HttpRequest request) throws IOException {
//create response
HttpResponse response = new HttpResponse(HttpStatusCode.OK);
response.addHeader("ETag", eTag);
if (lastModified > 0) response.addHeader("Last-Modified", DateTimeFormatter.RFC_1123_DATE_TIME.format(Instant.ofEpochMilli(lastModified)));
if (lastModified > 0) response.addHeader("Last-Modified", DateTimeFormatter.RFC_1123_DATE_TIME.format(Instant
.ofEpochMilli(lastModified)
.atOffset(ZoneOffset.UTC)
));
response.addHeader("Cache-Control", "public");
response.addHeader("Cache-Control", "max-age=" + TimeUnit.DAYS.toSeconds(1));

View File

@ -87,13 +87,20 @@ public void accept(SelectionKey selectionKey) {
() -> requestHandler.handle(request),
responseHandlerExecutor
);
futureResponse.thenAccept(response -> {
futureResponse.handle((response, error) -> {
if (error != null) {
Logger.global.logError("Unexpected error handling request", error);
response = new HttpResponse(HttpStatusCode.INTERNAL_SERVER_ERROR);
}
try {
response.read(channel); // do an initial read to trigger response sending intent
this.response = response;
} catch (IOException e) {
handleIOException(channel, e);
}
return null;
});
}