Fixed HEAD req. handling

This commit is contained in:
Norbi Peti 2018-05-31 00:48:16 +02:00
parent 78ef0fed4b
commit cbd8758356
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56

View file

@ -31,8 +31,8 @@ public class IOHelper {
public static void SendResponse(int code, String content, HttpExchange exchange) throws IOException { public static void SendResponse(int code, String content, HttpExchange exchange) throws IOException {
if (exchange.getRequestMethod().equalsIgnoreCase("HEAD")) { if (exchange.getRequestMethod().equalsIgnoreCase("HEAD")) {
exchange.sendResponseHeaders(code, -1); // -1 indicates no data exchange.sendResponseHeaders(200, -1); // -1 indicates no data
exchange.getResponseBody().close(); //exchange.getResponseBody().close(); - No stream is created for HEAD requests
return; return;
} }
try (BufferedOutputStream out = new BufferedOutputStream(exchange.getResponseBody())) { try (BufferedOutputStream out = new BufferedOutputStream(exchange.getResponseBody())) {
@ -57,8 +57,7 @@ public class IOHelper {
try { try {
if (exchange.getRequestBody().available() == 0) if (exchange.getRequestBody().available() == 0)
return ""; return "";
String content = IOUtils.toString(exchange.getRequestBody(), "UTF-8"); return IOUtils.toString(exchange.getRequestBody(), "UTF-8");
return content;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return ""; return "";
@ -73,8 +72,7 @@ public class IOHelper {
JsonElement e = new JsonParser().parse(content); JsonElement e = new JsonParser().parse(content);
if (e == null) if (e == null)
return null; return null;
JsonObject obj = e.getAsJsonObject(); return e.getAsJsonObject();
return obj;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;