diff --git a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/RetryUtil.java b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/RetryUtil.java index f8e160a2528..3f8e062833f 100644 --- a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/RetryUtil.java +++ b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/RetryUtil.java @@ -6,14 +6,25 @@ package io.opentelemetry.exporter.internal; import io.opentelemetry.sdk.common.export.GrpcStatusCode; +import java.time.DateTimeException; import java.time.Duration; import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.MonthDay; +import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; import java.time.format.DateTimeParseException; +import java.time.format.ResolverStyle; +import java.time.temporal.ChronoField; +import java.time.temporal.TemporalAccessor; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; +import java.util.Locale; import java.util.OptionalLong; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -29,6 +40,17 @@ public final class RetryUtil { private static final Set RETRYABLE_GRPC_STATUS_CODES; private static final Set RETRYABLE_HTTP_STATUS_CODES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(429, 502, 503, 504))); + private static final DateTimeFormatter RFC_850_DATE = + new DateTimeFormatterBuilder() + .appendPattern("EEEE, dd-MMM-") + // Keep the date unresolved until the two-digit year can be interpreted relative to now. + .appendValueReduced(ChronoField.YEAR_OF_ERA, 2, 2, 2000) + .appendPattern(" HH:mm:ss 'GMT'") + .toFormatter(Locale.US) + .withResolverStyle(ResolverStyle.STRICT); + private static final DateTimeFormatter ASCTIME_DATE = + DateTimeFormatter.ofPattern("EEE MMM ppd HH:mm:ss uuuu", Locale.US) + .withResolverStyle(ResolverStyle.STRICT); static { Set retryableGrpcStatusCodes = new HashSet<>(); @@ -84,12 +106,42 @@ static OptionalLong retryAfterNanos(@Nullable String retryAfter, Instant now) { } try { - Instant retryAfterInstant = - ZonedDateTime.parse(retryAfter, DateTimeFormatter.RFC_1123_DATE_TIME).toInstant(); + Instant retryAfterInstant = parseHttpDate(retryAfter, now); long delayNanos = Duration.between(now, retryAfterInstant).toNanos(); return OptionalLong.of(Math.max(0, delayNanos)); - } catch (DateTimeParseException | ArithmeticException ignored) { + } catch (DateTimeException | ArithmeticException ignored) { return OptionalLong.empty(); } } + + private static Instant parseHttpDate(String value, Instant now) { + try { + return ZonedDateTime.parse(value, DateTimeFormatter.RFC_1123_DATE_TIME).toInstant(); + } catch (DateTimeParseException ignored) { + // RFC 9110 also requires recipients to accept the two obsolete HTTP-date formats. + } + if (value.indexOf(',') < 0) { + return LocalDateTime.parse(value, ASCTIME_DATE).toInstant(ZoneOffset.UTC); + } + + TemporalAccessor parsed = RFC_850_DATE.parse(value); + MonthDay monthDay = MonthDay.from(parsed); + LocalTime time = LocalTime.from(parsed); + int twoDigitYear = parsed.get(ChronoField.YEAR_OF_ERA) % 100; + LocalDateTime futureLimit = LocalDateTime.ofInstant(now, ZoneOffset.UTC).plusYears(50); + // Pick the latest matching year, then check the full timestamp at the boundary. + int year = futureLimit.getYear() - Math.floorMod(futureLimit.getYear() - twoDigitYear, 100); + MonthDay limitDay = MonthDay.from(futureLimit); + if (year == futureLimit.getYear() + && (monthDay.isAfter(limitDay) + || (monthDay.equals(limitDay) && time.isAfter(futureLimit.toLocalTime())))) { + year -= 100; + } + + LocalDate date = LocalDate.of(year, monthDay.getMonth(), monthDay.getDayOfMonth()); + if (date.getDayOfWeek().getValue() != parsed.get(ChronoField.DAY_OF_WEEK)) { + throw new DateTimeException("Day of week does not match the RFC 850 date"); + } + return date.atTime(time).toInstant(ZoneOffset.UTC); + } } diff --git a/exporters/common/src/test/java/io/opentelemetry/exporter/internal/RetryUtilTest.java b/exporters/common/src/test/java/io/opentelemetry/exporter/internal/RetryUtilTest.java index 65b11e26ccc..48b34a3b235 100644 --- a/exporters/common/src/test/java/io/opentelemetry/exporter/internal/RetryUtilTest.java +++ b/exporters/common/src/test/java/io/opentelemetry/exporter/internal/RetryUtilTest.java @@ -8,6 +8,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.params.provider.Arguments.argumentSet; +import java.time.Duration; import java.time.Instant; import java.time.ZoneOffset; import java.time.ZonedDateTime; @@ -58,6 +59,31 @@ private static Stream retryAfterArgs() { ZonedDateTime.ofInstant(NOW.minusSeconds(1), ZoneOffset.UTC) .format(DateTimeFormatter.RFC_1123_DATE_TIME), NOW, - 0L)); + 0L), + argumentSet( + "RFC 850 date", "Friday, 17-Jul-26 00:00:45 GMT", NOW, TimeUnit.SECONDS.toNanos(45)), + argumentSet( + "RFC 850 date at the 50-year limit", + "Friday, 17-Jul-76 00:00:00 GMT", + NOW, + Duration.between(NOW, NOW.atOffset(ZoneOffset.UTC).plusYears(50)).toNanos()), + argumentSet( + "RFC 850 date beyond the 50-year limit resolves to the past", + "Saturday, 17-Jul-76 00:00:01 GMT", + NOW, + 0L), + argumentSet( + "RFC 850 date in the next century", + "Friday, 01-Jan-00 00:00:00 GMT", + Instant.parse("2099-12-31T23:59:59Z"), + TimeUnit.SECONDS.toNanos(1)), + argumentSet("asctime date", "Fri Jul 17 00:00:45 2026", NOW, TimeUnit.SECONDS.toNanos(45)), + argumentSet( + "asctime date with a space-padded day", + "Tue Jul 7 00:00:45 2026", + NOW.minusSeconds(TimeUnit.DAYS.toSeconds(10)), + TimeUnit.SECONDS.toNanos(45)), + argumentSet("invalid RFC 850 date", "Friday, 31-Feb-26 00:00:45 GMT", NOW, null), + argumentSet("invalid asctime date", "Fri Jul 17 24:00:00 2026", NOW, null)); } }