From 4dae134f8adf69654c0718fe7a8c744005293cd0 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 18 Dec 2017 14:33:21 +0100 Subject: [server] httpd: Better error handling on connection failures --- .../src/main/java/fi/iki/elonen/NanoHTTPD.java | 39 ++++++++++------------ 1 file changed, 17 insertions(+), 22 deletions(-) (limited to 'dozentenmodulserver/src/main/java') diff --git a/dozentenmodulserver/src/main/java/fi/iki/elonen/NanoHTTPD.java b/dozentenmodulserver/src/main/java/fi/iki/elonen/NanoHTTPD.java index f6312985..b50bdb72 100644 --- a/dozentenmodulserver/src/main/java/fi/iki/elonen/NanoHTTPD.java +++ b/dozentenmodulserver/src/main/java/fi/iki/elonen/NanoHTTPD.java @@ -547,7 +547,7 @@ public abstract class NanoHTTPD implements Runnable { /** * Sends given response to the socket. */ - protected void send(OutputStream outputStream) { + protected void send(OutputStream outputStream) throws IOException { String mime = mimeType; StringBuilder sb = new StringBuilder(); @@ -581,25 +581,21 @@ public abstract class NanoHTTPD implements Runnable { sendConnectionHeaderIfNotAlreadyPresent(sb, header); - try { - if (requestMethod != Method.HEAD && chunkedTransfer) { - sendAsChunked(outputStream, sb); - } else { - int pending = data != null ? data.available() : 0; - pending = sendContentLengthHeaderIfNotAlreadyPresent(sb, header, pending); - sb.append("\r\n"); - outputStream.write(sb.toString().getBytes(StandardCharsets.UTF_8)); - sb.setLength(0); - sendAsFixedLength(outputStream, pending); - } + if (requestMethod != Method.HEAD && chunkedTransfer) { + sendAsChunked(outputStream, sb); + } else { + int pending = data != null ? data.available() : 0; + pending = sendContentLengthHeaderIfNotAlreadyPresent(sb, header, pending); + sb.append("\r\n"); + outputStream.write(sb.toString().getBytes(StandardCharsets.UTF_8)); + sb.setLength(0); + sendAsFixedLength(outputStream, pending); + } - if (sb.length() != 0) { - outputStream.write(sb.toString().getBytes(StandardCharsets.UTF_8)); - } - safeClose(data); - } catch (IOException ioe) { - // Couldn't write? No can do. + if (sb.length() != 0) { + outputStream.write(sb.toString().getBytes(StandardCharsets.UTF_8)); } + safeClose(data); } protected int sendContentLengthHeaderIfNotAlreadyPresent(StringBuilder sb, @@ -844,14 +840,10 @@ public abstract class NanoHTTPD implements Runnable { try { read = inputStream.read(buf, 0, BUFSIZE); } catch (Exception e) { - safeClose(inputStream); - safeClose(outputStream); throw e; } if (read == -1) { // socket was been closed - safeClose(inputStream); - safeClose(outputStream); throw new SocketException("NanoHttpd Shutdown"); } while (read > 0) { @@ -863,6 +855,9 @@ public abstract class NanoHTTPD implements Runnable { if (maxRequestSize != 0 && rlen > maxRequestSize) throw new SocketException("Request too large"); } + if (splitbyte == 0) { + throw new SocketException("Connection closed"); + } } if (splitbyte < rlen) { -- cgit v1.2.3-55-g7522