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.util.internal.ObjectUtil.checkNotNull;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.TreeSet;
26
27
28
29
30
31
32
33
34
35
36
37 public final class ServerCookieDecoder extends CookieDecoder {
38
39 private static final String RFC2965_VERSION = "$Version";
40
41 private static final String RFC2965_PATH = "$" + CookieHeaderNames.PATH;
42
43 private static final String RFC2965_DOMAIN = "$" + CookieHeaderNames.DOMAIN;
44
45 private static final String RFC2965_PORT = "$Port";
46
47
48
49
50
51 public static final ServerCookieDecoder STRICT = new ServerCookieDecoder(true);
52
53
54
55
56 public static final ServerCookieDecoder LAX = new ServerCookieDecoder(false);
57
58 private ServerCookieDecoder(boolean strict) {
59 super(strict);
60 }
61
62
63
64
65
66
67
68 public List<Cookie> decodeAll(String header) {
69 List<Cookie> cookies = new ArrayList<Cookie>();
70 decode(cookies, header);
71 return Collections.unmodifiableList(cookies);
72 }
73
74
75
76
77
78
79 public Set<Cookie> decode(String header) {
80 Set<Cookie> cookies = new TreeSet<Cookie>();
81 decode(cookies, header);
82 return cookies;
83 }
84
85
86
87
88 private void decode(Collection<? super Cookie> cookies, String header) {
89 final int headerLen = checkNotNull(header, "header").length();
90
91 if (headerLen == 0) {
92 return;
93 }
94
95 int i = 0;
96
97 boolean rfc2965Style = false;
98 if (header.regionMatches(true, 0, RFC2965_VERSION, 0, RFC2965_VERSION.length())) {
99
100 i = header.indexOf(';') + 1;
101 rfc2965Style = true;
102 }
103
104 loop: for (;;) {
105
106
107 for (;;) {
108 if (i == headerLen) {
109 break loop;
110 }
111 char c = header.charAt(i);
112 if (c == '\t' || c == '\n' || c == 0x0b || c == '\f'
113 || c == '\r' || c == ' ' || c == ',' || c == ';') {
114 i++;
115 continue;
116 }
117 break;
118 }
119
120 int nameBegin = i;
121 int nameEnd;
122 int valueBegin;
123 int valueEnd;
124
125 for (;;) {
126
127 char curChar = header.charAt(i);
128 if (curChar == ';') {
129
130 nameEnd = i;
131 valueBegin = valueEnd = -1;
132 break;
133
134 } else if (curChar == '=') {
135
136 nameEnd = i;
137 i++;
138 if (i == headerLen) {
139
140 valueBegin = valueEnd = 0;
141 break;
142 }
143
144 valueBegin = i;
145
146 int semiPos = header.indexOf(';', i);
147 valueEnd = i = semiPos > 0 ? semiPos : headerLen;
148 break;
149 } else {
150 i++;
151 }
152
153 if (i == headerLen) {
154
155 nameEnd = headerLen;
156 valueBegin = valueEnd = -1;
157 break;
158 }
159 }
160
161 if (rfc2965Style && (header.regionMatches(nameBegin, RFC2965_PATH, 0, RFC2965_PATH.length()) ||
162 header.regionMatches(nameBegin, RFC2965_DOMAIN, 0, RFC2965_DOMAIN.length()) ||
163 header.regionMatches(nameBegin, RFC2965_PORT, 0, RFC2965_PORT.length()))) {
164
165
166 continue;
167 }
168
169 DefaultCookie cookie = initCookie(header, nameBegin, nameEnd, valueBegin, valueEnd);
170 if (cookie != null) {
171 cookies.add(cookie);
172 }
173 }
174 }
175 }