Skip to content

Commit

Permalink
Remove skipBytes in favor of offset
Browse files Browse the repository at this point in the history
Addressing review comment
bazelbuild#14258 (comment)
  • Loading branch information
aherrmann committed Jan 24, 2023
1 parent e33f62b commit 475c5a0
Showing 1 changed file with 3 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ final class HttpDownloadHandler extends AbstractHttpHandler<HttpObject> {
private String path;
/** the offset at which to download */
private long offset;
/** the bytes to skip in a full or chunked response */
private OptionalInt skipBytes;

public HttpDownloadHandler(
Credentials credentials, ImmutableList<Entry<String, String>> extraHttpHeaders) {
Expand Down Expand Up @@ -91,14 +89,6 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Ex
if (contentLengthSet) {
contentLength = HttpUtil.getContentLength(response);
}
if (offset != 0) {
// We are in a retried download and received a full response.
// We need to skip `offset` bytes of the response to continue writing from the offset.
if (!skipBytes.isPresent()) {
// This is the first chunk, or the full response.
skipBytes = OptionalInt.of((int)offset);
}
}
downloadSucceeded = response.status().equals(HttpResponseStatus.OK);
if (!downloadSucceeded) {
out = new ByteArrayOutputStream();
Expand All @@ -111,13 +101,13 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Ex

ByteBuf content = ((HttpContent) msg).content();
int readableBytes = content.readableBytes();
if (skipBytes.isPresent() && skipBytes.getAsInt() > 0) {
int skipNow = skipBytes.getAsInt();
if (offset > 0) {
int skipNow = (int)offset;
if (skipNow >= readableBytes) {
skipNow = readableBytes;
}
content.readerIndex(content.readerIndex() + skipNow);
skipBytes = OptionalInt.of(skipBytes.getAsInt() - skipNow);
offset -= skipNow;
readableBytes = readableBytes - skipNow;
}
content.readBytes(out, readableBytes);
Expand Down Expand Up @@ -153,7 +143,6 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
out = cmd.out();
path = constructPath(cmd.uri(), cmd.digest().getHash(), cmd.casDownload());
offset = cmd.offset();
skipBytes = OptionalInt.empty();
HttpRequest request = buildRequest(path, constructHost(cmd.uri()));
addCredentialHeaders(request, cmd.uri());
addExtraRemoteHeaders(request);
Expand Down

0 comments on commit 475c5a0

Please sign in to comment.