This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-vfs.git
The following commit(s) were added to refs/heads/master by this push: new 8b60a89 [VFS-805] HTTP seek always exhausts response. 8b60a89 is described below commit 8b60a892d4ac6428362aa2b307c4a8143987429f Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Thu Jul 15 15:57:20 2021 -0400 [VFS-805] HTTP seek always exhausts response. Modified version of https://github.com/apache/commons-vfs/pull/186 --- .../http4/MonitoredHttpResponseContentInputStream.java | 11 +++++++++++ .../http5/MonitoredHttpResponseContentInputStream.java | 14 ++++++++++++++ .../apache/commons/vfs2/util/MonitorInputStream.java | 17 ++++++++++++++--- src/changes/changes.xml | 3 +++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/MonitoredHttpResponseContentInputStream.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/MonitoredHttpResponseContentInputStream.java index 42d7364..d8f6b37 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/MonitoredHttpResponseContentInputStream.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/MonitoredHttpResponseContentInputStream.java @@ -39,6 +39,17 @@ final class MonitoredHttpResponseContentInputStream extends MonitorInputStream { this.httpResponse = httpResponse; } + /** + * Prevent closing the stream itself if the httpResponse is closeable. + * Closing the stream may consume all remaining data no matter how large (VFS-805). + */ + @Override + protected void closeSuper() throws IOException { + if (!(httpResponse instanceof CloseableHttpResponse)) { + super.closeSuper(); + } + } + @Override protected void onClose() throws IOException { if (httpResponse instanceof CloseableHttpResponse) { diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/MonitoredHttpResponseContentInputStream.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/MonitoredHttpResponseContentInputStream.java index bfbba7c..0140b7d 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/MonitoredHttpResponseContentInputStream.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/MonitoredHttpResponseContentInputStream.java @@ -20,6 +20,7 @@ import java.io.IOException; import org.apache.commons.vfs2.util.MonitorInputStream; import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.io.entity.NullEntity; /** * An InputStream that cleans up the {@code org.apache.hc.core5.http.ClassicHttpResponse} on close. @@ -38,8 +39,21 @@ final class MonitoredHttpResponseContentInputStream extends MonitorInputStream { this.httpResponse = httpResponse; } + /** + * Prevent closing the stream itself if the httpResponse is closeable. + * Closing the stream may consume all remaining data no matter how large (VFS-805). + */ + @Override + protected void closeSuper() throws IOException { + // Suppressed close() invocation on the underlying input stream + } + @Override protected void onClose() throws IOException { + // Replace the response's entity with a dummy entity in order to prevent + // exhausting all data (VFS-805) + httpResponse.setEntity(NullEntity.INSTANCE); + // Note that this also calls close on the dummy entity httpResponse.close(); } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/MonitorInputStream.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/MonitorInputStream.java index ec79ae5..030a1c5 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/MonitorInputStream.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/MonitorInputStream.java @@ -82,7 +82,7 @@ public class MonitorInputStream extends BufferedInputStream { // Close the stream IOException exc = null; try { - super.close(); + closeSuper(); } catch (final IOException ioe) { exc = ioe; } @@ -109,6 +109,17 @@ public class MonitorInputStream extends BufferedInputStream { } /** + * This method exists in order to allow overriding whether to actually close + * the underlying stream (VFS-805). There are cases where closing that stream will + * consume any amount of remaining data. In such cases closing a different + * entity instead (such as an HttpResponse) may be more appropriate. + * @throws IOException if an IO error occurs. + */ + protected void closeSuper() throws IOException { + super.close(); + } + + /** * Called after the stream has been closed. This implementation does nothing. * * @throws IOException if an error occurs. @@ -121,7 +132,7 @@ public class MonitorInputStream extends BufferedInputStream { * Reads a character. * * @return The character that was read as an integer. - * @throws IOException if an error occurs. + * @throws IOException if an IO error occurs. */ @Override public int read() throws IOException { // lgtm [java/non-sync-override] @@ -144,7 +155,7 @@ public class MonitorInputStream extends BufferedInputStream { * @param offset The offset at which to start reading. * @param length The maximum number of bytes to read. * @return The number of bytes read. - * @throws IOException if an error occurs. + * @throws IOException if an IO error occurs. */ @Override public int read(final byte[] buffer, final int offset, final int length) throws IOException { // lgtm [java/non-sync-override] diff --git a/src/changes/changes.xml b/src/changes/changes.xml index afb07f4..a3172bc 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -108,6 +108,9 @@ The <action> type attribute can be add,update,fix,remove. - org.apache.commons.vfs2.provider.http4.Http4FileObject.getInternalURI() - org.apache.commons.vfs2.provider.http5.Http5FileObject.getInternalURI() </action> + <action type="fix" issue="VFS-805" dev="ggregory" due-to="Claus Stadler, Gary Gregory"> + HTTP seek always exhausts response #186. + </action> <!-- UPDATES --> <action type="update" dev="ggregory" due-to="Gary Gregory"> Bump Log4j 2.14.0 -> 2.14.1.