View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.cookie;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import org.apache.http.FormattedHeader;
34  import org.apache.http.Header;
35  import org.apache.http.HeaderElement;
36  import org.apache.http.annotation.NotThreadSafe;
37  import org.apache.http.client.utils.DateUtils;
38  import org.apache.http.cookie.ClientCookie;
39  import org.apache.http.cookie.Cookie;
40  import org.apache.http.cookie.CookieOrigin;
41  import org.apache.http.cookie.MalformedCookieException;
42  import org.apache.http.cookie.SM;
43  import org.apache.http.message.BasicHeaderElement;
44  import org.apache.http.message.BasicHeaderValueFormatter;
45  import org.apache.http.message.BufferedHeader;
46  import org.apache.http.message.ParserCursor;
47  import org.apache.http.util.Args;
48  import org.apache.http.util.CharArrayBuffer;
49  
50  
51  /**
52   * Cookie specification that strives to closely mimic (mis)behavior of
53   * common web browser applications such as Microsoft Internet Explorer
54   * and Mozilla FireFox.
55   *
56   *
57   * @since 4.0
58   */
59  @NotThreadSafe // superclass is @NotThreadSafe
60  public class BrowserCompatSpec extends CookieSpecBase {
61  
62  
63      private static final String[] DEFAULT_DATE_PATTERNS = new String[] {
64          DateUtils.PATTERN_RFC1123,
65          DateUtils.PATTERN_RFC1036,
66          DateUtils.PATTERN_ASCTIME,
67          "EEE, dd-MMM-yyyy HH:mm:ss z",
68          "EEE, dd-MMM-yyyy HH-mm-ss z",
69          "EEE, dd MMM yy HH:mm:ss z",
70          "EEE dd-MMM-yyyy HH:mm:ss z",
71          "EEE dd MMM yyyy HH:mm:ss z",
72          "EEE dd-MMM-yyyy HH-mm-ss z",
73          "EEE dd-MMM-yy HH:mm:ss z",
74          "EEE dd MMM yy HH:mm:ss z",
75          "EEE,dd-MMM-yy HH:mm:ss z",
76          "EEE,dd-MMM-yyyy HH:mm:ss z",
77          "EEE, dd-MM-yyyy HH:mm:ss z",
78      };
79  
80      private final String[] datepatterns;
81  
82      /** Default constructor */
83      public BrowserCompatSpec(final String[] datepatterns, final BrowserCompatSpecFactory.SecurityLevel securityLevel) {
84          super();
85          if (datepatterns != null) {
86              this.datepatterns = datepatterns.clone();
87          } else {
88              this.datepatterns = DEFAULT_DATE_PATTERNS;
89          }
90          switch (securityLevel) {
91              case SECURITYLEVEL_DEFAULT:
92                  registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
93                  break;
94              case SECURITYLEVEL_IE_MEDIUM:
95                  registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler() {
96                          @Override
97                          public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException {
98                              // No validation
99                          }
100                     }
101                 );
102                 break;
103             default:
104                 throw new RuntimeException("Unknown security level");
105         }
106 
107         registerAttribHandler(ClientCookie.DOMAIN_ATTR, new BasicDomainHandler());
108         registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler());
109         registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
110         registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
111         registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(
112                 this.datepatterns));
113         registerAttribHandler(ClientCookie.VERSION_ATTR, new BrowserCompatVersionAttributeHandler());
114     }
115 
116     /** Default constructor */
117     public BrowserCompatSpec(final String[] datepatterns) {
118         this(datepatterns, BrowserCompatSpecFactory.SecurityLevel.SECURITYLEVEL_DEFAULT);
119     }
120 
121     /** Default constructor */
122     public BrowserCompatSpec() {
123         this(null, BrowserCompatSpecFactory.SecurityLevel.SECURITYLEVEL_DEFAULT);
124     }
125 
126     @Override
127     public List<Cookie> parse(final Header header, final CookieOrigin origin)
128             throws MalformedCookieException {
129         Args.notNull(header, "Header");
130         Args.notNull(origin, "Cookie origin");
131         final String headername = header.getName();
132         if (!headername.equalsIgnoreCase(SM.SET_COOKIE)) {
133             throw new MalformedCookieException("Unrecognized cookie header '"
134                     + header.toString() + "'");
135         }
136         HeaderElement[] helems = header.getElements();
137         boolean versioned = false;
138         boolean netscape = false;
139         for (final HeaderElement helem: helems) {
140             if (helem.getParameterByName("version") != null) {
141                 versioned = true;
142             }
143             if (helem.getParameterByName("expires") != null) {
144                netscape = true;
145             }
146         }
147         if (netscape || !versioned) {
148             // Need to parse the header again, because Netscape style cookies do not correctly
149             // support multiple header elements (comma cannot be treated as an element separator)
150             final NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
151             final CharArrayBuffer buffer;
152             final ParserCursor cursor;
153             if (header instanceof FormattedHeader) {
154                 buffer = ((FormattedHeader) header).getBuffer();
155                 cursor = new ParserCursor(
156                         ((FormattedHeader) header).getValuePos(),
157                         buffer.length());
158             } else {
159                 final String s = header.getValue();
160                 if (s == null) {
161                     throw new MalformedCookieException("Header value is null");
162                 }
163                 buffer = new CharArrayBuffer(s.length());
164                 buffer.append(s);
165                 cursor = new ParserCursor(0, buffer.length());
166             }
167             helems = new HeaderElement[] { parser.parseHeader(buffer, cursor) };
168         }
169         return parse(helems, origin);
170     }
171 
172     private static boolean isQuoteEnclosed(final String s) {
173         return s != null && s.startsWith("\"") && s.endsWith("\"");
174     }
175 
176     @Override
177     public List<Header> formatCookies(final List<Cookie> cookies) {
178         Args.notEmpty(cookies, "List of cookies");
179         final CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size());
180         buffer.append(SM.COOKIE);
181         buffer.append(": ");
182         for (int i = 0; i < cookies.size(); i++) {
183             final Cookie cookie = cookies.get(i);
184             if (i > 0) {
185                 buffer.append("; ");
186             }
187             final String cookieName = cookie.getName();
188             final String cookieValue = cookie.getValue();
189             if (cookie.getVersion() > 0 && !isQuoteEnclosed(cookieValue)) {
190                 BasicHeaderValueFormatter.INSTANCE.formatHeaderElement(
191                         buffer,
192                         new BasicHeaderElement(cookieName, cookieValue),
193                         false);
194             } else {
195                 // Netscape style cookies do not support quoted values
196                 buffer.append(cookieName);
197                 buffer.append("=");
198                 if (cookieValue != null) {
199                     buffer.append(cookieValue);
200                 }
201             }
202         }
203         final List<Header> headers = new ArrayList<Header>(1);
204         headers.add(new BufferedHeader(buffer));
205         return headers;
206     }
207 
208     @Override
209     public int getVersion() {
210         return 0;
211     }
212 
213     @Override
214     public Header getVersionHeader() {
215         return null;
216     }
217 
218     @Override
219     public String toString() {
220         return "compatibility";
221     }
222 
223 }