loadForRequest method

  1. @override
Future<List<Cookie>> loadForRequest(
  1. Uri uri
)
override

Load the cookies for specified uri.

Implementation

@override
Future<List<Cookie>> loadForRequest(Uri uri) async {
  final list = <Cookie>[];
  final urlPath = uri.path;
  // Load cookies without "domain" attribute, include port.
  final hostname = uri.host;
  for (final domain in hostCookies.keys) {
    if (hostname == domain) {
      final cookies =
          hostCookies[domain]!.cast<String, Map<String, dynamic>>();
      // Sort by best match (longer path first)
      final keys = cookies.keys.toList()
        ..sort((a, b) => b.length.compareTo(a.length));
      for (final path in keys) {
        if (_isPathMatch(urlPath, path)) {
          final values = cookies[path]!;
          for (final key in values.keys) {
            final SerializableCookie cookie = values[key];
            if (_check(uri.scheme, cookie)) {
              // preserve cookies that with same name but in different paths when request (as Chrome);
              // eg(in request header): Cookie: a=1;  a=2;  a=3
              list.add(cookie.cookie);
            }
          }
        }
      }
    }
  }
  // Load cookies with "domain" attribute, Ignore port.
  domainCookies.forEach(
    (domain, cookies) {
      if (_isDomainMatch(uri.host, domain)) {
        cookies.forEach(
          (path, values) {
            if (_isPathMatch(urlPath, path)) {
              values.forEach((key, v) {
                if (_check(uri.scheme, v)) {
                  list.add(v.cookie);
                }
              });
            }
          },
        );
      }
    },
  );
  return list;
}