Skip to content

Commit 32ddccb

Browse files
author
Stephane Landelle
committed
AsyncHttpProviderUtils.convertExpireField shouldn't be Exception based, close AsyncHttpClient#264
1 parent 8283b33 commit 32ddccb

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
import java.io.InputStream;
1919
import java.io.UnsupportedEncodingException;
2020
import java.net.URI;
21-
import java.text.ParseException;
21+
import java.text.ParsePosition;
2222
import java.text.SimpleDateFormat;
2323
import java.util.Collection;
24+
import java.util.Date;
2425
import java.util.List;
2526
import java.util.Locale;
2627

@@ -514,30 +515,30 @@ public static Cookie parseCookie(String value) {
514515
}
515516

516517
public static int convertExpireField(String timestring) throws Exception {
517-
Exception exception = null;
518518
String trimmedTimeString = removeQuote(timestring.trim());
519519
long now = System.currentTimeMillis();
520+
Date date = null;
521+
520522
for (SimpleDateFormat sdf : simpleDateFormat.get()) {
521-
try {
522-
long expire = sdf.parse(trimmedTimeString).getTime();
523-
return (int) ((expire - now) / 1000);
524-
} catch (ParseException e) {
525-
exception = e;
526-
} catch (NumberFormatException e) {
527-
exception = e;
528-
}
523+
date = sdf.parse(trimmedTimeString, new ParsePosition(0));
524+
if (date != null)
525+
break;
529526
}
530527

531-
throw exception;
528+
if (date != null) {
529+
long expire = date.getTime();
530+
return (int) ((expire - now) / 1000);
531+
} else
532+
throw new IllegalArgumentException("Not a valid expire field " + trimmedTimeString);
532533
}
533534

534535
private final static String removeQuote(String s) {
535-
if (s.startsWith("\"")) {
536-
s = s.substring(1);
537-
}
536+
if (!s.isEmpty()) {
537+
if (s.charAt(0) == '"')
538+
s = s.substring(1);
538539

539-
if (s.endsWith("\"")) {
540-
s = s.substring(0, s.length() - 1);
540+
if (s.charAt(s.length() - 1) == '"')
541+
s = s.substring(0, s.length() - 1);
541542
}
542543
return s;
543544
}

0 commit comments

Comments
 (0)