Skip to content

Commit 5b5e2b6

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-34854
1 parent a5b0399 commit 5b5e2b6

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
@@ -216,9 +216,8 @@ public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IO
216216
context = HttpClientContext.create();
217217
}
218218

219-
// Request configuration not set in the context
220-
if (!(context instanceof HttpClientContext clientContext && clientContext.getRequestConfig() != null) &&
221-
context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
219+
// No custom request configuration was set
220+
if (!hasCustomRequestConfig(context)) {
222221
RequestConfig config = null;
223222
// Use request configuration given by the user, when available
224223
if (httpRequest instanceof Configurable configurable) {
@@ -237,6 +236,18 @@ public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IO
237236
return new HttpComponentsClientHttpRequest(client, httpRequest, context);
238237
}
239238

239+
@SuppressWarnings("deprecation") // HttpClientContext.REQUEST_CONFIG
240+
private static boolean hasCustomRequestConfig(HttpContext context) {
241+
if (context instanceof HttpClientContext clientContext) {
242+
// Prior to 5.4, the default config was set to RequestConfig.DEFAULT
243+
// As of 5.4, it is set to null
244+
RequestConfig requestConfig = clientContext.getRequestConfig();
245+
return requestConfig != null && !requestConfig.equals(RequestConfig.DEFAULT);
246+
}
247+
// Prior to 5.4, the config was stored as an attribute
248+
return context.getAttribute(HttpClientContext.REQUEST_CONFIG) != null;
249+
}
250+
240251

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

0 commit comments

Comments
 (0)