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    *   http://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 org.jboss.netty.handler.codec.http.cookie;
17  
18  import org.jboss.netty.handler.codec.http.HttpConstants;
19  
20  import java.util.BitSet;
21  
22  final class CookieUtil {
23  
24      private static final BitSet VALID_COOKIE_VALUE_OCTETS = validCookieValueOctets();
25  
26      private static final BitSet VALID_COOKIE_NAME_OCTETS = validCookieNameOctets(VALID_COOKIE_VALUE_OCTETS);
27  
28      // US-ASCII characters excluding CTLs, whitespace, DQUOTE, comma, semicolon, and backslash
29      private static BitSet validCookieValueOctets() {
30          BitSet bits = new BitSet(8);
31          for (int i = 35; i < 127; i++) {
32              // US-ASCII characters excluding CTLs (%x00-1F / %x7F)
33              bits.set(i);
34          }
35          bits.set('"', false);  // exclude DQUOTE = %x22
36          bits.set(',', false);  // exclude comma = %x2C
37          bits.set(';', false);  // exclude semicolon = %x3B
38          bits.set('\\', false); // exclude backslash = %x5C
39          return bits;
40      }
41  
42      //    token          = 1*<any CHAR except CTLs or separators>
43      //    separators     = "(" | ")" | "<" | ">" | "@"
44      //                   | "," | ";" | ":" | "\" | <">
45      //                   | "/" | "[" | "]" | "?" | "="
46      //                   | "{" | "}" | SP | HT
47      private static BitSet validCookieNameOctets(BitSet validCookieValueOctets) {
48          BitSet bits = new BitSet(8);
49          bits.or(validCookieValueOctets);
50          bits.set('(', false);
51          bits.set(')', false);
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('\t', false);
65          return bits;
66      }
67  
68      /**
69       * @param buf a buffer where some cookies were maybe encoded
70       * @return the buffer String without the trailing separator, or null if no cookie was appended.
71       */
72      static String stripTrailingSeparatorOrNull(StringBuilder buf) {
73          return buf.length() == 0 ? null : stripTrailingSeparator(buf);
74      }
75  
76      static String stripTrailingSeparator(StringBuilder buf) {
77          if (buf.length() > 0) {
78              buf.setLength(buf.length() - 2);
79          }
80          return buf.toString();
81      }
82  
83      static void add(StringBuilder sb, String name, long val) {
84          sb.append(name);
85          sb.append((char) HttpConstants.EQUALS);
86          sb.append(val);
87          sb.append((char) HttpConstants.SEMICOLON);
88          sb.append((char) HttpConstants.SP);
89      }
90  
91      static void add(StringBuilder sb, String name, String val) {
92          sb.append(name);
93          sb.append((char) HttpConstants.EQUALS);
94          sb.append(val);
95          sb.append((char) HttpConstants.SEMICOLON);
96          sb.append((char) HttpConstants.SP);
97      }
98  
99      static void add(StringBuilder sb, String name) {
100         sb.append(name);
101         sb.append((char) HttpConstants.SEMICOLON);
102         sb.append((char) HttpConstants.SP);
103     }
104 
105     static void addQuoted(StringBuilder sb, String name, String val) {
106         if (val == null) {
107             val = "";
108         }
109 
110         sb.append(name);
111         sb.append((char) HttpConstants.EQUALS);
112         sb.append((char) HttpConstants.DOUBLE_QUOTE);
113         sb.append(val);
114         sb.append((char) HttpConstants.DOUBLE_QUOTE);
115         sb.append((char) HttpConstants.SEMICOLON);
116         sb.append((char) HttpConstants.SP);
117     }
118 
119     static int firstInvalidCookieNameOctet(CharSequence cs) {
120         return firstInvalidOctet(cs, VALID_COOKIE_NAME_OCTETS);
121     }
122 
123     static int firstInvalidCookieValueOctet(CharSequence cs) {
124         return firstInvalidOctet(cs, VALID_COOKIE_VALUE_OCTETS);
125     }
126 
127     static int firstInvalidOctet(CharSequence cs, BitSet bits) {
128         for (int i = 0; i < cs.length(); i++) {
129             char c = cs.charAt(i);
130             if (!bits.get(c)) {
131                 return i;
132             }
133         }
134         return -1;
135     }
136 
137     static CharSequence unwrapValue(CharSequence cs) {
138         final int len = cs.length();
139         if (len > 0 && cs.charAt(0) == '"') {
140             if (len >= 2 && cs.charAt(len - 1) == '"') {
141                 // properly balanced
142                 return len == 2 ? "" : cs.subSequence(1, len - 1);
143             } else {
144                 return null;
145             }
146         }
147         return cs;
148     }
149 
150     private CookieUtil() {
151         // Unused
152     }
153 }