cookie_jar
cookie_jar copied to clipboard
DefaultCookieJar 处理 Set-Cookie 时行为不标准
根据 RFC6265 4.1.2.4,当服务端 Set-Cookie 字段缺失 Path 时,应该使用当前页面的“目录”作为默认的 cookie 路径。也就是说,如果 URI 为 /w/login
,那么 Cookie 的路径应该为 /w/
。
According to RFC6265 4.1.2.4, user agent uses the "directory" of request-uri as default path if there is no 'Path' component in Set-Cookie
line. In that way, if the URI is /w/login
,the path of cookie will be /w/
by default.
但是在 cookie_jar 3.0.1 版本中,default_cookie_jar.dart 文件的 saveFromResponse
函数:
But in version 3.0.1 of cookie_jar
, in method saveFromResponse
, default_cookie_jar.dart.
@override
Future<void> saveFromResponse(Uri uri, List<Cookie> cookies) async {
for (final cookie in cookies) {
var domain = cookie.domain;
String path;
var index = 0;
// Save cookies with "domain" attribute
if (domain != null) {
if (domain.startsWith('.')) {
domain = domain.substring(1);
}
path = cookie.path ?? '/';
} else {
index = 1;
// Save cookies without "domain" attribute
// 注意,问题出在这里. NOTICE HERE.
path = cookie.path ?? (uri.path.isEmpty ? '/' : uri.path);
domain = uri.host;
}
var mapDomain =
_cookies[index][domain] ?? <String, Map<String, dynamic>>{};
mapDomain = mapDomain.cast<String, Map<String, dynamic>>();
final map = mapDomain[path] ?? <String, dynamic>{};
map[cookie.name] = SerializableCookie(cookie);
if (_isExpired(map[cookie.name])) {
map.remove(cookie.name);
}
mapDomain[path] = map.cast<String, SerializableCookie>();
_cookies[index][domain] =
mapDomain.cast<String, Map<String, SerializableCookie>>();
}
}
默认使用了 uri.path
,和预期及浏览器行为不一致。这导致了我的爬虫出了点问题……
The method uses uri.path
which does in a different way with browsers. So my crawler crashed...