changeset 54834:e57bcfd7bf79

Merge
author chegar
date Fri, 08 Feb 2019 13:07:44 +0000
parents e402ec38ba9a 4ce47bc1fb92
children 50a5d0353570
files src/java.net.http/share/classes/jdk/internal/net/http/MultiExchange.java test/jdk/java/net/httpclient/Response204.java
diffstat 2 files changed, 36 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.net.http/share/classes/jdk/internal/net/http/MultiExchange.java	Fri Feb 08 12:44:16 2019 +0000
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/MultiExchange.java	Fri Feb 08 13:07:44 2019 +0000
@@ -259,7 +259,7 @@
 
     private boolean bodyIsPresent(Response r) {
         HttpHeaders headers = r.headers();
-        if (headers.firstValue("Content-length").isPresent())
+        if (headers.firstValueAsLong("Content-length").orElse(0L) != 0L)
             return true;
         if (headers.firstValue("Transfer-encoding").isPresent())
             return true;
--- a/test/jdk/java/net/httpclient/Response204.java	Fri Feb 08 12:44:16 2019 +0000
+++ b/test/jdk/java/net/httpclient/Response204.java	Fri Feb 08 13:07:44 2019 +0000
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @bug 8211437 8216974
+ * @bug 8211437 8216974 8218662
  * @run main/othervm -Djdk.httpclient.HttpClient.log=headers,requests Response204
  * @summary
  */
@@ -33,12 +33,13 @@
 import java.net.http.HttpClient;
 import java.net.http.HttpRequest;
 import java.net.http.HttpResponse;
-import java.util.*;
+import java.net.http.HttpResponse.BodyHandlers;
 import java.util.concurrent.*;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.logging.*;
 import java.io.*;
 import java.net.*;
+import static java.net.http.HttpClient.Builder.NO_PROXY;
 
 /**
  * Verify that a 204 response code with no content-length is handled correctly
@@ -58,6 +59,7 @@
         InetSocketAddress addr = new InetSocketAddress (InetAddress.getLoopbackAddress(), 0);
         HttpServer server = HttpServer.create (addr, 0);
         HttpContext ctx = server.createContext ("/test", handler);
+        server.createContext ("/zero", new ZeroContentLengthHandler());
         ExecutorService executor = Executors.newCachedThreadPool();
         server.setExecutor (executor);
         server.start ();
@@ -91,12 +93,31 @@
             // check for 8216974
             Exception error = serverError.get();
             if (error != null) throw error;
+
+            // Test 3
+            testZeroContentLength(uri.resolve("/zero/xxyy"));
+            System.out.println ("OK 3");
         } finally {
             server.stop(2);
             executor.shutdown();
         }
     }
 
+    static void testZeroContentLength(URI uri) throws Exception {
+        System.out.println("--- testZeroContentLength ---");
+        HttpClient client = HttpClient.newBuilder().proxy(NO_PROXY).build();
+        HttpRequest request = HttpRequest.newBuilder(uri).build();
+        HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
+        System.out.println("Received response:" + response);
+        System.out.println("Received headers:" + response.headers());
+        if (response.statusCode() != 204)
+            throw new RuntimeException("Expected 204, got:" + response.statusCode());
+        if (response.body() != null && !response.body().equals(""))
+            throw new RuntimeException("Expected empty response, got: " + response.body());
+        if (response.headers().firstValueAsLong("Content-Length").orElse(-1L) != 0L)
+            throw new RuntimeException("Expected Content-Length:0, in: " + response.headers());
+    }
+
     public static boolean error = false;
 
     static class Handler implements HttpHandler {
@@ -133,4 +154,16 @@
             t.close();
         }
     }
+
+    // A handler that returns a 204 with a `Content-Length: 0` header/value
+    static class ZeroContentLengthHandler implements HttpHandler {
+        public void handle(HttpExchange t) throws IOException {
+            try (InputStream is = t.getRequestBody()) {
+                is.readAllBytes();
+            }
+            t.getResponseHeaders().set("Content-length", "0");
+            t.sendResponseHeaders(204, -1);
+            t.close();
+        }
+    }
 }