CookieHeader.parse constructor

CookieHeader.parse(
  1. String value
)

Parses the Cookie header value and returns a CookieHeader instance.

This method processes the header value, extracting the cookies into a list.

Implementation

factory CookieHeader.parse(final String value) {
  if (value.isEmpty) {
    throw const FormatException('Value cannot be empty');
  }

  final splitValues = value.splitTrimAndFilterUnique(separator: ';');

  final cookies = splitValues.map(Cookie.parse).toList();
  final names = cookies
      .map((final cookie) => cookie.name.toLowerCase())
      .toList();
  final uniqueNames = names.toSet();

  if (names.length != uniqueNames.length) {
    throw const FormatException(
      'Supplied multiple Name and Value attributes',
    );
  }

  return CookieHeader.cookies(cookies);
}