|
| 1 | +/* |
| 2 | + * Copyright (c) 2002-2024 Gargoyle Software Inc. |
| 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 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +package org.htmlunit.http; |
| 16 | + |
| 17 | +import java.lang.ref.SoftReference; |
| 18 | +import java.text.ParsePosition; |
| 19 | +import java.text.SimpleDateFormat; |
| 20 | +import java.util.Calendar; |
| 21 | +import java.util.Date; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Locale; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.TimeZone; |
| 26 | + |
| 27 | +/** |
| 28 | + * Http related utils. |
| 29 | + * |
| 30 | + * @author Ronald Brill |
| 31 | + */ |
| 32 | +public final class HttpUtils { |
| 33 | + |
| 34 | + private static final TimeZone GMT = TimeZone.getTimeZone("GMT"); |
| 35 | + private static final Date DEFAULT_TWO_DIGIT_YEAR_START; |
| 36 | + |
| 37 | + private static final ThreadLocal<SoftReference<Map<String, SimpleDateFormat>>> |
| 38 | + THREADLOCAL_FORMATS = new ThreadLocal<>(); |
| 39 | + |
| 40 | + private static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz"; |
| 41 | + |
| 42 | + private static final String[] DEFAULT_PATTERNS = new String[] { |
| 43 | + PATTERN_RFC1123, |
| 44 | + "EEE, dd-MMM-yy HH:mm:ss zzz", // RFC1036 |
| 45 | + "EEE MMM d HH:mm:ss yyyy" // ASCTIME |
| 46 | + }; |
| 47 | + |
| 48 | + static { |
| 49 | + final Calendar calendar = Calendar.getInstance(); |
| 50 | + calendar.setTimeZone(GMT); |
| 51 | + calendar.set(2000, Calendar.JANUARY, 1, 0, 0, 0); |
| 52 | + calendar.set(Calendar.MILLISECOND, 0); |
| 53 | + |
| 54 | + DEFAULT_TWO_DIGIT_YEAR_START = calendar.getTime(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Parses the specified date string, assuming that it is formatted |
| 59 | + * according to RFC 1123, RFC 1036 or as an ANSI C HTTP date header. |
| 60 | + * |
| 61 | + * @param dateString the string to parse as a date |
| 62 | + * @return the date version of the specified string, |
| 63 | + * or {@code null} if the specified string is {@code null} or unparseable |
| 64 | + */ |
| 65 | + public static Date parseDate(final String dateString) { |
| 66 | + if (dateString == null) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + String dateValue = dateString; |
| 71 | + // trim single quotes around date if present |
| 72 | + if (dateValue.length() > 1 && dateValue.startsWith("'") && dateValue.endsWith("'")) { |
| 73 | + dateValue = dateValue.substring(1, dateValue.length() - 1); |
| 74 | + } |
| 75 | + |
| 76 | + for (final String datePattern : DEFAULT_PATTERNS) { |
| 77 | + final SimpleDateFormat dateFormat = formatFor(datePattern); |
| 78 | + |
| 79 | + final ParsePosition pos = new ParsePosition(0); |
| 80 | + final Date result = dateFormat.parse(dateValue, pos); |
| 81 | + if (pos.getIndex() != 0) { |
| 82 | + return result; |
| 83 | + } |
| 84 | + } |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Formats the given date according to the RFC 1123 pattern |
| 90 | + * 'EEE, dd MMM yyyy HH:mm:ss zzz'. |
| 91 | + * |
| 92 | + * @param date The date to format. |
| 93 | + * @return An RFC 1123 formatted date string. |
| 94 | + */ |
| 95 | + public static String formatDate(final Date date) { |
| 96 | + final SimpleDateFormat formatter = formatFor(PATTERN_RFC1123); |
| 97 | + return formatter.format(date); |
| 98 | + } |
| 99 | + |
| 100 | + // cache the format per thread |
| 101 | + private static SimpleDateFormat formatFor(final String pattern) { |
| 102 | + final SoftReference<Map<String, SimpleDateFormat>> ref = THREADLOCAL_FORMATS.get(); |
| 103 | + Map<String, SimpleDateFormat> formats = ref == null ? null : ref.get(); |
| 104 | + if (formats == null) { |
| 105 | + formats = new HashMap<>(); |
| 106 | + THREADLOCAL_FORMATS.set(new SoftReference<>(formats)); |
| 107 | + } |
| 108 | + |
| 109 | + SimpleDateFormat format = formats.get(pattern); |
| 110 | + if (format == null) { |
| 111 | + format = new SimpleDateFormat(pattern, Locale.US); |
| 112 | + format.setTimeZone(GMT); |
| 113 | + format.set2DigitYearStart(DEFAULT_TWO_DIGIT_YEAR_START); |
| 114 | + formats.put(pattern, format); |
| 115 | + } |
| 116 | + |
| 117 | + return format; |
| 118 | + } |
| 119 | + |
| 120 | + private HttpUtils() { |
| 121 | + } |
| 122 | +} |
0 commit comments