Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/main/java/org/htmlunit/httpclient/HtmlUnitMaxAgeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.htmlunit.httpclient;

import java.util.Date;
import java.util.regex.Pattern;

import org.apache.http.cookie.MalformedCookieException;
import org.apache.http.cookie.SetCookie;
Expand All @@ -25,22 +26,36 @@
* Customized BasicMaxAgeHandler for HtmlUnit.
*
* @author Ronald Brill
* @author Lai Quang Duong
*/
final class HtmlUnitMaxAgeHandler extends BasicMaxAgeHandler {

// Max-Age should be 400 days at most
// https://httpwg.org/http-extensions/draft-ietf-httpbis-rfc6265bis.html#section-5.5
private static final int MAX_MAX_AGE = 400 * 24 * 60 * 60;

private static final Pattern MAX_AGE_PATTERN = Pattern.compile("-?[0-9]+");

@Override
public void parse(final SetCookie cookie, final String value)
throws MalformedCookieException {
Args.notNull(cookie, "Cookie");
if (value == null) {
if (value == null || value.isEmpty()) {
throw new MalformedCookieException("Missing value for 'max-age' attribute");
}
final int age;
if (!MAX_AGE_PATTERN.matcher(value).matches()) {
throw new MalformedCookieException("Invalid 'max-age' attribute: " + value);
}
if (value.startsWith("-")) {
cookie.setExpiryDate(new Date(0L));

Check warning on line 50 in src/main/java/org/htmlunit/httpclient/HtmlUnitMaxAgeHandler.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 Usage of java.util.Date should be replaced with classes from java.time Raw Output: {"level":"warning","locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/httpclient/HtmlUnitMaxAgeHandler.java"},"region":{"endColumn":46,"endLine":50,"startColumn":34,"startLine":50}}}],"message":{"text":"Usage of java.util.Date should be replaced with classes from java.time"},"ruleId":"ReplaceJavaUtilDate","ruleIndex":3}
return;
}
int age;
try {
age = Integer.parseInt(value);
age = Math.min(Integer.parseInt(value), MAX_MAX_AGE);
}
catch (final NumberFormatException e) {
throw new MalformedCookieException("Invalid 'max-age' attribute: " + value, e);
age = MAX_MAX_AGE;
}
cookie.setExpiryDate(new Date(System.currentTimeMillis() + age * 1000L));
}
Expand Down
Loading