Support long Expires dates and commas in cookie values (#96)
- Contribution by @valodzka
This commit is contained in:
parent
50fafdc3d3
commit
92cbe6f980
@ -70,7 +70,7 @@ public interface Cookie extends Comparable<Cookie> {
|
||||
/**
|
||||
* 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,
|
||||
@ -78,7 +78,7 @@ public interface Cookie extends Comparable<Cookie> {
|
||||
* immediately. If {@code -1} is specified, this cookie will be removed
|
||||
* when a user terminates browser.
|
||||
*/
|
||||
void setMaxAge(int maxAge);
|
||||
void setMaxAge(long maxAge);
|
||||
|
||||
/**
|
||||
* Returns the version of this cookie.
|
||||
|
@ -42,7 +42,11 @@ import java.util.regex.Pattern;
|
||||
public class CookieDecoder {
|
||||
|
||||
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 = ",";
|
||||
|
||||
@ -121,7 +125,7 @@ public class CookieDecoder {
|
||||
String commentURL = null;
|
||||
String domain = null;
|
||||
String path = null;
|
||||
int maxAge = -1;
|
||||
long maxAge = -1;
|
||||
List<Integer> ports = new ArrayList<Integer>(2);
|
||||
|
||||
for (int j = i + 1; j < names.size(); j++, i++) {
|
||||
@ -150,8 +154,7 @@ public class CookieDecoder {
|
||||
if (maxAgeMillis <= 0) {
|
||||
maxAge = 0;
|
||||
} else {
|
||||
maxAge = (int) (maxAgeMillis / 1000) +
|
||||
(maxAgeMillis % 1000 != 0? 1 : 0);
|
||||
maxAge = maxAgeMillis / 1000 + (maxAgeMillis % 1000 != 0? 1 : 0);
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
// Ignore.
|
||||
|
@ -53,7 +53,7 @@ public class DefaultCookie implements Cookie {
|
||||
private boolean discard;
|
||||
private Set<Integer> ports = Collections.emptySet();
|
||||
private Set<Integer> unmodifiablePorts = ports;
|
||||
private int maxAge = -1;
|
||||
private long maxAge = -1;
|
||||
private int version;
|
||||
private boolean secure;
|
||||
private boolean httpOnly;
|
||||
@ -211,12 +211,12 @@ public class DefaultCookie implements Cookie {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxAge() {
|
||||
public long getMaxAge() {
|
||||
return maxAge;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMaxAge(int maxAge) {
|
||||
public void setMaxAge(long maxAge) {
|
||||
if (maxAge < -1) {
|
||||
throw new IllegalArgumentException(
|
||||
"maxAge must be either -1, 0, or a positive integer: " +
|
||||
|
@ -17,9 +17,11 @@ package io.netty.handler.codec.http;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@ -344,4 +346,29 @@ public class CookieDecoderTest {
|
||||
|
||||
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=®ion=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=®ion=BE", c.getValue());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user