author | juh |
Tue, 30 Jul 2013 11:04:19 -0700 | |
changeset 19069 | 1d9cb0d080e3 |
parent 18178 | ee71c923891d |
child 21278 | ef8a3a2a72f2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
19069 | 2 |
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.net; |
|
27 |
||
28 |
import java.util.Map; |
|
29 |
import java.util.List; |
|
30 |
import java.util.Collections; |
|
31 |
import java.util.Comparator; |
|
32 |
import java.io.IOException; |
|
4324
5fd48b8b450b
6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents:
4154
diff
changeset
|
33 |
import sun.util.logging.PlatformLogger; |
2 | 34 |
|
35 |
/** |
|
36 |
* CookieManager provides a concrete implementation of {@link CookieHandler}, |
|
37 |
* which separates the storage of cookies from the policy surrounding accepting |
|
38 |
* and rejecting cookies. A CookieManager is initialized with a {@link CookieStore} |
|
39 |
* which manages storage, and a {@link CookiePolicy} object, which makes |
|
40 |
* policy decisions on cookie acceptance/rejection. |
|
41 |
* |
|
42 |
* <p> The HTTP cookie management in java.net package looks like: |
|
43 |
* <blockquote> |
|
18156 | 44 |
* <pre>{@code |
2 | 45 |
* use |
46 |
* CookieHandler <------- HttpURLConnection |
|
47 |
* ^ |
|
48 |
* | impl |
|
49 |
* | use |
|
50 |
* CookieManager -------> CookiePolicy |
|
51 |
* | use |
|
52 |
* |--------> HttpCookie |
|
53 |
* | ^ |
|
54 |
* | | use |
|
55 |
* | use | |
|
56 |
* |--------> CookieStore |
|
57 |
* ^ |
|
58 |
* | impl |
|
59 |
* | |
|
60 |
* Internal in-memory implementation |
|
18156 | 61 |
* }</pre> |
2 | 62 |
* <ul> |
63 |
* <li> |
|
64 |
* CookieHandler is at the core of cookie management. User can call |
|
65 |
* CookieHandler.setDefault to set a concrete CookieHanlder implementation |
|
66 |
* to be used. |
|
67 |
* </li> |
|
68 |
* <li> |
|
69 |
* CookiePolicy.shouldAccept will be called by CookieManager.put to see whether |
|
70 |
* or not one cookie should be accepted and put into cookie store. User can use |
|
71 |
* any of three pre-defined CookiePolicy, namely ACCEPT_ALL, ACCEPT_NONE and |
|
72 |
* ACCEPT_ORIGINAL_SERVER, or user can define his own CookiePolicy implementation |
|
73 |
* and tell CookieManager to use it. |
|
74 |
* </li> |
|
75 |
* <li> |
|
76 |
* CookieStore is the place where any accepted HTTP cookie is stored in. |
|
77 |
* If not specified when created, a CookieManager instance will use an internal |
|
78 |
* in-memory implementation. Or user can implements one and tell CookieManager |
|
79 |
* to use it. |
|
80 |
* </li> |
|
81 |
* <li> |
|
82 |
* Currently, only CookieStore.add(URI, HttpCookie) and CookieStore.get(URI) |
|
83 |
* are used by CookieManager. Others are for completeness and might be needed |
|
84 |
* by a more sophisticated CookieStore implementation, e.g. a NetscapeCookieSotre. |
|
85 |
* </li> |
|
86 |
* </ul> |
|
87 |
* </blockquote> |
|
88 |
* |
|
89 |
* <p>There're various ways user can hook up his own HTTP cookie management behavior, e.g. |
|
90 |
* <blockquote> |
|
91 |
* <ul> |
|
92 |
* <li>Use CookieHandler.setDefault to set a brand new {@link CookieHandler} implementation |
|
93 |
* <li>Let CookieManager be the default {@link CookieHandler} implementation, |
|
94 |
* but implement user's own {@link CookieStore} and {@link CookiePolicy} |
|
95 |
* and tell default CookieManager to use them: |
|
96 |
* <blockquote><pre> |
|
97 |
* // this should be done at the beginning of an HTTP session |
|
98 |
* CookieHandler.setDefault(new CookieManager(new MyCookieStore(), new MyCookiePolicy())); |
|
99 |
* </pre></blockquote> |
|
100 |
* <li>Let CookieManager be the default {@link CookieHandler} implementation, but |
|
101 |
* use customized {@link CookiePolicy}: |
|
102 |
* <blockquote><pre> |
|
103 |
* // this should be done at the beginning of an HTTP session |
|
104 |
* CookieHandler.setDefault(new CookieManager()); |
|
105 |
* // this can be done at any point of an HTTP session |
|
106 |
* ((CookieManager)CookieHandler.getDefault()).setCookiePolicy(new MyCookiePolicy()); |
|
107 |
* </pre></blockquote> |
|
108 |
* </ul> |
|
109 |
* </blockquote> |
|
110 |
* |
|
1950
dd893f20667b
6585546: Please update API doc for java.net.CookieManager
jccollet
parents:
715
diff
changeset
|
111 |
* <p>The implementation conforms to <a href="http://www.ietf.org/rfc/rfc2965.txt">RFC 2965</a>, section 3.3. |
2 | 112 |
* |
1950
dd893f20667b
6585546: Please update API doc for java.net.CookieManager
jccollet
parents:
715
diff
changeset
|
113 |
* @see CookiePolicy |
2 | 114 |
* @author Edward Wang |
115 |
* @since 1.6 |
|
116 |
*/ |
|
117 |
public class CookieManager extends CookieHandler |
|
118 |
{ |
|
119 |
/* ---------------- Fields -------------- */ |
|
120 |
||
121 |
private CookiePolicy policyCallback; |
|
122 |
||
123 |
||
124 |
private CookieStore cookieJar = null; |
|
125 |
||
126 |
||
127 |
/* ---------------- Ctors -------------- */ |
|
128 |
||
129 |
/** |
|
130 |
* Create a new cookie manager. |
|
131 |
* |
|
132 |
* <p>This constructor will create new cookie manager with default |
|
133 |
* cookie store and accept policy. The effect is same as |
|
19069 | 134 |
* {@code CookieManager(null, null)}. |
2 | 135 |
*/ |
136 |
public CookieManager() { |
|
137 |
this(null, null); |
|
138 |
} |
|
139 |
||
140 |
||
141 |
/** |
|
142 |
* Create a new cookie manager with specified cookie store and cookie policy. |
|
143 |
* |
|
19069 | 144 |
* @param store a {@code CookieStore} to be used by cookie manager. |
145 |
* if {@code null}, cookie manager will use a default one, |
|
2 | 146 |
* which is an in-memory CookieStore implmentation. |
19069 | 147 |
* @param cookiePolicy a {@code CookiePolicy} instance |
2 | 148 |
* to be used by cookie manager as policy callback. |
19069 | 149 |
* if {@code null}, ACCEPT_ORIGINAL_SERVER will |
2 | 150 |
* be used. |
151 |
*/ |
|
152 |
public CookieManager(CookieStore store, |
|
153 |
CookiePolicy cookiePolicy) |
|
154 |
{ |
|
155 |
// use default cookie policy if not specify one |
|
156 |
policyCallback = (cookiePolicy == null) ? CookiePolicy.ACCEPT_ORIGINAL_SERVER |
|
157 |
: cookiePolicy; |
|
158 |
||
159 |
// if not specify CookieStore to use, use default one |
|
160 |
if (store == null) { |
|
3858
ea9c34fc8590
6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
2924
diff
changeset
|
161 |
cookieJar = new InMemoryCookieStore(); |
2 | 162 |
} else { |
163 |
cookieJar = store; |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
||
168 |
/* ---------------- Public operations -------------- */ |
|
169 |
||
170 |
/** |
|
171 |
* To set the cookie policy of this cookie manager. |
|
172 |
* |
|
19069 | 173 |
* <p> A instance of {@code CookieManager} will have |
2 | 174 |
* cookie policy ACCEPT_ORIGINAL_SERVER by default. Users always |
175 |
* can call this method to set another cookie policy. |
|
176 |
* |
|
19069 | 177 |
* @param cookiePolicy the cookie policy. Can be {@code null}, which |
2 | 178 |
* has no effects on current cookie policy. |
179 |
*/ |
|
180 |
public void setCookiePolicy(CookiePolicy cookiePolicy) { |
|
181 |
if (cookiePolicy != null) policyCallback = cookiePolicy; |
|
182 |
} |
|
183 |
||
184 |
||
185 |
/** |
|
186 |
* To retrieve current cookie store. |
|
187 |
* |
|
188 |
* @return the cookie store currently used by cookie manager. |
|
189 |
*/ |
|
190 |
public CookieStore getCookieStore() { |
|
191 |
return cookieJar; |
|
192 |
} |
|
193 |
||
194 |
||
195 |
public Map<String, List<String>> |
|
196 |
get(URI uri, Map<String, List<String>> requestHeaders) |
|
197 |
throws IOException |
|
198 |
{ |
|
199 |
// pre-condition check |
|
200 |
if (uri == null || requestHeaders == null) { |
|
201 |
throw new IllegalArgumentException("Argument is null"); |
|
202 |
} |
|
203 |
||
204 |
Map<String, List<String>> cookieMap = |
|
205 |
new java.util.HashMap<String, List<String>>(); |
|
206 |
// if there's no default CookieStore, no way for us to get any cookie |
|
207 |
if (cookieJar == null) |
|
208 |
return Collections.unmodifiableMap(cookieMap); |
|
209 |
||
480 | 210 |
boolean secureLink = "https".equalsIgnoreCase(uri.getScheme()); |
2 | 211 |
List<HttpCookie> cookies = new java.util.ArrayList<HttpCookie>(); |
480 | 212 |
String path = uri.getPath(); |
213 |
if (path == null || path.isEmpty()) { |
|
214 |
path = "/"; |
|
215 |
} |
|
2 | 216 |
for (HttpCookie cookie : cookieJar.get(uri)) { |
217 |
// apply path-matches rule (RFC 2965 sec. 3.3.4) |
|
480 | 218 |
// and check for the possible "secure" tag (i.e. don't send |
219 |
// 'secure' cookies over unsecure links) |
|
220 |
if (pathMatches(path, cookie.getPath()) && |
|
221 |
(secureLink || !cookie.getSecure())) { |
|
4154 | 222 |
// Enforce httponly attribute |
223 |
if (cookie.isHttpOnly()) { |
|
224 |
String s = uri.getScheme(); |
|
225 |
if (!"http".equalsIgnoreCase(s) && !"https".equalsIgnoreCase(s)) { |
|
226 |
continue; |
|
227 |
} |
|
228 |
} |
|
480 | 229 |
// Let's check the authorize port list if it exists |
230 |
String ports = cookie.getPortlist(); |
|
231 |
if (ports != null && !ports.isEmpty()) { |
|
232 |
int port = uri.getPort(); |
|
233 |
if (port == -1) { |
|
234 |
port = "https".equals(uri.getScheme()) ? 443 : 80; |
|
235 |
} |
|
236 |
if (isInPortList(ports, port)) { |
|
237 |
cookies.add(cookie); |
|
238 |
} |
|
239 |
} else { |
|
240 |
cookies.add(cookie); |
|
241 |
} |
|
2 | 242 |
} |
243 |
} |
|
244 |
||
245 |
// apply sort rule (RFC 2965 sec. 3.3.4) |
|
246 |
List<String> cookieHeader = sortByPath(cookies); |
|
247 |
||
248 |
cookieMap.put("Cookie", cookieHeader); |
|
249 |
return Collections.unmodifiableMap(cookieMap); |
|
250 |
} |
|
251 |
||
252 |
public void |
|
253 |
put(URI uri, Map<String, List<String>> responseHeaders) |
|
254 |
throws IOException |
|
255 |
{ |
|
256 |
// pre-condition check |
|
257 |
if (uri == null || responseHeaders == null) { |
|
258 |
throw new IllegalArgumentException("Argument is null"); |
|
259 |
} |
|
260 |
||
261 |
||
262 |
// if there's no default CookieStore, no need to remember any cookie |
|
263 |
if (cookieJar == null) |
|
264 |
return; |
|
265 |
||
4324
5fd48b8b450b
6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents:
4154
diff
changeset
|
266 |
PlatformLogger logger = PlatformLogger.getLogger("java.net.CookieManager"); |
2 | 267 |
for (String headerKey : responseHeaders.keySet()) { |
268 |
// RFC 2965 3.2.2, key must be 'Set-Cookie2' |
|
269 |
// we also accept 'Set-Cookie' here for backward compatibility |
|
270 |
if (headerKey == null |
|
271 |
|| !(headerKey.equalsIgnoreCase("Set-Cookie2") |
|
272 |
|| headerKey.equalsIgnoreCase("Set-Cookie") |
|
273 |
) |
|
274 |
) |
|
275 |
{ |
|
276 |
continue; |
|
277 |
} |
|
278 |
||
279 |
for (String headerValue : responseHeaders.get(headerKey)) { |
|
280 |
try { |
|
4324
5fd48b8b450b
6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents:
4154
diff
changeset
|
281 |
List<HttpCookie> cookies; |
5fd48b8b450b
6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents:
4154
diff
changeset
|
282 |
try { |
5fd48b8b450b
6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents:
4154
diff
changeset
|
283 |
cookies = HttpCookie.parse(headerValue); |
5fd48b8b450b
6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents:
4154
diff
changeset
|
284 |
} catch (IllegalArgumentException e) { |
5fd48b8b450b
6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents:
4154
diff
changeset
|
285 |
// Bogus header, make an empty list and log the error |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
286 |
cookies = java.util.Collections.emptyList(); |
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
18174
diff
changeset
|
287 |
if (logger.isLoggable(PlatformLogger.Level.SEVERE)) { |
4324
5fd48b8b450b
6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents:
4154
diff
changeset
|
288 |
logger.severe("Invalid cookie for " + uri + ": " + headerValue); |
5fd48b8b450b
6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents:
4154
diff
changeset
|
289 |
} |
5fd48b8b450b
6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents:
4154
diff
changeset
|
290 |
} |
2 | 291 |
for (HttpCookie cookie : cookies) { |
480 | 292 |
if (cookie.getPath() == null) { |
293 |
// If no path is specified, then by default |
|
294 |
// the path is the directory of the page/doc |
|
295 |
String path = uri.getPath(); |
|
296 |
if (!path.endsWith("/")) { |
|
297 |
int i = path.lastIndexOf("/"); |
|
298 |
if (i > 0) { |
|
299 |
path = path.substring(0, i + 1); |
|
300 |
} else { |
|
301 |
path = "/"; |
|
302 |
} |
|
303 |
} |
|
304 |
cookie.setPath(path); |
|
305 |
} |
|
2924
a67f12d92bdd
6349566: java.net.CookieManager doesn't set default domain
jccollet
parents:
1950
diff
changeset
|
306 |
|
a67f12d92bdd
6349566: java.net.CookieManager doesn't set default domain
jccollet
parents:
1950
diff
changeset
|
307 |
// As per RFC 2965, section 3.3.1: |
a67f12d92bdd
6349566: java.net.CookieManager doesn't set default domain
jccollet
parents:
1950
diff
changeset
|
308 |
// Domain Defaults to the effective request-host. (Note that because |
a67f12d92bdd
6349566: java.net.CookieManager doesn't set default domain
jccollet
parents:
1950
diff
changeset
|
309 |
// there is no dot at the beginning of effective request-host, |
a67f12d92bdd
6349566: java.net.CookieManager doesn't set default domain
jccollet
parents:
1950
diff
changeset
|
310 |
// the default Domain can only domain-match itself.) |
a67f12d92bdd
6349566: java.net.CookieManager doesn't set default domain
jccollet
parents:
1950
diff
changeset
|
311 |
if (cookie.getDomain() == null) { |
18174
806bf0d6f0c7
7169142: CookieHandler does not work with localhost
khazra
parents:
18156
diff
changeset
|
312 |
String host = uri.getHost(); |
806bf0d6f0c7
7169142: CookieHandler does not work with localhost
khazra
parents:
18156
diff
changeset
|
313 |
if (host != null && !host.contains(".")) |
806bf0d6f0c7
7169142: CookieHandler does not work with localhost
khazra
parents:
18156
diff
changeset
|
314 |
host += ".local"; |
806bf0d6f0c7
7169142: CookieHandler does not work with localhost
khazra
parents:
18156
diff
changeset
|
315 |
cookie.setDomain(host); |
2924
a67f12d92bdd
6349566: java.net.CookieManager doesn't set default domain
jccollet
parents:
1950
diff
changeset
|
316 |
} |
480 | 317 |
String ports = cookie.getPortlist(); |
318 |
if (ports != null) { |
|
319 |
int port = uri.getPort(); |
|
320 |
if (port == -1) { |
|
321 |
port = "https".equals(uri.getScheme()) ? 443 : 80; |
|
322 |
} |
|
323 |
if (ports.isEmpty()) { |
|
324 |
// Empty port list means this should be restricted |
|
325 |
// to the incoming URI port |
|
326 |
cookie.setPortlist("" + port ); |
|
327 |
if (shouldAcceptInternal(uri, cookie)) { |
|
328 |
cookieJar.add(uri, cookie); |
|
329 |
} |
|
330 |
} else { |
|
331 |
// Only store cookies with a port list |
|
332 |
// IF the URI port is in that list, as per |
|
333 |
// RFC 2965 section 3.3.2 |
|
334 |
if (isInPortList(ports, port) && |
|
335 |
shouldAcceptInternal(uri, cookie)) { |
|
336 |
cookieJar.add(uri, cookie); |
|
337 |
} |
|
338 |
} |
|
339 |
} else { |
|
340 |
if (shouldAcceptInternal(uri, cookie)) { |
|
341 |
cookieJar.add(uri, cookie); |
|
342 |
} |
|
2 | 343 |
} |
344 |
} |
|
345 |
} catch (IllegalArgumentException e) { |
|
346 |
// invalid set-cookie header string |
|
347 |
// no-op |
|
348 |
} |
|
349 |
} |
|
350 |
} |
|
351 |
} |
|
352 |
||
353 |
||
354 |
/* ---------------- Private operations -------------- */ |
|
355 |
||
356 |
// to determine whether or not accept this cookie |
|
357 |
private boolean shouldAcceptInternal(URI uri, HttpCookie cookie) { |
|
358 |
try { |
|
359 |
return policyCallback.shouldAccept(uri, cookie); |
|
360 |
} catch (Exception ignored) { // pretect against malicious callback |
|
361 |
return false; |
|
362 |
} |
|
363 |
} |
|
364 |
||
365 |
||
480 | 366 |
static private boolean isInPortList(String lst, int port) { |
367 |
int i = lst.indexOf(","); |
|
368 |
int val = -1; |
|
369 |
while (i > 0) { |
|
370 |
try { |
|
371 |
val = Integer.parseInt(lst.substring(0, i)); |
|
372 |
if (val == port) { |
|
373 |
return true; |
|
374 |
} |
|
375 |
} catch (NumberFormatException numberFormatException) { |
|
376 |
} |
|
377 |
lst = lst.substring(i+1); |
|
378 |
i = lst.indexOf(","); |
|
379 |
} |
|
380 |
if (!lst.isEmpty()) { |
|
381 |
try { |
|
382 |
val = Integer.parseInt(lst); |
|
383 |
if (val == port) { |
|
384 |
return true; |
|
385 |
} |
|
386 |
} catch (NumberFormatException numberFormatException) { |
|
387 |
} |
|
388 |
} |
|
389 |
return false; |
|
390 |
} |
|
391 |
||
2 | 392 |
/* |
393 |
* path-matches algorithm, as defined by RFC 2965 |
|
394 |
*/ |
|
395 |
private boolean pathMatches(String path, String pathToMatchWith) { |
|
396 |
if (path == pathToMatchWith) |
|
397 |
return true; |
|
398 |
if (path == null || pathToMatchWith == null) |
|
399 |
return false; |
|
400 |
if (path.startsWith(pathToMatchWith)) |
|
401 |
return true; |
|
402 |
||
403 |
return false; |
|
404 |
} |
|
405 |
||
406 |
||
407 |
/* |
|
408 |
* sort cookies with respect to their path: those with more specific Path attributes |
|
409 |
* precede those with less specific, as defined in RFC 2965 sec. 3.3.4 |
|
410 |
*/ |
|
411 |
private List<String> sortByPath(List<HttpCookie> cookies) { |
|
412 |
Collections.sort(cookies, new CookiePathComparator()); |
|
413 |
||
414 |
List<String> cookieHeader = new java.util.ArrayList<String>(); |
|
415 |
for (HttpCookie cookie : cookies) { |
|
416 |
// Netscape cookie spec and RFC 2965 have different format of Cookie |
|
417 |
// header; RFC 2965 requires a leading $Version="1" string while Netscape |
|
418 |
// does not. |
|
419 |
// The workaround here is to add a $Version="1" string in advance |
|
420 |
if (cookies.indexOf(cookie) == 0 && cookie.getVersion() > 0) { |
|
421 |
cookieHeader.add("$Version=\"1\""); |
|
422 |
} |
|
423 |
||
424 |
cookieHeader.add(cookie.toString()); |
|
425 |
} |
|
426 |
return cookieHeader; |
|
427 |
} |
|
428 |
||
429 |
||
430 |
static class CookiePathComparator implements Comparator<HttpCookie> { |
|
431 |
public int compare(HttpCookie c1, HttpCookie c2) { |
|
432 |
if (c1 == c2) return 0; |
|
433 |
if (c1 == null) return -1; |
|
434 |
if (c2 == null) return 1; |
|
435 |
||
436 |
// path rule only applies to the cookies with same name |
|
437 |
if (!c1.getName().equals(c2.getName())) return 0; |
|
438 |
||
439 |
// those with more specific Path attributes precede those with less specific |
|
440 |
if (c1.getPath().startsWith(c2.getPath())) |
|
441 |
return -1; |
|
442 |
else if (c2.getPath().startsWith(c1.getPath())) |
|
443 |
return 1; |
|
444 |
else |
|
445 |
return 0; |
|
446 |
} |
|
447 |
} |
|
448 |
} |