package org.glassfish.grizzly.http;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.grizzly.Grizzly;
import org.glassfish.grizzly.http.util.BufferChunk;
import org.glassfish.grizzly.http.util.ByteChunk;
import org.glassfish.grizzly.http.util.CookieParserUtils;
import org.glassfish.grizzly.http.util.CookieUtils;
import org.glassfish.grizzly.http.util.DataChunk;
import org.glassfish.grizzly.http.util.Header;
import org.glassfish.grizzly.http.util.MimeHeaders;
public final class Cookies {
private static final Cookie[] EMPTY_COOKIE_ARRAY = new Cookie[0];
private static final Logger logger = Grizzly.logger(Cookies.class);
private static final int INITIAL_SIZE = 4;
private Cookie[] cookies = new Cookie[INITIAL_SIZE];
private Cookie[] processedCookies;
private boolean isProcessed;
private boolean isRequest;
private MimeHeaders ;
private int nextUnusedCookieIndex = 0;
private int storedCookieCount;
static final char SEPARATORS[] = {'\t', ' ', '\"', '\'', '(', ')', ',',
':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '{', '}'};
static final boolean separators[] = new boolean[128];
static {
for (int i = 0; i < 128; i++) {
separators[i] = false;
}
for (int i = 0; i < SEPARATORS.length; i++) {
separators[SEPARATORS[i]] = true;
}
}
public boolean initialized() {
return headers != null;
}
public Cookie[] get() {
if (!isProcessed) {
isProcessed = true;
if (isRequest) {
processClientCookies();
} else {
processServerCookies();
}
processedCookies = nextUnusedCookieIndex > 0 ?
copyTo(new Cookie[nextUnusedCookieIndex]) :
EMPTY_COOKIE_ARRAY;
}
return processedCookies;
}
public void (final MimeHeaders headers) {
setHeaders(headers, true);
}
public void (final MimeHeaders headers, final boolean isRequest) {
this.headers = headers;
this.isRequest = isRequest;
}
public Cookie getNextUnusedCookie() {
if (nextUnusedCookieIndex < storedCookieCount) {
return cookies[nextUnusedCookieIndex++];
} else {
Cookie cookie = new Cookie();
if (nextUnusedCookieIndex == cookies.length) {
Cookie[] temp = new Cookie[cookies.length + INITIAL_SIZE];
System.arraycopy(cookies, 0, temp, 0, cookies.length);
cookies = temp;
}
storedCookieCount++;
cookies[nextUnusedCookieIndex++] = cookie;
return cookie;
}
}
public void recycle() {
for (int i = 0; i < nextUnusedCookieIndex; i++) {
cookies[i].recycle();
}
processedCookies = null;
nextUnusedCookieIndex = 0;
headers = null;
isRequest = false;
isProcessed = false;
}
private Cookie[] copyTo(Cookie[] destination) {
if (nextUnusedCookieIndex > 0) {
System.arraycopy(cookies, 0, destination, 0, nextUnusedCookieIndex);
}
return destination;
}
private void processClientCookies() {
if (headers == null) {
return;
}
int pos = 0;
while (pos >= 0) {
pos = headers.indexOf(Header.Cookie, pos);
if (pos < 0) {
break;
}
DataChunk cookieValue = headers.getValue(pos);
if (cookieValue == null || cookieValue.isNull()) {
pos++;
continue;
}
if (cookieValue.getType() == DataChunk.Type.Bytes) {
if (logger.isLoggable(Level.FINE)) {
log("Parsing b[]: " + cookieValue.toString());
}
final ByteChunk byteChunk = cookieValue.getByteChunk();
CookieParserUtils.parseClientCookies(this, byteChunk.getBuffer(),
byteChunk.getStart(),
byteChunk.getLength());
} else if (cookieValue.getType() == DataChunk.Type.Buffer) {
if (logger.isLoggable(Level.FINE)) {
log("Parsing buffer: " + cookieValue.toString());
}
final BufferChunk bufferChunk = cookieValue.getBufferChunk();
CookieParserUtils.parseClientCookies(this, bufferChunk.getBuffer(),
bufferChunk.getStart(),
bufferChunk.getLength());
} else {
if (logger.isLoggable(Level.FINE)) {
log("Parsing string: " + cookieValue.toString());
}
final String value = cookieValue.toString();
CookieParserUtils.parseClientCookies(this, value,
CookieUtils.COOKIE_VERSION_ONE_STRICT_COMPLIANCE,
CookieUtils.RFC_6265_SUPPORT_ENABLED);
}
pos++;
}
}
private void processServerCookies() {
if (headers == null) {
return;
}
int pos = 0;
while (pos >= 0) {
pos = headers.indexOf(Header.SetCookie, pos);
if (pos < 0) {
break;
}
DataChunk cookieValue = headers.getValue(pos);
if (cookieValue == null || cookieValue.isNull()) {
pos++;
continue;
}
if (cookieValue.getType() == DataChunk.Type.Bytes) {
if (logger.isLoggable(Level.FINE)) {
log("Parsing b[]: " + cookieValue.toString());
}
final ByteChunk byteChunk = cookieValue.getByteChunk();
CookieParserUtils.parseServerCookies(this, byteChunk.getBuffer(),
byteChunk.getStart(),
byteChunk.getLength(),
CookieUtils.COOKIE_VERSION_ONE_STRICT_COMPLIANCE,
CookieUtils.RFC_6265_SUPPORT_ENABLED);
} else if (cookieValue.getType() == DataChunk.Type.Buffer) {
if (logger.isLoggable(Level.FINE)) {
log("Parsing b[]: " + cookieValue.toString());
}
final BufferChunk bufferChunk = cookieValue.getBufferChunk();
CookieParserUtils.parseServerCookies(this, bufferChunk.getBuffer(),
bufferChunk.getStart(),
bufferChunk.getLength(),
CookieUtils.COOKIE_VERSION_ONE_STRICT_COMPLIANCE,
CookieUtils.RFC_6265_SUPPORT_ENABLED);
} else {
if (logger.isLoggable(Level.FINE)) {
log("Parsing string: " + cookieValue.toString());
}
final String value = cookieValue.toString();
CookieParserUtils.parseServerCookies(this, value,
CookieUtils.COOKIE_VERSION_ONE_STRICT_COMPLIANCE,
CookieUtils.RFC_6265_SUPPORT_ENABLED);
}
pos++;
}
}
@Override
public String toString() {
return Arrays.toString(cookies);
}
private static void log(String s) {
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "Cookies: {0}", s);
}
}
public Cookie findByName(String cookieName) {
final Cookie[] cookiesArray = get();
for (Cookie cookie : cookiesArray) {
if (cookie.lazyNameEquals(cookieName)) {
return cookie;
}
}
return null;
}
}