|
18 | 18 | import java.io.InputStream;
|
19 | 19 | import java.io.UnsupportedEncodingException;
|
20 | 20 | import java.net.URI;
|
21 |
| -import java.text.ParseException; |
| 21 | +import java.text.ParsePosition; |
22 | 22 | import java.text.SimpleDateFormat;
|
23 | 23 | import java.util.Collection;
|
| 24 | +import java.util.Date; |
24 | 25 | import java.util.List;
|
25 | 26 | import java.util.Locale;
|
26 | 27 |
|
@@ -514,30 +515,30 @@ public static Cookie parseCookie(String value) {
|
514 | 515 | }
|
515 | 516 |
|
516 | 517 | public static int convertExpireField(String timestring) throws Exception {
|
517 |
| - Exception exception = null; |
518 | 518 | String trimmedTimeString = removeQuote(timestring.trim());
|
519 | 519 | long now = System.currentTimeMillis();
|
| 520 | + Date date = null; |
| 521 | + |
520 | 522 | 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; |
529 | 526 | }
|
530 | 527 |
|
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); |
532 | 533 | }
|
533 | 534 |
|
534 | 535 | 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); |
538 | 539 |
|
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); |
541 | 542 | }
|
542 | 543 | return s;
|
543 | 544 | }
|
|
0 commit comments