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 java.util.ArrayList;
18  import java.util.BitSet;
19  import java.util.Calendar;
20  import java.util.Collections;
21  import java.util.Comparator;
22  import java.util.Date;
23  import java.util.List;
24  import java.util.Locale;
25  
26  import org.apache.http.FormattedHeader;
27  import org.apache.http.Header;
28  import org.apache.http.HeaderElement;
29  import org.apache.http.NameValuePair;
30  import org.apache.http.ParseException;
31  import org.apache.http.client.utils.DateUtils;
32  import org.apache.http.cookie.Cookie;
33  import org.apache.http.cookie.CookieAttributeHandler;
34  import org.apache.http.cookie.CookieOrigin;
35  import org.apache.http.cookie.CookiePathComparator;
36  import org.apache.http.cookie.MalformedCookieException;
37  import org.apache.http.cookie.SM;
38  import org.apache.http.impl.cookie.BasicClientCookie;
39  import org.apache.http.impl.cookie.BasicCommentHandler;
40  import org.apache.http.impl.cookie.CookieSpecBase;
41  import org.apache.http.message.BasicHeader;
42  import org.apache.http.message.BasicHeaderElement;
43  import org.apache.http.message.BasicNameValuePair;
44  import org.apache.http.message.BufferedHeader;
45  import org.apache.http.message.ParserCursor;
46  import org.apache.http.message.TokenParser;
47  import org.apache.http.util.CharArrayBuffer;
48  import org.htmlunit.BrowserVersion;
49  import org.htmlunit.util.StringUtils;
50  
51  /**
52   * Customized BrowserCompatSpec for HtmlUnit.
53   * <p>
54   * Workaround for <a href="https://issues.apache.org/jira/browse/HTTPCLIENT-1006">HttpClient bug 1006</a>:
55   * quotes are wrongly removed in cookie's values.
56   * </p>
57   * Implementation is based on the HttpClient code.
58   *
59   * @author Mike Bowler
60   * @author Noboru Sinohara
61   * @author David D. Kilzer
62   * @author Marc Guillemot
63   * @author Brad Clarke
64   * @author Ahmed Ashour
65   * @author Nicolas Belisle
66   * @author Ronald Brill
67   * @author John J Murdoch
68   */
69  public class HtmlUnitBrowserCompatCookieSpec extends CookieSpecBase {
70  
71      /** The cookie name used for cookies with no name (HttpClient doesn't like empty names). */
72      public static final String EMPTY_COOKIE_NAME = "HTMLUNIT_EMPTY_COOKIE";
73  
74      /** Workaround for domain of local files. */
75      public static final String LOCAL_FILESYSTEM_DOMAIN = "LOCAL_FILESYSTEM";
76  
77      /**
78       * Comparator for sending cookies in right order.
79       * See specification:
80       * - RFC2109 (#4.3.4) http://www.ietf.org/rfc/rfc2109.txt
81       * - RFC2965 (#3.3.4) http://www.ietf.org/rfc/rfc2965.txt http://www.ietf.org/rfc/rfc2109.txt
82       */
83      private static final Comparator<Cookie> COOKIE_COMPARATOR = new CookiePathComparator();
84  
85      private static final NetscapeDraftHeaderParser DEFAULT_NETSCAPE_DRAFT_HEADER_PARSER
86                              = new NetscapeDraftHeaderParser();
87  
88      static final Date DATE_1_1_1970;
89  
90      static {
91          final Calendar calendar = Calendar.getInstance(Locale.ROOT);
92          calendar.setTimeZone(DateUtils.GMT);
93          calendar.set(1970, Calendar.JANUARY, 1, 0, 0, 0);
94          calendar.set(Calendar.MILLISECOND, 0);
95          DATE_1_1_1970 = calendar.getTime();
96      }
97  
98      /**
99       * Constructor.
100      *
101      * @param browserVersion the {@link BrowserVersion} to simulate
102      */
103     public HtmlUnitBrowserCompatCookieSpec(final BrowserVersion browserVersion) {
104         super(new HtmlUnitVersionAttributeHandler(),
105                 new HtmlUnitDomainHandler(browserVersion),
106                 new HtmlUnitPathHandler(),
107                 new HtmlUnitMaxAgeHandler(),
108                 new HtmlUnitSecureHandler(),
109                 new BasicCommentHandler(),
110                 new HtmlUnitExpiresHandler(browserVersion),
111                 new HtmlUnitHttpOnlyHandler(),
112                 new HtmlUnitSameSiteHandler());
113     }
114 
115     /**
116      * A hack to support cookies without a name; such cookies get the name {@link #EMPTY_COOKIE_NAME}.
117      *
118      * @param header the cookie header
119      * @return the given header or a new header with the cookie name added
120      */
121     private static Header supportEmptyCookieName(final Header header) {
122         final String text = header.getValue();
123         int endPos = text.indexOf(';');
124         if (endPos < 0) {
125             endPos = text.indexOf('=');
126         }
127         else {
128             final int pos = text.indexOf('=');
129             if (pos > endPos) {
130                 endPos = -1;
131             }
132             else {
133                 endPos = pos;
134             }
135         }
136         if (endPos < 0) {
137             return new BasicHeader(header.getName(), EMPTY_COOKIE_NAME + "=" + header.getValue());
138         }
139         if (endPos == 0 || StringUtils.isBlank(text.substring(0, endPos))) {
140             return new BasicHeader(header.getName(), EMPTY_COOKIE_NAME + header.getValue());
141         }
142         return header;
143     }
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
149     public List<Cookie> parse(final Header rawHeader, final CookieOrigin origin) throws MalformedCookieException {
150         final Header header = supportEmptyCookieName(rawHeader);
151 
152         final String headername = header.getName();
153         if (!SM.SET_COOKIE.equalsIgnoreCase(headername)) {
154             throw new MalformedCookieException("Unrecognized cookie header '" + header + "'");
155         }
156         final HeaderElement[] helems = header.getElements();
157         boolean versioned = false;
158         boolean netscape = false;
159         for (final HeaderElement helem: helems) {
160             if (helem.getParameterByName("version") != null) {
161                 versioned = true;
162             }
163             if (helem.getParameterByName("expires") != null) {
164                 netscape = true;
165             }
166         }
167 
168         final List<Cookie> cookies;
169         if (netscape || !versioned) {
170             // Need to parse the header again, because Netscape style cookies do not correctly
171             // support multiple header elements (comma cannot be treated as an element separator)
172             final CharArrayBuffer buffer;
173             final ParserCursor cursor;
174             if (header instanceof FormattedHeader formattedHeader) {
175                 buffer = formattedHeader.getBuffer();
176                 cursor = new ParserCursor(
177                         formattedHeader.getValuePos(),
178                         buffer.length());
179             }
180             else {
181                 final String s = header.getValue();
182                 if (s == null) {
183                     throw new MalformedCookieException("Header value is null");
184                 }
185                 buffer = new CharArrayBuffer(s.length());
186                 buffer.append(s);
187                 cursor = new ParserCursor(0, buffer.length());
188             }
189             final HeaderElement elem = DEFAULT_NETSCAPE_DRAFT_HEADER_PARSER.parseHeader(buffer, cursor);
190             final String name = elem.getName();
191             if (name == null || name.isEmpty()) {
192                 throw new MalformedCookieException("Cookie name may not be empty");
193             }
194             final String value = elem.getValue();
195             final BasicClientCookie cookie = new BasicClientCookie(name, value);
196             cookie.setPath(getDefaultPath(origin));
197             cookie.setDomain(getDefaultDomain(origin));
198 
199             // cycle through the parameters
200             final NameValuePair[] attribs = elem.getParameters();
201             for (int j = attribs.length - 1; j >= 0; j--) {
202                 final NameValuePair attrib = attribs[j];
203                 final String s = attrib.getName().toLowerCase(Locale.ROOT);
204                 cookie.setAttribute(s, attrib.getValue());
205                 final CookieAttributeHandler handler = findAttribHandler(s);
206                 if (handler != null) {
207                     handler.parse(cookie, attrib.getValue());
208                 }
209             }
210             // Override version for Netscape style cookies
211             if (netscape) {
212                 cookie.setVersion(0);
213             }
214             cookies = Collections.singletonList(cookie);
215         }
216         else {
217             cookies = parse(helems, origin);
218         }
219 
220         for (final Cookie c : cookies) {
221             // re-add quotes around value if parsing as incorrectly trimmed them
222             if (header.getValue().contains(c.getName() + "=\"" + c.getValue())) {
223                 ((BasicClientCookie) c).setValue('"' + c.getValue() + '"');
224             }
225         }
226         return cookies;
227     }
228 
229     @Override
230     public List<Header> formatCookies(final List<Cookie> cookies) {
231         cookies.sort(COOKIE_COMPARATOR);
232 
233         final CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size());
234         buffer.append(SM.COOKIE);
235         buffer.append(": ");
236         final int size = cookies.size();
237         for (int i = 0; i < size; i++) {
238             final Cookie cookie = cookies.get(i);
239             if (i > 0) {
240                 buffer.append("; ");
241             }
242             final String cookieName = cookie.getName();
243             final String cookieValue = cookie.getValue();
244             if (cookie.getVersion() > 0 && !isQuoteEnclosed(cookieValue)) {
245                 HtmlUnitBrowserCompatCookieHeaderValueFormatter.INSTANCE.formatHeaderElement(
246                         buffer,
247                         new BasicHeaderElement(cookieName, cookieValue),
248                         false);
249             }
250             else {
251                 // Netscape style cookies do not support quoted values
252                 buffer.append(cookieName);
253                 buffer.append("=");
254                 if (cookieValue != null) {
255                     buffer.append(cookieValue);
256                 }
257             }
258         }
259         final List<Header> headers = new ArrayList<>(1);
260         headers.add(new BufferedHeader(buffer));
261         return headers;
262     }
263 
264     private static boolean isQuoteEnclosed(final String s) {
265         return s != null
266                 && s.length() > 1
267                 && '\"' == s.charAt(0)
268                 && '\"' == s.charAt(s.length() - 1);
269     }
270 
271     @Override
272     public int getVersion() {
273         return 0;
274     }
275 
276     @Override
277     public Header getVersionHeader() {
278         return null;
279     }
280 
281     @Override
282     public String toString() {
283         return "compatibility";
284     }
285 
286     private static final class NetscapeDraftHeaderParser {
287 
288         private static final char PARAM_DELIMITER = ';';
289 
290         // IMPORTANT!
291         // These private static variables must be treated as immutable and never exposed outside this class
292         private static final BitSet TOKEN_DELIMS = TokenParser.INIT_BITSET('=', PARAM_DELIMITER);
293         private static final BitSet VALUE_DELIMS = TokenParser.INIT_BITSET(PARAM_DELIMITER);
294 
295         private static final TokenParser TOKEN_PARSER = TokenParser.INSTANCE;
296 
297         HeaderElement parseHeader(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException {
298             final NameValuePair nvp = parseNameValuePair(buffer, cursor);
299             final List<NameValuePair> params = new ArrayList<>();
300             while (!cursor.atEnd()) {
301                 final NameValuePair param = parseNameValuePair(buffer, cursor);
302                 params.add(param);
303             }
304 
305             return new BasicHeaderElement(nvp.getName(), nvp.getValue(),
306                     params.toArray(new NameValuePair[0]));
307         }
308 
309         private NameValuePair parseNameValuePair(final CharArrayBuffer buffer, final ParserCursor cursor) {
310             final String name = TOKEN_PARSER.parseToken(buffer, cursor, TOKEN_DELIMS);
311             if (cursor.atEnd()) {
312                 return new BasicNameValuePair(name, null);
313             }
314 
315             final int delim = buffer.charAt(cursor.getPos());
316             cursor.updatePos(cursor.getPos() + 1);
317             if (delim != '=') {
318                 return new BasicNameValuePair(name, null);
319             }
320 
321             final String value = TOKEN_PARSER.parseToken(buffer, cursor, VALUE_DELIMS);
322             if (!cursor.atEnd()) {
323                 cursor.updatePos(cursor.getPos() + 1);
324             }
325 
326             return new BasicNameValuePair(name, value);
327         }
328     }
329 }