mirror of
https://github.com/Athou/commafeed.git
synced 2026-11-03 19:05:03 +00:00
don't stream the response at all if it exceeds the max size
This commit is contained in:
@@ -16,6 +16,7 @@ import lombok.EqualsAndHashCode;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Lombok;
|
import lombok.Lombok;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.experimental.Delegate;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import org.apache.commons.lang3.ArrayUtils;
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
@@ -30,9 +31,12 @@ import org.apache.hc.client5.http.protocol.RedirectLocations;
|
|||||||
import org.apache.hc.client5.http.utils.DateUtils;
|
import org.apache.hc.client5.http.utils.DateUtils;
|
||||||
import org.apache.hc.core5.concurrent.FutureCallback;
|
import org.apache.hc.core5.concurrent.FutureCallback;
|
||||||
import org.apache.hc.core5.http.ContentType;
|
import org.apache.hc.core5.http.ContentType;
|
||||||
|
import org.apache.hc.core5.http.EntityDetails;
|
||||||
|
import org.apache.hc.core5.http.HttpException;
|
||||||
import org.apache.hc.core5.http.HttpStatus;
|
import org.apache.hc.core5.http.HttpStatus;
|
||||||
import org.apache.hc.core5.http.Message;
|
import org.apache.hc.core5.http.Message;
|
||||||
import org.apache.hc.core5.http.NameValuePair;
|
import org.apache.hc.core5.http.NameValuePair;
|
||||||
|
import org.apache.hc.core5.http.nio.AsyncEntityConsumer;
|
||||||
import org.apache.hc.core5.http.nio.AsyncRequestProducer;
|
import org.apache.hc.core5.http.nio.AsyncRequestProducer;
|
||||||
import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
|
import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
|
||||||
import org.apache.hc.core5.http.nio.entity.AbstractBinAsyncEntityConsumer;
|
import org.apache.hc.core5.http.nio.entity.AbstractBinAsyncEntityConsumer;
|
||||||
@@ -165,11 +169,12 @@ public class HttpGetter {
|
|||||||
.setProtocolUpgradeEnabled(false)
|
.setProtocolUpgradeEnabled(false)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
|
long maxResponseSize = config.httpClient().maxResponseSize().asLongValue();
|
||||||
AsyncRequestProducer req = SimpleRequestProducer.create(request.toSimpleHttpRequest());
|
AsyncRequestProducer req = SimpleRequestProducer.create(request.toSimpleHttpRequest());
|
||||||
AsyncResponseConsumer<Message<org.apache.hc.core5.http.HttpResponse, byte[]>> resp =
|
AsyncResponseConsumer<Message<org.apache.hc.core5.http.HttpResponse, byte[]>> resp =
|
||||||
new BasicResponseConsumer<>(
|
new BasicResponseConsumer<>(
|
||||||
new LimitedBytesEntityConsumer(
|
new LimitedContentLengthEntityConsumer(
|
||||||
config.httpClient().maxResponseSize().asLongValue()));
|
new LimitedBytesEntityConsumer(maxResponseSize), maxResponseSize));
|
||||||
FutureCallback<Message<org.apache.hc.core5.http.HttpResponse, byte[]>> callback =
|
FutureCallback<Message<org.apache.hc.core5.http.HttpResponse, byte[]>> callback =
|
||||||
new FutureCallback<>() {
|
new FutureCallback<>() {
|
||||||
@Override
|
@Override
|
||||||
@@ -281,6 +286,23 @@ public class HttpGetter {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private record LimitedContentLengthEntityConsumer(
|
||||||
|
@Delegate AsyncEntityConsumer<byte[]> delegate, long maxBytes)
|
||||||
|
implements AsyncEntityConsumer<byte[]> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void streamStart(EntityDetails entityDetails, FutureCallback<byte[]> resultCallback)
|
||||||
|
throws HttpException, IOException {
|
||||||
|
long contentLength = entityDetails.getContentLength();
|
||||||
|
if (contentLength > maxBytes) {
|
||||||
|
throw new IOException(
|
||||||
|
"Response size (%s bytes) exceeds the maximum allowed size (%s bytes)"
|
||||||
|
.formatted(contentLength, maxBytes));
|
||||||
|
}
|
||||||
|
delegate.streamStart(entityDetails, resultCallback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
private static class LimitedBytesEntityConsumer extends AbstractBinAsyncEntityConsumer<byte[]> {
|
private static class LimitedBytesEntityConsumer extends AbstractBinAsyncEntityConsumer<byte[]> {
|
||||||
|
|
||||||
|
|||||||
@@ -360,7 +360,8 @@ class HttpGetterTest {
|
|||||||
|
|
||||||
IOException e = Assertions.assertThrows(IOException.class, () -> getter.get(this.feedUrl));
|
IOException e = Assertions.assertThrows(IOException.class, () -> getter.get(this.feedUrl));
|
||||||
Assertions.assertEquals(
|
Assertions.assertEquals(
|
||||||
"Response size exceeds the maximum allowed size (10000 bytes)", e.getMessage());
|
"Response size (100000 bytes) exceeds the maximum allowed size (10000 bytes)",
|
||||||
|
e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user