From 65bc309999bd98628b05b3068dc0ec4915fd87d1 Mon Sep 17 00:00:00 2001 From: Jeremy Lakeman Date: Wed, 24 May 2017 16:38:42 +0930 Subject: [PATCH] Allow POST requests with no Content-Length --- http_server.c | 15 ++++++------ .../servaldna/keyring/KeyringCommon.java | 24 ------------------- 2 files changed, 8 insertions(+), 31 deletions(-) diff --git a/http_server.c b/http_server.c index f02ab847..ddb70726 100644 --- a/http_server.c +++ b/http_server.c @@ -1305,11 +1305,15 @@ static int http_request_start_body(struct http_request *r) assert(r->version_major != 0); assert(r->parsed <= r->end); + // No header should probably be treated the same as no content + // Though some server implementations disagree + // http://lists.w3.org/Archives/Public/ietf-http-wg/2010JulSep/0275.html + if (r->request_header.content_length == CONTENT_LENGTH_UNKNOWN && !r->request_header.chunked) + r->request_header.content_length = 0; + if (r->verb == HTTP_VERB_GET) { // TODO: Implement HEAD requests (only send response header, not body) - if (r->request_header.content_length == CONTENT_LENGTH_UNKNOWN) - r->request_header.content_length = 0; - if (r->request_header.content_length != 0) { + if (r->request_header.chunked || r->request_header.content_length != 0) { IDEBUGF(r->debug, "Malformed HTTP %s request: non-zero Content-Length not allowed", r->verb); return 400; } @@ -1324,9 +1328,6 @@ static int http_request_start_body(struct http_request *r) r->decoder = http_request_decode_chunks; r->end = r->decode_ptr = r->parsed; r->chunk_state = CHUNK_SIZE; - }else if (r->request_header.content_length == CONTENT_LENGTH_UNKNOWN) { - IDEBUGF(r->debug, "Malformed HTTP %s request: missing Content-Length or Transfer-Encoding: chunked header", r->verb); - return 411; // Length Required } if (r->request_header.content_length == 0) { r->parser = NULL; @@ -1376,7 +1377,7 @@ static int http_request_start_body(struct http_request *r) } } - if (_run_out(r)) + if (r->request_content_remaining && _run_out(r)) return 100; return 0; } diff --git a/java-api/src/org/servalproject/servaldna/keyring/KeyringCommon.java b/java-api/src/org/servalproject/servaldna/keyring/KeyringCommon.java index 0b1f0f7e..973b9190 100644 --- a/java-api/src/org/servalproject/servaldna/keyring/KeyringCommon.java +++ b/java-api/src/org/servalproject/servaldna/keyring/KeyringCommon.java @@ -49,20 +49,6 @@ public class KeyringCommon public KeyringIdentity identity; } - private static void dumpStatus(Status status, PrintStream out) - { - out.println("input_stream=" + status.input_stream); - out.println("http_status_code=" + status.http_status_code); - out.println("http_status_message=" + status.http_status_message); - if (status.identity == null) { - out.println("identity=null"); - } else { - out.println("identity.subscriber=" + status.identity.subscriber); - out.println("identity.did=" + status.identity.did); - out.println("identity.name=" + status.identity.name); - } - } - protected static Status receiveResponse(HttpURLConnection conn, int expected_response_code) throws IOException, ServalDInterfaceException { int[] expected_response_codes = { expected_response_code }; @@ -176,13 +162,6 @@ public class KeyringCommon } } - private static void dumpHeaders(HttpURLConnection conn, PrintStream out) - { - for (Map.Entry> e: conn.getHeaderFields().entrySet()) - for (String v: e.getValue()) - out.println("received header " + e.getKey() + ": " + v); - } - private static String quoteString(String unquoted) { if (unquoted == null) @@ -214,7 +193,6 @@ public class KeyringCommon Status status = receiveRestfulResponse(conn, HttpURLConnection.HTTP_OK); try { decodeRestfulStatus(status); - dumpStatus(status, System.err); if (status.identity == null) throw new ServalDInterfaceException("invalid JSON response; missing identity"); @@ -241,7 +219,6 @@ public class KeyringCommon Status status = receiveRestfulResponse(conn, HttpURLConnection.HTTP_CREATED); try { decodeRestfulStatus(status); - dumpStatus(status, System.err); if (status.identity == null) throw new ServalDInterfaceException("invalid JSON response; missing identity"); return status.identity; @@ -263,7 +240,6 @@ public class KeyringCommon Status status = receiveRestfulResponse(conn, HttpURLConnection.HTTP_OK); try { decodeRestfulStatus(status); - dumpStatus(status, System.err); if (status.identity == null) throw new ServalDInterfaceException("invalid JSON response; missing identity"); return status.identity;