From bfcedb5f2464c6f9cbbf634c0d8be407e47680b5 Mon Sep 17 00:00:00 2001 From: Meteorite Date: Sat, 27 Jun 2015 11:39:36 +0300 Subject: Close user provided Response data InputStream even in case of error --- core/src/main/java/fi/iki/elonen/NanoHTTPD.java | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'core') diff --git a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java index ef42bf5..b004aad 100644 --- a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java +++ b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java @@ -669,6 +669,7 @@ public abstract class NanoHTTPD { @Override public void execute() throws IOException { + Response r = null; try { // Read the first 8192 bytes. // The full header should fit in here. @@ -738,7 +739,7 @@ public abstract class NanoHTTPD { boolean keepAlive = protocolVersion.equals("HTTP/1.1") && (connection == null || !connection.matches("(?i).*close.*")); // Ok, now do the serve() - Response r = serve(this); + r = serve(this); if (r == null) { throw new ResponseException(Response.Status.INTERNAL_ERROR, "SERVER INTERNAL ERROR: Serve() returned a null response."); } else { @@ -761,14 +762,15 @@ public abstract class NanoHTTPD { // exception up the call stack. throw ste; } catch (IOException ioe) { - Response r = newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); - r.send(this.outputStream); + Response resp = newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); + resp.send(this.outputStream); safeClose(this.outputStream); } catch (ResponseException re) { - Response r = newFixedLengthResponse(re.getStatus(), NanoHTTPD.MIME_PLAINTEXT, re.getMessage()); - r.send(this.outputStream); + Response resp = newFixedLengthResponse(re.getStatus(), NanoHTTPD.MIME_PLAINTEXT, re.getMessage()); + resp.send(this.outputStream); safeClose(this.outputStream); } finally { + safeClose(r); this.tempFileManager.clear(); } } @@ -1059,7 +1061,7 @@ public abstract class NanoHTTPD { /** * HTTP response. Return one of these from serve(). */ - public static class Response { + public static class Response implements Closeable { public interface IStatus { @@ -1202,6 +1204,13 @@ public abstract class NanoHTTPD { keepAlive = true; } + @Override + public void close() throws IOException { + if (this.data != null) { + this.data.close(); + } + } + /** * Adds given line to the header. */ -- cgit v1.2.3