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