From 6f11711e2799a8df9ebd77e1f441ec4f237a4d10 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Mon, 5 May 2025 14:38:30 +0200 Subject: [PATCH] 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 --- .../HttpComponentsClientHttpRequestFactory.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java index 6d62a8ffbb3..bbddc4f05da 100644 --- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java +++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java @@ -248,9 +248,8 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest context = HttpClientContext.create(); } - // Request configuration not set in the context - if (!(context instanceof HttpClientContext clientContext && clientContext.getRequestConfig() != null) && - context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) { + // No custom request configuration was set + if (!hasCustomRequestConfig(context)) { RequestConfig config = null; // Use request configuration given by the user, when available if (httpRequest instanceof Configurable configurable) { @@ -269,6 +268,18 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest return new HttpComponentsClientHttpRequest(client, httpRequest, context); } + @SuppressWarnings("deprecation") // HttpClientContext.REQUEST_CONFIG + private static boolean hasCustomRequestConfig(HttpContext context) { + if (context instanceof HttpClientContext clientContext) { + // Prior to 5.4, the default config was set to RequestConfig.DEFAULT + // As of 5.4, it is set to null + RequestConfig requestConfig = clientContext.getRequestConfig(); + return requestConfig != null && !requestConfig.equals(RequestConfig.DEFAULT); + } + // Prior to 5.4, the config was stored as an attribute + return context.getAttribute(HttpClientContext.REQUEST_CONFIG) != null; + } + /** * Create a default {@link RequestConfig} to use with the given client.