Skip to content

Commit da4029d

Browse files
Stephane Landellenormanmaurer
authored andcommitted
Generate Expires attribute along MaxAge one so IE can honor it, close netty#1466
Motivation: Internet Explorer doesn't honor Set-Cookie header Max-Age attribute. It only honors the Expires one. Modification: Always generate an Expires attribute along the Max-Age one. Result: Internet Explorer compatible expiring cookies. Close netty#1466.
1 parent c910dc6 commit da4029d

2 files changed

Lines changed: 33 additions & 17 deletions

File tree

codec-http/src/main/java/io/netty/handler/codec/http/ServerCookieEncoder.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,13 @@ public static String encode(Cookie cookie) {
5252
add(buf, cookie.getName(), cookie.getValue());
5353

5454
if (cookie.getMaxAge() != Long.MIN_VALUE) {
55-
if (cookie.getVersion() == 0) {
56-
addUnquoted(buf, CookieHeaderNames.EXPIRES,
57-
HttpHeaderDateFormat.get().format(
58-
new Date(System.currentTimeMillis() +
59-
cookie.getMaxAge() * 1000L)));
60-
} else {
55+
if (cookie.getVersion() != 0) {
6156
add(buf, CookieHeaderNames.MAX_AGE, cookie.getMaxAge());
6257
}
58+
addUnquoted(buf, CookieHeaderNames.EXPIRES,
59+
HttpHeaderDateFormat.get().format(
60+
new Date(System.currentTimeMillis() +
61+
cookie.getMaxAge() * 1000L)));
6362
}
6463

6564
if (cookie.getPath() != null) {

codec-http/src/test/java/io/netty/handler/codec/http/CookieEncoderTest.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@
1515
*/
1616
package io.netty.handler.codec.http;
1717

18-
import static org.junit.Assert.*;
18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertTrue;
21+
import static org.junit.Assert.fail;
1922

2023
import java.text.DateFormat;
24+
import java.text.ParseException;
2125
import java.util.Date;
2226
import java.util.List;
27+
import java.util.regex.Matcher;
28+
import java.util.regex.Pattern;
2329

2430
import org.junit.Test;
2531

@@ -56,38 +62,49 @@ public void testEncodingSingleCookieV0() {
5662
}
5763
}
5864

65+
private void matchCookie(String cookieValue, String pattern, int maxAge) throws ParseException {
66+
Matcher matcher = Pattern.compile(pattern).matcher(cookieValue);
67+
assertTrue(matcher.find());
68+
Date expiresDate = HttpHeaderDateFormat.get().parse(matcher.group(1));
69+
long diff = (expiresDate.getTime() - System.currentTimeMillis()) / 1000;
70+
// 1 sec should be fine
71+
assertTrue(Math.abs(diff - maxAge) <= 1);
72+
}
73+
5974
@Test
60-
public void testEncodingSingleCookieV1() {
61-
String result = "myCookie=myValue; Max-Age=50; Path=\"/apathsomewhere\"; " +
75+
public void testEncodingSingleCookieV1() throws ParseException {
76+
int maxAge = 50;
77+
String result = "myCookie=myValue; Max-Age=" + maxAge + "; Expires=(.+?); Path=\"/apathsomewhere\"; " +
6278
"Domain=.adomainsomewhere; Secure; Comment=\"this is a Comment\"; Version=1";
6379
Cookie cookie = new DefaultCookie("myCookie", "myValue");
6480
cookie.setVersion(1);
6581
cookie.setComment("this is a Comment");
6682
cookie.setDomain(".adomainsomewhere");
67-
cookie.setMaxAge(50);
83+
cookie.setMaxAge(maxAge);
6884
cookie.setPath("/apathsomewhere");
6985
cookie.setSecure(true);
7086
String encodedCookie = ServerCookieEncoder.encode(cookie);
71-
assertEquals(result, encodedCookie);
87+
matchCookie(encodedCookie, result, maxAge);
7288
}
7389

7490
@Test
75-
public void testEncodingSingleCookieV2() {
76-
String result = "myCookie=myValue; Max-Age=50; Path=\"/apathsomewhere\"; Domain=.adomainsomewhere; " +
77-
"Secure; Comment=\"this is a Comment\"; Version=1; CommentURL=\"http://aurl.com\"; " +
78-
"Port=\"80,8080\"; Discard";
91+
public void testEncodingSingleCookieV2() throws ParseException {
92+
int maxAge = 50;
93+
String result = "myCookie=myValue; Max-Age=" + maxAge + "; Expires=(.+?); Path=\"/apathsomewhere\"; " +
94+
"Domain=.adomainsomewhere; Secure; Comment=\"this is a Comment\"; Version=1; " +
95+
"CommentURL=\"http://aurl.com\"; Port=\"80,8080\"; Discard";
7996
Cookie cookie = new DefaultCookie("myCookie", "myValue");
8097
cookie.setVersion(1);
8198
cookie.setComment("this is a Comment");
8299
cookie.setCommentUrl("http://aurl.com");
83100
cookie.setDomain(".adomainsomewhere");
84101
cookie.setDiscard(true);
85-
cookie.setMaxAge(50);
102+
cookie.setMaxAge(maxAge);
86103
cookie.setPath("/apathsomewhere");
87104
cookie.setPorts(80, 8080);
88105
cookie.setSecure(true);
89106
String encodedCookie = ServerCookieEncoder.encode(cookie);
90-
assertEquals(result, encodedCookie);
107+
matchCookie(encodedCookie, result, maxAge);
91108
}
92109

93110
@Test

0 commit comments

Comments
 (0)