@@ -137,8 +137,7 @@ public static Cookie decode(String header) {
137
137
138
138
} else {
139
139
// cookie attribute
140
- String attrValue = valueBegin == -1 ? null : header .substring (valueBegin , valueEnd );
141
- cookieBuilder .appendAttribute (header , nameBegin , nameEnd , attrValue );
140
+ cookieBuilder .appendAttribute (header , nameBegin , nameEnd , valueBegin , valueEnd );
142
141
}
143
142
}
144
143
return cookieBuilder .cookie ();
@@ -188,67 +187,70 @@ public Cookie cookie() {
188
187
* where the key starts in the header
189
188
* @param keyEnd
190
189
* where the key ends in the header
191
- * @param value
192
- * the decoded value
190
+ * @param valueBegin
191
+ * where the value starts in the header
192
+ * @param valueEnd
193
+ * where the value ends in the header
193
194
*/
194
- public void appendAttribute (String header , int keyStart , int keyEnd , String value ) {
195
- setCookieAttribute (header , keyStart , keyEnd , value );
195
+ public void appendAttribute (String header , int keyStart , int keyEnd , int valueBegin , int valueEnd ) {
196
+ setCookieAttribute (header , keyStart , keyEnd , valueBegin , valueEnd );
196
197
}
197
198
198
- private void setCookieAttribute (String header , int keyStart , int keyEnd , String value ) {
199
+ private void setCookieAttribute (String header , int keyStart , int keyEnd , int valueBegin , int valueEnd ) {
199
200
200
201
int length = keyEnd - keyStart ;
201
202
202
203
if (length == 4 ) {
203
- parse4 (header , keyStart , value );
204
+ parse4 (header , keyStart , valueBegin , valueEnd );
204
205
} else if (length == 6 ) {
205
- parse6 (header , keyStart , value );
206
+ parse6 (header , keyStart , valueBegin , valueEnd );
206
207
} else if (length == 7 ) {
207
- parse7 (header , keyStart , value );
208
+ parse7 (header , keyStart , valueBegin , valueEnd );
208
209
} else if (length == 8 ) {
209
- parse8 (header , keyStart , value );
210
+ parse8 (header , keyStart , valueBegin , valueEnd );
210
211
}
211
212
}
212
213
213
- private void parse4 (String header , int nameStart , String value ) {
214
+ private void parse4 (String header , int nameStart , int valueBegin , int valueEnd ) {
214
215
if (header .regionMatches (true , nameStart , PATH , 0 , 4 )) {
215
- path = value ;
216
+ path = computeValue ( header , valueBegin , valueEnd ) ;
216
217
}
217
218
}
218
219
219
- private void parse6 (String header , int nameStart , String value ) {
220
+ private void parse6 (String header , int nameStart , int valueBegin , int valueEnd ) {
220
221
if (header .regionMatches (true , nameStart , DOMAIN , 0 , 5 )) {
221
- domain = value . length () > 0 ? value . toString () : null ;
222
+ domain = computeValue ( header , valueBegin , valueEnd ) ;
222
223
} else if (header .regionMatches (true , nameStart , SECURE , 0 , 5 )) {
223
224
secure = true ;
224
225
}
225
226
}
226
227
227
- private void setExpire (String value ) {
228
- expires = value ;
229
- }
230
-
231
- private void setMaxAge (String value ) {
232
- try {
233
- maxAge = Math .max (Integer .valueOf (value ), 0 );
234
- } catch (NumberFormatException e1 ) {
235
- // ignore failure to parse -> treat as session cookie
236
- }
237
- }
238
-
239
- private void parse7 (String header , int nameStart , String value ) {
228
+ private void parse7 (String header , int nameStart , int valueBegin , int valueEnd ) {
240
229
if (header .regionMatches (true , nameStart , EXPIRES , 0 , 7 )) {
241
- setExpire ( value );
230
+ expires = computeValue ( header , valueBegin , valueEnd );
242
231
} else if (header .regionMatches (true , nameStart , MAX_AGE , 0 , 7 )) {
243
- setMaxAge (value );
232
+ try {
233
+ maxAge = Math .max (Integer .valueOf (computeValue (header , valueBegin , valueEnd )), 0 );
234
+ } catch (NumberFormatException e1 ) {
235
+ // ignore failure to parse -> treat as session cookie
236
+ }
244
237
}
245
238
}
246
239
247
- private void parse8 (String header , int nameStart , String value ) {
240
+ private void parse8 (String header , int nameStart , int valueBegin , int valueEnd ) {
248
241
249
242
if (header .regionMatches (true , nameStart , HTTPONLY , 0 , 8 )) {
250
243
httpOnly = true ;
251
244
}
252
245
}
246
+
247
+ private String computeValue (String header , int valueBegin , int valueEnd ) {
248
+ if (valueBegin == -1 || valueBegin == valueEnd ) {
249
+ return null ;
250
+ } else {
251
+ String trimmed = header .substring (valueBegin , valueEnd ).trim ();
252
+ return trimmed .isEmpty () ? null : trimmed ;
253
+ }
254
+ }
253
255
}
254
256
}
0 commit comments