1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http;
17
18 import static org.jboss.netty.handler.codec.http.CookieUtil.firstInvalidCookieNameOctet;
19 import static org.jboss.netty.handler.codec.http.CookieUtil.firstInvalidCookieValueOctet;
20 import static org.jboss.netty.handler.codec.http.CookieUtil.unwrapValue;
21 import org.jboss.netty.handler.codec.http.cookie.CookieHeaderNames;
22 import org.jboss.netty.util.internal.StringUtil;
23 import org.jboss.netty.logging.InternalLogger;
24 import org.jboss.netty.logging.InternalLoggerFactory;
25
26 import java.text.ParseException;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Set;
31 import java.util.TreeSet;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 @Deprecated
50 public final class CookieDecoder {
51
52 private final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
53
54 private static final String COMMENT = "Comment";
55
56 private static final String COMMENTURL = "CommentURL";
57
58 private static final String DISCARD = "Discard";
59
60 private static final String PORT = "Port";
61
62 private static final String VERSION = "Version";
63
64 private static final char COMMA = ',';
65
66 private final boolean strict;
67
68
69
70
71 public CookieDecoder() {
72 this(false);
73 }
74
75
76
77
78
79
80
81 public CookieDecoder(boolean strict) {
82 this.strict = strict;
83 }
84
85
86
87
88
89
90 public Set<Cookie> decode(String header) {
91 List<String> names = new ArrayList<String>(8);
92 List<String> values = new ArrayList<String>(8);
93 extractKeyValuePairs(header, names, values);
94
95 if (names.isEmpty()) {
96 return Collections.emptySet();
97 }
98
99 int i;
100 int version = 0;
101
102
103
104 if (names.get(0).equalsIgnoreCase(VERSION)) {
105 try {
106 version = Integer.parseInt(values.get(0));
107 } catch (NumberFormatException e) {
108
109 }
110 i = 1;
111 } else {
112 i = 0;
113 }
114
115 if (names.size() <= i) {
116
117 return Collections.emptySet();
118 }
119
120 Set<Cookie> cookies = new TreeSet<Cookie>();
121 for (; i < names.size(); i ++) {
122 String name = names.get(i);
123 String value = values.get(i);
124 if (value == null) {
125 value = "";
126 }
127
128 Cookie c = initCookie(name, value);
129
130 if (c == null) {
131 break;
132 }
133
134 boolean discard = false;
135 boolean secure = false;
136 boolean httpOnly = false;
137 String comment = null;
138 String commentURL = null;
139 String domain = null;
140 String path = null;
141 int maxAge = Integer.MIN_VALUE;
142 List<Integer> ports = new ArrayList<Integer>(2);
143
144 for (int j = i + 1; j < names.size(); j++, i++) {
145 name = names.get(j);
146 value = values.get(j);
147
148 if (DISCARD.equalsIgnoreCase(name)) {
149 discard = true;
150 } else if (CookieHeaderNames.SECURE.equalsIgnoreCase(name)) {
151 secure = true;
152 } else if (CookieHeaderNames.HTTPONLY.equalsIgnoreCase(name)) {
153 httpOnly = true;
154 } else if (COMMENT.equalsIgnoreCase(name)) {
155 comment = value;
156 } else if (COMMENTURL.equalsIgnoreCase(name)) {
157 commentURL = value;
158 } else if (CookieHeaderNames.DOMAIN.equalsIgnoreCase(name)) {
159 domain = value;
160 } else if (CookieHeaderNames.PATH.equalsIgnoreCase(name)) {
161 path = value;
162 } else if (CookieHeaderNames.EXPIRES.equalsIgnoreCase(name)) {
163 try {
164 long maxAgeMillis =
165 HttpHeaderDateFormat.get().parse(value).getTime() -
166 System.currentTimeMillis();
167
168 maxAge = (int) (maxAgeMillis / 1000 + (maxAgeMillis % 1000 != 0? 1 : 0));
169 } catch (ParseException e) {
170
171 }
172 } else if (CookieHeaderNames.MAX_AGE.equalsIgnoreCase(name)) {
173 maxAge = Integer.parseInt(value);
174 } else if (VERSION.equalsIgnoreCase(name)) {
175 version = Integer.parseInt(value);
176 } else if (PORT.equalsIgnoreCase(name)) {
177 String[] portList = StringUtil.split(value, COMMA);
178 for (String s1: portList) {
179 try {
180 ports.add(Integer.valueOf(s1));
181 } catch (NumberFormatException e) {
182
183 }
184 }
185 } else {
186 break;
187 }
188 }
189
190 c.setVersion(version);
191 c.setMaxAge(maxAge);
192 c.setPath(path);
193 c.setDomain(domain);
194 c.setSecure(secure);
195 c.setHttpOnly(httpOnly);
196 if (version > 0) {
197 c.setComment(comment);
198 }
199 if (version > 1) {
200 c.setCommentUrl(commentURL);
201 c.setPorts(ports);
202 c.setDiscard(discard);
203 }
204
205 cookies.add(c);
206 }
207
208 return cookies;
209 }
210
211 private static void extractKeyValuePairs(
212 final String header, final List<String> names, final List<String> values) {
213 final int headerLen = header.length();
214 loop: for (int i = 0;;) {
215
216
217 for (;;) {
218 if (i == headerLen) {
219 break loop;
220 }
221 switch (header.charAt(i)) {
222 case '\t': case '\n': case 0x0b: case '\f': case '\r':
223 case ' ': case ',': case ';':
224 i ++;
225 continue;
226 }
227 break;
228 }
229
230
231 for (;;) {
232 if (i == headerLen) {
233 break loop;
234 }
235 if (header.charAt(i) == '$') {
236 i ++;
237 continue;
238 }
239 break;
240 }
241
242 String name;
243 String value;
244
245 if (i == headerLen) {
246 name = null;
247 value = null;
248 } else {
249 int newNameStart = i;
250 keyValLoop: for (;;) {
251 switch (header.charAt(i)) {
252 case ';':
253
254 name = header.substring(newNameStart, i);
255 value = null;
256 break keyValLoop;
257 case '=':
258
259 name = header.substring(newNameStart, i);
260 i ++;
261 if (i == headerLen) {
262
263 value = "";
264 break keyValLoop;
265 }
266
267 int newValueStart = i;
268 char c = header.charAt(i);
269 if (c == '"' || c == '\'') {
270
271 StringBuilder newValueBuf = new StringBuilder(header.length() - i);
272 final char q = c;
273 boolean hadBackslash = false;
274 i ++;
275 for (;;) {
276 if (i == headerLen) {
277 value = newValueBuf.toString();
278 break keyValLoop;
279 }
280 if (hadBackslash) {
281 hadBackslash = false;
282 c = header.charAt(i ++);
283 switch (c) {
284 case '\\': case '"': case '\'':
285
286 newValueBuf.setCharAt(newValueBuf.length() - 1, c);
287 break;
288 default:
289
290 newValueBuf.append(c);
291 }
292 } else {
293 c = header.charAt(i ++);
294 if (c == q) {
295 value = newValueBuf.toString();
296 break keyValLoop;
297 }
298 newValueBuf.append(c);
299 if (c == '\\') {
300 hadBackslash = true;
301 }
302 }
303 }
304 } else {
305
306 int semiPos = header.indexOf(';', i);
307 if (semiPos > 0) {
308 value = header.substring(newValueStart, semiPos);
309 i = semiPos;
310 } else {
311 value = header.substring(newValueStart);
312 i = headerLen;
313 }
314 }
315 break keyValLoop;
316 default:
317 i ++;
318 }
319
320 if (i == headerLen) {
321
322 name = header.substring(newNameStart);
323 value = null;
324 break;
325 }
326 }
327 }
328
329 names.add(name);
330 values.add(value);
331 }
332 }
333
334 private DefaultCookie initCookie(String name, String value) {
335 if (name == null || name.length() == 0) {
336 logger.debug("Skipping cookie with null name");
337 return null;
338 }
339
340 if (value == null) {
341 logger.debug("Skipping cookie with null value");
342 return null;
343 }
344
345 CharSequence unwrappedValue = unwrapValue(value);
346 if (unwrappedValue == null) {
347 if (logger.isDebugEnabled()) {
348 logger.debug("Skipping cookie because starting quotes are not properly balanced in '"
349 + unwrappedValue + "'");
350 }
351 return null;
352 }
353
354 int invalidOctetPos;
355 if (strict && (invalidOctetPos = firstInvalidCookieNameOctet(name)) >= 0) {
356 if (logger.isDebugEnabled()) {
357 logger.debug("Skipping cookie because name '" + name + "' contains invalid char '"
358 + name.charAt(invalidOctetPos) + "'");
359 }
360 return null;
361 }
362
363 final boolean wrap = unwrappedValue.length() != value.length();
364
365 if (strict && (invalidOctetPos = firstInvalidCookieValueOctet(unwrappedValue)) >= 0) {
366 if (logger.isDebugEnabled()) {
367 logger.debug("Skipping cookie because value '" + unwrappedValue
368 + "' contains invalid char '" + unwrappedValue.charAt(invalidOctetPos) + "'");
369 }
370 return null;
371 }
372
373 DefaultCookie cookie = new DefaultCookie(name, unwrappedValue.toString());
374 cookie.setWrap(wrap);
375 return cookie;
376 }
377 }