Support long Expires dates and commas in cookie values (#96)

- Contribution by @valodzka
This commit is contained in:
Trustin Lee 2012-06-08 09:25:39 +09:00
parent 50fafdc3d3
commit 92cbe6f980
4 changed files with 39 additions and 9 deletions

View File

@ -70,7 +70,7 @@ public interface Cookie extends Comparable<Cookie> {
/** /**
* Returns the max age of this cookie in seconds. * Returns the max age of this cookie in seconds.
*/ */
int getMaxAge(); long getMaxAge();
/** /**
* Sets the max age of this cookie in seconds. If {@code 0} is specified, * Sets the max age of this cookie in seconds. If {@code 0} is specified,
@ -78,7 +78,7 @@ public interface Cookie extends Comparable<Cookie> {
* immediately. If {@code -1} is specified, this cookie will be removed * immediately. If {@code -1} is specified, this cookie will be removed
* when a user terminates browser. * when a user terminates browser.
*/ */
void setMaxAge(int maxAge); void setMaxAge(long maxAge);
/** /**
* Returns the version of this cookie. * Returns the version of this cookie.

View File

@ -42,7 +42,11 @@ import java.util.regex.Pattern;
public class CookieDecoder { public class CookieDecoder {
private static final Pattern PATTERN = private static final Pattern PATTERN =
Pattern.compile("(?:\\s|[;,])*\\$*([^;=]+)(?:=(?:[\"']((?:\\\\.|[^\"])*)[\"']|([^;,]*)))?(\\s*(?:[;,]+\\s*|$))"); Pattern.compile(
// See: https://github.com/netty/netty/pull/96
//"(?:\\s|[;,])*\\$*([^;=]+)(?:=(?:[\"']((?:\\\\.|[^\"])*)[\"']|([^;,]*)))?(\\s*(?:[;,]+\\s*|$))"
"(?:\\s|[;,])*\\$*([^;=]+)(?:=(?:[\"']((?:\\\\.|[^\"])*)[\"']|([^;]*)))?(\\s*(?:[;,]+\\s*|$))"
);
private static final String COMMA = ","; private static final String COMMA = ",";
@ -121,7 +125,7 @@ public class CookieDecoder {
String commentURL = null; String commentURL = null;
String domain = null; String domain = null;
String path = null; String path = null;
int maxAge = -1; long maxAge = -1;
List<Integer> ports = new ArrayList<Integer>(2); List<Integer> ports = new ArrayList<Integer>(2);
for (int j = i + 1; j < names.size(); j++, i++) { for (int j = i + 1; j < names.size(); j++, i++) {
@ -150,8 +154,7 @@ public class CookieDecoder {
if (maxAgeMillis <= 0) { if (maxAgeMillis <= 0) {
maxAge = 0; maxAge = 0;
} else { } else {
maxAge = (int) (maxAgeMillis / 1000) + maxAge = maxAgeMillis / 1000 + (maxAgeMillis % 1000 != 0? 1 : 0);
(maxAgeMillis % 1000 != 0? 1 : 0);
} }
} catch (ParseException e) { } catch (ParseException e) {
// Ignore. // Ignore.

View File

@ -53,7 +53,7 @@ public class DefaultCookie implements Cookie {
private boolean discard; private boolean discard;
private Set<Integer> ports = Collections.emptySet(); private Set<Integer> ports = Collections.emptySet();
private Set<Integer> unmodifiablePorts = ports; private Set<Integer> unmodifiablePorts = ports;
private int maxAge = -1; private long maxAge = -1;
private int version; private int version;
private boolean secure; private boolean secure;
private boolean httpOnly; private boolean httpOnly;
@ -211,12 +211,12 @@ public class DefaultCookie implements Cookie {
} }
@Override @Override
public int getMaxAge() { public long getMaxAge() {
return maxAge; return maxAge;
} }
@Override @Override
public void setMaxAge(int maxAge) { public void setMaxAge(long maxAge) {
if (maxAge < -1) { if (maxAge < -1) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"maxAge must be either -1, 0, or a positive integer: " + "maxAge must be either -1, 0, or a positive integer: " +

View File

@ -17,9 +17,11 @@ package io.netty.handler.codec.http;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set; import java.util.Set;
import java.util.TimeZone;
import org.junit.Test; import org.junit.Test;
@ -344,4 +346,29 @@ public class CookieDecoderTest {
assertFalse(it.hasNext()); assertFalse(it.hasNext());
} }
@Test
public void testDecodingLongDates() {
Calendar cookieDate = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cookieDate.set(9999, 11, 31, 23, 59, 59);
long expectedMaxAge = (cookieDate.getTimeInMillis() - System.currentTimeMillis()) / 1000;
String source = "Format=EU; expires=Fri, 31-Dec-9999 23:59:59 GMT; path=/";
Set<Cookie> cookies = new CookieDecoder().decode(source);
Cookie c = cookies.iterator().next();
assertTrue(Math.abs(expectedMaxAge - c.getMaxAge()) < 2);
}
@Test
public void testDecodingValueWithComma() {
String source = "UserCookie=timeZoneName=(GMT+04:00) Moscow, St. Petersburg, Volgograd&promocode=&region=BE;" +
" expires=Sat, 01-Dec-2012 10:53:31 GMT; path=/";
Set<Cookie> cookies = new CookieDecoder().decode(source);
Cookie c = cookies.iterator().next();
assertEquals("timeZoneName=(GMT+04:00) Moscow, St. Petersburg, Volgograd&promocode=&region=BE", c.getValue());
}
} }