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.Collections;
32  import java.util.List;
33  import java.util.Locale;
34  
35  import org.apache.http.FormattedHeader;
36  import org.apache.http.Header;
37  import org.apache.http.HeaderElement;
38  import org.apache.http.NameValuePair;
39  import org.apache.http.annotation.ThreadSafe;
40  import org.apache.http.client.utils.DateUtils;
41  import org.apache.http.cookie.Cookie;
42  import org.apache.http.cookie.CookieAttributeHandler;
43  import org.apache.http.cookie.CookieOrigin;
44  import org.apache.http.cookie.MalformedCookieException;
45  import org.apache.http.cookie.SM;
46  import org.apache.http.message.BasicHeaderElement;
47  import org.apache.http.message.BasicHeaderValueFormatter;
48  import org.apache.http.message.BufferedHeader;
49  import org.apache.http.message.ParserCursor;
50  import org.apache.http.util.Args;
51  import org.apache.http.util.CharArrayBuffer;
52  
53  
54  /**
55   * Cookie specification that strives to closely mimic (mis)behavior of
56   * common web browser applications such as Microsoft Internet Explorer
57   * and Mozilla FireFox.
58   *
59   * @deprecated (4.4) use {@link org.apache.http.impl.cookie.DefaultCookieSpec}.
60   *
61   * @since 4.0
62   */
63  @ThreadSafe
64  public class BrowserCompatSpec extends CookieSpecBase {
65  
66  
67      private static final String[] DEFAULT_DATE_PATTERNS = new String[] {
68          DateUtils.PATTERN_RFC1123,
69          DateUtils.PATTERN_RFC1036,
70          DateUtils.PATTERN_ASCTIME,
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-yyyy HH:mm:ss z",
75          "EEE dd MMM yyyy HH:mm:ss z",
76          "EEE dd-MMM-yyyy HH-mm-ss z",
77          "EEE dd-MMM-yy HH:mm:ss z",
78          "EEE dd MMM yy HH:mm:ss z",
79          "EEE,dd-MMM-yy HH:mm:ss z",
80          "EEE,dd-MMM-yyyy HH:mm:ss z",
81          "EEE, dd-MM-yyyy HH:mm:ss z",
82      };
83  
84      /** Default constructor */
85      public BrowserCompatSpec(final String[] datepatterns, final BrowserCompatSpecFactory.SecurityLevel securityLevel) {
86          super(new BrowserCompatVersionAttributeHandler(),
87                  new BasicDomainHandler(),
88                  securityLevel == BrowserCompatSpecFactory.SecurityLevel.SECURITYLEVEL_IE_MEDIUM ?
89                          new BasicPathHandler() {
90                              @Override
91                              public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException {
92                                  // No validation
93                              }
94                          } : new BasicPathHandler(),
95                  new BasicMaxAgeHandler(),
96                  new BasicSecureHandler(),
97                  new BasicCommentHandler(),
98                  new BasicExpiresHandler(datepatterns != null ? datepatterns.clone() : DEFAULT_DATE_PATTERNS));
99      }
100 
101     /** Default constructor */
102     public BrowserCompatSpec(final String[] datepatterns) {
103         this(datepatterns, BrowserCompatSpecFactory.SecurityLevel.SECURITYLEVEL_DEFAULT);
104     }
105 
106     /** Default constructor */
107     public BrowserCompatSpec() {
108         this(null, BrowserCompatSpecFactory.SecurityLevel.SECURITYLEVEL_DEFAULT);
109     }
110 
111     @Override
112     public List<Cookie> parse(final Header header, final CookieOrigin origin)
113             throws MalformedCookieException {
114         Args.notNull(header, "Header");
115         Args.notNull(origin, "Cookie origin");
116         final String headername = header.getName();
117         if (!headername.equalsIgnoreCase(SM.SET_COOKIE)) {
118             throw new MalformedCookieException("Unrecognized cookie header '"
119                     + header.toString() + "'");
120         }
121         final HeaderElement[] helems = header.getElements();
122         boolean versioned = false;
123         boolean netscape = false;
124         for (final HeaderElement helem: helems) {
125             if (helem.getParameterByName("version") != null) {
126                 versioned = true;
127             }
128             if (helem.getParameterByName("expires") != null) {
129                netscape = true;
130             }
131         }
132         if (netscape || !versioned) {
133             // Need to parse the header again, because Netscape style cookies do not correctly
134             // support multiple header elements (comma cannot be treated as an element separator)
135             final NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
136             final CharArrayBuffer buffer;
137             final ParserCursor cursor;
138             if (header instanceof FormattedHeader) {
139                 buffer = ((FormattedHeader) header).getBuffer();
140                 cursor = new ParserCursor(
141                         ((FormattedHeader) header).getValuePos(),
142                         buffer.length());
143             } else {
144                 final String s = header.getValue();
145                 if (s == null) {
146                     throw new MalformedCookieException("Header value is null");
147                 }
148                 buffer = new CharArrayBuffer(s.length());
149                 buffer.append(s);
150                 cursor = new ParserCursor(0, buffer.length());
151             }
152             final HeaderElement elem = parser.parseHeader(buffer, cursor);
153             final String name = elem.getName();
154             final String value = elem.getValue();
155             if (name == null || name.isEmpty()) {
156                 throw new MalformedCookieException("Cookie name may not be empty");
157             }
158             final BasicClientCookie cookie = new BasicClientCookie(name, value);
159             cookie.setPath(getDefaultPath(origin));
160             cookie.setDomain(getDefaultDomain(origin));
161 
162             // cycle through the parameters
163             final NameValuePair[] attribs = elem.getParameters();
164             for (int j = attribs.length - 1; j >= 0; j--) {
165                 final NameValuePair attrib = attribs[j];
166                 final String s = attrib.getName().toLowerCase(Locale.ROOT);
167                 cookie.setAttribute(s, attrib.getValue());
168                 final CookieAttributeHandler handler = findAttribHandler(s);
169                 if (handler != null) {
170                     handler.parse(cookie, attrib.getValue());
171                 }
172             }
173             // Override version for Netscape style cookies
174             if (netscape) {
175                 cookie.setVersion(0);
176             }
177             return Collections.<Cookie>singletonList(cookie);
178         } else {
179             return parse(helems, origin);
180         }
181     }
182 
183     private static boolean isQuoteEnclosed(final String s) {
184         return s != null && s.startsWith("\"") && s.endsWith("\"");
185     }
186 
187     @Override
188     public List<Header> formatCookies(final List<Cookie> cookies) {
189         Args.notEmpty(cookies, "List of cookies");
190         final CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size());
191         buffer.append(SM.COOKIE);
192         buffer.append(": ");
193         for (int i = 0; i < cookies.size(); i++) {
194             final Cookie cookie = cookies.get(i);
195             if (i > 0) {
196                 buffer.append("; ");
197             }
198             final String cookieName = cookie.getName();
199             final String cookieValue = cookie.getValue();
200             if (cookie.getVersion() > 0 && !isQuoteEnclosed(cookieValue)) {
201                 BasicHeaderValueFormatter.INSTANCE.formatHeaderElement(
202                         buffer,
203                         new BasicHeaderElement(cookieName, cookieValue),
204                         false);
205             } else {
206                 // Netscape style cookies do not support quoted values
207                 buffer.append(cookieName);
208                 buffer.append("=");
209                 if (cookieValue != null) {
210                     buffer.append(cookieValue);
211                 }
212             }
213         }
214         final List<Header> headers = new ArrayList<Header>(1);
215         headers.add(new BufferedHeader(buffer));
216         return headers;
217     }
218 
219     @Override
220     public int getVersion() {
221         return 0;
222     }
223 
224     @Override
225     public Header getVersionHeader() {
226         return null;
227     }
228 
229     @Override
230     public String toString() {
231         return "compatibility";
232     }
233 
234 }