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.annotation.NotThreadSafe;
34  
35  import org.apache.http.FormattedHeader;
36  import org.apache.http.Header;
37  import org.apache.http.HeaderElement;
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.CharArrayBuffer;
48  
49  /**
50   * Cookie specification that strives to closely mimic (mis)behavior of
51   * common web browser applications such as Microsoft Internet Explorer
52   * and Mozilla FireFox.
53   *
54   *
55   * @since 4.0
56   */
57  @NotThreadSafe // superclass is @NotThreadSafe
58  public class BrowserCompatSpec extends CookieSpecBase {
59  
60      private static final String[] DEFAULT_DATE_PATTERNS = new String[] {
61          DateUtils.PATTERN_RFC1123,
62          DateUtils.PATTERN_RFC1036,
63          DateUtils.PATTERN_ASCTIME,
64          "EEE, dd-MMM-yyyy HH:mm:ss z",
65          "EEE, dd-MMM-yyyy HH-mm-ss z",
66          "EEE, dd MMM yy HH:mm:ss z",
67          "EEE dd-MMM-yyyy HH:mm:ss z",
68          "EEE dd MMM yyyy HH:mm:ss z",
69          "EEE dd-MMM-yyyy HH-mm-ss z",
70          "EEE dd-MMM-yy HH:mm:ss z",
71          "EEE dd MMM yy HH:mm:ss z",
72          "EEE,dd-MMM-yy HH:mm:ss z",
73          "EEE,dd-MMM-yyyy HH:mm:ss z",
74          "EEE, dd-MM-yyyy HH:mm:ss z",
75      };
76  
77      private final String[] datepatterns;
78  
79      /** Default constructor */
80      public BrowserCompatSpec(final String[] datepatterns) {
81          super();
82          if (datepatterns != null) {
83              this.datepatterns = datepatterns.clone();
84          } else {
85              this.datepatterns = DEFAULT_DATE_PATTERNS;
86          }
87          registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
88          registerAttribHandler(ClientCookie.DOMAIN_ATTR, new BasicDomainHandler());
89          registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler());
90          registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
91          registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
92          registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(
93                  this.datepatterns));
94          registerAttribHandler(ClientCookie.VERSION_ATTR, new BrowserCompatVersionAttributeHandler());
95      }
96  
97      /** Default constructor */
98      public BrowserCompatSpec() {
99          this(null);
100     }
101 
102     public List<Cookie> parse(final Header header, final CookieOrigin origin)
103             throws MalformedCookieException {
104         if (header == null) {
105             throw new IllegalArgumentException("Header may not be null");
106         }
107         if (origin == null) {
108             throw new IllegalArgumentException("Cookie origin may not be null");
109         }
110         String headername = header.getName();
111         if (!headername.equalsIgnoreCase(SM.SET_COOKIE)) {
112             throw new MalformedCookieException("Unrecognized cookie header '"
113                     + header.toString() + "'");
114         }
115         HeaderElement[] helems = header.getElements();
116         boolean versioned = false;
117         boolean netscape = false;
118         for (HeaderElement helem: helems) {
119             if (helem.getParameterByName("version") != null) {
120                 versioned = true;
121             }
122             if (helem.getParameterByName("expires") != null) {
123                netscape = true;
124             }
125         }
126         if (netscape || !versioned) {
127             // Need to parse the header again, because Netscape style cookies do not correctly
128             // support multiple header elements (comma cannot be treated as an element separator)
129             NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
130             CharArrayBuffer buffer;
131             ParserCursor cursor;
132             if (header instanceof FormattedHeader) {
133                 buffer = ((FormattedHeader) header).getBuffer();
134                 cursor = new ParserCursor(
135                         ((FormattedHeader) header).getValuePos(),
136                         buffer.length());
137             } else {
138                 String s = header.getValue();
139                 if (s == null) {
140                     throw new MalformedCookieException("Header value is null");
141                 }
142                 buffer = new CharArrayBuffer(s.length());
143                 buffer.append(s);
144                 cursor = new ParserCursor(0, buffer.length());
145             }
146             helems = new HeaderElement[] { parser.parseHeader(buffer, cursor) };
147         }
148         return parse(helems, origin);
149     }
150 
151     public List<Header> formatCookies(final List<Cookie> cookies) {
152         if (cookies == null) {
153             throw new IllegalArgumentException("List of cookies may not be null");
154         }
155         if (cookies.isEmpty()) {
156             throw new IllegalArgumentException("List of cookies may not be empty");
157         }
158         CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size());
159         buffer.append(SM.COOKIE);
160         buffer.append(": ");
161         for (int i = 0; i < cookies.size(); i++) {
162             Cookie cookie = cookies.get(i);
163             if (i > 0) {
164                 buffer.append("; ");
165             }
166             String cookieName = cookie.getName();
167             String cookieValue = cookie.getValue();
168             if (cookie.getVersion() > 0 &&
169                     !(cookieValue.startsWith("\"") && cookieValue.endsWith("\""))) {
170                 BasicHeaderValueFormatter.DEFAULT.formatHeaderElement(
171                         buffer,
172                         new BasicHeaderElement(cookieName, cookieValue),
173                         false);
174             } else {
175                 // Netscape style cookies do not support quoted values
176                 buffer.append(cookieName);
177                 buffer.append("=");
178                 if (cookieValue != null) {
179                     buffer.append(cookieValue);
180                 }
181             }
182         }
183         List<Header> headers = new ArrayList<Header>(1);
184         headers.add(new BufferedHeader(buffer));
185         return headers;
186     }
187 
188     public int getVersion() {
189         return 0;
190     }
191 
192     public Header getVersionHeader() {
193         return null;
194     }
195 
196     @Override
197     public String toString() {
198         return "compatibility";
199     }
200 
201 }