Read Expires cookie attribute in HttpComponents connector

Prior to this commit, the HttpComponents implementation for the
`WebClient` would only consider the max-age attribute of response
cookies when parsing the response. This is not aligned with other client
implementations that consider the max-age attribute first, and then the
expires if the former was not present. The expires date is then
translated into a max-age duration. This behavior is done naturally by
several implementations.

This commit updates the `HttpComponentsClientHttpResponse` to do the
same.

Fixes gh-33157
This commit is contained in:
Brian Clozel
2024-09-13 15:49:06 +02:00
parent b388ff60dd
commit 5efb385e64
2 changed files with 47 additions and 3 deletions

View File

@@ -17,6 +17,10 @@
package org.springframework.http.client.reactive;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import org.apache.hc.client5.http.cookie.Cookie;
import org.apache.hc.client5.http.protocol.HttpClientContext;
@@ -70,9 +74,22 @@ class HttpComponentsClientHttpResponse extends AbstractClientHttpResponse {
}
private static long getMaxAgeSeconds(Cookie cookie) {
String expiresAttribute = cookie.getAttribute(Cookie.EXPIRES_ATTR);
String maxAgeAttribute = cookie.getAttribute(Cookie.MAX_AGE_ATTR);
return (maxAgeAttribute != null ? Long.parseLong(maxAgeAttribute) : -1);
if (maxAgeAttribute != null) {
return Long.parseLong(maxAgeAttribute);
}
// only consider expires if max-age is not present
else if (expiresAttribute != null) {
try {
ZonedDateTime expiresDate = ZonedDateTime.parse(expiresAttribute, DateTimeFormatter.RFC_1123_DATE_TIME);
return Duration.between(ZonedDateTime.now(expiresDate.getZone()), expiresDate).toSeconds();
}
catch (DateTimeParseException ex) {
// ignore
}
}
return -1;
}
}