-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathCookie.java
More file actions
589 lines (516 loc) · 17.4 KB
/
Cookie.java
File metadata and controls
589 lines (516 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
* @(#)Cookie.java 0.3-3 06/05/2001
*
* This file is part of the HTTPClient package
* Copyright (C) 1996-2001 Ronald Tschal�r
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA
*
* For questions, suggestions, bug-reports, enhancement-requests etc.
* I may be contacted at:
*
* ronald@innovation.ch
*
* The HTTPClient's home page is located at:
*
* http://www.innovation.ch/java/HTTPClient/
*
*/
package HTTPClient;
import java.io.Serializable;
import java.net.ProtocolException;
import java.util.Date;
/**
* This class represents an http cookie as specified in <a
* href="http://home.netscape.com/newsref/std/cookie_spec.html">Netscape's
* cookie spec</a>; however, because not even Netscape follows their own spec,
* and because very few folks out there actually read specs but instead just
* look whether Netscape accepts their stuff, the Set-Cookie header field
* parser actually tries to follow what Netscape has implemented, instead of
* what the spec says. Additionally, the parser it will also recognize the
* Max-Age parameter from <a
* href="http://www.ietf.org/rfc/rfc2109.txt">rfc-2109</a>, as that uses the
* same header field (Set-Cookie).
*
* <P>Some notes about how Netscape (4.7) parses:
* <ul>
* <LI>Quoting: only quotes around the expires value are recognized as such;
* quotes around any other value are treated as part of the value.
* <LI>White space: white space around names and values is ignored
* <LI>Default path: if no path parameter is given, the path defaults to the
* path in the request-uri up to, but not including, the last '/'. Note
* that this is entirely different from what the spec says.
* <LI>Commas and other delimiters: Netscape just parses until the next ';'.
* This means will allow commas etc inside values.
* </ul>
*
* @version 0.3-3 06/05/2001
* @author Ronald Tschal�r
* @since V0.3
*/
public class Cookie implements Serializable
{
/** Make this compatible with V0.3-2 */
private static final long serialVersionUID = 8599975325569296615L;
protected String name;
protected String value;
protected Date expires;
protected String domain;
protected String path;
protected boolean secure;
/**
* Create a cookie.
*
* @param name the cookie name
* @param value the cookie value
* @param domain the host this cookie will be sent to
* @param path the path prefix for which this cookie will be sent
* @param epxires the Date this cookie expires, null if at end of
* session
* @param secure if true this cookie will only be over secure connections
* @exception NullPointerException if <var>name</var>, <var>value</var>,
* <var>domain</var>, or <var>path</var>
* is null
* @since V0.3-1
*/
public Cookie(String name, String value, String domain, String path,
Date expires, boolean secure)
{
if (name == null) throw new NullPointerException("missing name");
if (value == null) throw new NullPointerException("missing value");
if (domain == null) throw new NullPointerException("missing domain");
if (path == null) throw new NullPointerException("missing path");
this.name = name;
this.value = value;
this.domain = domain.toLowerCase();
this.path = path;
this.expires = expires;
this.secure = secure;
if (this.domain.indexOf('.') == -1) this.domain += ".local";
}
/**
* Use <code>parse()</code> to create cookies.
*
* @see #parse(java.lang.String, HTTPClient.RoRequest)
*/
protected Cookie(RoRequest req)
{
name = null;
value = null;
expires = null;
domain = req.getConnection().getHost();
if (domain.indexOf('.') == -1) domain += ".local";
path = Util.getPath(req.getRequestURI());
/* This does not follow netscape's spec at all, but it's the way
* netscape seems to do it, and because people rely on that we
* therefore also have to do it...
*/
int slash = path.lastIndexOf('/');
if (slash >= 0)
path = path.substring(0, slash);
secure = false;
}
/**
* Parses the Set-Cookie header into an array of Cookies.
*
* @param set_cookie the Set-Cookie header received from the server
* @param req the request used
* @return an array of Cookies as parsed from the Set-Cookie header
* @exception ProtocolException if an error occurs during parsing
*/
protected static Cookie[] parse(String set_cookie, RoRequest req)
throws ProtocolException
{
int beg = 0,
end = 0,
start = 0;
// Cope with .NET nonsense.
// See http://www.hanselman.com/blog/HttpOnlyCookiesOnASPNET11.aspx
if (set_cookie.toLowerCase().indexOf("httponly") != -1) {
// Typical section of a .NET cookie:
// ... ; path=/;HttpOnly, language=en-US; path=/;HttpOnly
//
// We remove all instances of "HttpOnly," that follow a semi-colon,
// and all instances of HttpOnly following a semi-colon at the end of
// the cookie. This leaves something that is more conventional
// This shouldn't break any valid cookies.
set_cookie = set_cookie.replaceAll(";(?i)\\s*HttpOnly,",";,");
set_cookie = set_cookie.replaceAll(";(?i)\\s*HttpOnly$",";");
set_cookie = set_cookie.replaceAll(";(?i)\\s*HttpOnly; (?i)\\s*secure$",";");
set_cookie = set_cookie.replaceAll(";(?i)\\s*HttpOnly;",";");
}
char[] buf = set_cookie.toCharArray();
int len = buf.length;
Cookie cookie_arr[] = new Cookie[0], curr;
cookies: while (true) // get all cookies
{
beg = Util.skipSpace(buf, beg);
if (beg >= len) break; // no more left
if (buf[beg] == ',') // empty header
{
beg++;
continue;
}
curr = new Cookie(req);
start = beg;
// get cookie name and value first
end = set_cookie.indexOf('=', beg);
if (end == -1)
throw new ProtocolException("Bad Set-Cookie header: " +
set_cookie + "\nNo '=' found " +
"for token starting at " +
"position " + beg);
curr.name = set_cookie.substring(beg, end).trim();
beg = Util.skipSpace(buf, end+1);
int comma = set_cookie.indexOf(',', beg);
int semic = set_cookie.indexOf(';', beg);
if (comma == -1 && semic == -1) end = len;
else if (comma == -1) end = semic;
else if (semic == -1) end = comma;
else
{
if (comma > semic)
end = semic;
else
{
// try to handle broken servers which put commas
// into cookie values
int eq = set_cookie.indexOf('=', comma);
if (eq > 0 && eq < semic)
end = set_cookie.lastIndexOf(',', eq);
else
end = semic;
}
}
curr.value = set_cookie.substring(beg, end).trim();
beg = end;
// now parse attributes
boolean legal = true;
parts: while (true) // parse all parts
{
if (beg >= len || buf[beg] == ',') break;
// skip empty fields
if (buf[beg] == ';')
{
beg = Util.skipSpace(buf, beg+1);
continue;
}
// first check for secure, as this is the only one w/o a '='
if ((beg+6 <= len) &&
set_cookie.regionMatches(true, beg, "secure", 0, 6))
{
curr.secure = true;
beg += 6;
beg = Util.skipSpace(buf, beg);
if (beg < len && buf[beg] == ';') // consume ";"
beg = Util.skipSpace(buf, beg+1);
else if (beg < len && buf[beg] != ',')
throw new ProtocolException("Bad Set-Cookie header: " +
set_cookie + "\nExpected " +
"';' or ',' at position " +
beg);
continue;
}
// alright, must now be of the form x=y
end = set_cookie.indexOf('=', beg);
if (end == -1)
throw new ProtocolException("Bad Set-Cookie header: " +
set_cookie + "\nNo '=' found " +
"for token starting at " +
"position " + beg);
String name = set_cookie.substring(beg, end).trim();
beg = Util.skipSpace(buf, end+1);
if (name.equalsIgnoreCase("expires"))
{
/* Netscape ignores quotes around the date, and some twits
* actually send that...
*/
if (set_cookie.charAt(beg) == '\"')
beg = Util.skipSpace(buf, beg+1);
/* cut off the weekday if it is there. This is a little
* tricky because the comma is also used between cookies
* themselves. To make sure we don't inadvertantly
* mistake a date for a weekday we only skip letters.
*/
int pos = beg;
while (pos < len &&
(buf[pos] >= 'a' && buf[pos] <= 'z' ||
buf[pos] >= 'A' && buf[pos] <= 'Z'))
pos++;
pos = Util.skipSpace(buf, pos);
if (pos < len && buf[pos] == ',' && pos > beg)
beg = pos+1;
}
comma = set_cookie.indexOf(',', beg);
semic = set_cookie.indexOf(';', beg);
if (comma == -1 && semic == -1) end = len;
else if (comma == -1) end = semic;
else if (semic == -1) end = comma;
else end = Math.min(comma, semic);
String value = set_cookie.substring(beg, end).trim();
legal &= setAttribute(curr, name, value, set_cookie);
beg = end;
if (beg < len && buf[beg] == ';') // consume ";"
beg = Util.skipSpace(buf, beg+1);
}
if (legal)
{
cookie_arr = Util.resizeArray(cookie_arr, cookie_arr.length+1);
cookie_arr[cookie_arr.length-1] = curr;
} else
Log.write(Log.COOKI, "Cooki: Ignoring cookie: " + curr);
}
return cookie_arr;
}
/**
* Set the given attribute, if valid.
*
* @param cookie the cookie on which to set the value
* @param name the name of the attribute
* @param value the value of the attribute
* @param set_cookie the complete Set-Cookie header
* @return true if the attribute is legal; false otherwise
*/
private static boolean setAttribute(Cookie cookie, String name,
String value, String set_cookie)
throws ProtocolException
{
if (name.equalsIgnoreCase("expires"))
{
if (value.charAt(value.length()-1) == '\"')
value = value.substring(0, value.length()-1).trim();
try
// This is too strict...
// { cookie.expires = Util.parseHttpDate(value); }
{ cookie.expires = new Date(value); }
catch (IllegalArgumentException iae)
{
/* More broken servers to deal with... Ignore expires
* if it's invalid
throw new ProtocolException("Bad Set-Cookie header: " +
set_cookie + "\nInvalid date found at " +
"position " + beg);
*/
Log.write(Log.COOKI, "Cooki: Bad Set-Cookie header: " + set_cookie +
"\n Invalid date `" + value + "'");
}
}
else if (name.equals("max-age")) // from rfc-2109
{
if (cookie.expires != null) return true;
if (value.charAt(0) == '\"' && value.charAt(value.length()-1) == '\"')
value = value.substring(1, value.length()-1).trim();
int age;
try
{ age = Integer.parseInt(value); }
catch (NumberFormatException nfe)
{
throw new ProtocolException("Bad Set-Cookie header: " +
set_cookie + "\nMax-Age '" + value +
"' not a number");
}
cookie.expires = new Date(System.currentTimeMillis() + age*1000L);
}
else if (name.equalsIgnoreCase("domain"))
{
// you get everything these days...
if (value.length() == 0)
{
Log.write(Log.COOKI, "Cooki: Bad Set-Cookie header: " + set_cookie +
"\n domain is empty - ignoring domain");
return true;
}
// domains are case insensitive.
value = value.toLowerCase();
// add leading dot, if missing
if (value.length() != 0 && value.charAt(0) != '.' &&
!value.equals(cookie.domain))
value = '.' + value;
// must be the same domain as in the url
if (!cookie.domain.endsWith(value))
{
Log.write(Log.COOKI, "Cooki: Bad Set-Cookie header: " + set_cookie +
"\n Current domain " + cookie.domain +
" does not match given parsed " + value);
return false;
}
/* Netscape's original 2-/3-dot rule really doesn't work because
* many countries use a shallow hierarchy (similar to the special
* TLDs defined in the spec). While the rules in rfc-2965 aren't
* perfect either, they are better. OTOH, some sites use a domain
* so that the host name minus the domain name contains a dot (e.g.
* host x.x.yahoo.com and domain .yahoo.com). So, for the seven
* special TLDs we use the 2-dot rule, and for all others we use
* the rules in the state-man draft instead.
*/
// domain must be either .local or must contain at least
// two dots
if (!value.equals(".local") && value.indexOf('.', 1) == -1)
{
Log.write(Log.COOKI, "Cooki: Bad Set-Cookie header: " + set_cookie +
"\n Domain attribute " + value +
"isn't .local and doesn't have at " +
"least 2 dots");
return false;
}
// If TLD not special then host minus domain may not
// contain any dots
String top = null;
if (value.length() > 3 )
top = value.substring(value.length()-4);
if (top == null || !(
top.equalsIgnoreCase(".com") ||
top.equalsIgnoreCase(".edu") ||
top.equalsIgnoreCase(".net") ||
top.equalsIgnoreCase(".org") ||
top.equalsIgnoreCase(".gov") ||
top.equalsIgnoreCase(".mil") ||
top.equalsIgnoreCase(".int")))
{
int dl = cookie.domain.length(), vl = value.length();
if (dl > vl &&
cookie.domain.substring(0, dl-vl).indexOf('.') != -1)
{
Log.write(Log.COOKI, "Cooki: Bad Set-Cookie header: " + set_cookie +
"\n Domain attribute " + value +
"is more than one level below " +
"current domain " + cookie.domain);
return false;
}
}
cookie.domain = value;
}
else if (name.equalsIgnoreCase("path"))
cookie.path = value;
else
; // unknown attribute - ignore
return true;
}
/**
* Return the name of this cookie.
*/
public String getName()
{
return name;
}
/**
* Return the value of this cookie.
*/
public String getValue()
{
return value;
}
/**
* @return the expiry date of this cookie, or null if none set.
*/
public Date expires()
{
return expires;
}
/**
* @return true if the cookie should be discarded at the end of the
* session; false otherwise
*/
public boolean discard()
{
return (expires == null);
}
/**
* Return the domain this cookie is valid in.
*/
public String getDomain()
{
return domain;
}
/**
* Return the path this cookie is associated with.
*/
public String getPath()
{
return path;
}
/**
* Return whether this cookie should only be sent over secure connections.
*/
public boolean isSecure()
{
return secure;
}
/**
* @return true if this cookie has expired
*/
public boolean hasExpired()
{
return (expires != null && expires.getTime() <= System.currentTimeMillis());
}
/**
* @param req the request to be sent
* @return true if this cookie should be sent with the request
*/
protected boolean sendWith(RoRequest req)
{
HTTPConnection con = req.getConnection();
String eff_host = con.getHost();
if (eff_host.indexOf('.') == -1) eff_host += ".local";
return ((domain.charAt(0) == '.' && eff_host.endsWith(domain) ||
domain.charAt(0) != '.' && eff_host.equals(domain)) &&
Util.getPath(req.getRequestURI()).startsWith(path) &&
(!secure || con.getProtocol().equals("https") ||
con.getProtocol().equals("shttp")) && con.getIncludeCookies());
}
/**
* Hash up name, path and domain into new hash.
*/
public int hashCode()
{
return (name.hashCode() + path.hashCode() + domain.hashCode());
}
/**
* Two cookies match if the name, path and domain match.
*/
public boolean equals(Object obj)
{
if ((obj != null) && (obj instanceof Cookie))
{
Cookie other = (Cookie) obj;
return (this.name.equals(other.name) &&
this.path.equals(other.path) &&
this.domain.equals(other.domain));
}
return false;
}
/**
* @return a string suitable for sending in a Cookie header.
*/
protected String toExternalForm()
{
return name + "=" + value;
}
/**
* Create a string containing all the cookie fields. The format is that
* used in the Set-Cookie header.
*/
public String toString()
{
StringBuffer res = new StringBuffer(name.length() + value.length() + 30);
res.append(name).append('=').append(value);
if (expires != null) res.append("; expires=").append(expires);
if (path != null) res.append("; path=").append(path);
if (domain != null) res.append("; domain=").append(domain);
if (secure) res.append("; secure");
return res.toString();
}
}