aboutsummaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorJarno Elonen <elonen@iki.fi>2015-08-08 12:35:21 +0300
committerJarno Elonen <elonen@iki.fi>2015-08-08 12:35:21 +0300
commit0760946bcdaf5b41827c9aa2fcfae4326b788265 (patch)
tree9ebca76faae199c4109b5c9803049cf02d01b2af /core/src
parent23529ea639a36e83142e987ee4239549419a4417 (diff)
downloadnanohttpd-0760946bcdaf5b41827c9aa2fcfae4326b788265.tar.gz
Moved body length calculation to separate function. Related to issue #217.
Diffstat (limited to 'core/src')
-rw-r--r--core/src/main/java/fi/iki/elonen/NanoHTTPD.java30
1 files changed, 21 insertions, 9 deletions
diff --git a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
index 647f58b..1336d15 100644
--- a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
+++ b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
@@ -739,7 +739,14 @@ public abstract class NanoHTTPD {
boolean keepAlive = protocolVersion.equals("HTTP/1.1") && (connection == null || !connection.matches("(?i).*close.*"));
// Ok, now do the serve()
+
+ // TODO: long body_size = getBodySize();
+ // TODO: long pos_before_serve = this.inputStream.totalRead()
+ // (requires implementaion for totalRead())
r = serve(this);
+ // TODO: this.inputStream.skip(body_size -
+ // (this.inputStream.totalRead() - pos_before_serve))
+
if (r == null) {
throw new ResponseException(Response.Status.INTERNAL_ERROR, "SERVER INTERNAL ERROR: Serve() returned a null response.");
} else {
@@ -880,21 +887,26 @@ public abstract class NanoHTTPD {
return this.uri;
}
+ /**
+ * Deduce body length in bytes. Either from "content-length" header or
+ * read bytes.
+ */
+ public long getBodySize() {
+ if (this.headers.containsKey("content-length")) {
+ return Integer.parseInt(this.headers.get("content-length"));
+ } else if (this.splitbyte < this.rlen) {
+ return this.rlen - this.splitbyte;
+ }
+ return 0;
+ }
+
@Override
public void parseBody(Map<String, String> files) throws IOException, ResponseException {
final int REQUEST_BUFFER_LEN = 512;
final int MEMORY_STORE_LIMIT = 1024;
RandomAccessFile randomAccessFile = null;
try {
- long size;
- if (this.headers.containsKey("content-length")) {
- size = Integer.parseInt(this.headers.get("content-length"));
- } else if (this.splitbyte < this.rlen) {
- size = this.rlen - this.splitbyte;
- } else {
- size = 0;
- }
-
+ long size = getBodySize();
ByteArrayOutputStream baos = null;
DataOutput request_data_output = null;