HTTPCLIENT-2426: Do not wrap a discarded (null) response body in a co… · apache/httpcomponents-client@ab20c86 · GitHub
Skip to content

Commit ab20c86

Browse files
committed
HTTPCLIENT-2426: Do not wrap a discarded (null) response body in a content decoder
(cherry picked from commit 06aac6d)
1 parent f092f56 commit ab20c86

7 files changed

Lines changed: 74 additions & 6 deletions

File tree

‎httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/InflatingAsyncDataConsumer.java‎

Lines changed: 2 additions & 1 deletion

‎httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/InflatingBrotliDataConsumer.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.hc.core5.http.Header;
3636
import org.apache.hc.core5.http.HttpException;
3737
import org.apache.hc.core5.http.nio.AsyncDataConsumer;
38+
import org.apache.hc.core5.util.Args;
3839
import org.apache.hc.core5.http.nio.CapacityChannel;
3940
import org.apache.hc.core5.util.Asserts;
4041

@@ -70,7 +71,7 @@ public final class InflatingBrotliDataConsumer implements AsyncDataConsumer {
7071

7172

7273
public InflatingBrotliDataConsumer(final AsyncDataConsumer downstream) {
73-
this.downstream = downstream;
74+
this.downstream = Args.notNull(downstream, "Downstream data consumer");
7475
try {
7576
this.decoder = new DecoderJNI.Wrapper(8 * 1024);
7677
} catch (final IOException e) {

‎httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/InflatingGzipDataConsumer.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.hc.core5.http.Header;
4040
import org.apache.hc.core5.http.HttpException;
4141
import org.apache.hc.core5.http.nio.AsyncDataConsumer;
42+
import org.apache.hc.core5.util.Args;
4243
import org.apache.hc.core5.http.nio.CapacityChannel;
4344

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

6768
public InflatingGzipDataConsumer(final AsyncDataConsumer downstream) {
68-
this.downstream = downstream;
69+
this.downstream = Args.notNull(downstream, "Downstream data consumer");
6970
}
7071

7172
@Override

‎httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/InflatingZstdDataConsumer.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.apache.hc.core5.http.Header;
3737
import org.apache.hc.core5.http.HttpException;
3838
import org.apache.hc.core5.http.nio.AsyncDataConsumer;
39+
import org.apache.hc.core5.util.Args;
3940
import org.apache.hc.core5.http.nio.CapacityChannel;
4041

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

7576
public InflatingZstdDataConsumer(final AsyncDataConsumer downstream) {
76-
this.downstream = downstream;
77+
this.downstream = Args.notNull(downstream, "Downstream data consumer");
7778
inDirect.limit(0);
7879
outDirect.limit(0);
7980
}

‎httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/ContentCompressionAsyncExec.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ public AsyncDataConsumer handleResponse(final HttpResponse rsp,
152152
ContentCodingSupport.validate(codecs, maxCodecListLen);
153153
if (!codecs.isEmpty()) {
154154
AsyncDataConsumer downstream = cb.handleResponse(rsp, wrapEntityDetails(details));
155+
if (downstream == null) {
156+
return null;
157+
}
155158
for (int i = codecs.size() - 1; i >= 0; i--) {
156159
final String codec = codecs.get(i);
157160
final UnaryOperator<AsyncDataConsumer> op = decoders.lookup(codec);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* ====================================================================
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
* ====================================================================
20+
*
21+
* This software consists of voluntary contributions made by many
22+
* individuals on behalf of the Apache Software Foundation. For more
23+
* information on the Apache Software Foundation, please see
24+
* <http://www.apache.org/>.
25+
*
26+
*/
27+
package org.apache.hc.client5.http.async.methods;
28+
29+
import static org.junit.jupiter.api.Assertions.assertThrows;
30+
31+
import org.junit.jupiter.api.Test;
32+
class TestInflatingGzipDataConsumerNullDownstream {
33+
34+
@Test
35+
void gzipConsumerRejectsNullDownstreamAtConstruction() {
36+
assertThrows(NullPointerException.class, () -> new InflatingGzipDataConsumer(null));
37+
}
38+
39+
}

‎httpclient5/src/test/java/org/apache/hc/client5/http/impl/async/TestContentCompressionAsyncExec.java‎

Lines changed: 24 additions & 2 deletions

0 commit comments

Comments
 (0)