12
12
*/
13
13
package org .asynchttpclient .cookie ;
14
14
15
+ import static org .asynchttpclient .util .Assertions .*;
16
+
15
17
import java .util .BitSet ;
16
18
import java .util .Date ;
17
19
18
20
public class CookieUtil {
19
21
20
- private static final BitSet VALID_COOKIE_VALUE_OCTETS = validCookieValueOctets ();
21
-
22
22
private static final BitSet VALID_COOKIE_NAME_OCTETS = validCookieNameOctets ();
23
+ private static final BitSet VALID_COOKIE_VALUE_OCTETS = validCookieValueOctets ();
24
+ private static final BitSet VALID_COOKIE_ATTRIBUTE_VALUE_OCTETS = validCookieAttributeValueOctets ();
25
+
26
+ // token = 1*<any CHAR except CTLs or separators>
27
+ // separators = "(" | ")" | "<" | ">" | "@"
28
+ // | "," | ";" | ":" | "\" | <">
29
+ // | "/" | "[" | "]" | "?" | "="
30
+ // | "{" | "}" | SP | HT
31
+ private static BitSet validCookieNameOctets () {
32
+ BitSet bits = new BitSet ();
33
+ for (int i = 32 ; i < 127 ; i ++) {
34
+ bits .set (i );
35
+ }
36
+ int [] separators = new int [] { '(' , ')' , '<' , '>' , '@' , ',' , ';' , ':' , '\\' , '"' , '/' , '[' , ']' , '?' , '=' , '{' , '}' , ' ' , '\t' };
37
+ for (int separator : separators ) {
38
+ bits .set (separator , false );
39
+ }
40
+ return bits ;
41
+ }
23
42
24
43
// cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E
25
44
// US-ASCII characters excluding CTLs, whitespace, DQUOTE, comma, semicolon, and backslash
@@ -40,28 +59,51 @@ private static BitSet validCookieValueOctets() {
40
59
}
41
60
return bits ;
42
61
}
43
-
44
- // token = 1*<any CHAR except CTLs or separators>
45
- // separators = "(" | ")" | "<" | ">" | "@"
46
- // | "," | ";" | ":" | "\" | <">
47
- // | "/" | "[" | "]" | "?" | "="
48
- // | "{" | "}" | SP | HT
49
- private static BitSet validCookieNameOctets () {
62
+
63
+ private static BitSet validCookieAttributeValueOctets () {
50
64
BitSet bits = new BitSet ();
51
65
for (int i = 32 ; i < 127 ; i ++) {
52
66
bits .set (i );
53
67
}
54
- int [] separators = new int [] { '(' , ')' , '<' , '>' , '@' , ',' , ';' , ':' , '\\' , '"' , '/' , '[' , ']' , '?' , '=' , '{' , '}' , ' ' , '\t' };
55
- for (int separator : separators ) {
56
- bits .set (separator , false );
57
- }
68
+ bits .set (';' , false );
58
69
return bits ;
59
70
}
60
71
61
- static int firstInvalidCookieNameOctet (CharSequence cs ) {
62
- return firstInvalidOctet (cs , VALID_COOKIE_NAME_OCTETS );
72
+ static String validateCookieName (String name ) {
73
+ name = assertNotNull (name , "name" ).trim ();
74
+ assertNotEmpty (name , "name" );
75
+ int i = firstInvalidOctet (name , VALID_COOKIE_NAME_OCTETS );
76
+ if (i != -1 ) {
77
+ throw new IllegalArgumentException ("name contains prohibited character: " + name .charAt (i ));
78
+ }
79
+ return name ;
80
+ }
81
+
82
+ static String validateCookieValue (String value ) {
83
+ value = assertNotNull (value , "name" ).trim ();
84
+ CharSequence unwrappedValue = unwrapValue (value );
85
+ int i = firstInvalidOctet (unwrappedValue , VALID_COOKIE_VALUE_OCTETS );
86
+ if (i != -1 ) {
87
+ throw new IllegalArgumentException ("value contains prohibited character: " + unwrappedValue .charAt (i ));
88
+ }
89
+ return value ;
63
90
}
64
91
92
+ static String validateCookieAttribute (String name , String value ) {
93
+ if (value == null ) {
94
+ return null ;
95
+ }
96
+ value = value .trim ();
97
+ if (value .length () == 0 ) {
98
+ return null ;
99
+ }
100
+ int i = firstInvalidOctet (value , VALID_COOKIE_ATTRIBUTE_VALUE_OCTETS );
101
+ if (i != -1 ) {
102
+ throw new IllegalArgumentException (name + " contains prohibited character: " + value .charAt (i ));
103
+ }
104
+ return value ;
105
+ }
106
+
65
107
static int firstInvalidCookieValueOctet (CharSequence cs ) {
66
108
return firstInvalidOctet (cs , VALID_COOKIE_VALUE_OCTETS );
67
109
}
0 commit comments