1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http.cookie;
17
18 import static org.jboss.netty.handler.codec.http.cookie.CookieUtil.firstInvalidCookieNameOctet;
19 import static org.jboss.netty.handler.codec.http.cookie.CookieUtil.firstInvalidCookieValueOctet;
20 import static org.jboss.netty.handler.codec.http.cookie.CookieUtil.unwrapValue;
21
22 import java.nio.CharBuffer;
23
24 import org.jboss.netty.logging.InternalLogger;
25 import org.jboss.netty.logging.InternalLoggerFactory;
26
27
28
29
30 public abstract class CookieDecoder {
31
32 private final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
33
34 private final boolean strict;
35
36 protected CookieDecoder(boolean strict) {
37 this.strict = strict;
38 }
39
40 protected DefaultCookie initCookie(String header, int nameBegin, int nameEnd, int valueBegin, int valueEnd) {
41 if (nameBegin == -1 || nameBegin == nameEnd) {
42 logger.debug("Skipping cookie with null name");
43 return null;
44 }
45
46 if (valueBegin == -1) {
47 logger.debug("Skipping cookie with null value");
48 return null;
49 }
50
51 CharSequence wrappedValue = CharBuffer.wrap(header, valueBegin, valueEnd);
52 CharSequence unwrappedValue = unwrapValue(wrappedValue);
53 if (unwrappedValue == null) {
54 if (logger.isDebugEnabled()) {
55 logger.debug("Skipping cookie because starting quotes are not properly balanced in '"
56 + wrappedValue + "'");
57 }
58 return null;
59 }
60
61 final String name = header.substring(nameBegin, nameEnd);
62
63 int invalidOctetPos;
64 if (strict && (invalidOctetPos = firstInvalidCookieNameOctet(name)) >= 0) {
65 if (logger.isDebugEnabled()) {
66 logger.debug("Skipping cookie because name '" + name + "' contains invalid char '"
67 + name.charAt(invalidOctetPos) + "'");
68 }
69 return null;
70 }
71
72 final boolean wrap = unwrappedValue.length() != valueEnd - valueBegin;
73
74 if (strict && (invalidOctetPos = firstInvalidCookieValueOctet(unwrappedValue)) >= 0) {
75 if (logger.isDebugEnabled()) {
76 logger.debug("Skipping cookie because value '" + unwrappedValue
77 + "' contains invalid char '" + unwrappedValue.charAt(invalidOctetPos) + "'");
78 }
79 return null;
80 }
81
82 DefaultCookie cookie = new DefaultCookie(name, unwrappedValue.toString());
83 cookie.setWrap(wrap);
84 return cookie;
85 }
86 }