storeFromRes method

Future<void> storeFromRes(
  1. Response res
)

Implementation

Future<void> storeFromRes(Response<dynamic> res) async {
  final setCookies = res.headers["Set-Cookie"];
  if (setCookies != null) {
    _log("Set-Cookie headers $setCookies");
    final now = DateTime.now();
    for (final setCookie in setCookies) {
      final i = setCookie.indexOf('=');
      final j = setCookie.indexOf(';', i);

      final key = setCookie.substring(0, i);
      late final String value;
      late final DateTime expires;

      if (j == -1) {
        value = setCookie.substring(i + 1);
        expires = now.add(const Duration(days: 400));
      } else {
        value = setCookie.substring(i + 1, j);
        expires =
            findExpiresAttr(setCookie, j) ?? findMaxAgeAttr(setCookie, j, now) ?? now.add(const Duration(days: 400));
      }

      final cookie = Cookie(name: key, value: value, expires: expires);
      _cookies.remove(cookie);
      if (now.isBefore(expires)) {
        _cookies.add(cookie);
      }
    }
    await storeAll();
  }
}