readSetCookie function
Parse one named cookie out of the response's Set-Cookie headers.
Takes the values as a LIST because a response may set several cookies, each
in its own header. Joining them into one string and splitting again is not
safe: a Set-Cookie value legitimately contains commas (in Expires), so
the join is ambiguous and the session cookie can be lost behind another one.
Deliberately narrow otherwise: this client talks to a single origin and cares about a single cookie, so modelling domains and paths would be a lot of surface for no benefit. Returns null when none of the headers set the cookie, and the empty string when the server clears it.
Implementation
String? readSetCookie(List<String> setCookieValues, String name) {
for (final value in setCookieValues) {
// A single value may still arrive newline-joined from some clients.
for (final entry in value.split('\n')) {
final trimmed = entry.trim();
final separator = trimmed.indexOf('=');
if (separator == -1) continue;
if (trimmed.substring(0, separator).trim() != name) continue;
return trimmed.substring(separator + 1).split(';').first;
}
}
return null;
}