Correctly set maxAge and expires in ResponseCookie

Issue: SPR-16940
This commit is contained in:
Rossen Stoyanchev
2018-06-14 13:07:17 -04:00
parent 24acae1195
commit 425c311d3c
3 changed files with 83 additions and 8 deletions

View File

@@ -1218,9 +1218,14 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* @see #setZonedDateTime(String, ZonedDateTime)
*/
public void setDate(String headerName, long date) {
set(headerName, formatDate(date));
}
// Package private: also used in ResponseCookie..
static String formatDate(long date) {
Instant instant = Instant.ofEpochMilli(date);
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, GMT);
set(headerName, DATE_FORMATTERS[0].format(zonedDateTime));
ZonedDateTime time = ZonedDateTime.ofInstant(instant, GMT);
return DATE_FORMATTERS[0].format(time);
}
/**

View File

@@ -139,12 +139,10 @@ public final class ResponseCookie extends HttpCookie {
sb.append("; Domain=").append(this.domain);
}
if (!this.maxAge.isNegative()) {
sb.append("; Max-Age=").append(this.maxAge);
sb.append("; Max-Age=").append(this.maxAge.getSeconds());
sb.append("; Expires=");
HttpHeaders headers = new HttpHeaders();
long seconds = this.maxAge.getSeconds();
headers.setExpires(seconds > 0 ? System.currentTimeMillis() + seconds : 0);
sb.append(headers.getFirst(HttpHeaders.EXPIRES));
long millis = this.maxAge.getSeconds() > 0 ? System.currentTimeMillis() + this.maxAge.toMillis() : 0;
sb.append(HttpHeaders.formatDate(millis));
}
if (this.secure) {
@@ -241,7 +239,7 @@ public final class ResponseCookie extends HttpCookie {
ResponseCookieBuilder maxAge(Duration maxAge);
/**
* Set the cookie "Max-Age" attribute in seconds.
* Variant of {@link #maxAge(Duration)} accepting a value in seconds.
*/
ResponseCookieBuilder maxAge(long maxAgeSeconds);