Skip to content

Commit 082283f

Browse files
committed
("AsyncHttpProviderUtils uses static references to SimpleDateFormat, which is not Thread Safe")
1 parent 20a9c3f commit 082283f

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/main/java/com/ning/http/util/AsyncHttpProviderUtils.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,24 @@ public class AsyncHttpProviderUtils {
4545

4646
private final static String BODY_NOT_COMPUTED = "Response's body hasn't been computed by your AsyncHandler.";
4747

48-
private final static SimpleDateFormat[] RFC2822_LIKE_DATE_FORMATS =
49-
{
50-
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US),
51-
new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US),
52-
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US),
53-
new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss Z", Locale.US),
54-
};
48+
49+
protected final static ThreadLocal<SimpleDateFormat[]> simpleDateFormat = new ThreadLocal<SimpleDateFormat[]>() {
50+
protected SimpleDateFormat[] initialValue() {
51+
52+
return new SimpleDateFormat[]
53+
{
54+
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US),
55+
new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US),
56+
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US),
57+
new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss Z", Locale.US),
58+
};
59+
}
60+
};
61+
62+
public final static SimpleDateFormat[] get() {
63+
return simpleDateFormat.get();
64+
}
65+
5566

5667
//space ' '
5768
static final byte SP = 32;
@@ -442,7 +453,7 @@ public static Cookie parseCookie(String value) {
442453

443454
private static int convertExpireField(String timestring) throws Exception {
444455
Exception exception = null;
445-
for (SimpleDateFormat sdf : RFC2822_LIKE_DATE_FORMATS) {
456+
for (SimpleDateFormat sdf : simpleDateFormat.get()) {
446457
try {
447458
long expire = sdf.parse(removeQuote(timestring.trim())).getTime();
448459
return (int) (expire - System.currentTimeMillis()) / 1000;

0 commit comments

Comments
 (0)