|
12 | 12 | */
|
13 | 13 | package org.asynchttpclient.cookie;
|
14 | 14 |
|
15 |
| -import org.asynchttpclient.util.StringUtils; |
16 |
| - |
| 15 | +import java.util.Arrays; |
17 | 16 | import java.util.Collection;
|
| 17 | +import java.util.Comparator; |
| 18 | + |
| 19 | +import org.asynchttpclient.util.StringUtils; |
18 | 20 |
|
19 | 21 | public final class CookieEncoder {
|
20 | 22 |
|
| 23 | + /** |
| 24 | + * Sort cookies into decreasing order of path length, breaking ties by sorting into increasing chronological order of creation time, as recommended by RFC 6265. |
| 25 | + */ |
| 26 | + private static final Comparator<Cookie> COOKIE_COMPARATOR = new Comparator<Cookie>() { |
| 27 | + @Override |
| 28 | + public int compare(Cookie c1, Cookie c2) { |
| 29 | + String path1 = c1.getPath(); |
| 30 | + String path2 = c2.getPath(); |
| 31 | + // Cookies with unspecified path default to the path of the request. We don't |
| 32 | + // know the request path here, but we assume that the length of an unspecified |
| 33 | + // path is longer than any specified path (i.e. pathless cookies come first), |
| 34 | + // because setting cookies with a path longer than the request path is of |
| 35 | + // limited use. |
| 36 | + int len1 = path1 == null ? Integer.MAX_VALUE : path1.length(); |
| 37 | + int len2 = path2 == null ? Integer.MAX_VALUE : path2.length(); |
| 38 | + int diff = len2 - len1; |
| 39 | + if (diff != 0) { |
| 40 | + return diff; |
| 41 | + } |
| 42 | + // Rely on Java's sort stability to retain creation order in cases where |
| 43 | + // cookies have same path length. |
| 44 | + return -1; |
| 45 | + } |
| 46 | + }; |
| 47 | + |
21 | 48 | private CookieEncoder() {
|
22 | 49 | }
|
23 | 50 |
|
24 | 51 | public static String encode(Collection<Cookie> cookies) {
|
25 | 52 | StringBuilder sb = StringUtils.stringBuilder();
|
26 | 53 |
|
27 |
| - for (Cookie cookie : cookies) { |
28 |
| - add(sb, cookie.getName(), cookie.getValue(), cookie.isWrap()); |
| 54 | + if (cookies.isEmpty()) { |
| 55 | + return ""; |
| 56 | + |
| 57 | + } else if (cookies.size() == 1) { |
| 58 | + Cookie cookie = cookies.iterator().next(); |
| 59 | + if (cookie != null) { |
| 60 | + add(sb, cookie.getName(), cookie.getValue(), cookie.isWrap()); |
| 61 | + } |
| 62 | + |
| 63 | + } else { |
| 64 | + Cookie[] cookiesSorted = cookies.toArray(new Cookie[cookies.size()]); |
| 65 | + Arrays.sort(cookiesSorted, COOKIE_COMPARATOR); |
| 66 | + for (int i = 0; i < cookiesSorted.length; i++) { |
| 67 | + Cookie cookie = cookiesSorted[i]; |
| 68 | + if (cookie != null) { |
| 69 | + add(sb, cookie.getName(), cookie.getValue(), cookie.isWrap()); |
| 70 | + } |
| 71 | + } |
29 | 72 | }
|
30 | 73 |
|
31 | 74 | if (sb.length() > 0) {
|
|
0 commit comments