package io.undertow.util;
import io.undertow.UndertowMessages;
import java.util.BitSet;
public final class Rfc6265CookieSupport {
private static final BitSet domainValid = new BitSet(128);
static {
for (char c = '0'; c <= '9'; c++) {
domainValid.set(c);
}
for (char c = 'a'; c <= 'z'; c++) {
domainValid.set(c);
}
for (char c = 'A'; c <= 'Z'; c++) {
domainValid.set(c);
}
domainValid.set('.');
domainValid.set('-');
}
public static void validateCookieValue(String value) {
int start = 0;
int end = value.length();
if (end > 1 && value.charAt(0) == '"' && value.charAt(end - 1) == '"') {
start = 1;
end--;
}
char[] chars = value.toCharArray();
for (int i = start; i < end; i++) {
char c = chars[i];
if (c < 0x21 || c == 0x22 || c == 0x2c || c == 0x3b || c == 0x5c || c == 0x7f) {
throw UndertowMessages.MESSAGES.invalidCookieValue(Integer.toString(c));
}
}
}
public static void validateDomain(String domain) {
int i = 0;
int prev = -1;
int cur = -1;
char[] chars = domain.toCharArray();
while (i < chars.length) {
prev = cur;
cur = chars[i];
if (!domainValid.get(cur)) {
throw UndertowMessages.MESSAGES.invalidCookieDomain(domain);
}
if ((prev == '.' || prev == -1) && (cur == '.' || cur == '-')) {
throw UndertowMessages.MESSAGES.invalidCookieDomain(domain);
}
if (prev == '-' && cur == '.') {
throw UndertowMessages.MESSAGES.invalidCookieDomain(domain);
}
i++;
}
if (cur == '.' || cur == '-') {
throw UndertowMessages.MESSAGES.invalidCookieDomain(domain);
}
}
public static void validatePath(String path) {
char[] chars = path.toCharArray();
for (int i = 0; i < chars.length; i++) {
char ch = chars[i];
if (ch < 0x20 || ch > 0x7E || ch == ';') {
throw UndertowMessages.MESSAGES.invalidCookiePath(path);
}
}
}
}