View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.httpclient;
16  
17  import static org.htmlunit.BrowserVersionFeatures.HTTP_COOKIE_EXTENDED_DATE_PATTERNS_1;
18  import static org.htmlunit.BrowserVersionFeatures.HTTP_COOKIE_EXTENDED_DATE_PATTERNS_2;
19  
20  import java.util.Date;
21  import java.util.regex.Pattern;
22  
23  import org.apache.http.client.utils.DateUtils;
24  import org.apache.http.cookie.MalformedCookieException;
25  import org.apache.http.cookie.SetCookie;
26  import org.apache.http.impl.cookie.BasicExpiresHandler;
27  import org.htmlunit.BrowserVersion;
28  
29  /**
30   * Customized BasicExpiresHandler for HtmlUnit.
31   *
32   * @author Mike Bowler
33   * @author Noboru Sinohara
34   * @author David D. Kilzer
35   * @author Marc Guillemot
36   * @author Brad Clarke
37   * @author Ahmed Ashour
38   * @author Nicolas Belisle
39   * @author Ronald Brill
40   * @author John J Murdoch
41   */
42  final class HtmlUnitExpiresHandler extends BasicExpiresHandler {
43  
44      // simplified patterns from BrowserCompatSpec, with yy patterns before similar yyyy patterns
45      private static final Pattern DATE_SEPARATORS = Pattern.compile("[ ,:-]+");
46  
47      private static final String[] DEFAULT_DATE_PATTERNS = {
48          "EEE MMM dd yyyy HH mm ss 'GMT'Z",
49          "EEE dd MMM yy HH mm ss zzz",
50          "EEE dd MMM yyyy HH mm ss zzz",
51          "EEE MMM d HH mm ss yyyy",
52          "EEE dd MMM yy HH mm ss z ",
53          "EEE dd MMM yyyy HH mm ss z ",
54          "EEE dd MM yy HH mm ss z ",
55          "EEE dd MM yyyy HH mm ss z ",
56      };
57  
58      private static final String[] EXTENDED_DATE_PATTERNS_1 = {
59          "EEE MMM dd yyyy HH mm ss 'GMT'Z",
60          "EEE dd MMM yy HH mm ss zzz",
61          "EEE dd MMM yyyy HH mm ss zzz",
62          "EEE MMM d HH mm ss yyyy",
63          "EEE dd MMM yy HH mm ss z ",
64          "EEE dd MMM yyyy HH mm ss z ",
65          "EEE dd MM yy HH mm ss z ",
66          "EEE dd MM yyyy HH mm ss z ",
67          "d/M/yyyy"
68      };
69  
70      private static final String[] EXTENDED_DATE_PATTERNS_2 = {
71          "EEE MMM dd yyyy HH mm ss 'GMT'Z",
72          "EEE dd MMM yy HH mm ss zzz",
73          "EEE dd MMM yyyy HH mm ss zzz",
74          "EEE MMM d HH mm ss yyyy",
75          "EEE dd MMM yy HH mm ss z ",
76          "EEE dd MMM yyyy HH mm ss z ",
77          "EEE dd MM yy HH mm ss z ",
78          "EEE dd MM yyyy HH mm ss z ",
79          "EEE dd MMM yy HH MM ss z",
80          "MMM dd yy HH mm ss"
81      };
82  
83      private final BrowserVersion browserVersion_;
84  
85      HtmlUnitExpiresHandler(final BrowserVersion browserVersion) {
86          super(DEFAULT_DATE_PATTERNS);
87          browserVersion_ = browserVersion;
88      }
89  
90      @Override
91      public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {
92          final String unquoted = value.startsWith("\"") && value.endsWith("\"")
93                                      ? value.substring(1, value.length() - 1)
94                                      : value;
95          final String date = DATE_SEPARATORS.matcher(unquoted).replaceAll(" ");
96  
97          Date startDate = null;
98          String[] datePatterns = DEFAULT_DATE_PATTERNS;
99  
100         if (null != browserVersion_) {
101             startDate = HtmlUnitBrowserCompatCookieSpec.DATE_1_1_1970;
102 
103             if (browserVersion_.hasFeature(HTTP_COOKIE_EXTENDED_DATE_PATTERNS_1)) {
104                 datePatterns = EXTENDED_DATE_PATTERNS_1;
105             }
106 
107             if (browserVersion_.hasFeature(HTTP_COOKIE_EXTENDED_DATE_PATTERNS_2)) {
108                 datePatterns = EXTENDED_DATE_PATTERNS_2;
109             }
110         }
111 
112         final Date expiry = DateUtils.parseDate(date, datePatterns, startDate);
113         cookie.setExpiryDate(expiry);
114     }
115 }