Skip to content

Commit 0566970

Browse files
committed
Make response headers validation configurable, close AsyncHttpClient#1043
1 parent dd6aed0 commit 0566970

File tree

5 files changed

+52
-54
lines changed

5 files changed

+52
-54
lines changed

client/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -29,64 +29,51 @@ public interface AsyncHttpClientConfig {
2929
String getAhcVersion();
3030

3131
/**
32-
* Return the name of {@link AsyncHttpClient}, which is used for thread
33-
* naming and debugging.
32+
* Return the name of {@link AsyncHttpClient}, which is used for thread naming and debugging.
3433
*
3534
* @return the name.
3635
*/
3736
String getThreadPoolName();
3837

3938
/**
40-
* Return the maximum number of connections an {@link AsyncHttpClient} can
41-
* handle.
39+
* Return the maximum number of connections an {@link AsyncHttpClient} can handle.
4240
*
43-
* @return the maximum number of connections an {@link AsyncHttpClient} can
44-
* handle.
41+
* @return the maximum number of connections an {@link AsyncHttpClient} can handle.
4542
*/
4643
int getMaxConnections();
4744

4845
/**
49-
* Return the maximum number of connections per hosts an
50-
* {@link AsyncHttpClient} can handle.
46+
* Return the maximum number of connections per hosts an {@link AsyncHttpClient} can handle.
5147
*
52-
* @return the maximum number of connections per host an
53-
* {@link AsyncHttpClient} can handle.
48+
* @return the maximum number of connections per host an {@link AsyncHttpClient} can handle.
5449
*/
5550
int getMaxConnectionsPerHost();
5651

5752
/**
58-
* Return the maximum time in millisecond an {@link AsyncHttpClient} can
59-
* wait when connecting to a remote host
53+
* Return the maximum time in millisecond an {@link AsyncHttpClient} can wait when connecting to a remote host
6054
*
61-
* @return the maximum time in millisecond an {@link AsyncHttpClient} can
62-
* wait when connecting to a remote host
55+
* @return the maximum time in millisecond an {@link AsyncHttpClient} can wait when connecting to a remote host
6356
*/
6457
int getConnectTimeout();
6558

6659
/**
67-
* Return the maximum time in millisecond an {@link AsyncHttpClient} can
68-
* stay idle.
60+
* Return the maximum time in millisecond an {@link AsyncHttpClient} can stay idle.
6961
*
70-
* @return the maximum time in millisecond an {@link AsyncHttpClient} can
71-
* stay idle.
62+
* @return the maximum time in millisecond an {@link AsyncHttpClient} can stay idle.
7263
*/
7364
int getReadTimeout();
7465

7566
/**
76-
* Return the maximum time in millisecond an {@link AsyncHttpClient} will
77-
* keep connection in pool.
67+
* Return the maximum time in millisecond an {@link AsyncHttpClient} will keep connection in pool.
7868
*
79-
* @return the maximum time in millisecond an {@link AsyncHttpClient} will
80-
* keep connection in pool.
69+
* @return the maximum time in millisecond an {@link AsyncHttpClient} will keep connection in pool.
8170
*/
8271
int getPooledConnectionIdleTimeout();
8372

8473
/**
85-
* Return the maximum time in millisecond an {@link AsyncHttpClient} waits
86-
* until the response is completed.
74+
* Return the maximum time in millisecond an {@link AsyncHttpClient} waits until the response is completed.
8775
*
88-
* @return the maximum time in millisecond an {@link AsyncHttpClient} waits
89-
* until the response is completed.
76+
* @return the maximum time in millisecond an {@link AsyncHttpClient} waits until the response is completed.
9077
*/
9178
int getRequestTimeout();
9279

@@ -126,13 +113,10 @@ public interface AsyncHttpClientConfig {
126113
boolean isCompressionEnforced();
127114

128115
/**
129-
* Return the {@link java.util.concurrent.ThreadFactory} an
130-
* {@link AsyncHttpClient} use for handling asynchronous response.
116+
* Return the {@link java.util.concurrent.ThreadFactory} an {@link AsyncHttpClient} use for handling asynchronous response.
131117
*
132-
* @return the {@link java.util.concurrent.ThreadFactory} an
133-
* {@link AsyncHttpClient} use for handling asynchronous response.
134-
* If no {@link ThreadFactory} has been explicitly provided, this
135-
* method will return <code>null</code>
118+
* @return the {@link java.util.concurrent.ThreadFactory} an {@link AsyncHttpClient} use for handling asynchronous response. If no {@link ThreadFactory} has been explicitly
119+
* provided, this method will return <code>null</code>
136120
*/
137121
ThreadFactory getThreadFactory();
138122

@@ -179,11 +163,9 @@ public interface AsyncHttpClientConfig {
179163
List<IOExceptionFilter> getIoExceptionFilters();
180164

181165
/**
182-
* Return the number of time the library will retry when an
183-
* {@link java.io.IOException} is throw by the remote server
166+
* Return the number of time the library will retry when an {@link java.io.IOException} is throw by the remote server
184167
*
185-
* @return the number of time the library will retry when an
186-
* {@link java.io.IOException} is throw by the remote server
168+
* @return the number of time the library will retry when an {@link java.io.IOException} is throw by the remote server
187169
*/
188170
int getMaxRequestRetry();
189171

@@ -193,28 +175,22 @@ public interface AsyncHttpClientConfig {
193175
boolean isDisableUrlEncodingForBoundRequests();
194176

195177
/**
196-
* In the case of a POST/Redirect/Get scenario where the server uses a 302
197-
* for the redirect, should AHC respond to the redirect with a GET or
198-
* whatever the original method was. Unless configured otherwise, for a 302,
199-
* AHC, will use a GET for this case.
178+
* In the case of a POST/Redirect/Get scenario where the server uses a 302 for the redirect, should AHC respond to the redirect with a GET or whatever the original method was.
179+
* Unless configured otherwise, for a 302, AHC, will use a GET for this case.
200180
*
201-
* @return <code>true</code> if string 302 handling is to be used, otherwise
202-
* <code>false</code>.
181+
* @return <code>true</code> if string 302 handling is to be used, otherwise <code>false</code>.
203182
*/
204183
boolean isStrict302Handling();
205184

206185
/**
207-
* Return the maximum time in millisecond an {@link AsyncHttpClient} will
208-
* keep connection in the pool, or -1 to keep connection while possible.
186+
* Return the maximum time in millisecond an {@link AsyncHttpClient} will keep connection in the pool, or -1 to keep connection while possible.
209187
*
210-
* @return the maximum time in millisecond an {@link AsyncHttpClient} will
211-
* keep connection in the pool, or -1 to keep connection while
212-
* possible.
188+
* @return the maximum time in millisecond an {@link AsyncHttpClient} will keep connection in the pool, or -1 to keep connection while possible.
213189
*/
214190
int getConnectionTtl();
215191

216192
boolean isUseOpenSsl();
217-
193+
218194
boolean isAcceptAnyCertificate();
219195

220196
/**
@@ -279,6 +255,8 @@ public interface AsyncHttpClientConfig {
279255

280256
KeepAliveStrategy getKeepAliveStrategy();
281257

258+
boolean isValidateResponseHeaders();
259+
282260
interface AdditionalChannelInitializer {
283261

284262
void initChannel(Channel channel) throws Exception;

client/src/main/java/org/asynchttpclient/DefaultAsyncHttpClientConfig.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
import org.asynchttpclient.util.ProxyUtils;
4242

4343
/**
44-
* Configuration class to use with a {@link AsyncHttpClient}. System property
45-
* can be also used to configure this object default behavior by doing: <br>
44+
* Configuration class to use with a {@link AsyncHttpClient}. System property can be also used to configure this object default behavior by doing: <br>
4645
* -Dorg.asynchttpclient.nameOfTheProperty
4746
*
4847
* @see AsyncHttpClientConfig for documentation
@@ -73,6 +72,7 @@ public class DefaultAsyncHttpClientConfig implements AsyncHttpClientConfig {
7372
private final boolean disableZeroCopy;
7473
private final boolean keepEncodingHeader;
7574
private final ProxyServerSelector proxyServerSelector;
75+
private final boolean validateResponseHeaders;
7676

7777
// timeouts
7878
private final int connectTimeout;
@@ -136,6 +136,7 @@ private DefaultAsyncHttpClientConfig(//
136136
boolean disableZeroCopy,//
137137
boolean keepEncodingHeader,//
138138
ProxyServerSelector proxyServerSelector,//
139+
boolean validateResponseHeaders,//
139140

140141
// timeouts
141142
int connectTimeout,//
@@ -198,6 +199,7 @@ private DefaultAsyncHttpClientConfig(//
198199
this.disableZeroCopy = disableZeroCopy;
199200
this.keepEncodingHeader = keepEncodingHeader;
200201
this.proxyServerSelector = proxyServerSelector;
202+
this.validateResponseHeaders = validateResponseHeaders;
201203

202204
// timeouts
203205
this.connectTimeout = connectTimeout;
@@ -373,12 +375,17 @@ public KeepAliveStrategy getKeepAliveStrategy() {
373375
return keepAliveStrategy;
374376
}
375377

378+
@Override
379+
public boolean isValidateResponseHeaders() {
380+
return validateResponseHeaders;
381+
}
382+
376383
// ssl
377384
@Override
378385
public boolean isUseOpenSsl() {
379386
return useOpenSsl;
380387
}
381-
388+
382389
@Override
383390
public boolean isAcceptAnyCertificate() {
384391
return acceptAnyCertificate;
@@ -530,6 +537,7 @@ public static class Builder {
530537
private ProxyServerSelector proxyServerSelector;
531538
private boolean useProxySelector = defaultUseProxySelector();
532539
private boolean useProxyProperties = defaultUseProxyProperties();
540+
private boolean validateResponseHeaders = defaultValidateResponseHeaders();
533541

534542
// timeouts
535543
private int connectTimeout = defaultConnectTimeout();
@@ -676,7 +684,7 @@ public Builder setRealm(Realm realm) {
676684
this.realm = realm;
677685
return this;
678686
}
679-
687+
680688
public Builder setRealm(Realm.Builder realmBuilder) {
681689
this.realm = realmBuilder.build();
682690
return this;
@@ -707,11 +715,16 @@ public Builder setProxyServerSelector(ProxyServerSelector proxyServerSelector) {
707715
return this;
708716
}
709717

718+
public Builder setValidateResponseHeaders(boolean validateResponseHeaders) {
719+
this.validateResponseHeaders = validateResponseHeaders;
720+
return this;
721+
}
722+
710723
public Builder setProxyServer(ProxyServer proxyServer) {
711724
this.proxyServerSelector = ProxyUtils.createProxyServerSelector(proxyServer);
712725
return this;
713726
}
714-
727+
715728
public Builder setProxyServer(ProxyServer.Builder proxyServerBuilder) {
716729
this.proxyServerSelector = ProxyUtils.createProxyServerSelector(proxyServerBuilder.build());
717730
return this;
@@ -970,6 +983,7 @@ public DefaultAsyncHttpClientConfig build() {
970983
disableZeroCopy, //
971984
keepEncodingHeader, //
972985
resolveProxyServerSelector(), //
986+
validateResponseHeaders, //
973987
connectTimeout, //
974988
requestTimeout, //
975989
readTimeout, //

client/src/main/java/org/asynchttpclient/config/AsyncHttpClientConfigDefaults.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public static boolean defaultUseProxySelector() {
7878
public static boolean defaultUseProxyProperties() {
7979
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT_CONFIG_ROOT + "useProxyProperties");
8080
}
81+
82+
public static boolean defaultValidateResponseHeaders() {
83+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT_CONFIG_ROOT + "validateResponseHeaders");
84+
}
8185

8286
public static boolean defaultStrict302Handling() {
8387
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT_CONFIG_ROOT + "strict302Handling");

client/src/main/java/org/asynchttpclient/netty/channel/ChannelManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ private HttpClientCodec newHttpClientCodec() {
377377
config.getHttpClientCodecMaxInitialLineLength(),//
378378
config.getHttpClientCodecMaxHeaderSize(),//
379379
config.getHttpClientCodecMaxChunkSize(),//
380-
false);
380+
false,
381+
config.isValidateResponseHeaders());
381382
}
382383

383384
private SslHandler createSslHandler(String peerHost, int peerPort) {

client/src/main/resources/ahc-default.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ org.asynchttpclient.userAgent=AHC/2.0
1313
org.asynchttpclient.enabledProtocols=TLSv1.2, TLSv1.1, TLSv1
1414
org.asynchttpclient.useProxySelector=false
1515
org.asynchttpclient.useProxyProperties=false
16+
org.asynchttpclient.validateResponseHeaders=true
1617
org.asynchttpclient.strict302Handling=false
1718
org.asynchttpclient.keepAlive=true
1819
org.asynchttpclient.requestCompressionLevel=-1

0 commit comments

Comments
 (0)