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 package org.apache.http.impl.cookie;
28
29 import org.apache.http.annotation.Immutable;
30
31 import org.apache.http.cookie.Cookie;
32 import org.apache.http.cookie.CookieAttributeHandler;
33 import org.apache.http.cookie.CookieOrigin;
34 import org.apache.http.cookie.CookieRestrictionViolationException;
35 import org.apache.http.cookie.MalformedCookieException;
36 import org.apache.http.cookie.SetCookie;
37
38
39
40
41
42 @Immutable
43 public class BasicPathHandler implements CookieAttributeHandler {
44
45 public BasicPathHandler() {
46 super();
47 }
48
49 public void parse(final SetCookie cookie, String value)
50 throws MalformedCookieException {
51 if (cookie == null) {
52 throw new IllegalArgumentException("Cookie may not be null");
53 }
54 if (value == null || value.trim().length() == 0) {
55 value = "/";
56 }
57 cookie.setPath(value);
58 }
59
60 public void validate(final Cookie cookie, final CookieOrigin origin)
61 throws MalformedCookieException {
62 if (!match(cookie, origin)) {
63 throw new CookieRestrictionViolationException(
64 "Illegal path attribute \"" + cookie.getPath()
65 + "\". Path of origin: \"" + origin.getPath() + "\"");
66 }
67 }
68
69 public boolean match(final Cookie cookie, final CookieOrigin origin) {
70 if (cookie == null) {
71 throw new IllegalArgumentException("Cookie may not be null");
72 }
73 if (origin == null) {
74 throw new IllegalArgumentException("Cookie origin may not be null");
75 }
76 String targetpath = origin.getPath();
77 String topmostPath = cookie.getPath();
78 if (topmostPath == null) {
79 topmostPath = "/";
80 }
81 if (topmostPath.length() > 1 && topmostPath.endsWith("/")) {
82 topmostPath = topmostPath.substring(0, topmostPath.length() - 1);
83 }
84 boolean match = targetpath.startsWith (topmostPath);
85
86
87 if (match && targetpath.length() != topmostPath.length()) {
88 if (!topmostPath.endsWith("/")) {
89 match = (targetpath.charAt(topmostPath.length()) == '/');
90 }
91 }
92 return match;
93 }
94
95 }