Skip to content

Commit

Permalink
filter invalid cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
duwen committed Feb 25, 2019
1 parent 733cb80 commit 9b9696b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions package_src/lib/src/interceptors/cookie_mgr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class CookieManager extends Interceptor {
/// CookieJar please refer to [cookie_jar](https://github.com/flutterchina/cookie_jar)
final CookieJar cookieJar;

static const invalidCookieValue = "_invalid_";

/// Dart SDK will cause an exception When response cookie's value is empty,
/// eg. 'Set-Cookie: session=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT'
///
Expand All @@ -28,8 +30,11 @@ class CookieManager extends Interceptor {

@override
onRequest(RequestOptions options) {
var cookies = cookieJar.loadForRequest(options.uri)
..addAll(options.cookies);
var cookies = cookieJar.loadForRequest(options.uri);
cookies.removeWhere((cookie) =>
cookie.value == invalidCookieValue &&
cookie.expires.isBefore(DateTime.now()));
cookies.addAll(options.cookies);
String cookie = getCookies(cookies);
if (cookie.isNotEmpty) options.headers[HttpHeaders.cookieHeader] = cookie;
}
Expand Down Expand Up @@ -69,7 +74,7 @@ class CookieManager extends Interceptor {
var _cookie = cookie.split(";");
var kv = _cookie.first?.split("=");
if (kv != null && kv[1].isEmpty) {
kv[1] = "_invalid_";
kv[1] = invalidCookieValue;
_cookie[0] = kv.join('=');
if (_cookie.length > 1) {
int i = 1;
Expand Down
2 changes: 1 addition & 1 deletion package_src/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dio
description: A powerful Http client for Dart, which supports Interceptors, FormData, Request Cancellation, File Downloading, Timeout etc.
version: 2.0.12
version: 2.0.13
homepage: https://github.com/flutterchina/dio
author: wendux <824783146@qq.com>

Expand Down

0 comments on commit 9b9696b

Please sign in to comment.