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.handler.codec.http.HttpRequest;
19 import io.netty5.util.internal.StringUtil;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Comparator;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import static io.netty5.handler.codec.http.cookie.CookieUtil.add;
29 import static io.netty5.handler.codec.http.cookie.CookieUtil.addQuoted;
30 import static io.netty5.handler.codec.http.cookie.CookieUtil.stripTrailingSeparator;
31 import static io.netty5.handler.codec.http.cookie.CookieUtil.stripTrailingSeparatorOrNull;
32 import static java.util.Objects.requireNonNull;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public final class ClientCookieEncoder extends CookieEncoder {
49
50
51
52
53
54 public static final ClientCookieEncoder STRICT = new ClientCookieEncoder(true);
55
56
57
58
59
60 public static final ClientCookieEncoder LAX = new ClientCookieEncoder(false);
61
62 private ClientCookieEncoder(boolean strict) {
63 super(strict);
64 }
65
66
67
68
69
70
71
72
73
74
75 public String encode(String name, String value) {
76 return encode(new DefaultCookie(name, value));
77 }
78
79
80
81
82
83
84
85 public String encode(Cookie cookie) {
86 StringBuilder buf = StringUtil.threadLocalStringBuilder();
87 encode(buf, requireNonNull(cookie, "cookie"));
88 return stripTrailingSeparator(buf);
89 }
90
91
92
93
94
95
96 static final Comparator<Cookie> COOKIE_COMPARATOR = (c1, c2) -> {
97 String path1 = c1.path();
98 String path2 = c2.path();
99
100
101
102
103
104 int len1 = path1 == null ? Integer.MAX_VALUE : path1.length();
105 int len2 = path2 == null ? Integer.MAX_VALUE : path2.length();
106
107
108
109 return len2 - len1;
110 };
111
112
113
114
115
116
117
118
119 public String encode(Cookie... cookies) {
120 if (requireNonNull(cookies, "cookies").length == 0) {
121 return null;
122 }
123
124 StringBuilder buf = StringUtil.threadLocalStringBuilder();
125 if (strict) {
126 if (cookies.length == 1) {
127 encode(buf, cookies[0]);
128 } else {
129 Cookie[] cookiesSorted = Arrays.copyOf(cookies, cookies.length);
130 Arrays.sort(cookiesSorted, COOKIE_COMPARATOR);
131 for (Cookie c : cookiesSorted) {
132 encode(buf, c);
133 }
134 }
135 } else {
136 for (Cookie c : cookies) {
137 encode(buf, c);
138 }
139 }
140 return stripTrailingSeparatorOrNull(buf);
141 }
142
143
144
145
146
147
148
149
150 public String encode(Collection<? extends Cookie> cookies) {
151 if (requireNonNull(cookies, "cookies").isEmpty()) {
152 return null;
153 }
154
155 StringBuilder buf = StringUtil.threadLocalStringBuilder();
156 if (strict) {
157 if (cookies.size() == 1) {
158 encode(buf, cookies.iterator().next());
159 } else {
160 Cookie[] cookiesSorted = cookies.toArray(new Cookie[0]);
161 Arrays.sort(cookiesSorted, COOKIE_COMPARATOR);
162 for (Cookie c : cookiesSorted) {
163 encode(buf, c);
164 }
165 }
166 } else {
167 for (Cookie c : cookies) {
168 encode(buf, c);
169 }
170 }
171 return stripTrailingSeparatorOrNull(buf);
172 }
173
174
175
176
177
178
179
180 public String encode(Iterable<? extends Cookie> cookies) {
181 Iterator<? extends Cookie> cookiesIt = requireNonNull(cookies, "cookies").iterator();
182 if (!cookiesIt.hasNext()) {
183 return null;
184 }
185
186 StringBuilder buf = StringUtil.threadLocalStringBuilder();
187 if (strict) {
188 Cookie firstCookie = cookiesIt.next();
189 if (!cookiesIt.hasNext()) {
190 encode(buf, firstCookie);
191 } else {
192 final Cookie[] cookiesSorted;
193 if (cookies instanceof Collection) {
194 cookiesSorted = new Cookie[((Collection) cookies).size()];
195 cookiesSorted[0] = firstCookie;
196 for (int i = 1; cookiesIt.hasNext(); i++) {
197 cookiesSorted[i] = cookiesIt.next();
198 }
199 } else {
200 List<Cookie> cookiesList = new ArrayList<>();
201 cookiesList.add(firstCookie);
202 while (cookiesIt.hasNext()) {
203 cookiesList.add(cookiesIt.next());
204 }
205 cookiesSorted = cookiesList.toArray(new Cookie[0]);
206 }
207
208 Arrays.sort(cookiesSorted, COOKIE_COMPARATOR);
209 for (Cookie c : cookiesSorted) {
210 encode(buf, c);
211 }
212 }
213 } else {
214 while (cookiesIt.hasNext()) {
215 encode(buf, cookiesIt.next());
216 }
217 }
218 return stripTrailingSeparatorOrNull(buf);
219 }
220
221 private void encode(StringBuilder buf, Cookie c) {
222 final String name = c.name();
223 final String value = c.value() != null ? c.value() : "";
224
225 validateCookie(name, value);
226
227 if (c.wrap()) {
228 addQuoted(buf, name, value);
229 } else {
230 add(buf, name, value);
231 }
232 }
233 }