Skip to content

Commit

Permalink
Fix GCS Mock Range Downloads
Browse files Browse the repository at this point in the history
We were not correctly respecting the download range which lead
to the GCS SDK client closing the connection at times.

Closes elastic#51446
  • Loading branch information
original-brownbear committed Feb 26, 2020
1 parent dd565f3 commit 45dfcb3
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,14 @@ public void handle(final HttpExchange exchange) throws IOException {
if (matcher.find() == false) {
throw new AssertionError("Range bytes header does not match expected format: " + range);
}

BytesReference response = Integer.parseInt(matcher.group(1)) == 0 ? blob : BytesArray.EMPTY;
final int offset = Integer.parseInt(matcher.group(1));
final int end = Integer.parseInt(matcher.group(2));
BytesReference response = blob;
exchange.getResponseHeaders().add("Content-Type", "application/octet-stream");
final int bufferedLength = response.length();
if (offset > 0 || bufferedLength > end) {
response = response.slice(offset, Math.min(end + 1 - offset, bufferedLength - offset));
}
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length());
response.writeTo(exchange.getResponseBody());
} else {
Expand Down

0 comments on commit 45dfcb3

Please sign in to comment.