|
@@ -81,6 +81,8 @@ public class GoogleCloudStorageHttpHandler implements HttpHandler {
|
|
|
assert read == -1 : "Request body should have been empty but saw [" + read + "]";
|
|
|
}
|
|
|
try {
|
|
|
+ // Request body is closed in the finally block
|
|
|
+ final InputStream wrappedRequest = 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<String, String> params = new HashMap<>();
|
|
@@ -159,7 +161,7 @@ public class GoogleCloudStorageHttpHandler implements HttpHandler {
|
|
|
// 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(new BufferedInputStream(exchange.getRequestBody()))) {
|
|
|
+ for (String line : Streams.readAllLines(new BufferedInputStream(wrappedRequest))) {
|
|
|
if (line.length() == 0 || line.startsWith("--") || line.toLowerCase(Locale.ROOT).startsWith("content")) {
|
|
|
batch.append(line).append('\n');
|
|
|
} else if (line.startsWith("DELETE")) {
|
|
@@ -179,7 +181,7 @@ public class GoogleCloudStorageHttpHandler implements HttpHandler {
|
|
|
|
|
|
} else if (Regex.simpleMatch("POST /upload/storage/v1/b/" + bucket + "/*uploadType=multipart*", request)) {
|
|
|
// Multipart upload
|
|
|
- Optional<Tuple<String, BytesArray>> content = parseMultipartRequestBody(exchange.getRequestBody());
|
|
|
+ Optional<Tuple<String, BytesArray>> content = parseMultipartRequestBody(wrappedRequest);
|
|
|
if (content.isPresent()) {
|
|
|
blobs.put(content.get().v1(), content.get().v2());
|
|
|
|
|
@@ -198,7 +200,7 @@ public class GoogleCloudStorageHttpHandler implements HttpHandler {
|
|
|
final String blobName = params.get("name");
|
|
|
blobs.put(blobName, BytesArray.EMPTY);
|
|
|
|
|
|
- byte[] response = Streams.readFully(exchange.getRequestBody()).utf8ToString().getBytes(UTF_8);
|
|
|
+ byte[] response = Streams.readFully(wrappedRequest).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"
|
|
@@ -224,7 +226,7 @@ public class GoogleCloudStorageHttpHandler implements HttpHandler {
|
|
|
final int end = getContentRangeEnd(range);
|
|
|
|
|
|
final ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
- long bytesRead = Streams.copy(exchange.getRequestBody(), out);
|
|
|
+ long bytesRead = Streams.copy(wrappedRequest, out);
|
|
|
int length = Math.max(end + 1, limit != null ? limit : 0);
|
|
|
if ((int) bytesRead > length) {
|
|
|
throw new AssertionError("Requesting more bytes than available for blob");
|
|
@@ -249,6 +251,8 @@ public class GoogleCloudStorageHttpHandler implements HttpHandler {
|
|
|
exchange.sendResponseHeaders(RestStatus.INTERNAL_SERVER_ERROR.getStatus(), -1);
|
|
|
}
|
|
|
} finally {
|
|
|
+ int read = exchange.getRequestBody().read();
|
|
|
+ assert read == -1 : "Request body should have been fully read here but saw [" + read + "]";
|
|
|
exchange.close();
|
|
|
}
|
|
|
}
|