8218662: Allow 204 responses with Content-Length:0
Reviewed-by: michaelm
This commit is contained in:
parent
10a281c485
commit
fb05ae21bb
@ -217,7 +217,7 @@ class MultiExchange<T> {
|
||||
|
||||
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;
|
||||
|
@ -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 com.sun.net.httpserver.*;
|
||||
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 @@ public class Response204 {
|
||||
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 @@ public class Response204 {
|
||||
} 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 @@ public class Response204 {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user