Fix HTTPCLIENT-2331: Skip cookie header generation if a Cookie header is already present (#561)

This fix ensures that if a Cookie header is already present in the request, HttpClient skips adding additional Cookie headers from the BasicCookieStore.
This commit is contained in:
Arturo Bernal 2024-06-20 08:05:55 +02:00 committed by GitHub
parent 4c42c70a51
commit 336263da6c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 0 deletions

View File

@ -45,6 +45,7 @@ import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.http.EntityDetails; import org.apache.hc.core5.http.EntityDetails;
import org.apache.hc.core5.http.Header; import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpException; import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpRequest; import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpRequestInterceptor; import org.apache.hc.core5.http.HttpRequestInterceptor;
import org.apache.hc.core5.http.Method; import org.apache.hc.core5.http.Method;
@ -90,6 +91,17 @@ public class RequestAddCookies implements HttpRequestInterceptor {
return; return;
} }
final Header cookieHeader = request.getHeader(HttpHeaders.COOKIE);
// Check if a Cookie header is already present
if (cookieHeader != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Skipping cookie addition, Cookie header already present in the request");
}
// Skip adding cookies if the Cookie header is already present
return;
}
final HttpClientContext clientContext = HttpClientContext.cast(context); final HttpClientContext clientContext = HttpClientContext.cast(context);
final String exchangeId = clientContext.getExchangeId(); final String exchangeId = clientContext.getExchangeId();

View File

@ -410,4 +410,27 @@ public class TestRequestAddCookies {
Assertions.assertEquals("name1=value; name2=value; name3=value", headers1[0].getValue()); Assertions.assertEquals("name1=value; name2=value; name3=value", headers1[0].getValue());
} }
@Test
public void testSkipAddingCookiesWhenCookieHeaderPresent() throws Exception {
// Prepare a request with an existing Cookie header
final HttpRequest request = new BasicHttpRequest("GET", "/");
request.addHeader("Cookie", "existingCookie=existingValue");
final HttpRoute route = new HttpRoute(this.target, null, false);
final HttpClientContext context = HttpClientContext.create();
context.setRoute(route);
context.setCookieStore(this.cookieStore);
context.setCookieSpecRegistry(this.cookieSpecRegistry);
final HttpRequestInterceptor interceptor = RequestAddCookies.INSTANCE;
interceptor.process(request, null, context);
// Check that no additional cookies were added
final Header[] headers = request.getHeaders("Cookie");
Assertions.assertNotNull(headers);
Assertions.assertEquals(1, headers.length);
Assertions.assertEquals("existingCookie=existingValue", headers[0].getValue());
}
} }