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.netty.handler.codec.http;
17  
18  import java.util.BitSet;
19  
20  /**
21   * @deprecated Duplicate of package private ${@link io.netty.handler.codec.http.cookie.CookieUtil}
22   */
23  @Deprecated
24  final class CookieUtil {
25  
26      private static final BitSet VALID_COOKIE_VALUE_OCTETS = validCookieValueOctets();
27  
28      private static final BitSet VALID_COOKIE_NAME_OCTETS = validCookieNameOctets(VALID_COOKIE_VALUE_OCTETS);
29  
30      // US-ASCII characters excluding CTLs, whitespace, DQUOTE, comma, semicolon, and backslash
31      private static BitSet validCookieValueOctets() {
32          BitSet bits = new BitSet(8);
33          for (int i = 35; i < 127; i++) {
34              // US-ASCII characters excluding CTLs (%x00-1F / %x7F)
35              bits.set(i);
36          }
37          bits.set('"', false);  // exclude DQUOTE = %x22
38          bits.set(',', false);  // exclude comma = %x2C
39          bits.set(';', false);  // exclude semicolon = %x3B
40          bits.set('\\', false); // exclude backslash = %x5C
41          return bits;
42      }
43  
44      //    token          = 1*<any CHAR except CTLs or separators>
45      //    separators     = "(" | ")" | "<" | ">" | "@"
46      //                   | "," | ";" | ":" | "\" | <">
47      //                   | "/" | "[" | "]" | "?" | "="
48      //                   | "{" | "}" | SP | HT
49      private static BitSet validCookieNameOctets(BitSet validCookieValueOctets) {
50          BitSet bits = new BitSet(8);
51          bits.or(validCookieValueOctets);
52          bits.set('(', false);
53          bits.set(')', false);
54          bits.set('<', false);
55          bits.set('>', false);
56          bits.set('@', false);
57          bits.set(':', false);
58          bits.set('/', false);
59          bits.set('[', false);
60          bits.set(']', false);
61          bits.set('?', false);
62          bits.set('=', false);
63          bits.set('{', false);
64          bits.set('}', false);
65          bits.set(' ', false);
66          bits.set('\t', false);
67          return bits;
68      }
69  
70      static int firstInvalidCookieNameOctet(CharSequence cs) {
71          return firstInvalidOctet(cs, VALID_COOKIE_NAME_OCTETS);
72      }
73  
74      static int firstInvalidCookieValueOctet(CharSequence cs) {
75          return firstInvalidOctet(cs, VALID_COOKIE_VALUE_OCTETS);
76      }
77  
78      static int firstInvalidOctet(CharSequence cs, BitSet bits) {
79          for (int i = 0; i < cs.length(); i++) {
80              char c = cs.charAt(i);
81              if (!bits.get(c)) {
82                  return i;
83              }
84          }
85          return -1;
86      }
87  
88      static CharSequence unwrapValue(CharSequence cs) {
89          final int len = cs.length();
90          if (len > 0 && cs.charAt(0) == '"') {
91              if (len >= 2 && cs.charAt(len - 1) == '"') {
92                  // properly balanced
93                  return len == 2 ? "" : cs.subSequence(1, len - 1);
94              } else {
95                  return null;
96              }
97          }
98          return cs;
99      }
100 
101     private CookieUtil() {
102         // Unused
103     }
104 }