View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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  
22  import org.apache.http.client.utils.DateUtils;
23  import org.apache.http.cookie.MalformedCookieException;
24  import org.apache.http.cookie.SetCookie;
25  import org.apache.http.impl.cookie.BasicExpiresHandler;
26  import org.htmlunit.BrowserVersion;
27  
28  /**
29   * Customized BasicExpiresHandler for HtmlUnit.
30   *
31   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
32   * @author Noboru Sinohara
33   * @author David D. Kilzer
34   * @author Marc Guillemot
35   * @author Brad Clarke
36   * @author Ahmed Ashour
37   * @author Nicolas Belisle
38   * @author Ronald Brill
39   * @author John J Murdoch
40   */
41  final class HtmlUnitExpiresHandler extends BasicExpiresHandler {
42  
43      // simplified patterns from BrowserCompatSpec, with yy patterns before similar yyyy patterns
44      private static final String[] DEFAULT_DATE_PATTERNS = {
45          "EEE MMM dd yyyy HH mm ss 'GMT'Z",
46          "EEE dd MMM yy HH mm ss zzz",
47          "EEE dd MMM yyyy HH mm ss zzz",
48          "EEE MMM d HH mm ss yyyy",
49          "EEE dd MMM yy HH mm ss z ",
50          "EEE dd MMM yyyy HH mm ss z ",
51          "EEE dd MM yy HH mm ss z ",
52          "EEE dd MM yyyy HH mm ss z ",
53      };
54  
55      private static final String[] EXTENDED_DATE_PATTERNS_1 = {
56          "EEE MMM dd yyyy HH mm ss 'GMT'Z",
57          "EEE dd MMM yy HH mm ss zzz",
58          "EEE dd MMM yyyy HH mm ss zzz",
59          "EEE MMM d HH mm ss yyyy",
60          "EEE dd MMM yy HH mm ss z ",
61          "EEE dd MMM yyyy HH mm ss z ",
62          "EEE dd MM yy HH mm ss z ",
63          "EEE dd MM yyyy HH mm ss z ",
64          "d/M/yyyy"
65      };
66  
67      private static final String[] EXTENDED_DATE_PATTERNS_2 = {
68          "EEE MMM dd yyyy HH mm ss 'GMT'Z",
69          "EEE dd MMM yy HH mm ss zzz",
70          "EEE dd MMM yyyy HH mm ss zzz",
71          "EEE MMM d HH mm ss yyyy",
72          "EEE dd MMM yy HH mm ss z ",
73          "EEE dd MMM yyyy HH mm ss z ",
74          "EEE dd MM yy HH mm ss z ",
75          "EEE dd MM yyyy HH mm ss z ",
76          "EEE dd MMM yy HH MM ss z",
77          "MMM dd yy HH mm ss"
78      };
79  
80      private final BrowserVersion browserVersion_;
81  
82      HtmlUnitExpiresHandler(final BrowserVersion browserVersion) {
83          super(DEFAULT_DATE_PATTERNS);
84          browserVersion_ = browserVersion;
85      }
86  
87      @Override
88      public void parse(final SetCookie cookie, String value) throws MalformedCookieException {
89          if (value.startsWith("\"") && value.endsWith("\"")) {
90              value = value.substring(1, value.length() - 1);
91          }
92          value = value.replaceAll("[ ,:-]+", " ");
93  
94          Date startDate = null;
95          String[] datePatterns = DEFAULT_DATE_PATTERNS;
96  
97          if (null != browserVersion_) {
98              startDate = HtmlUnitBrowserCompatCookieSpec.DATE_1_1_1970;
99  
100             if (browserVersion_.hasFeature(HTTP_COOKIE_EXTENDED_DATE_PATTERNS_1)) {
101                 datePatterns = EXTENDED_DATE_PATTERNS_1;
102             }
103 
104             if (browserVersion_.hasFeature(HTTP_COOKIE_EXTENDED_DATE_PATTERNS_2)) {
105                 datePatterns = EXTENDED_DATE_PATTERNS_2;
106             }
107         }
108 
109         final Date expiry = DateUtils.parseDate(value, datePatterns, startDate);
110         cookie.setExpiryDate(expiry);
111     }
112 }