Core HTTP protocol support library for Eclipse Jetty providing header handling, parsing, generation, compliance validation, and content processing capabilities
—
Complete cookie parsing and management with RFC compliance support, attribute handling, and efficient parsing for both Cookie and Set-Cookie headers.
Core cookie representation with attributes and builder pattern.
/**
* HTTP cookie representation with attributes
*/
interface HttpCookie {
/** Get cookie name */
String getName();
/** Get cookie value */
String getValue();
/** Get cookie version (0 for Netscape, 1 for RFC 2965) */
int getVersion();
/** Get cookie domain attribute */
String getDomain();
/** Get cookie path attribute */
String getPath();
/** Get cookie comment (RFC 2965) */
String getComment();
/** Get max age in seconds (-1 for session cookie) */
long getMaxAge();
/** Check if cookie has Secure attribute */
boolean isSecure();
/** Check if cookie has HttpOnly attribute */
boolean isHttpOnly();
/** Get SameSite attribute */
SameSite getSameSite();
/** Get all cookie attributes as map */
Map<String, String> getAttributes();
/** Create simple cookie with name and value */
static HttpCookie from(String name, String value);
/** Create cookie builder */
static HttpCookie build(String name, String value);
/**
* SameSite attribute values
*/
enum SameSite {
/** No SameSite restriction */
NONE("None"),
/** Lax SameSite policy */
LAX("Lax"),
/** Strict SameSite policy */
STRICT("Strict");
/** Get attribute value string */
String getAttributeValue();
}
}Cookie header parsing interface for Cookie headers.
/**
* Interface for parsing Cookie headers
*/
interface CookieParser {
/** Parse single cookie field */
void parseField(HttpField field);
/** Parse multiple cookie fields */
void parseFields(HttpFields fields);
}Standard cookie parser implementation with compliance support.
/**
* Standard cookie parser implementation
*/
class CookieCutter implements CookieParser {
/** Create parser with default compliance */
CookieCutter();
/** Create parser with specific compliance and violation listener */
CookieCutter(CookieCompliance compliance,
ComplianceViolation.Listener complianceListener);
/** Parse cookie field and extract cookies */
void parseField(HttpField field);
/** Parse multiple cookie fields */
void parseFields(HttpFields fields);
/** Get parsed cookies */
HttpCookie[] getCookies();
/** Reset parser state */
void reset();
/** Set cookie compliance mode */
void setCookieCompliance(CookieCompliance compliance);
/** Get current compliance mode */
CookieCompliance getCookieCompliance();
}Specialized parsers for Set-Cookie headers with different RFC compliance.
/**
* RFC 6265 Set-Cookie parser
*/
class RFC6265CookieParser implements CookieParser {
/** Create RFC 6265 compliant parser */
RFC6265CookieParser();
/** Create parser with compliance and violation listener */
RFC6265CookieParser(CookieCompliance compliance,
ComplianceViolation.Listener listener);
/** Parse Set-Cookie field */
void parseField(HttpField field);
/** Get parsed cookie */
HttpCookie getCookie();
}
/**
* Legacy Set-Cookie parser with more lenient parsing
*/
class SetCookieParser implements CookieParser {
/** Create lenient Set-Cookie parser */
SetCookieParser();
/** Create parser with compliance settings */
SetCookieParser(CookieCompliance compliance,
ComplianceViolation.Listener listener);
/** Parse Set-Cookie field */
void parseField(HttpField field);
/** Get parsed cookie */
HttpCookie getCookie();
}Cookie compliance modes and violation handling.
/**
* Cookie compliance modes and validation
*/
class CookieCompliance {
/** Strict RFC 6265 compliance */
static final CookieCompliance RFC6265;
/** RFC 2965 compatibility mode */
static final CookieCompliance RFC2965;
/** Legacy Netscape cookie compatibility */
static final CookieCompliance LEGACY;
/** Check if violation is allowed in this compliance mode */
boolean allows(Violation violation);
/** Get set of allowed violations */
Set<Violation> getAllowed();
/** Get compliance mode name */
String getName();
/**
* Cookie compliance violations
*/
enum Violation {
/** Invalid cookie name characters */
INVALID_COOKIE_NAME,
/** Invalid cookie value characters */
INVALID_COOKIE_VALUE,
/** Reserved cookie name (like $Path, $Domain) */
RESERVED_NAMES_NOT_DOLLAR_PREFIXED,
/** Attribute without value */
ATTRIBUTE_WITHOUT_VALUE,
/** Invalid attribute name */
INVALID_ATTRIBUTE,
/** Comma separator in cookie value */
COMMA_SEPARATOR;
}
}Cookie storage and management interface.
/**
* Cookie storage and management interface
*/
interface HttpCookieStore {
/** Add cookie to store */
void add(HttpURI uri, HttpCookie cookie);
/** Get cookies for URI */
List<HttpCookie> get(HttpURI uri);
/** Remove cookie from store */
boolean remove(HttpURI uri, HttpCookie cookie);
/** Remove all cookies */
boolean removeAll();
/** Get all cookies in store */
List<HttpCookie> getCookies();
/** Get URIs with cookies */
List<HttpURI> getURIs();
}Efficient cookie caching for frequently used cookies.
/**
* Cache for frequently used cookies
*/
class CookieCache {
/** Create cache with default size */
CookieCache();
/** Create cache with specific size */
CookieCache(int size);
/** Get cached cookie by name and value */
HttpCookie get(String name, String value);
/** Put cookie in cache */
void put(HttpCookie cookie);
/** Clear cache */
void clear();
/** Get cache size */
int size();
}Usage Examples:
import org.eclipse.jetty.http.*;
// Parse Cookie header
MutableHttpFields headers = new MutableHttpFields();
headers.add(HttpHeader.COOKIE, "session=abc123; user=john; theme=dark");
CookieCutter parser = new CookieCutter();
parser.parseFields(headers);
HttpCookie[] cookies = parser.getCookies();
for (HttpCookie cookie : cookies) {
String name = cookie.getName(); // "session", "user", "theme"
String value = cookie.getValue(); // "abc123", "john", "dark"
}
// Parse Set-Cookie header with RFC 6265 compliance
HttpField setCookieField = new HttpField(HttpHeader.SET_COOKIE,
"sessionId=xyz789; Path=/; Domain=.example.com; Secure; HttpOnly; SameSite=Strict; Max-Age=3600");
RFC6265CookieParser setCookieParser = new RFC6265CookieParser();
setCookieParser.parseField(setCookieField);
HttpCookie cookie = setCookieParser.getCookie();
String name = cookie.getName(); // "sessionId"
String value = cookie.getValue(); // "xyz789"
String domain = cookie.getDomain(); // ".example.com"
String path = cookie.getPath(); // "/"
boolean secure = cookie.isSecure(); // true
boolean httpOnly = cookie.isHttpOnly(); // true
HttpCookie.SameSite sameSite = cookie.getSameSite(); // STRICT
long maxAge = cookie.getMaxAge(); // 3600
// Create cookies programmatically
HttpCookie simpleCookie = HttpCookie.from("username", "alice");
HttpCookie complexCookie = HttpCookie.build("auth", "token123")
.domain(".example.com")
.path("/api")
.secure(true)
.httpOnly(true)
.sameSite(HttpCookie.SameSite.LAX)
.maxAge(7200)
.build();
// Cookie compliance handling
CookieCompliance strict = CookieCompliance.RFC6265;
CookieCompliance lenient = CookieCompliance.LEGACY;
ComplianceViolation.Listener violationListener =
(mode, violation, details) -> {
System.out.println("Cookie violation: " + violation + " - " + details);
};
CookieCutter compliantParser = new CookieCutter(strict, violationListener);
// Cookie storage
HttpCookieStore cookieStore = new HttpCookieStore() {
private final Map<String, List<HttpCookie>> cookies = new HashMap<>();
@Override
public void add(HttpURI uri, HttpCookie cookie) {
cookies.computeIfAbsent(uri.getHost(), k -> new ArrayList<>()).add(cookie);
}
@Override
public List<HttpCookie> get(HttpURI uri) {
return cookies.getOrDefault(uri.getHost(), Collections.emptyList());
}
// ... implement other methods
};
// Add cookies to store
HttpURI uri = HttpURI.from("https://example.com/api");
cookieStore.add(uri, HttpCookie.from("session", "abc123"));
// Retrieve cookies for URI
List<HttpCookie> uriCookies = cookieStore.get(uri);
// Cookie caching for performance
CookieCache cache = new CookieCache(100);
cache.put(HttpCookie.from("frequent", "value"));
HttpCookie cached = cache.get("frequent", "value");
// Working with cookie attributes
Map<String, String> attributes = cookie.getAttributes();
String customAttr = attributes.get("CustomAttribute");
// Handle different SameSite values
switch (cookie.getSameSite()) {
case STRICT:
// Handle strict same-site policy
break;
case LAX:
// Handle lax same-site policy
break;
case NONE:
// Handle no same-site restriction (requires Secure)
break;
}
// Check for session vs persistent cookies
if (cookie.getMaxAge() == -1) {
// Session cookie (expires when browser closes)
} else {
// Persistent cookie with expiration
}Install with Tessl CLI
npx tessl i tessl/maven-org-eclipse-jetty--jetty-http