Class Cookies

java.lang.Object
com.jsgalactic.axiom.security.Cookies

public final class Cookies extends Object
The cookies of a request, parsed strictly from the Cookie header (RFC 6265bis section 4.2).

Accepted: name=value pairs separated by exactly "; ". A name is a non-empty HTTP token; a value consists of cookie octets only (visible ASCII without space, double quote, comma, semicolon or backslash) and may be empty. Anything else (quoted values, stray whitespace, a pair without =, a nameless cookie, control or non-ASCII characters, more than 128 pairs, a header longer than the limit) makes the whole header invalid: parse(java.lang.String) throws, and of(Request) answers "no cookies", so security decisions fail closed instead of reading a half-understood header.

A name that appears twice is ambiguous (a sibling sub-domain can plant a second cookie of the same name), so get(java.lang.String) treats it as absent; all(java.lang.String) shows every value.

Immutable and thread-safe. Values are credentials when they are session identifiers or tokens: toString() lists only the names.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Default longest Cookie header accepted, in characters.
    static final int
    Most cookie pairs accepted in one header.
  • Method Summary

    Modifier and Type
    Method
    Description
    all(String name)
    Returns every value sent for the name, in header order.
    get(String name)
    Returns the value of the cookie, if it was sent exactly once.
    Returns the distinct cookie names.
    static Cookies
    Returns no cookies.
    static Cookies
    of(Request request)
    Reads the request's cookies; a missing, oversized or malformed header yields none.
    static Cookies
    of(Request request, int maxLength)
    Reads the request's cookies with a header size limit; a missing, oversized or malformed header yields none.
    static Cookies
    parse(String header)
    Parses a Cookie header value strictly with the default size limit.
    static Cookies
    parse(String header, int maxLength)
    Parses a Cookie header value strictly.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • DEFAULT_MAX_LENGTH

      public static final int DEFAULT_MAX_LENGTH
      Default longest Cookie header accepted, in characters.
      See Also:
    • MAX_PAIRS

      public static final int MAX_PAIRS
      Most cookie pairs accepted in one header.
      See Also:
  • Method Details

    • none

      public static Cookies none()
      Returns no cookies.
      Returns:
      an empty set
    • of

      public static Cookies of(Request request)
      Reads the request's cookies; a missing, oversized or malformed header yields none.
      Parameters:
      request - the request
      Returns:
      the cookies, empty when the header is absent or invalid
    • of

      public static Cookies of(Request request, int maxLength)
      Reads the request's cookies with a header size limit; a missing, oversized or malformed header yields none.
      Parameters:
      request - the request
      maxLength - longest accepted header, 64 to 65,536 characters
      Returns:
      the cookies, empty when the header is absent or invalid
    • parse

      public static Cookies parse(String header)
      Parses a Cookie header value strictly with the default size limit.
      Parameters:
      header - the header value
      Returns:
      the cookies
      Throws:
      IllegalArgumentException - if the header is malformed or too long
    • parse

      public static Cookies parse(String header, int maxLength)
      Parses a Cookie header value strictly.
      Parameters:
      header - the header value
      maxLength - longest accepted header, 64 to 65,536 characters
      Returns:
      the cookies
      Throws:
      IllegalArgumentException - if the header is malformed or longer than maxLength
    • get

      public Optional<String> get(String name)
      Returns the value of the cookie, if it was sent exactly once.
      Parameters:
      name - cookie name, case-sensitive
      Returns:
      the value, or empty when absent or sent more than once
    • all

      public List<String> all(String name)
      Returns every value sent for the name, in header order.
      Parameters:
      name - cookie name, case-sensitive
      Returns:
      the values, possibly none
    • names

      public Set<String> names()
      Returns the distinct cookie names.
      Returns:
      the names in header order
    • toString

      public String toString()
      Overrides:
      toString in class Object