summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2017-12-18 14:33:21 +0100
committerSimon Rettberg2017-12-18 14:33:21 +0100
commit4dae134f8adf69654c0718fe7a8c744005293cd0 (patch)
tree0019a28292cee405fed93de0f152f8789ab3848a
parent"Netzwerk-Share" -> "Netzlaufwerke" (diff)
downloadtutor-module-4dae134f8adf69654c0718fe7a8c744005293cd0.tar.gz
tutor-module-4dae134f8adf69654c0718fe7a8c744005293cd0.tar.xz
tutor-module-4dae134f8adf69654c0718fe7a8c744005293cd0.zip
[server] httpd: Better error handling on connection failures
-rw-r--r--dozentenmodulserver/src/main/java/fi/iki/elonen/NanoHTTPD.java39
1 files changed, 17 insertions, 22 deletions
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) {