1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
29 private static BitSet validCookieValueOctets() {
30 BitSet bits = new BitSet(8);
31 for (int i = 35; i < 127; i++) {
32
33 bits.set(i);
34 }
35 bits.set('"', false);
36 bits.set(',', false);
37 bits.set(';', false);
38 bits.set('\\', false);
39 return bits;
40 }
41
42
43
44
45
46
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
70
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
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
152 }
153 }