1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package libcore.net.http; 18 19 import dalvik.annotation.compat.UnsupportedAppUsage; 20 import java.text.DateFormat; 21 import java.text.ParseException; 22 import java.text.SimpleDateFormat; 23 import java.util.Date; 24 import java.util.Locale; 25 import java.util.TimeZone; 26 27 /** 28 * Best-effort parser for HTTP dates. 29 */ 30 public final class HttpDate { 31 32 /** 33 * Most websites serve cookies in the blessed format. Eagerly create the parser to ensure such 34 * cookies are on the fast path. 35 */ 36 private static final ThreadLocal<DateFormat> STANDARD_DATE_FORMAT 37 = new ThreadLocal<DateFormat>() { 38 @Override protected DateFormat initialValue() { 39 DateFormat rfc1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); 40 rfc1123.setTimeZone(TimeZone.getTimeZone("UTC")); 41 return rfc1123; 42 } 43 }; 44 45 /** 46 * If we fail to parse a date in a non-standard format, try each of these formats in sequence. 47 */ 48 private static final String[] BROWSER_COMPATIBLE_DATE_FORMATS = new String[] { 49 /* This list comes from {@code org.apache.http.impl.cookie.BrowserCompatSpec}. */ 50 "EEEE, dd-MMM-yy HH:mm:ss zzz", // RFC 1036 51 "EEE MMM d HH:mm:ss yyyy", // ANSI C asctime() 52 "EEE, dd-MMM-yyyy HH:mm:ss z", 53 "EEE, dd-MMM-yyyy HH-mm-ss z", 54 "EEE, dd MMM yy HH:mm:ss z", 55 "EEE dd-MMM-yyyy HH:mm:ss z", 56 "EEE dd MMM yyyy HH:mm:ss z", 57 "EEE dd-MMM-yyyy HH-mm-ss z", 58 "EEE dd-MMM-yy HH:mm:ss z", 59 "EEE dd MMM yy HH:mm:ss z", 60 "EEE,dd-MMM-yy HH:mm:ss z", 61 "EEE,dd-MMM-yyyy HH:mm:ss z", 62 "EEE, dd-MM-yyyy HH:mm:ss z", 63 64 /* RI bug 6641315 claims a cookie of this format was once served by www.yahoo.com */ 65 "EEE MMM d yyyy HH:mm:ss z", 66 }; 67 68 /** 69 * Returns the date for {@code value}. Returns null if the value couldn't be 70 * parsed. 71 */ 72 @UnsupportedAppUsage parse(String value)73 public static Date parse(String value) { 74 try { 75 return STANDARD_DATE_FORMAT.get().parse(value); 76 } catch (ParseException ignore) { 77 } 78 for (String formatString : BROWSER_COMPATIBLE_DATE_FORMATS) { 79 try { 80 return new SimpleDateFormat(formatString, Locale.US).parse(value); 81 } catch (ParseException ignore) { 82 } 83 } 84 return null; 85 } 86 87 /** 88 * Returns the string for {@code value}. 89 */ 90 @UnsupportedAppUsage format(Date value)91 public static String format(Date value) { 92 return STANDARD_DATE_FORMAT.get().format(value); 93 } 94 } 95