Skip to content

Commit 36db630

Browse files
committed
Have empty cookie attributes be null instead of empty String, close AsyncHttpClient#877
1 parent 0555941 commit 36db630

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

src/main/java/com/ning/http/client/cookie/CookieDecoder.java

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ public static Cookie decode(String header) {
137137

138138
} else {
139139
// 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);
142141
}
143142
}
144143
return cookieBuilder.cookie();
@@ -188,67 +187,70 @@ public Cookie cookie() {
188187
* where the key starts in the header
189188
* @param keyEnd
190189
* 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
193194
*/
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);
196197
}
197198

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) {
199200

200201
int length = keyEnd - keyStart;
201202

202203
if (length == 4) {
203-
parse4(header, keyStart, value);
204+
parse4(header, keyStart, valueBegin, valueEnd);
204205
} else if (length == 6) {
205-
parse6(header, keyStart, value);
206+
parse6(header, keyStart, valueBegin, valueEnd);
206207
} else if (length == 7) {
207-
parse7(header, keyStart, value);
208+
parse7(header, keyStart, valueBegin, valueEnd);
208209
} else if (length == 8) {
209-
parse8(header, keyStart, value);
210+
parse8(header, keyStart, valueBegin, valueEnd);
210211
}
211212
}
212213

213-
private void parse4(String header, int nameStart, String value) {
214+
private void parse4(String header, int nameStart, int valueBegin, int valueEnd) {
214215
if (header.regionMatches(true, nameStart, PATH, 0, 4)) {
215-
path = value;
216+
path = computeValue(header, valueBegin, valueEnd);
216217
}
217218
}
218219

219-
private void parse6(String header, int nameStart, String value) {
220+
private void parse6(String header, int nameStart, int valueBegin, int valueEnd) {
220221
if (header.regionMatches(true, nameStart, DOMAIN, 0, 5)) {
221-
domain = value.length() > 0 ? value.toString() : null;
222+
domain = computeValue(header, valueBegin, valueEnd);
222223
} else if (header.regionMatches(true, nameStart, SECURE, 0, 5)) {
223224
secure = true;
224225
}
225226
}
226227

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) {
240229
if (header.regionMatches(true, nameStart, EXPIRES, 0, 7)) {
241-
setExpire(value);
230+
expires = computeValue(header, valueBegin, valueEnd);
242231
} 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+
}
244237
}
245238
}
246239

247-
private void parse8(String header, int nameStart, String value) {
240+
private void parse8(String header, int nameStart, int valueBegin, int valueEnd) {
248241

249242
if (header.regionMatches(true, nameStart, HTTPONLY, 0, 8)) {
250243
httpOnly = true;
251244
}
252245
}
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+
}
253255
}
254256
}

0 commit comments

Comments
 (0)