Skip to content

Commit f7d71b5

Browse files
Fix GCS Mock Range Downloads (#52804) (#52830)
We were not correctly respecting the download range which lead to the GCS SDK client closing the connection at times. Also, fixes another instance of failing to drain the request fully before sending the response headers. Closes #51446
1 parent b788ec7 commit f7d71b5

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public class FakeOAuth2HttpHandler implements HttpHandler {
3535
@Override
3636
public void handle(final HttpExchange exchange) throws IOException {
3737
try {
38+
while (exchange.getRequestBody().read(BUFFER) >= 0) ;
3839
byte[] response = ("{\"access_token\":\"foo\",\"token_type\":\"Bearer\",\"expires_in\":3600}").getBytes(UTF_8);
3940
exchange.getResponseHeaders().add("Content-Type", "application/json");
4041
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length);
4142
exchange.getResponseBody().write(response);
42-
while (exchange.getRequestBody().read(BUFFER) >= 0) ;
4343
} finally {
4444
int read = exchange.getRequestBody().read();
4545
assert read == -1 : "Request body should have been fully read here but saw [" + read + "]";

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,14 @@ public void handle(final HttpExchange exchange) throws IOException {
142142
if (matcher.find() == false) {
143143
throw new AssertionError("Range bytes header does not match expected format: " + range);
144144
}
145-
146-
BytesReference response = Integer.parseInt(matcher.group(1)) == 0 ? blob : BytesArray.EMPTY;
145+
final int offset = Integer.parseInt(matcher.group(1));
146+
final int end = Integer.parseInt(matcher.group(2));
147+
BytesReference response = blob;
147148
exchange.getResponseHeaders().add("Content-Type", "application/octet-stream");
149+
final int bufferedLength = response.length();
150+
if (offset > 0 || bufferedLength > end) {
151+
response = response.slice(offset, Math.min(end + 1 - offset, bufferedLength - offset));
152+
}
148153
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length());
149154
response.writeTo(exchange.getResponseBody());
150155
} else {

0 commit comments

Comments
 (0)