|
| 1 | +package com.qiniu.android.utils; |
| 2 | + |
| 3 | +import android.text.TextUtils; |
| 4 | + |
| 5 | +import java.text.DateFormat; |
| 6 | +import java.text.ParsePosition; |
| 7 | +import java.text.SimpleDateFormat; |
| 8 | +import java.util.Date; |
| 9 | +import java.util.Locale; |
| 10 | + |
| 11 | +import static okhttp3.internal.Util.UTC; |
| 12 | + |
| 13 | +/** |
| 14 | + * Best-effort parser for HTTP dates. |
| 15 | + */ |
| 16 | +public final class HttpDate { |
| 17 | + |
| 18 | + /** |
| 19 | + * Most websites serve cookies in the blessed format. Eagerly create the parser to ensure such |
| 20 | + * cookies are on the fast path. |
| 21 | + */ |
| 22 | + private static final ThreadLocal<DateFormat> STANDARD_DATE_FORMAT = |
| 23 | + new ThreadLocal<DateFormat>() { |
| 24 | + @Override |
| 25 | + protected DateFormat initialValue() { |
| 26 | + // Date format specified by RFC 7231 section 7.1.1.1. |
| 27 | + DateFormat rfc1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US); |
| 28 | + rfc1123.setLenient(false); |
| 29 | + rfc1123.setTimeZone(UTC); |
| 30 | + return rfc1123; |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + /** |
| 35 | + * If we fail to parse a date in a non-standard format, try each of these formats in sequence. |
| 36 | + */ |
| 37 | + private static final String[] BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS = new String[]{ |
| 38 | + // HTTP formats required by RFC2616 but with any timezone. |
| 39 | + "EEE, dd MMM yyyy HH:mm:ss zzz", // RFC 822, updated by RFC 1123 with any TZ |
| 40 | + "EEEE, dd-MMM-yy HH:mm:ss zzz", // RFC 850, obsoleted by RFC 1036 with any TZ. |
| 41 | + "EEE MMM d HH:mm:ss yyyy", // ANSI C's asctime() format |
| 42 | + // Alternative formats. |
| 43 | + "EEE, dd-MMM-yyyy HH:mm:ss z", |
| 44 | + "EEE, dd-MMM-yyyy HH-mm-ss z", |
| 45 | + "EEE, dd MMM yy HH:mm:ss z", |
| 46 | + "EEE dd-MMM-yyyy HH:mm:ss z", |
| 47 | + "EEE dd MMM yyyy HH:mm:ss z", |
| 48 | + "EEE dd-MMM-yyyy HH-mm-ss z", |
| 49 | + "EEE dd-MMM-yy HH:mm:ss z", |
| 50 | + "EEE dd MMM yy HH:mm:ss z", |
| 51 | + "EEE,dd-MMM-yy HH:mm:ss z", |
| 52 | + "EEE,dd-MMM-yyyy HH:mm:ss z", |
| 53 | + "EEE, dd-MM-yyyy HH:mm:ss z", |
| 54 | + |
| 55 | + /* RI bug 6641315 claims a cookie of this format was once served by www.yahoo.com */ |
| 56 | + "EEE MMM d yyyy HH:mm:ss z", |
| 57 | + }; |
| 58 | + |
| 59 | + private static final DateFormat[] BROWSER_COMPATIBLE_DATE_FORMATS = |
| 60 | + new DateFormat[BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS.length]; |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns the date for {@code value}. Returns null if the value couldn't be parsed. |
| 64 | + */ |
| 65 | + public static Date parse(String value) { |
| 66 | + if (TextUtils.isEmpty(value)) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + ParsePosition position = new ParsePosition(0); |
| 71 | + Date result = STANDARD_DATE_FORMAT.get().parse(value, position); |
| 72 | + if (position.getIndex() == value.length()) { |
| 73 | + // STANDARD_DATE_FORMAT must match exactly; all text must be consumed, e.g. no ignored |
| 74 | + // non-standard trailing "+01:00". Those cases are covered below. |
| 75 | + return result; |
| 76 | + } |
| 77 | + synchronized (BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS) { |
| 78 | + for (int i = 0, count = BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS.length; i < count; i++) { |
| 79 | + DateFormat format = BROWSER_COMPATIBLE_DATE_FORMATS[i]; |
| 80 | + if (format == null) { |
| 81 | + format = new SimpleDateFormat(BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS[i], Locale.US); |
| 82 | + // Set the timezone to use when interpreting formats that don't have a timezone. GMT is |
| 83 | + // specified by RFC 7231. |
| 84 | + format.setTimeZone(UTC); |
| 85 | + BROWSER_COMPATIBLE_DATE_FORMATS[i] = format; |
| 86 | + } |
| 87 | + position.setIndex(0); |
| 88 | + result = format.parse(value, position); |
| 89 | + if (position.getIndex() != 0) { |
| 90 | + // Something was parsed. It's possible the entire string was not consumed but we ignore |
| 91 | + // that. If any of the BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS ended in "'GMT'" we'd have |
| 92 | + // to also check that position.getIndex() == value.length() otherwise parsing might have |
| 93 | + // terminated early, ignoring things like "+01:00". Leaving this as != 0 means that any |
| 94 | + // trailing junk is ignored. |
| 95 | + return result; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + private HttpDate() { |
| 103 | + } |
| 104 | +} |
| 105 | + |
0 commit comments