diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/utils/DateUtils.java b/httpclient5/src/main/java/org/apache/hc/client5/http/utils/DateUtils.java index b1365afa43..13b1db0898 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/utils/DateUtils.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/utils/DateUtils.java @@ -34,6 +34,7 @@ import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; +import java.time.temporal.ChronoField; import java.util.Date; import java.util.Locale; import java.util.TimeZone; @@ -73,36 +74,55 @@ public final class DateUtils { /** * Date format pattern used to parse HTTP date headers in RFC 1036 format. + * + * @deprecated use {@link #FORMATTER_RFC1036}. This pattern reads a two-digit year + * from 70 to 99 as 2070 to 2099. */ + @Deprecated public static final String PATTERN_RFC1036 = "EEE, dd-MMM-yy HH:mm:ss zzz"; /** * Date formatter used to parse HTTP date headers in RFC 1036 format. + *

+ * A two-digit year from 70 to 99 is read as 1970 to 1999, and a two-digit + * year from 00 to 69 as 2000 to 2069, as RFC 6265 section 5.1.1 requires. + *

* * @since 5.2 */ public static final DateTimeFormatter FORMATTER_RFC1036 = new DateTimeFormatterBuilder() .parseLenient() .parseCaseInsensitive() - .appendPattern(PATTERN_RFC1036) + .appendPattern("EEE, dd-MMM-") + .appendValueReduced(ChronoField.YEAR_OF_ERA, 2, 2, 1970) + .appendPattern(" HH:mm:ss zzz") .toFormatter(Locale.ENGLISH); /** * Date format pattern used to parse HTTP date headers in ANSI C * {@code asctime()} format. + * + * @deprecated use {@link #FORMATTER_ASCTIME}. This pattern has no time zone + * and does not accept a space-padded day of month. */ + @Deprecated public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy"; /** - * Date formatter used to parse HTTP date headers in in ANSI C {@code asctime()} format. + * Date formatter used to parse HTTP date headers in ANSI C {@code asctime()} format. + *

+ * This format has no time zone. Values are read as UTC, as RFC 9110 section 5.6.7 requires. + * A day of month below 10 may be padded with a space ({@code Nov 6}). + *

* * @since 5.2 */ public static final DateTimeFormatter FORMATTER_ASCTIME = new DateTimeFormatterBuilder() .parseLenient() .parseCaseInsensitive() - .appendPattern(PATTERN_ASCTIME) - .toFormatter(Locale.ENGLISH); + .appendPattern("EEE MMM ppd HH:mm:ss yyyy") + .toFormatter(Locale.ENGLISH) + .withZone(ZoneOffset.UTC); /** * Standard date formatters: {@link #FORMATTER_RFC1123}, {@link #FORMATTER_RFC1036}, {@link #FORMATTER_ASCTIME}. @@ -192,8 +212,8 @@ public static Instant parseDate(final CharSequence dateValue, final DateTimeForm } /** - * Parses the instant value using the standard date/time formats ({@link #PATTERN_RFC1123}, - * {@link #PATTERN_RFC1036}, {@link #PATTERN_ASCTIME}). + * Parses the instant value using the standard date/time formats ({@link #FORMATTER_RFC1123}, + * {@link #FORMATTER_RFC1036}, {@link #FORMATTER_ASCTIME}). * * @param dateValue the instant value to parse * diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestDateUtils.java b/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestDateUtils.java index ad5e40f48d..efd62081aa 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestDateUtils.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestDateUtils.java @@ -120,6 +120,34 @@ void testTwoDigitYearDateParse() { DateUtils.parseDate("Friday, 14-Oct-05 00:00:00 GMT", DateUtils.FORMATTER_RFC1036)); } + @Test + void testTwoDigitYearBefore2000DateParse() { + Assertions.assertEquals(createInstant(1970, Month.JANUARY, 1), + DateUtils.parseDate("Thursday, 01-Jan-70 00:00:00 GMT", DateUtils.FORMATTER_RFC1036)); + Assertions.assertEquals(createInstant(1994, Month.NOVEMBER, 6).plus(8, ChronoUnit.HOURS) + .plus(49, ChronoUnit.MINUTES).plus(37, ChronoUnit.SECONDS), + DateUtils.parseStandardDate("Sunday, 06-Nov-94 08:49:37 GMT")); + Assertions.assertEquals(createInstant(2069, Month.DECEMBER, 31), + DateUtils.parseStandardDate("Tuesday, 31-Dec-69 00:00:00 GMT")); + Assertions.assertEquals(createInstant(1999, Month.DECEMBER, 31), + DateUtils.parseStandardDate("Friday, 31-Dec-99 00:00:00 GMT")); + Assertions.assertEquals(createInstant(2000, Month.JANUARY, 1), + DateUtils.parseStandardDate("Saturday, 01-Jan-00 00:00:00 GMT")); + } + + @Test + void testAsctimeDateParse() { + Assertions.assertEquals(createInstant(1994, Month.NOVEMBER, 6).plus(8, ChronoUnit.HOURS) + .plus(49, ChronoUnit.MINUTES).plus(37, ChronoUnit.SECONDS), + DateUtils.parseDate("Sun Nov 6 08:49:37 1994", DateUtils.FORMATTER_ASCTIME)); + Assertions.assertEquals(createInstant(1994, Month.NOVEMBER, 6).plus(8, ChronoUnit.HOURS) + .plus(49, ChronoUnit.MINUTES).plus(37, ChronoUnit.SECONDS), + DateUtils.parseDate("Sun Nov 6 08:49:37 1994", DateUtils.FORMATTER_ASCTIME)); + Assertions.assertEquals(createInstant(2024, Month.FEBRUARY, 22).plus(17, ChronoUnit.HOURS) + .plus(20, ChronoUnit.MINUTES).plus(18, ChronoUnit.SECONDS), + DateUtils.parseStandardDate("Thu Feb 22 17:20:18 2024")); + } + @Test void testParseQuotedDate() { Assertions.assertEquals(createInstant(2005, Month.OCTOBER, 14), @@ -133,6 +161,8 @@ void testBasicDateFormat() { Assertions.assertEquals("Fri, 14 Oct 2005 00:00:00 GMT", DateUtils.formatDate(instant, DateUtils.FORMATTER_RFC1123)); Assertions.assertEquals("Fri, 14-Oct-05 00:00:00 GMT", DateUtils.formatDate(instant, DateUtils.FORMATTER_RFC1036)); Assertions.assertEquals("Fri Oct 14 00:00:00 2005", DateUtils.formatDate(instant, DateUtils.FORMATTER_ASCTIME)); + Assertions.assertEquals("Sun Nov 6 00:00:00 1994", + DateUtils.formatDate(createInstant(1994, Month.NOVEMBER, 6), DateUtils.FORMATTER_ASCTIME)); } }