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 package edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.cookie;
47
48 import java.util.Collection;
49 import java.util.Date;
50 import java.util.LinkedList;
51 import java.util.List;
52
53 import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.Cookie;
54 import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.Header;
55 import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.HeaderElement;
56 import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.NameValuePair;
57 import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.util.DateParseException;
58 import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.util.DateUtil;
59 import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.logging.Log;
60 import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.logging.LogFactory;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 public class CookieSpecBase implements CookieSpec {
80
81
82 protected static final Log LOG = LogFactory.getLog(CookieSpec.class);
83
84
85 private Collection datepatterns = null;
86
87
88 public CookieSpecBase() {
89 super();
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 public Cookie[] parse(String host, int port, String path,
125 boolean secure, final String header)
126 throws MalformedCookieException {
127
128 LOG.trace("enter CookieSpecBase.parse("
129 + "String, port, path, boolean, Header)");
130
131 if (host == null) {
132 throw new IllegalArgumentException(
133 "Host of origin may not be null");
134 }
135 if (host.trim().equals("")) {
136 throw new IllegalArgumentException(
137 "Host of origin may not be blank");
138 }
139 if (port < 0) {
140 throw new IllegalArgumentException("Invalid port: " + port);
141 }
142 if (path == null) {
143 throw new IllegalArgumentException(
144 "Path of origin may not be null.");
145 }
146 if (header == null) {
147 throw new IllegalArgumentException("Header may not be null.");
148 }
149
150 if (path.trim().equals("")) {
151 path = PATH_DELIM;
152 }
153 host = host.toLowerCase();
154
155 String defaultPath = path;
156 int lastSlashIndex = defaultPath.lastIndexOf(PATH_DELIM);
157 if (lastSlashIndex >= 0) {
158 if (lastSlashIndex == 0) {
159
160 lastSlashIndex = 1;
161 }
162 defaultPath = defaultPath.substring(0, lastSlashIndex);
163 }
164
165 HeaderElement[] headerElements = null;
166
167 boolean isNetscapeCookie = false;
168 int i1 = header.toLowerCase().indexOf("expires=");
169 if (i1 != -1) {
170 i1 += "expires=".length();
171 int i2 = header.indexOf(";", i1);
172 if (i2 == -1) {
173 i2 = header.length();
174 }
175 try {
176 DateUtil.parseDate(header.substring(i1, i2), this.datepatterns);
177 isNetscapeCookie = true;
178 } catch (DateParseException e) {
179
180 }
181 }
182 if (isNetscapeCookie) {
183 headerElements = new HeaderElement[] {
184 new HeaderElement(header.toCharArray())
185 };
186 } else {
187 headerElements = HeaderElement.parseElements(header.toCharArray());
188 }
189
190 Cookieddleware/grouperInstallerExt/org/apache/commons/httpclient/Cookie.html#Cookie">Cookie[] cookies = new Cookie[headerElements.length];
191
192 for (int i = 0; i < headerElements.length; i++) {
193
194 HeaderElement headerelement = headerElements[i];
195 Cookie cookie = null;
196 try {
197 cookie = new Cookie(host,
198 headerelement.getName(),
199 headerelement.getValue(),
200 defaultPath,
201 null,
202 false);
203 } catch (IllegalArgumentException e) {
204 throw new MalformedCookieException(e.getMessage());
205 }
206
207 NameValuePair[] parameters = headerelement.getParameters();
208
209 if (parameters != null) {
210
211 for (int j = 0; j < parameters.length; j++) {
212 parseAttribute(parameters[j], cookie);
213 }
214 }
215 cookies[i] = cookie;
216 }
217 return cookies;
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 public Cookie[] parse(
255 String host, int port, String path, boolean secure, final Header header)
256 throws MalformedCookieException {
257
258 LOG.trace("enter CookieSpecBase.parse("
259 + "String, port, path, boolean, String)");
260 if (header == null) {
261 throw new IllegalArgumentException("Header may not be null.");
262 }
263 return parse(host, port, path, secure, header.getValue());
264 }
265
266
267
268
269
270
271
272
273
274
275
276
277 public void parseAttribute(
278 final NameValuePair attribute, final Cookie cookie)
279 throws MalformedCookieException {
280
281 if (attribute == null) {
282 throw new IllegalArgumentException("Attribute may not be null.");
283 }
284 if (cookie == null) {
285 throw new IllegalArgumentException("Cookie may not be null.");
286 }
287 final String paramName = attribute.getName().toLowerCase();
288 String paramValue = attribute.getValue();
289
290 if (paramName.equals("path")) {
291
292 if ((paramValue == null) || (paramValue.trim().equals(""))) {
293 paramValue = "/";
294 }
295 cookie.setPath(paramValue);
296 cookie.setPathAttributeSpecified(true);
297
298 } else if (paramName.equals("domain")) {
299
300 if (paramValue == null) {
301 throw new MalformedCookieException(
302 "Missing value for domain attribute");
303 }
304 if (paramValue.trim().equals("")) {
305 throw new MalformedCookieException(
306 "Blank value for domain attribute");
307 }
308 cookie.setDomain(paramValue);
309 cookie.setDomainAttributeSpecified(true);
310
311 } else if (paramName.equals("max-age")) {
312
313 if (paramValue == null) {
314 throw new MalformedCookieException(
315 "Missing value for max-age attribute");
316 }
317 int age;
318 try {
319 age = Integer.parseInt(paramValue);
320 } catch (NumberFormatException e) {
321 throw new MalformedCookieException ("Invalid max-age "
322 + "attribute: " + e.getMessage());
323 }
324 cookie.setExpiryDate(
325 new Date(System.currentTimeMillis() + age * 1000L));
326
327 } else if (paramName.equals("secure")) {
328
329 cookie.setSecure(true);
330
331 } else if (paramName.equals("comment")) {
332
333 cookie.setComment(paramValue);
334
335 } else if (paramName.equals("expires")) {
336
337 if (paramValue == null) {
338 throw new MalformedCookieException(
339 "Missing value for expires attribute");
340 }
341
342 try {
343 cookie.setExpiryDate(DateUtil.parseDate(paramValue, this.datepatterns));
344 } catch (DateParseException dpe) {
345 LOG.debug("Error parsing cookie date", dpe);
346 throw new MalformedCookieException(
347 "Unable to parse expiration date parameter: "
348 + paramValue);
349 }
350 } else {
351 if (LOG.isDebugEnabled()) {
352 LOG.debug("Unrecognized cookie attribute: "
353 + attribute.toString());
354 }
355 }
356 }
357
358
359 public Collection getValidDateFormats() {
360 return this.datepatterns;
361 }
362
363 public void setValidDateFormats(final Collection datepatterns) {
364 this.datepatterns = datepatterns;
365 }
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380 public void validate(String host, int port, String path,
381 boolean secure, final Cookie cookie)
382 throws MalformedCookieException {
383
384 LOG.trace("enter CookieSpecBase.validate("
385 + "String, port, path, boolean, Cookie)");
386 if (host == null) {
387 throw new IllegalArgumentException(
388 "Host of origin may not be null");
389 }
390 if (host.trim().equals("")) {
391 throw new IllegalArgumentException(
392 "Host of origin may not be blank");
393 }
394 if (port < 0) {
395 throw new IllegalArgumentException("Invalid port: " + port);
396 }
397 if (path == null) {
398 throw new IllegalArgumentException(
399 "Path of origin may not be null.");
400 }
401 if (path.trim().equals("")) {
402 path = PATH_DELIM;
403 }
404 host = host.toLowerCase();
405
406 if (cookie.getVersion() < 0) {
407 throw new MalformedCookieException ("Illegal version number "
408 + cookie.getValue());
409 }
410
411
412
413
414
415
416
417
418
419 if (host.indexOf(".") >= 0) {
420
421
422
423
424 if (!host.endsWith(cookie.getDomain())) {
425 String s = cookie.getDomain();
426 if (s.startsWith(".")) {
427 s = s.substring(1, s.length());
428 }
429 if (!host.equals(s)) {
430 throw new MalformedCookieException(
431 "Illegal domain attribute \"" + cookie.getDomain()
432 + "\". Domain of origin: \"" + host + "\"");
433 }
434 }
435 } else {
436 if (!host.equals(cookie.getDomain())) {
437 throw new MalformedCookieException(
438 "Illegal domain attribute \"" + cookie.getDomain()
439 + "\". Domain of origin: \"" + host + "\"");
440 }
441 }
442
443
444
445
446 if (!path.startsWith(cookie.getPath())) {
447 throw new MalformedCookieException(
448 "Illegal path attribute \"" + cookie.getPath()
449 + "\". Path of origin: \"" + path + "\"");
450 }
451 }
452
453
454
455
456
457
458
459
460
461
462
463
464
465 public boolean match(String host, int port, String path,
466 boolean secure, final Cookie cookie) {
467
468 LOG.trace("enter CookieSpecBase.match("
469 + "String, int, String, boolean, Cookie");
470
471 if (host == null) {
472 throw new IllegalArgumentException(
473 "Host of origin may not be null");
474 }
475 if (host.trim().equals("")) {
476 throw new IllegalArgumentException(
477 "Host of origin may not be blank");
478 }
479 if (port < 0) {
480 throw new IllegalArgumentException("Invalid port: " + port);
481 }
482 if (path == null) {
483 throw new IllegalArgumentException(
484 "Path of origin may not be null.");
485 }
486 if (cookie == null) {
487 throw new IllegalArgumentException("Cookie may not be null");
488 }
489 if (path.trim().equals("")) {
490 path = PATH_DELIM;
491 }
492 host = host.toLowerCase();
493 if (cookie.getDomain() == null) {
494 LOG.warn("Invalid cookie state: domain not specified");
495 return false;
496 }
497 if (cookie.getPath() == null) {
498 LOG.warn("Invalid cookie state: path not specified");
499 return false;
500 }
501
502 return
503
504 (cookie.getExpiryDate() == null
505 || cookie.getExpiryDate().after(new Date()))
506
507 && (domainMatch(host, cookie.getDomain()))
508
509 && (pathMatch(path, cookie.getPath()))
510
511
512 && (cookie.getSecure() ? secure : true);
513 }
514
515
516
517
518
519
520
521 public boolean domainMatch(final String host, String domain) {
522 if (host.equals(domain)) {
523 return true;
524 }
525 if (!domain.startsWith(".")) {
526 domain = "." + domain;
527 }
528 return host.endsWith(domain) || host.equals(domain.substring(1));
529 }
530
531
532
533
534
535
536
537 public boolean pathMatch(final String path, final String topmostPath) {
538 boolean match = path.startsWith (topmostPath);
539
540
541 if (match && path.length() != topmostPath.length()) {
542 if (!topmostPath.endsWith(PATH_DELIM)) {
543 match = (path.charAt(topmostPath.length()) == PATH_DELIM_CHAR);
544 }
545 }
546 return match;
547 }
548
549
550
551
552
553
554
555
556
557
558
559
560
561 public Cookie[] match(String host, int port, String path,
562 boolean secure, final Cookie cookies[]) {
563
564 LOG.trace("enter CookieSpecBase.match("
565 + "String, int, String, boolean, Cookie[])");
566
567 if (cookies == null) {
568 return null;
569 }
570 List matching = new LinkedList();
571 for (int i = 0; i < cookies.length; i++) {
572 if (match(host, port, path, secure, cookies[i])) {
573 addInPathOrder(matching, cookies[i]);
574 }
575 }
576 return (Cookie/grouperInstallerExt/org/apache/commons/httpclient/Cookie.html#Cookie">Cookie[]) matching.toArray(new Cookie[matching.size()]);
577 }
578
579
580
581
582
583
584
585
586
587
588
589 private static void addInPathOrder(List list, Cookie addCookie) {
590 int i = 0;
591
592 for (i = 0; i < list.size(); i++) {
593 Cookieref="../../../../../../../../../edu/internet2/middleware/grouperInstallerExt/org/apache/commons/httpclient/Cookie.html#Cookie">Cookie c = (Cookie) list.get(i);
594 if (addCookie.compare(addCookie, c) > 0) {
595 break;
596 }
597 }
598 list.add(i, addCookie);
599 }
600
601
602
603
604
605
606 public String formatCookie(Cookie cookie) {
607 LOG.trace("enter CookieSpecBase.formatCookie(Cookie)");
608 if (cookie == null) {
609 throw new IllegalArgumentException("Cookie may not be null");
610 }
611 StringBuffer buf = new StringBuffer();
612 buf.append(cookie.getName());
613 buf.append("=");
614 String s = cookie.getValue();
615 if (s != null) {
616 buf.append(s);
617 }
618 return buf.toString();
619 }
620
621
622
623
624
625
626
627
628
629 public String formatCookies(Cookie[] cookies)
630 throws IllegalArgumentException {
631 LOG.trace("enter CookieSpecBase.formatCookies(Cookie[])");
632 if (cookies == null) {
633 throw new IllegalArgumentException("Cookie array may not be null");
634 }
635 if (cookies.length == 0) {
636 throw new IllegalArgumentException("Cookie array may not be empty");
637 }
638
639 StringBuffer buffer = new StringBuffer();
640 for (int i = 0; i < cookies.length; i++) {
641 if (i > 0) {
642 buffer.append("; ");
643 }
644 buffer.append(formatCookie(cookies[i]));
645 }
646 return buffer.toString();
647 }
648
649
650
651
652
653
654
655
656
657 public Header formatCookieHeader(Cookie[] cookies) {
658 LOG.trace("enter CookieSpecBase.formatCookieHeader(Cookie[])");
659 return new Header("Cookie", formatCookies(cookies));
660 }
661
662
663
664
665
666
667
668
669 public Header formatCookieHeader(Cookie cookie) {
670 LOG.trace("enter CookieSpecBase.formatCookieHeader(Cookie)");
671 return new Header("Cookie", formatCookie(cookie));
672 }
673
674 }