mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-22 12:04:39 +02:00
Merge
This commit is contained in:
commit
ac5daa7a22
2 changed files with 36 additions and 3 deletions
|
@ -259,7 +259,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 8216974
|
||||
* @bug 8211437 8216974 8218662
|
||||
* @run main/othervm -Djdk.httpclient.HttpClient.log=headers,requests Response204
|
||||
* @summary
|
||||
*/
|
||||
|
@ -33,12 +33,13 @@ import com.sun.net.httpserver.*;
|
|||
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 @@ public class Response204 {
|
|||
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 @@ public class Response204 {
|
|||
// 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 @@ 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…
Add table
Add a link
Reference in a new issue