1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http.cookie;
17
18 import static io.netty.handler.codec.http.cookie.CookieUtil.firstInvalidCookieNameOctet;
19 import static io.netty.handler.codec.http.cookie.CookieUtil.firstInvalidCookieValueOctet;
20 import static io.netty.handler.codec.http.cookie.CookieUtil.unwrapValue;
21
22 import java.nio.CharBuffer;
23
24 import io.netty.util.internal.logging.InternalLogger;
25 import io.netty.util.internal.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 logger.debug("Skipping cookie because starting quotes are not properly balanced in '{}'",
55 wrappedValue);
56 return null;
57 }
58
59 final String name = header.substring(nameBegin, nameEnd);
60
61 int invalidOctetPos;
62 if (strict && (invalidOctetPos = firstInvalidCookieNameOctet(name)) >= 0) {
63 if (logger.isDebugEnabled()) {
64 logger.debug("Skipping cookie because name '{}' contains invalid char '{}'",
65 name, name.charAt(invalidOctetPos));
66 }
67 return null;
68 }
69
70 final boolean wrap = unwrappedValue.length() != valueEnd - valueBegin;
71
72 if (strict && (invalidOctetPos = firstInvalidCookieValueOctet(unwrappedValue)) >= 0) {
73 if (logger.isDebugEnabled()) {
74 logger.debug("Skipping cookie because value '{}' contains invalid char '{}'",
75 unwrappedValue, unwrappedValue.charAt(invalidOctetPos));
76 }
77 return null;
78 }
79
80 DefaultCookie cookie = new DefaultCookie(name, unwrappedValue.toString());
81 cookie.setWrap(wrap);
82 return cookie;
83 }
84 }