forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correctly report error when HTTP connection was closed before Content…
…-Length was fulfilled. Instead of reporting checksum mismatch, the downloader will now report ``` Connection was closed before Content-Length was fulfilled. Bytes read %s but wanted %s ``` when HTTP connection is closed before Content-Length is fulfilled. Closes bazelbuild#13918. PiperOrigin-RevId: 395411418
- Loading branch information
1 parent
d1da696
commit 0e90eab
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
.../google/devtools/build/lib/bazel/repository/downloader/CheckContentLengthInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2021 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package com.google.devtools.build.lib.bazel.repository.downloader; | ||
|
||
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Input stream that guarantees its contents match a size. | ||
* | ||
* <p>This class is not thread safe, but it is safe to message pass this object between threads. | ||
*/ | ||
@ThreadCompatible | ||
public class CheckContentLengthInputStream extends InputStream { | ||
private final InputStream delegate; | ||
private final long expectedSize; | ||
private long actualSize; | ||
|
||
public CheckContentLengthInputStream(InputStream delegate, long expectedSize) { | ||
this.delegate = delegate; | ||
this.expectedSize = expectedSize; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
int result = delegate.read(); | ||
if (result == -1) { | ||
checkContentLength(); | ||
} else { | ||
actualSize += result; | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public int read(byte[] buffer, int offset, int length) throws IOException { | ||
int amount = delegate.read(buffer, offset, length); | ||
if (amount == -1) { | ||
checkContentLength(); | ||
} else { | ||
actualSize += amount; | ||
} | ||
return amount; | ||
} | ||
|
||
@Override | ||
public int available() throws IOException { | ||
return delegate.available(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
delegate.close(); | ||
checkContentLength(); | ||
} | ||
|
||
private void checkContentLength() throws IOException { | ||
if (actualSize < expectedSize) { | ||
throw new UnrecoverableHttpException( | ||
String.format( | ||
"Connection was closed before Content-Length was fulfilled. Bytes read %s but wanted" | ||
+ " %s", | ||
actualSize, expectedSize)); | ||
} else if (actualSize > expectedSize) { | ||
throw new UnrecoverableHttpException( | ||
String.format( | ||
"Received more bytes than Content-Length. Bytes read %s but wanted %s", | ||
actualSize, expectedSize)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters