in httpclient5/src/main/java/org/apache/hc/client5/http/impl/cookie/RFC6265CookieSpec.java [110:184]
public final List<Cookie> parse(final Header header, final CookieOrigin origin) throws MalformedCookieException {
Args.notNull(header, "Header");
Args.notNull(origin, "Cookie origin");
if (!header.getName().equalsIgnoreCase("Set-Cookie")) {
throw new MalformedCookieException("Unrecognized cookie header: '" + header + "'");
}
final CharArrayBuffer buffer;
final Tokenizer.Cursor cursor;
if (header instanceof FormattedHeader) {
buffer = ((FormattedHeader) header).getBuffer();
cursor = new Tokenizer.Cursor(((FormattedHeader) header).getValuePos(), buffer.length());
} else {
final String s = header.getValue();
if (s == null) {
throw new MalformedCookieException("Header value is null");
}
buffer = new CharArrayBuffer(s.length());
buffer.append(s);
cursor = new Tokenizer.Cursor(0, buffer.length());
}
final String name = tokenParser.parseToken(buffer, cursor, TOKEN_DELIMS);
if (name.isEmpty()) {
return Collections.emptyList();
}
if (cursor.atEnd()) {
return Collections.emptyList();
}
final int valueDelim = buffer.charAt(cursor.getPos());
cursor.updatePos(cursor.getPos() + 1);
if (valueDelim != '=') {
throw new MalformedCookieException("Cookie value is invalid: '" + header + "'");
}
final String value = tokenParser.parseValue(buffer, cursor, VALUE_DELIMS);
if (!cursor.atEnd()) {
cursor.updatePos(cursor.getPos() + 1);
}
final BasicClientCookie cookie = new BasicClientCookie(name, value);
cookie.setPath(getDefaultPath(origin));
cookie.setDomain(getDefaultDomain(origin));
cookie.setCreationDate(Instant.now());
final Map<String, String> attribMap = new LinkedHashMap<>();
while (!cursor.atEnd()) {
final String paramName = tokenParser.parseToken(buffer, cursor, TOKEN_DELIMS)
.toLowerCase(Locale.ROOT);
String paramValue = null;
if (!cursor.atEnd()) {
final int paramDelim = buffer.charAt(cursor.getPos());
cursor.updatePos(cursor.getPos() + 1);
if (paramDelim == EQUAL_CHAR) {
paramValue = tokenParser.parseToken(buffer, cursor, VALUE_DELIMS);
if (!cursor.atEnd()) {
cursor.updatePos(cursor.getPos() + 1);
}
}
}
cookie.setAttribute(paramName, paramValue);
attribMap.put(paramName, paramValue);
}
// Ignore 'Expires' if 'Max-Age' is present
if (attribMap.containsKey(Cookie.MAX_AGE_ATTR)) {
attribMap.remove(Cookie.EXPIRES_ATTR);
}
for (final Map.Entry<String, String> entry: attribMap.entrySet()) {
final String paramName = entry.getKey();
final String paramValue = entry.getValue();
final CookieAttributeHandler handler = this.attribHandlerMap.get(paramName);
if (handler != null) {
handler.parse(cookie, paramValue);
}
}
return Collections.singletonList(cookie);
}