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.StringTokenizer;
31
32 import org.apache.http.annotation.Immutable;
33
34 import org.apache.http.cookie.ClientCookie;
35 import org.apache.http.cookie.Cookie;
36 import org.apache.http.cookie.CookieAttributeHandler;
37 import org.apache.http.cookie.CookieOrigin;
38 import org.apache.http.cookie.CookieRestrictionViolationException;
39 import org.apache.http.cookie.MalformedCookieException;
40 import org.apache.http.cookie.SetCookie;
41 import org.apache.http.cookie.SetCookie2;
42
43
44
45
46
47
48 @Immutable
49 public class RFC2965PortAttributeHandler implements CookieAttributeHandler {
50
51 public RFC2965PortAttributeHandler() {
52 super();
53 }
54
55
56
57
58
59
60
61
62
63
64 private static int[] parsePortAttribute(final String portValue)
65 throws MalformedCookieException {
66 StringTokenizer st = new StringTokenizer(portValue, ",");
67 int[] ports = new int[st.countTokens()];
68 try {
69 int i = 0;
70 while(st.hasMoreTokens()) {
71 ports[i] = Integer.parseInt(st.nextToken().trim());
72 if (ports[i] < 0) {
73 throw new MalformedCookieException ("Invalid Port attribute.");
74 }
75 ++i;
76 }
77 } catch (NumberFormatException e) {
78 throw new MalformedCookieException ("Invalid Port "
79 + "attribute: " + e.getMessage());
80 }
81 return ports;
82 }
83
84
85
86
87
88
89
90
91
92
93 private static boolean portMatch(int port, int[] ports) {
94 boolean portInList = false;
95 for (int i = 0, len = ports.length; i < len; i++) {
96 if (port == ports[i]) {
97 portInList = true;
98 break;
99 }
100 }
101 return portInList;
102 }
103
104
105
106
107 public void parse(final SetCookie cookie, final String portValue)
108 throws MalformedCookieException {
109 if (cookie == null) {
110 throw new IllegalArgumentException("Cookie may not be null");
111 }
112 if (cookie instanceof SetCookie2) {
113 SetCookie2 cookie2 = (SetCookie2) cookie;
114 if (portValue != null && portValue.trim().length() > 0) {
115 int[] ports = parsePortAttribute(portValue);
116 cookie2.setPorts(ports);
117 }
118 }
119 }
120
121
122
123
124
125 public void validate(final Cookie cookie, final CookieOrigin origin)
126 throws MalformedCookieException {
127 if (cookie == null) {
128 throw new IllegalArgumentException("Cookie may not be null");
129 }
130 if (origin == null) {
131 throw new IllegalArgumentException("Cookie origin may not be null");
132 }
133 int port = origin.getPort();
134 if (cookie instanceof ClientCookie
135 && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
136 if (!portMatch(port, cookie.getPorts())) {
137 throw new CookieRestrictionViolationException(
138 "Port attribute violates RFC 2965: "
139 + "Request port not found in cookie's port list.");
140 }
141 }
142 }
143
144
145
146
147
148
149 public boolean match(final Cookie cookie, final CookieOrigin origin) {
150 if (cookie == null) {
151 throw new IllegalArgumentException("Cookie may not be null");
152 }
153 if (origin == null) {
154 throw new IllegalArgumentException("Cookie origin may not be null");
155 }
156 int port = origin.getPort();
157 if (cookie instanceof ClientCookie
158 && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
159 if (cookie.getPorts() == null) {
160
161 return false;
162 }
163 if (!portMatch(port, cookie.getPorts())) {
164 return false;
165 }
166 }
167 return true;
168 }
169
170 }