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     public List<Cookie> parse(final Header header, final CookieOrigin origin)
127             throws MalformedCookieException {
128         Args.notNull(header, "Header");
129         Args.notNull(origin, "Cookie origin");
130         final String headername = header.getName();
131         if (!headername.equalsIgnoreCase(SM.SET_COOKIE)) {
132             throw new MalformedCookieException("Unrecognized cookie header '"
133                     + header.toString() + "'");
134         }
135         HeaderElement[] helems = header.getElements();
136         boolean versioned = false;
137         boolean netscape = false;
138         for (final HeaderElement helem: helems) {
139             if (helem.getParameterByName("version") != null) {
140                 versioned = true;
141             }
142             if (helem.getParameterByName("expires") != null) {
143                netscape = true;
144             }
145         }
146         if (netscape || !versioned) {
147             // Need to parse the header again, because Netscape style cookies do not correctly
148             // support multiple header elements (comma cannot be treated as an element separator)
149             final NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
150             final CharArrayBuffer buffer;
151             final ParserCursor cursor;
152             if (header instanceof FormattedHeader) {
153                 buffer = ((FormattedHeader) header).getBuffer();
154                 cursor = new ParserCursor(
155                         ((FormattedHeader) header).getValuePos(),
156                         buffer.length());
157             } else {
158                 final String s = header.getValue();
159                 if (s == null) {
160                     throw new MalformedCookieException("Header value is null");
161                 }
162                 buffer = new CharArrayBuffer(s.length());
163                 buffer.append(s);
164                 cursor = new ParserCursor(0, buffer.length());
165             }
166             helems = new HeaderElement[] { parser.parseHeader(buffer, cursor) };
167         }
168         return parse(helems, origin);
169     }
170 
171     private static boolean isQuoteEnclosed(final String s) {
172         return s != null && s.startsWith("\"") && s.endsWith("\"");
173     }
174 
175     public List<Header> formatCookies(final List<Cookie> cookies) {
176         Args.notEmpty(cookies, "List of cookies");
177         final CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size());
178         buffer.append(SM.COOKIE);
179         buffer.append(": ");
180         for (int i = 0; i < cookies.size(); i++) {
181             final Cookie cookie = cookies.get(i);
182             if (i > 0) {
183                 buffer.append("; ");
184             }
185             final String cookieName = cookie.getName();
186             final String cookieValue = cookie.getValue();
187             if (cookie.getVersion() > 0 && !isQuoteEnclosed(cookieValue)) {
188                 BasicHeaderValueFormatter.INSTANCE.formatHeaderElement(
189                         buffer,
190                         new BasicHeaderElement(cookieName, cookieValue),
191                         false);
192             } else {
193                 // Netscape style cookies do not support quoted values
194                 buffer.append(cookieName);
195                 buffer.append("=");
196                 if (cookieValue != null) {
197                     buffer.append(cookieValue);
198                 }
199             }
200         }
201         final List<Header> headers = new ArrayList<Header>(1);
202         headers.add(new BufferedHeader(buffer));
203         return headers;
204     }
205 
206     public int getVersion() {
207         return 0;
208     }
209 
210     public Header getVersionHeader() {
211         return null;
212     }
213 
214     @Override
215     public String toString() {
216         return "compatibility";
217     }
218 
219 }