1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.handler.codec.http.cookie;
17
18 import io.netty5.util.internal.logging.InternalLogger;
19 import io.netty5.util.internal.logging.InternalLoggerFactory;
20
21 import java.nio.CharBuffer;
22
23 import static io.netty5.handler.codec.http.cookie.CookieUtil.firstInvalidCookieNameOctet;
24 import static io.netty5.handler.codec.http.cookie.CookieUtil.firstInvalidCookieValueOctet;
25 import static io.netty5.handler.codec.http.cookie.CookieUtil.unwrapValue;
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 }