Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.nio.AsyncDataConsumer;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.http.nio.CapacityChannel;

/**
Expand Down Expand Up @@ -65,7 +66,7 @@ public final class InflatingAsyncDataConsumer implements AsyncDataConsumer {

public InflatingAsyncDataConsumer(
final AsyncDataConsumer downstream, final Boolean nowrapHint) {
this.downstream = downstream;
this.downstream = Args.notNull(downstream, "Downstream data consumer");
this.nowrapHint = nowrapHint;
this.inflater = new Inflater(nowrapHint == null || nowrapHint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.nio.AsyncDataConsumer;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.http.nio.CapacityChannel;
import org.apache.hc.core5.util.Asserts;

Expand Down Expand Up @@ -70,7 +71,7 @@ public final class InflatingBrotliDataConsumer implements AsyncDataConsumer {


public InflatingBrotliDataConsumer(final AsyncDataConsumer downstream) {
this.downstream = downstream;
this.downstream = Args.notNull(downstream, "Downstream data consumer");
try {
this.decoder = new DecoderJNI.Wrapper(8 * 1024);
} catch (final IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.nio.AsyncDataConsumer;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.http.nio.CapacityChannel;

/**
Expand All @@ -65,7 +66,7 @@ public final class InflatingGzipDataConsumer implements AsyncDataConsumer {
private final AtomicBoolean closed = new AtomicBoolean(false);

public InflatingGzipDataConsumer(final AsyncDataConsumer downstream) {
this.downstream = downstream;
this.downstream = Args.notNull(downstream, "Downstream data consumer");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.nio.AsyncDataConsumer;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.http.nio.CapacityChannel;

/**
Expand Down Expand Up @@ -73,7 +74,7 @@ public final class InflatingZstdDataConsumer implements AsyncDataConsumer {
private final AtomicBoolean closed = new AtomicBoolean(false);

public InflatingZstdDataConsumer(final AsyncDataConsumer downstream) {
this.downstream = downstream;
this.downstream = Args.notNull(downstream, "Downstream data consumer");
inDirect.limit(0);
outDirect.limit(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ public AsyncDataConsumer handleResponse(final HttpResponse rsp,
ContentCodingSupport.validate(codecs, maxCodecListLen);
if (!codecs.isEmpty()) {
AsyncDataConsumer downstream = cb.handleResponse(rsp, wrapEntityDetails(details));
if (downstream == null) {
return null;
}
for (int i = codecs.size() - 1; i >= 0; i--) {
final String codec = codecs.get(i);
final UnaryOperator<AsyncDataConsumer> op = decoders.lookup(codec);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.client5.http.async.methods;

import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;
class TestInflatingGzipDataConsumerNullDownstream {

@Test
void gzipConsumerRejectsNullDownstreamAtConstruction() {
assertThrows(NullPointerException.class, () -> new InflatingGzipDataConsumer(null));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -133,7 +134,8 @@ void testDeflateConsumerInserted() throws Exception {
when(details.getContentEncoding()).thenReturn("deflate");

final AsyncDataConsumer downstream = new StringAsyncEntityConsumer();
when(originalCb.handleResponse(same(rsp), same(details))).thenReturn(downstream);
// the exec passes a wrapped EntityDetails downstream, so match any (not same(details))
when(originalCb.handleResponse(same(rsp), any(EntityDetails.class))).thenReturn(downstream);

final AsyncDataConsumer wrapped = cb.handleResponse(rsp, details);

Expand Down Expand Up @@ -175,6 +177,10 @@ public AsyncDataConsumer apply(final AsyncDataConsumer d) {
final HttpResponse rsp = new BasicHttpResponse(200, "OK");
final EntityDetails details = mock(EntityDetails.class);
when(details.getContentEncoding()).thenReturn("whatever");
// a real (non-null) downstream so the exec proceeds to decoder selection and rejects the
// unknown coding; a null downstream would legitimately be discarded without decoding
when(originalCb.handleResponse(same(rsp), any(EntityDetails.class)))
.thenReturn(new StringAsyncEntityConsumer());

assertThrows(HttpException.class, () -> cb.handleResponse(rsp, details));
}
Expand All @@ -200,7 +206,7 @@ void testContentEncodingExceedsCodecListLenMax() throws Exception {
when(details1.getContentEncoding()).thenReturn("gzip,gzip,gzip,gzip,gzip");

final AsyncDataConsumer downstream1 = new StringAsyncEntityConsumer();
when(originalCb.handleResponse(same(rsp1), same(details1))).thenReturn(downstream1);
when(originalCb.handleResponse(same(rsp1), any(EntityDetails.class))).thenReturn(downstream1);

final AsyncDataConsumer wrapped = cb.handleResponse(rsp1, details1);

Expand All @@ -217,4 +223,20 @@ void testContentEncodingExceedsCodecListLenMax() throws Exception {
assertEquals("Codec list exceeds maximum of 5 elements", exception.getMessage());
}

@Test
void propagatesNullDiscardOnEncodedResponse() throws Exception {
final HttpRequest request = new BasicHttpRequest(Method.GET, "/");
final AsyncExecCallback cb = executeAndCapture(request);

final HttpResponse rsp = new BasicHttpResponse(302, "Found");
final EntityDetails details = mock(EntityDetails.class);
when(details.getContentEncoding()).thenReturn("gzip");

// an upstream exec (e.g. AsyncRedirectExec on a redirect) discards the body by returning null
when(originalCb.handleResponse(same(rsp), any(EntityDetails.class))).thenReturn(null);

// the null-discard signal must be propagated, not wrapped in a decoder (HTTPCLIENT-2426)
assertNull(cb.handleResponse(rsp, details));
}

}
Loading