Generate Expires attribute along MaxAge one so IE can honor it, close #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 #1466.
This commit is contained in:
parent
1c6b3307be
commit
598f33040e
@ -15,13 +15,14 @@
|
|||||||
*/
|
*/
|
||||||
package io.netty.handler.codec.http;
|
package io.netty.handler.codec.http;
|
||||||
|
|
||||||
|
import static io.netty.handler.codec.http.CookieEncoderUtil.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static io.netty.handler.codec.http.CookieEncoderUtil.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A <a href="http://tools.ietf.org/html/rfc6265">RFC6265</a> compliant cookie encoder to be used server side,
|
* A <a href="http://tools.ietf.org/html/rfc6265">RFC6265</a> compliant cookie encoder to be used server side,
|
||||||
* so some fields are sent (Version is typically ignored).
|
* so some fields are sent (Version is typically ignored).
|
||||||
@ -68,6 +69,8 @@ public final class ServerCookieEncoder {
|
|||||||
|
|
||||||
if (cookie.maxAge() != Long.MIN_VALUE) {
|
if (cookie.maxAge() != Long.MIN_VALUE) {
|
||||||
add(buf, CookieHeaderNames.MAX_AGE, cookie.maxAge());
|
add(buf, CookieHeaderNames.MAX_AGE, cookie.maxAge());
|
||||||
|
Date expires = new Date(cookie.maxAge() * 1000 + System.currentTimeMillis());
|
||||||
|
addUnquoted(buf, CookieHeaderNames.EXPIRES, HttpHeaderDateFormat.get().format(expires));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cookie.path() != null) {
|
if (cookie.path() != null) {
|
||||||
|
@ -17,22 +17,37 @@ package io.netty.handler.codec.http;
|
|||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class ServerCookieEncoderTest {
|
public class ServerCookieEncoderTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEncodingSingleCookieV0() {
|
public void testEncodingSingleCookieV0() throws ParseException {
|
||||||
String result = "myCookie=myValue; Max-Age=50; Path=/apathsomewhere; Domain=.adomainsomewhere; Secure";
|
|
||||||
|
int maxAge = 50;
|
||||||
|
|
||||||
|
String result =
|
||||||
|
"myCookie=myValue; Max-Age=50; Expires=(.+?); Path=/apathsomewhere; Domain=.adomainsomewhere; Secure";
|
||||||
Cookie cookie = new DefaultCookie("myCookie", "myValue");
|
Cookie cookie = new DefaultCookie("myCookie", "myValue");
|
||||||
cookie.setDomain(".adomainsomewhere");
|
cookie.setDomain(".adomainsomewhere");
|
||||||
cookie.setMaxAge(50);
|
cookie.setMaxAge(maxAge);
|
||||||
cookie.setPath("/apathsomewhere");
|
cookie.setPath("/apathsomewhere");
|
||||||
cookie.setSecure(true);
|
cookie.setSecure(true);
|
||||||
|
|
||||||
String encodedCookie = ServerCookieEncoder.encode(cookie);
|
String encodedCookie = ServerCookieEncoder.encode(cookie);
|
||||||
assertEquals(result, encodedCookie);
|
|
||||||
|
Matcher matcher = Pattern.compile(result).matcher(encodedCookie);
|
||||||
|
assertTrue(matcher.find());
|
||||||
|
Date expiresDate = HttpHeaderDateFormat.get().parse(matcher.group(1));
|
||||||
|
long diff = (expiresDate.getTime() - System.currentTimeMillis()) / 1000;
|
||||||
|
// 1 sec should be fine
|
||||||
|
assertTrue(Math.abs(diff - maxAge) <= 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user