Skip to content

Commit 6f11711

Browse files
committed
Fix HttpClient 5.3.x request config compatibility
As of gh-33806, the HttpComponents client request factory is forward compatible with the 5.4+ versions of that library for configuring HTTP request configuration. This change would not tkae into account configuration set at the Spring level because it would consider the default `RequestConfig` instance as a custom one. This commit ensures that Spring sets its own configuration if no custom configuration was set for all supported HttpComponents generations. Fixes gh-34851
1 parent c067919 commit 6f11711

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,8 @@ public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IO
248248
context = HttpClientContext.create();
249249
}
250250

251-
// Request configuration not set in the context
252-
if (!(context instanceof HttpClientContext clientContext && clientContext.getRequestConfig() != null) &&
253-
context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
251+
// No custom request configuration was set
252+
if (!hasCustomRequestConfig(context)) {
254253
RequestConfig config = null;
255254
// Use request configuration given by the user, when available
256255
if (httpRequest instanceof Configurable configurable) {
@@ -269,6 +268,18 @@ public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IO
269268
return new HttpComponentsClientHttpRequest(client, httpRequest, context);
270269
}
271270

271+
@SuppressWarnings("deprecation") // HttpClientContext.REQUEST_CONFIG
272+
private static boolean hasCustomRequestConfig(HttpContext context) {
273+
if (context instanceof HttpClientContext clientContext) {
274+
// Prior to 5.4, the default config was set to RequestConfig.DEFAULT
275+
// As of 5.4, it is set to null
276+
RequestConfig requestConfig = clientContext.getRequestConfig();
277+
return requestConfig != null && !requestConfig.equals(RequestConfig.DEFAULT);
278+
}
279+
// Prior to 5.4, the config was stored as an attribute
280+
return context.getAttribute(HttpClientContext.REQUEST_CONFIG) != null;
281+
}
282+
272283

273284
/**
274285
* Create a default {@link RequestConfig} to use with the given client.

0 commit comments

Comments
 (0)