ok2c commented on code in PR #456: URL: https://github.com/apache/httpcomponents-client/pull/456#discussion_r1235592336
########## httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java: ########## @@ -206,28 +207,53 @@ protected TimeValue getApparentAge(final HttpCacheEntry entry) { return TimeValue.ofSeconds(diff.getSeconds()); } + /** + * Extracts and processes the Age value from an HttpCacheEntry by tokenizing the Age header value. + * The Age header value is interpreted as a sequence of tokens, and the first token is parsed into a number + * representing the age in delta-seconds. If the first token cannot be parsed into a number, the Age value is + * considered as invalid and this method returns 0. If the first token represents a negative number or a number + * that exceeds Integer.MAX_VALUE, the Age value is set to MAX_AGE (in seconds). + * This method uses CacheSupport.parseTokens to robustly handle the Age header value. + * <p> + * Note: If the HttpCacheEntry contains multiple Age headers, only the first one is considered. + * + * @param entry The HttpCacheEntry from which to extract the Age value. + * @return The Age value in delta-seconds, or MAX_AGE in seconds if the Age value exceeds Integer.MAX_VALUE or + * is negative. If the Age value is invalid (cannot be parsed into a number or contains non-numeric characters), + * this method returns 0. + */ protected long getAgeValue(final HttpCacheEntry entry) { - // This is a header value, we leave as-is - long ageValue = 0; - for (final Header hdr : entry.getHeaders(HttpHeaders.AGE)) { - long hdrAge; + final Header age = entry.getFirstHeader(HttpHeaders.AGE); + if (age != null) { try { - hdrAge = Long.parseLong(hdr.getValue()); - if (hdrAge < 0) { - hdrAge = MAX_AGE.toSeconds(); + final AtomicReference<String> firstToken = new AtomicReference<>(); + CacheSupport.parseTokens(age, token -> firstToken.compareAndSet(null, token)); + final String s = firstToken.get(); + if (s != null) { + long ageValue = Long.parseLong(s); + if (ageValue < 0) { + ageValue = 0; // Handle negative age values as invalid + } else if (ageValue > Integer.MAX_VALUE) { + ageValue = MAX_AGE.toSeconds(); + } + return ageValue; + } + } catch (final NumberFormatException ex) { + if (LOG.isWarnEnabled()) { Review Comment: @arturobernalg Looks very good now. Please do reduce this one to DEBUG priority. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org For additional commands, e-mail: dev-h...@hc.apache.org