--- a/src/java.net.http/share/classes/jdk/internal/net/http/MultiExchange.java Fri Feb 08 11:26:21 2019 +0000
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/MultiExchange.java Fri Feb 08 12:23:16 2019 +0000
@@ -217,7 +217,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 11:26:21 2019 +0000
+++ b/test/jdk/java/net/httpclient/Response204.java Fri Feb 08 12:23:16 2019 +0000
@@ -23,7 +23,7 @@
/**
* @test
- * @bug 8211437
+ * @bug 8211437 8218662
* @run main/othervm -Djdk.httpclient.HttpClient.log=headers,requests Response204
* @summary
*/
@@ -33,11 +33,13 @@
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
+import java.net.http.HttpResponse.BodyHandlers;
import java.util.*;
import java.util.concurrent.*;
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
@@ -54,6 +56,7 @@
InetSocketAddress addr = new InetSocketAddress (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 ();
@@ -80,12 +83,31 @@
} catch (IOException ioe) {
System.out.println("OK 2");
}
+
+ // 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 {
@@ -106,4 +128,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();
+ }
+ }
}