aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorMeteorite <alexey.meteorite@gmail.com>2015-06-27 11:39:36 +0300
committerMeteorite <alexey.meteorite@gmail.com>2015-06-27 11:39:36 +0300
commitbfcedb5f2464c6f9cbbf634c0d8be407e47680b5 (patch)
tree1f9c2e934ea056649aece8a81d6e94a415ef0580 /core
parent9bf3b6490242dcc53f4d3f5303d3dda29db6df1b (diff)
downloadnanohttpd-bfcedb5f2464c6f9cbbf634c0d8be407e47680b5.tar.gz
Close user provided Response data InputStream even in case of error
Diffstat (limited to 'core')
-rw-r--r--core/src/main/java/fi/iki/elonen/NanoHTTPD.java21
1 files changed, 15 insertions, 6 deletions
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.
*/