20
20
import java .util .Set ;
21
21
import java .util .TreeSet ;
22
22
23
- public class Cookie {
23
+ public class Cookie implements Comparable < Cookie > {
24
24
private final String domain ;
25
25
private final String name ;
26
26
private final String value ;
27
27
private final String path ;
28
28
private final int maxAge ;
29
29
private final boolean secure ;
30
30
private final int version ;
31
+ private final boolean httpOnly ;
32
+ private final boolean discard ;
33
+ private final String comment ;
34
+ private final String commentUrl ;
35
+
31
36
private Set <Integer > ports = Collections .emptySet ();
32
37
private Set <Integer > unmodifiablePorts = ports ;
33
38
34
- public Cookie (String domain , String name , String value , String path , int maxAge , boolean secure ) {
35
- this .domain = domain ;
36
- this .name = name ;
37
- this .value = value ;
38
- this .path = path ;
39
- this .maxAge = maxAge ;
40
- this .secure = secure ;
41
- this .version = 1 ;
42
- }
39
+ public Cookie (String domain , String name , String value , String path , int maxAge , boolean secure , int version , boolean httpOnly , boolean discard , String comment , String commentUrl , Iterable <Integer > ports ) {
40
+
41
+ if (name == null ) {
42
+ throw new NullPointerException ("name" );
43
+ }
44
+ name = name .trim ();
45
+ if (name .length () == 0 ) {
46
+ throw new IllegalArgumentException ("empty name" );
47
+ }
48
+
49
+ for (int i = 0 ; i < name .length (); i ++) {
50
+ char c = name .charAt (i );
51
+ if (c > 127 ) {
52
+ throw new IllegalArgumentException ("name contains non-ascii character: " + name );
53
+ }
54
+
55
+ // Check prohibited characters.
56
+ switch (c ) {
57
+ case '\t' :
58
+ case '\n' :
59
+ case 0x0b :
60
+ case '\f' :
61
+ case '\r' :
62
+ case ' ' :
63
+ case ',' :
64
+ case ';' :
65
+ case '=' :
66
+ throw new IllegalArgumentException ("name contains one of the following prohibited characters: " + "=,; \\ t\\ r\\ n\\ v\\ f: " + name );
67
+ }
68
+ }
69
+
70
+ if (name .charAt (0 ) == '$' ) {
71
+ throw new IllegalArgumentException ("name starting with '$' not allowed: " + name );
72
+ }
73
+
74
+ if (value == null ) {
75
+ throw new NullPointerException ("value" );
76
+ }
43
77
44
- public Cookie (String domain , String name , String value , String path , int maxAge , boolean secure , int version ) {
45
- this .domain = domain ;
46
78
this .name = name ;
47
79
this .value = value ;
48
- this .path = path ;
80
+ this .domain = validateValue ("domain" , domain );
81
+ this .path = validateValue ("path" , path );
49
82
this .maxAge = maxAge ;
50
83
this .secure = secure ;
51
84
this .version = version ;
85
+ this .httpOnly = httpOnly ;
86
+
87
+ if (version > 0 ) {
88
+ this .comment = validateValue ("comment" , comment );
89
+ } else {
90
+ this .comment = null ;
91
+ }
92
+ if (version > 1 ) {
93
+ this .discard = discard ;
94
+ this .commentUrl = validateValue ("commentUrl" , commentUrl );
95
+ setPorts (ports );
96
+ } else {
97
+ this .discard = false ;
98
+ this .commentUrl = null ;
99
+ }
52
100
}
53
101
54
102
public String getDomain () {
@@ -79,35 +127,30 @@ public int getVersion() {
79
127
return version ;
80
128
}
81
129
130
+ public String getComment () {
131
+ return this .comment ;
132
+ }
133
+
134
+ public String getCommentUrl () {
135
+ return this .commentUrl ;
136
+ }
137
+
138
+ public boolean isHttpOnly () {
139
+ return httpOnly ;
140
+ }
141
+
142
+ public boolean isDiscard () {
143
+ return discard ;
144
+ }
145
+
82
146
public Set <Integer > getPorts () {
83
147
if (unmodifiablePorts == null ) {
84
148
unmodifiablePorts = Collections .unmodifiableSet (ports );
85
149
}
86
150
return unmodifiablePorts ;
87
151
}
88
152
89
- public void setPorts (int ... ports ) {
90
- if (ports == null ) {
91
- throw new NullPointerException ("ports" );
92
- }
93
-
94
- int [] portsCopy = ports .clone ();
95
- if (portsCopy .length == 0 ) {
96
- unmodifiablePorts = this .ports = Collections .emptySet ();
97
- } else {
98
- Set <Integer > newPorts = new TreeSet <Integer >();
99
- for (int p : portsCopy ) {
100
- if (p <= 0 || p > 65535 ) {
101
- throw new IllegalArgumentException ("port out of range: " + p );
102
- }
103
- newPorts .add (Integer .valueOf (p ));
104
- }
105
- this .ports = newPorts ;
106
- unmodifiablePorts = null ;
107
- }
108
- }
109
-
110
- public void setPorts (Iterable <Integer > ports ) {
153
+ private void setPorts (Iterable <Integer > ports ) {
111
154
Set <Integer > newPorts = new TreeSet <Integer >();
112
155
for (int p : ports ) {
113
156
if (p <= 0 || p > 65535 ) {
@@ -125,7 +168,89 @@ public void setPorts(Iterable<Integer> ports) {
125
168
126
169
@ Override
127
170
public String toString () {
128
- return String .format ("Cookie: domain=%s, name=%s, value=%s, path=%s, maxAge=%d, secure=%s" ,
129
- domain , name , value , path , maxAge , secure );
171
+ StringBuilder buf = new StringBuilder ();
172
+ buf .append (getName ());
173
+ buf .append ('=' );
174
+ buf .append (getValue ());
175
+ if (getDomain () != null ) {
176
+ buf .append ("; domain=" );
177
+ buf .append (getDomain ());
178
+ }
179
+ if (getPath () != null ) {
180
+ buf .append ("; path=" );
181
+ buf .append (getPath ());
182
+ }
183
+ if (getComment () != null ) {
184
+ buf .append ("; comment=" );
185
+ buf .append (getComment ());
186
+ }
187
+ if (getMaxAge () >= 0 ) {
188
+ buf .append ("; maxAge=" );
189
+ buf .append (getMaxAge ());
190
+ buf .append ('s' );
191
+ }
192
+ if (isSecure ()) {
193
+ buf .append ("; secure" );
194
+ }
195
+ if (isHttpOnly ()) {
196
+ buf .append ("; HTTPOnly" );
197
+ }
198
+ return buf .toString ();
199
+ }
200
+
201
+ private String validateValue (String name , String value ) {
202
+ if (value == null ) {
203
+ return null ;
204
+ }
205
+ value = value .trim ();
206
+ if (value .length () == 0 ) {
207
+ return null ;
208
+ }
209
+ for (int i = 0 ; i < value .length (); i ++) {
210
+ char c = value .charAt (i );
211
+ switch (c ) {
212
+ case '\r' :
213
+ case '\n' :
214
+ case '\f' :
215
+ case 0x0b :
216
+ case ';' :
217
+ throw new IllegalArgumentException (name + " contains one of the following prohibited characters: " + ";\\ r\\ n\\ f\\ v (" + value + ')' );
218
+ }
219
+ }
220
+ return value ;
221
+ }
222
+
223
+ public int compareTo (Cookie c ) {
224
+ int v ;
225
+ v = getName ().compareToIgnoreCase (c .getName ());
226
+ if (v != 0 ) {
227
+ return v ;
228
+ }
229
+
230
+ if (getPath () == null ) {
231
+ if (c .getPath () != null ) {
232
+ return -1 ;
233
+ }
234
+ } else if (c .getPath () == null ) {
235
+ return 1 ;
236
+ } else {
237
+ v = getPath ().compareTo (c .getPath ());
238
+ if (v != 0 ) {
239
+ return v ;
240
+ }
241
+ }
242
+
243
+ if (getDomain () == null ) {
244
+ if (c .getDomain () != null ) {
245
+ return -1 ;
246
+ }
247
+ } else if (c .getDomain () == null ) {
248
+ return 1 ;
249
+ } else {
250
+ v = getDomain ().compareToIgnoreCase (c .getDomain ());
251
+ return v ;
252
+ }
253
+
254
+ return 0 ;
130
255
}
131
- }
256
+ }
0 commit comments