From dbcb98396ee067ea164efb39f13ffe63e0fb39a1 Mon Sep 17 00:00:00 2001 From: Quentin de Longraye Date: Mon, 13 Jan 2025 12:41:34 +0100 Subject: [PATCH] [server] No body when HEAD requests to healthcheck (#11183) --- .../server/LanguageToolHttpHandler.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/languagetool-server/src/main/java/org/languagetool/server/LanguageToolHttpHandler.java b/languagetool-server/src/main/java/org/languagetool/server/LanguageToolHttpHandler.java index d2ebe3588c50..b64abe0e8311 100644 --- a/languagetool-server/src/main/java/org/languagetool/server/LanguageToolHttpHandler.java +++ b/languagetool-server/src/main/java/org/languagetool/server/LanguageToolHttpHandler.java @@ -131,10 +131,18 @@ public void handle(HttpExchange httpExchange) throws IOException { ServerMetricsCollector.getInstance().logFailedHealthcheck(); return; } else { - String ok = "OK"; httpExchange.getResponseHeaders().set("Content-Type", "text/plain"); - httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, ok.getBytes(ENCODING).length); - httpExchange.getResponseBody().write(ok.getBytes(ENCODING)); + + String requestMethod = httpExchange.getRequestMethod(); + if ("HEAD".equalsIgnoreCase(requestMethod)) { + // Send HTTP 200 without content + httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, -1); + } else { + String ok = "OK"; + httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, ok.getBytes(ENCODING).length); + httpExchange.getResponseBody().write(ok.getBytes(ENCODING)); + } + ServerMetricsCollector.getInstance().logResponse(HttpURLConnection.HTTP_OK); return; }