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 2086688157..3923302b87 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 @@ -117,7 +117,7 @@ public final class CookieDecoder { } else if (CookieHeaderNames.EXPIRES.equalsIgnoreCase(name)) { try { long maxAgeMillis = - new HttpHeaderDateFormat().parse(value).getTime() - + HttpHeaderDateFormat.get().parse(value).getTime() - System.currentTimeMillis(); maxAge = maxAgeMillis / 1000 + (maxAgeMillis % 1000 != 0? 1 : 0); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpHeaders.java b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpHeaders.java index 5e3fc33f03..fd82d6e5b8 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpHeaders.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpHeaders.java @@ -304,10 +304,10 @@ public class DefaultHttpHeaders extends HttpHeaders { return value.toString(); } if (value instanceof Date) { - return new HttpHeaderDateFormat().format((Date) value); + return HttpHeaderDateFormat.get().format((Date) value); } if (value instanceof Calendar) { - return new HttpHeaderDateFormat().format(((Calendar) value).getTime()); + return HttpHeaderDateFormat.get().format(((Calendar) value).getTime()); } return value.toString(); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaderDateFormat.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaderDateFormat.java index 1558ba9823..8df847ff41 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaderDateFormat.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaderDateFormat.java @@ -37,11 +37,22 @@ final class HttpHeaderDateFormat extends SimpleDateFormat { private final SimpleDateFormat format1 = new HttpHeaderDateFormatObsolete1(); private final SimpleDateFormat format2 = new HttpHeaderDateFormatObsolete2(); + private static ThreadLocal dateFormatThreadLocal = new ThreadLocal() { + @Override + protected HttpHeaderDateFormat initialValue() { + return new HttpHeaderDateFormat(); + } + }; + + static HttpHeaderDateFormat get() { + return dateFormatThreadLocal.get(); + } + /** * Standard date format

* Sun, 06 Nov 1994 08:49:37 GMT -> E, d MMM yyyy HH:mm:ss z */ - HttpHeaderDateFormat() { + private HttpHeaderDateFormat() { super("E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH); setTimeZone(TimeZone.getTimeZone("GMT")); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaders.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaders.java index f83ee67fb8..903c214c34 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaders.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaders.java @@ -750,7 +750,7 @@ public abstract class HttpHeaders implements Iterable> if (value == null) { throw new ParseException("header not found: " + name, 0); } - return new HttpHeaderDateFormat().parse(value); + return HttpHeaderDateFormat.get().parse(value); } /** @@ -768,7 +768,7 @@ public abstract class HttpHeaders implements Iterable> } try { - return new HttpHeaderDateFormat().parse(value); + return HttpHeaderDateFormat.get().parse(value); } catch (ParseException e) { return defaultValue; } @@ -782,7 +782,7 @@ public abstract class HttpHeaders implements Iterable> */ public static void setDateHeader(HttpMessage message, String name, Date value) { if (value != null) { - message.headers().set(name, new HttpHeaderDateFormat().format(value)); + message.headers().set(name, HttpHeaderDateFormat.get().format(value)); } else { message.headers().set(name, null); } @@ -947,7 +947,7 @@ public abstract class HttpHeaders implements Iterable> */ public static void setDate(HttpMessage message, Date value) { if (value != null) { - message.headers().set(Names.DATE, new HttpHeaderDateFormat().format(value)); + message.headers().set(Names.DATE, HttpHeaderDateFormat.get().format(value)); } else { message.headers().set(Names.DATE, null); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/ServerCookieEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/ServerCookieEncoder.java index b1575e4703..f46176606b 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/ServerCookieEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/ServerCookieEncoder.java @@ -54,7 +54,7 @@ public final class ServerCookieEncoder { if (cookie.getMaxAge() != Long.MIN_VALUE) { if (cookie.getVersion() == 0) { addUnquoted(buf, CookieHeaderNames.EXPIRES, - new HttpHeaderDateFormat().format( + HttpHeaderDateFormat.get().format( new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000L))); } else { 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 8192f5f580..4c14954e11 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 @@ -30,7 +30,7 @@ public class CookieDecoderTest { public void testDecodingSingleCookieV0() { String cookieString = "myCookie=myValue;expires=XXX;path=/apathsomewhere;domain=.adomainsomewhere;secure;"; cookieString = cookieString.replace("XXX", - new HttpHeaderDateFormat().format(new Date(System.currentTimeMillis() + 50000))); + HttpHeaderDateFormat.get().format(new Date(System.currentTimeMillis() + 50000))); Set cookies = CookieDecoder.decode(cookieString); assertEquals(1, cookies.size()); diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/CookieEncoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/CookieEncoderTest.java index 00aa3289c8..bd5149be57 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/CookieEncoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/CookieEncoderTest.java @@ -27,7 +27,7 @@ public class CookieEncoderTest { @Test public void testEncodingSingleCookieV0() { String result = "myCookie=myValue; Expires=XXX; Path=/apathsomewhere; Domain=.adomainsomewhere; Secure"; - DateFormat df = new HttpHeaderDateFormat(); + DateFormat df = HttpHeaderDateFormat.get(); Cookie cookie = new DefaultCookie("myCookie", "myValue"); cookie.setComment("this is a Comment"); cookie.setCommentUrl("http://aurl.com"); diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpHeaderDateFormatTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpHeaderDateFormatTest.java index a72c42024a..5a6fe025d9 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpHeaderDateFormatTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpHeaderDateFormatTest.java @@ -32,7 +32,7 @@ public class HttpHeaderDateFormatTest { @Test public void testParse() throws ParseException { - HttpHeaderDateFormat format = new HttpHeaderDateFormat(); + HttpHeaderDateFormat format = HttpHeaderDateFormat.get(); final Date parsedDateWithSingleDigitDay = format.parse("Sun, 6 Nov 1994 08:49:37 GMT"); Assert.assertNotNull(parsedDateWithSingleDigitDay); @@ -61,7 +61,7 @@ public class HttpHeaderDateFormatTest { @Test public void testFormat() { - HttpHeaderDateFormat format = new HttpHeaderDateFormat(); + HttpHeaderDateFormat format = HttpHeaderDateFormat.get(); final String formatted = format.format(DATE); Assert.assertNotNull(formatted);