|
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 ch.boye.httpclientandroidlib.impl.cookie; |
|
29 |
|
30 import java.util.List; |
|
31 |
|
32 import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; |
|
33 |
|
34 import ch.boye.httpclientandroidlib.FormattedHeader; |
|
35 import ch.boye.httpclientandroidlib.Header; |
|
36 import ch.boye.httpclientandroidlib.HeaderElement; |
|
37 import ch.boye.httpclientandroidlib.cookie.Cookie; |
|
38 import ch.boye.httpclientandroidlib.cookie.CookieOrigin; |
|
39 import ch.boye.httpclientandroidlib.cookie.CookieSpec; |
|
40 import ch.boye.httpclientandroidlib.cookie.MalformedCookieException; |
|
41 import ch.boye.httpclientandroidlib.cookie.SM; |
|
42 import ch.boye.httpclientandroidlib.cookie.SetCookie2; |
|
43 import ch.boye.httpclientandroidlib.message.ParserCursor; |
|
44 import ch.boye.httpclientandroidlib.util.CharArrayBuffer; |
|
45 |
|
46 /** |
|
47 * 'Meta' cookie specification that picks up a cookie policy based on |
|
48 * the format of cookies sent with the HTTP response. |
|
49 * |
|
50 * @since 4.0 |
|
51 */ |
|
52 @NotThreadSafe // CookieSpec fields are @NotThreadSafe |
|
53 public class BestMatchSpec implements CookieSpec { |
|
54 |
|
55 private final String[] datepatterns; |
|
56 private final boolean oneHeader; |
|
57 |
|
58 // Cached values of CookieSpec instances |
|
59 private RFC2965Spec strict; // @NotThreadSafe |
|
60 private RFC2109Spec obsoleteStrict; // @NotThreadSafe |
|
61 private BrowserCompatSpec compat; // @NotThreadSafe |
|
62 |
|
63 public BestMatchSpec(final String[] datepatterns, boolean oneHeader) { |
|
64 super(); |
|
65 this.datepatterns = datepatterns == null ? null : datepatterns.clone(); |
|
66 this.oneHeader = oneHeader; |
|
67 } |
|
68 |
|
69 public BestMatchSpec() { |
|
70 this(null, false); |
|
71 } |
|
72 |
|
73 private RFC2965Spec getStrict() { |
|
74 if (this.strict == null) { |
|
75 this.strict = new RFC2965Spec(this.datepatterns, this.oneHeader); |
|
76 } |
|
77 return strict; |
|
78 } |
|
79 |
|
80 private RFC2109Spec getObsoleteStrict() { |
|
81 if (this.obsoleteStrict == null) { |
|
82 this.obsoleteStrict = new RFC2109Spec(this.datepatterns, this.oneHeader); |
|
83 } |
|
84 return obsoleteStrict; |
|
85 } |
|
86 |
|
87 private BrowserCompatSpec getCompat() { |
|
88 if (this.compat == null) { |
|
89 this.compat = new BrowserCompatSpec(this.datepatterns); |
|
90 } |
|
91 return compat; |
|
92 } |
|
93 |
|
94 public List<Cookie> parse( |
|
95 final Header header, |
|
96 final CookieOrigin origin) throws MalformedCookieException { |
|
97 if (header == null) { |
|
98 throw new IllegalArgumentException("Header may not be null"); |
|
99 } |
|
100 if (origin == null) { |
|
101 throw new IllegalArgumentException("Cookie origin may not be null"); |
|
102 } |
|
103 HeaderElement[] helems = header.getElements(); |
|
104 boolean versioned = false; |
|
105 boolean netscape = false; |
|
106 for (HeaderElement helem: helems) { |
|
107 if (helem.getParameterByName("version") != null) { |
|
108 versioned = true; |
|
109 } |
|
110 if (helem.getParameterByName("expires") != null) { |
|
111 netscape = true; |
|
112 } |
|
113 } |
|
114 if (netscape || !versioned) { |
|
115 // Need to parse the header again, because Netscape style cookies do not correctly |
|
116 // support multiple header elements (comma cannot be treated as an element separator) |
|
117 NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT; |
|
118 CharArrayBuffer buffer; |
|
119 ParserCursor cursor; |
|
120 if (header instanceof FormattedHeader) { |
|
121 buffer = ((FormattedHeader) header).getBuffer(); |
|
122 cursor = new ParserCursor( |
|
123 ((FormattedHeader) header).getValuePos(), |
|
124 buffer.length()); |
|
125 } else { |
|
126 String s = header.getValue(); |
|
127 if (s == null) { |
|
128 throw new MalformedCookieException("Header value is null"); |
|
129 } |
|
130 buffer = new CharArrayBuffer(s.length()); |
|
131 buffer.append(s); |
|
132 cursor = new ParserCursor(0, buffer.length()); |
|
133 } |
|
134 helems = new HeaderElement[] { parser.parseHeader(buffer, cursor) }; |
|
135 return getCompat().parse(helems, origin); |
|
136 } else { |
|
137 if (SM.SET_COOKIE2.equals(header.getName())) { |
|
138 return getStrict().parse(helems, origin); |
|
139 } else { |
|
140 return getObsoleteStrict().parse(helems, origin); |
|
141 } |
|
142 } |
|
143 } |
|
144 |
|
145 public void validate( |
|
146 final Cookie cookie, |
|
147 final CookieOrigin origin) throws MalformedCookieException { |
|
148 if (cookie == null) { |
|
149 throw new IllegalArgumentException("Cookie may not be null"); |
|
150 } |
|
151 if (origin == null) { |
|
152 throw new IllegalArgumentException("Cookie origin may not be null"); |
|
153 } |
|
154 if (cookie.getVersion() > 0) { |
|
155 if (cookie instanceof SetCookie2) { |
|
156 getStrict().validate(cookie, origin); |
|
157 } else { |
|
158 getObsoleteStrict().validate(cookie, origin); |
|
159 } |
|
160 } else { |
|
161 getCompat().validate(cookie, origin); |
|
162 } |
|
163 } |
|
164 |
|
165 public boolean match(final Cookie cookie, final CookieOrigin origin) { |
|
166 if (cookie == null) { |
|
167 throw new IllegalArgumentException("Cookie may not be null"); |
|
168 } |
|
169 if (origin == null) { |
|
170 throw new IllegalArgumentException("Cookie origin may not be null"); |
|
171 } |
|
172 if (cookie.getVersion() > 0) { |
|
173 if (cookie instanceof SetCookie2) { |
|
174 return getStrict().match(cookie, origin); |
|
175 } else { |
|
176 return getObsoleteStrict().match(cookie, origin); |
|
177 } |
|
178 } else { |
|
179 return getCompat().match(cookie, origin); |
|
180 } |
|
181 } |
|
182 |
|
183 public List<Header> formatCookies(final List<Cookie> cookies) { |
|
184 if (cookies == null) { |
|
185 throw new IllegalArgumentException("List of cookies may not be null"); |
|
186 } |
|
187 int version = Integer.MAX_VALUE; |
|
188 boolean isSetCookie2 = true; |
|
189 for (Cookie cookie: cookies) { |
|
190 if (!(cookie instanceof SetCookie2)) { |
|
191 isSetCookie2 = false; |
|
192 } |
|
193 if (cookie.getVersion() < version) { |
|
194 version = cookie.getVersion(); |
|
195 } |
|
196 } |
|
197 if (version > 0) { |
|
198 if (isSetCookie2) { |
|
199 return getStrict().formatCookies(cookies); |
|
200 } else { |
|
201 return getObsoleteStrict().formatCookies(cookies); |
|
202 } |
|
203 } else { |
|
204 return getCompat().formatCookies(cookies); |
|
205 } |
|
206 } |
|
207 |
|
208 public int getVersion() { |
|
209 return getStrict().getVersion(); |
|
210 } |
|
211 |
|
212 public Header getVersionHeader() { |
|
213 return getStrict().getVersionHeader(); |
|
214 } |
|
215 |
|
216 @Override |
|
217 public String toString() { |
|
218 return "best-match"; |
|
219 } |
|
220 |
|
221 } |