From 92cbe6f98041ce435d91d692510eb6b934a97ee0 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Fri, 8 Jun 2012 09:25:39 +0900 Subject: [PATCH] Support long Expires dates and commas in cookie values (#96) - Contribution by @valodzka --- .../io/netty/handler/codec/http/Cookie.java | 4 +-- .../handler/codec/http/CookieDecoder.java | 11 +++++--- .../handler/codec/http/DefaultCookie.java | 6 ++--- .../handler/codec/http/CookieDecoderTest.java | 27 +++++++++++++++++++ 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/Cookie.java b/codec-http/src/main/java/io/netty/handler/codec/http/Cookie.java index d54a987fa8..21bf4e3f6d 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/Cookie.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/Cookie.java @@ -70,7 +70,7 @@ public interface Cookie extends Comparable { /** * 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 { * 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. diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/CookieDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/CookieDecoder.java index c001f9f041..960b609358 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/CookieDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/CookieDecoder.java @@ -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 ports = new ArrayList(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. diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultCookie.java b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultCookie.java index 974ead2df1..9970d21339 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultCookie.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultCookie.java @@ -53,7 +53,7 @@ public class DefaultCookie implements Cookie { private boolean discard; private Set ports = Collections.emptySet(); private Set 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: " + diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/CookieDecoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/CookieDecoderTest.java index c9da60e3f3..f1643e06bc 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/CookieDecoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/CookieDecoderTest.java @@ -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 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 cookies = new CookieDecoder().decode(source); + + Cookie c = cookies.iterator().next(); + assertEquals("timeZoneName=(GMT+04:00) Moscow, St. Petersburg, Volgograd&promocode=®ion=BE", c.getValue()); + } }