View Javadoc
1   /*
2    * Copyright 2015 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * A <a href="https://tools.ietf.org/html/rfc6265">RFC6265</a> compliant cookie encoder to be used client side, so
36   * only name=value pairs are sent.
37   *
38   * Note that multiple cookies are supposed to be sent at once in a single "Cookie" header.
39   *
40   * <pre>
41   * // Example
42   * {@link HttpRequest} req = ...;
43   * res.setHeader("Cookie", {@link ClientCookieEncoder}.encode("JSESSIONID", "1234"));
44   * </pre>
45   *
46   * @see ClientCookieDecoder
47   */
48  public final class ClientCookieEncoder extends CookieEncoder {
49  
50      /**
51       * Strict encoder that validates that name and value chars are in the valid scope and (for methods that accept
52       * multiple cookies) sorts cookies into order of decreasing path length, as specified in RFC6265.
53       */
54      public static final ClientCookieEncoder STRICT = new ClientCookieEncoder(true);
55  
56      /**
57       * Lax instance that doesn't validate name and value, and (for methods that accept multiple cookies) keeps
58       * cookies in the order in which they were given.
59       */
60      public static final ClientCookieEncoder LAX = new ClientCookieEncoder(false);
61  
62      private ClientCookieEncoder(boolean strict) {
63          super(strict);
64      }
65  
66      /**
67       * Encodes the specified cookie into a Cookie header value.
68       *
69       * @param name
70       *            the cookie name
71       * @param value
72       *            the cookie value
73       * @return a Rfc6265 style Cookie header value
74       */
75      public String encode(String name, String value) {
76          return encode(new DefaultCookie(name, value));
77      }
78  
79      /**
80       * Encodes the specified cookie into a Cookie header value.
81       *
82       * @param cookie the specified cookie
83       * @return a Rfc6265 style Cookie header value
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       * Sort cookies into decreasing order of path length, breaking ties by sorting into increasing chronological
93       * order of creation time, as recommended by RFC 6265.
94       */
95      // package-private for testing only
96      static final Comparator<Cookie> COOKIE_COMPARATOR = (c1, c2) -> {
97          String path1 = c1.path();
98          String path2 = c2.path();
99          // Cookies with unspecified path default to the path of the request. We don't
100         // know the request path here, but we assume that the length of an unspecified
101         // path is longer than any specified path (i.e. pathless cookies come first),
102         // because setting cookies with a path longer than the request path is of
103         // limited use.
104         int len1 = path1 == null ? Integer.MAX_VALUE : path1.length();
105         int len2 = path2 == null ? Integer.MAX_VALUE : path2.length();
106 
107         // Rely on Arrays.sort's stability to retain creation order in cases where
108         // cookies have same path length.
109         return len2 - len1;
110     };
111 
112     /**
113      * Encodes the specified cookies into a single Cookie header value.
114      *
115      * @param cookies
116      *            some cookies
117      * @return a Rfc6265 style Cookie header value, null if no cookies are passed.
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      * Encodes the specified cookies into a single Cookie header value.
145      *
146      * @param cookies
147      *            some cookies
148      * @return a Rfc6265 style Cookie header value, null if no cookies are passed.
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      * Encodes the specified cookies into a single Cookie header value.
176      *
177      * @param cookies some cookies
178      * @return a Rfc6265 style Cookie header value, null if no cookies are passed.
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 }