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 package org.apache.http.impl.cookie;
29
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Locale;
33
34 import org.apache.http.annotation.NotThreadSafe;
35
36 import org.apache.http.HeaderElement;
37 import org.apache.http.NameValuePair;
38 import org.apache.http.cookie.Cookie;
39 import org.apache.http.cookie.CookieAttributeHandler;
40 import org.apache.http.cookie.CookieOrigin;
41 import org.apache.http.cookie.MalformedCookieException;
42
43
44
45
46
47
48
49 @NotThreadSafe
50 public abstract class CookieSpecBase extends AbstractCookieSpec {
51
52 protected static String getDefaultPath(final CookieOrigin origin) {
53 String defaultPath = origin.getPath();
54 int lastSlashIndex = defaultPath.lastIndexOf('/');
55 if (lastSlashIndex >= 0) {
56 if (lastSlashIndex == 0) {
57
58 lastSlashIndex = 1;
59 }
60 defaultPath = defaultPath.substring(0, lastSlashIndex);
61 }
62 return defaultPath;
63 }
64
65 protected static String getDefaultDomain(final CookieOrigin origin) {
66 return origin.getHost();
67 }
68
69 protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin)
70 throws MalformedCookieException {
71 List<Cookie> cookies = new ArrayList<Cookie>(elems.length);
72 for (HeaderElement headerelement : elems) {
73 String name = headerelement.getName();
74 String value = headerelement.getValue();
75 if (name == null || name.length() == 0) {
76 throw new MalformedCookieException("Cookie name may not be empty");
77 }
78
79 BasicClientCookie cookie = new BasicClientCookie(name, value);
80 cookie.setPath(getDefaultPath(origin));
81 cookie.setDomain(getDefaultDomain(origin));
82
83
84 NameValuePair[] attribs = headerelement.getParameters();
85 for (int j = attribs.length - 1; j >= 0; j--) {
86 NameValuePair attrib = attribs[j];
87 String s = attrib.getName().toLowerCase(Locale.ENGLISH);
88
89 cookie.setAttribute(s, attrib.getValue());
90
91 CookieAttributeHandler handler = findAttribHandler(s);
92 if (handler != null) {
93 handler.parse(cookie, attrib.getValue());
94 }
95 }
96 cookies.add(cookie);
97 }
98 return cookies;
99 }
100
101 public void validate(final Cookie cookie, final CookieOrigin origin)
102 throws MalformedCookieException {
103 if (cookie == null) {
104 throw new IllegalArgumentException("Cookie may not be null");
105 }
106 if (origin == null) {
107 throw new IllegalArgumentException("Cookie origin may not be null");
108 }
109 for (CookieAttributeHandler handler: getAttribHandlers()) {
110 handler.validate(cookie, origin);
111 }
112 }
113
114 public boolean match(final Cookie cookie, final CookieOrigin origin) {
115 if (cookie == null) {
116 throw new IllegalArgumentException("Cookie may not be null");
117 }
118 if (origin == null) {
119 throw new IllegalArgumentException("Cookie origin may not be null");
120 }
121 for (CookieAttributeHandler handler: getAttribHandlers()) {
122 if (!handler.match(cookie, origin)) {
123 return false;
124 }
125 }
126 return true;
127 }
128
129 }