Skip to content

Commit

Permalink
fix: Dio does not expand Cache-Control values
Browse files Browse the repository at this point in the history
  • Loading branch information
llfbandit committed May 7, 2021
1 parent 2ae5510 commit a8521ee
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.3.1
- fix: Cache-Control parsing. Dio does not expand multi valued headers.

## 2.3.0
- feat: `allowPostMethod` added to `CacheOptions`.

Expand Down
26 changes: 16 additions & 10 deletions lib/src/model/cache_control.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,22 @@ class CacheControl {
final other = <String>[];

for (var value in headerValues) {
if (value == 'no-cache') {
noCache = true;
} else if (value == 'no-store') {
noStore = true;
} else if (value == 'public' || value == 'private') {
privacy = value;
} else if (value.startsWith('max-age')) {
maxAge = int.tryParse(value.substring(value.indexOf('=') + 1));
} else {
other.add(value);
// Expand values since dio does not do it !
for (var expandedValue in value.split(',')) {
expandedValue = expandedValue.trim();
if (expandedValue == 'no-cache') {
noCache = true;
} else if (expandedValue == 'no-store') {
noStore = true;
} else if (expandedValue == 'public' || expandedValue == 'private') {
privacy = expandedValue;
} else if (expandedValue.startsWith('max-age')) {
maxAge = int.tryParse(
expandedValue.substring(expandedValue.indexOf('=') + 1),
);
} else {
other.add(expandedValue);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Dio HTTP cache interceptor with multiple stores respecting HTTP dir
repository: https://github.com/llfbandit/dio_cache_interceptor
issue_tracker: https://github.com/llfbandit/dio_cache_interceptor/issues

version: 2.3.0
version: 2.3.1

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
2 changes: 1 addition & 1 deletion test/mock_httpclient_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class MockHttpClientAdapter extends HttpClientAdapter {
200,
headers: {
Headers.contentTypeHeader: [Headers.jsonContentType],
'cache-control': ['public', 'max-age=1'],
'cache-control': ['public, max-age=1'],
},
);
}
Expand Down

0 comments on commit a8521ee

Please sign in to comment.