From 9801783e9f965165ef389ffd96b9dc6709c30e3b Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Tue, 24 Oct 2023 08:52:45 +0200 Subject: [PATCH] Refactor RequestIfRange class to use DateUtils for date parsing. * Updates the RequestIfRange class to utilize DateUtils for parsing standard HTTP dates. * Optimize time difference check in RequestIfRange with Instant API. --- .../client5/http/protocol/RequestIfRange.java | 35 ++++--------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestIfRange.java b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestIfRange.java index 3910e619e9..112e8dd3f0 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestIfRange.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestIfRange.java @@ -30,10 +30,9 @@ import java.io.IOException; -import java.time.Duration; import java.time.Instant; -import java.time.format.DateTimeFormatter; +import org.apache.hc.client5.http.utils.DateUtils; import org.apache.hc.core5.annotation.Contract; import org.apache.hc.core5.annotation.ThreadingBehavior; import org.apache.hc.core5.http.EntityDetails; @@ -70,13 +69,6 @@ @Contract(threading = ThreadingBehavior.IMMUTABLE) public class RequestIfRange implements HttpRequestInterceptor { - /** - * This {@link DateTimeFormatter} is used to format and parse date-time objects in a specific format commonly - * used in HTTP protocol messages. The format includes the day of the week, day of the month, month, year, and time - * of day, all represented in GMT time. An example of a date-time string in this format is "Tue, 15 Nov 1994 08:12:31 GMT". - */ - private static final DateTimeFormatter FORMATTER = DateTimeFormatter.RFC_1123_DATE_TIME; - /** * Singleton instance. * @@ -121,36 +113,21 @@ public void process(final HttpRequest request, final EntityDetails entity, final throw new ProtocolException("'If-Range' header must not contain a weak entity tag."); } - final Header dateHeader = request.getFirstHeader(HttpHeaders.DATE); + final Instant dateInstant = DateUtils.parseStandardDate(request, HttpHeaders.DATE); - if (dateHeader == null) { + if (dateInstant == null) { return; } + final Instant lastModifiedInstant = DateUtils.parseStandardDate(request, HttpHeaders.LAST_MODIFIED); - final Instant lastModifiedInstant; - final Instant dateInstant; - final Header lastModifiedHeader = request.getFirstHeader(HttpHeaders.LAST_MODIFIED); - - if (lastModifiedHeader != null) { - final String lastModifiedValue = lastModifiedHeader.getValue(); - lastModifiedInstant = FORMATTER.parse(lastModifiedValue, Instant::from); - } - else { + if (lastModifiedInstant == null) { // If there's no Last-Modified header, we exit early because we can't deduce that it is strong. return; } - final String dateValue = dateHeader.getValue(); - dateInstant = FORMATTER.parse(dateValue, Instant::from); - - long difference = 0; - if (lastModifiedInstant != null && dateInstant != null) { - difference = Duration.between(lastModifiedInstant, dateInstant).getSeconds(); - } - // If the difference between the Last-Modified and Date headers is less than 1 second, throw an exception - if (difference < 1 && eTag!= null) { + if (lastModifiedInstant.plusSeconds(1).isAfter(dateInstant) && eTag != null) { throw new ProtocolException("'If-Range' header with a Date must be a strong validator."); } }