From 4d0e8e4d6a7f7a795751ee7f1d354d56ce128187 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Wed, 5 Feb 2020 12:48:56 +0100 Subject: [PATCH] Fix GCS Mock Http Handler JDK Bug There is an open JDK bug that is causing an assertion in the JDK's http server to trip if we don't drain the request body before sending response headers. See https://bugs.openjdk.java.net/browse/JDK-8180754 Working around this issue here by always draining the request at the beginning of the handler. Fixes #51446 --- .../fixture/gcs/GoogleCloudStorageHttpHandler.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/fixtures/gcs-fixture/src/main/java/fixture/gcs/GoogleCloudStorageHttpHandler.java b/test/fixtures/gcs-fixture/src/main/java/fixture/gcs/GoogleCloudStorageHttpHandler.java index 5b6d7f087ad75..944f025d6e3f5 100644 --- a/test/fixtures/gcs-fixture/src/main/java/fixture/gcs/GoogleCloudStorageHttpHandler.java +++ b/test/fixtures/gcs-fixture/src/main/java/fixture/gcs/GoogleCloudStorageHttpHandler.java @@ -88,7 +88,7 @@ public void handle(final HttpExchange exchange) throws IOException { } try { // Request body is closed in the finally block - final InputStream wrappedRequest = Streams.noCloseStream(exchange.getRequestBody()); + final BytesReference requestBody = Streams.readFully(Streams.noCloseStream(exchange.getRequestBody())); if (Regex.simpleMatch("GET /storage/v1/b/" + bucket + "/o*", request)) { // List Objects https://cloud.google.com/storage/docs/json_api/v1/objects/list final Map params = new HashMap<>(); @@ -155,7 +155,7 @@ public void handle(final HttpExchange exchange) throws IOException { // Batch https://cloud.google.com/storage/docs/json_api/v1/how-tos/batch final String uri = "/storage/v1/b/" + bucket + "/o/"; final StringBuilder batch = new StringBuilder(); - for (String line : Streams.readAllLines(wrappedRequest)) { + for (String line : Streams.readAllLines(requestBody.streamInput())) { if (line.length() == 0 || line.startsWith("--") || line.toLowerCase(Locale.ROOT).startsWith("content")) { batch.append(line).append('\n'); } else if (line.startsWith("DELETE")) { @@ -174,7 +174,7 @@ public void handle(final HttpExchange exchange) throws IOException { } else if (Regex.simpleMatch("POST /upload/storage/v1/b/" + bucket + "/*uploadType=multipart*", request)) { // Multipart upload - Optional> content = parseMultipartRequestBody(wrappedRequest); + Optional> content = parseMultipartRequestBody(requestBody.streamInput()); if (content.isPresent()) { blobs.put(content.get().v1(), content.get().v2()); @@ -194,7 +194,7 @@ public void handle(final HttpExchange exchange) throws IOException { final String blobName = params.get("name"); blobs.put(blobName, BytesArray.EMPTY); - byte[] response = Streams.readFully(wrappedRequest).utf8ToString().getBytes(UTF_8); + byte[] response = requestBody.utf8ToString().getBytes(UTF_8); exchange.getResponseHeaders().add("Content-Type", "application/json"); exchange.getResponseHeaders().add("Location", httpServerUrl(exchange) + "/upload/storage/v1/b/" + bucket + "/o?" + "uploadType=resumable" @@ -219,7 +219,7 @@ public void handle(final HttpExchange exchange) throws IOException { final int start = getContentRangeStart(range); final int end = getContentRangeEnd(range); - blob = new CompositeBytesReference(blob, Streams.readFully(wrappedRequest)); + blob = new CompositeBytesReference(blob, requestBody); blobs.put(blobName, blob); if (limit == null) {