Skip to content

Commit 505f75d

Browse files
Fix GCS Mock Http Handler JDK Bug (#51933) (#51944)
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
1 parent ce71e60 commit 505f75d

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

test/fixtures/gcs-fixture/src/main/java/fixture/gcs/GoogleCloudStorageHttpHandler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void handle(final HttpExchange exchange) throws IOException {
8888
}
8989
try {
9090
// Request body is closed in the finally block
91-
final InputStream wrappedRequest = Streams.noCloseStream(exchange.getRequestBody());
91+
final BytesReference requestBody = Streams.readFully(Streams.noCloseStream(exchange.getRequestBody()));
9292
if (Regex.simpleMatch("GET /storage/v1/b/" + bucket + "/o*", request)) {
9393
// List Objects https://cloud.google.com/storage/docs/json_api/v1/objects/list
9494
final Map<String, String> params = new HashMap<>();
@@ -155,7 +155,7 @@ public void handle(final HttpExchange exchange) throws IOException {
155155
// Batch https://cloud.google.com/storage/docs/json_api/v1/how-tos/batch
156156
final String uri = "/storage/v1/b/" + bucket + "/o/";
157157
final StringBuilder batch = new StringBuilder();
158-
for (String line : Streams.readAllLines(wrappedRequest)) {
158+
for (String line : Streams.readAllLines(requestBody.streamInput())) {
159159
if (line.length() == 0 || line.startsWith("--") || line.toLowerCase(Locale.ROOT).startsWith("content")) {
160160
batch.append(line).append('\n');
161161
} else if (line.startsWith("DELETE")) {
@@ -174,7 +174,7 @@ public void handle(final HttpExchange exchange) throws IOException {
174174

175175
} else if (Regex.simpleMatch("POST /upload/storage/v1/b/" + bucket + "/*uploadType=multipart*", request)) {
176176
// Multipart upload
177-
Optional<Tuple<String, BytesReference>> content = parseMultipartRequestBody(wrappedRequest);
177+
Optional<Tuple<String, BytesReference>> content = parseMultipartRequestBody(requestBody.streamInput());
178178
if (content.isPresent()) {
179179
blobs.put(content.get().v1(), content.get().v2());
180180

@@ -194,7 +194,7 @@ public void handle(final HttpExchange exchange) throws IOException {
194194
final String blobName = params.get("name");
195195
blobs.put(blobName, BytesArray.EMPTY);
196196

197-
byte[] response = Streams.readFully(wrappedRequest).utf8ToString().getBytes(UTF_8);
197+
byte[] response = requestBody.utf8ToString().getBytes(UTF_8);
198198
exchange.getResponseHeaders().add("Content-Type", "application/json");
199199
exchange.getResponseHeaders().add("Location", httpServerUrl(exchange) + "/upload/storage/v1/b/" + bucket + "/o?"
200200
+ "uploadType=resumable"
@@ -219,7 +219,7 @@ public void handle(final HttpExchange exchange) throws IOException {
219219
final int start = getContentRangeStart(range);
220220
final int end = getContentRangeEnd(range);
221221

222-
blob = new CompositeBytesReference(blob, Streams.readFully(wrappedRequest));
222+
blob = new CompositeBytesReference(blob, requestBody);
223223
blobs.put(blobName, blob);
224224

225225
if (limit == null) {

0 commit comments

Comments
 (0)