From 72e35d25d592b66d20c5ebba2dec3b60333fa863 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 8 Dec 2017 23:52:05 +0100 Subject: [PATCH] Added HTTP HEAD handling & better error handling sendResponseHeaders failed silently which broke even the error message --- src/buttondevteam/website/io/IOHelper.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/buttondevteam/website/io/IOHelper.java b/src/buttondevteam/website/io/IOHelper.java index 15a7432..723819c 100644 --- a/src/buttondevteam/website/io/IOHelper.java +++ b/src/buttondevteam/website/io/IOHelper.java @@ -29,9 +29,19 @@ public class IOHelper { } public static void SendResponse(int code, String content, HttpExchange exchange) throws IOException { + if (exchange.getRequestMethod().equalsIgnoreCase("HEAD")) { + exchange.sendResponseHeaders(code, 0); + exchange.getResponseBody().close(); + return; + } try (BufferedOutputStream out = new BufferedOutputStream(exchange.getResponseBody())) { try (ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) { - exchange.sendResponseHeaders(code, bis.available()); + try { + exchange.sendResponseHeaders(code, bis.available()); + } catch (IOException e) { + if (!e.getMessage().equals("headers already sent")) + throw e; // If an error occurs after sending the response headers send the error page even if the headers are for the original + } // This code will send *some page* (most likely an error page) with the original headers instead of failing to do anything byte[] buffer = new byte[512]; int count; while ((count = bis.read(buffer)) != -1) { @@ -139,7 +149,7 @@ public class IOHelper { return null; WebUser user = ChromaGamerBase.getUser(cookies.get("user_id").getValue(), WebUser.class); if (user != null && cookies.get("session_id") != null - && cookies.get("session_id").getValue().equals(user.sessionID().get())) { + && cookies.get("session_id").getValue().equals(user.sessionID().get().toString())) { if (cookies.getExpireTimeParsed().minusYears(1).isBefore(ZonedDateTime.now(ZoneId.of("GMT")))) LoginUser(exchange, user); return user;