From a915aa35778617b91b8133aa308d568e1c745105 Mon Sep 17 00:00:00 2001 From: Markus Klostermann Date: Wed, 8 Oct 2025 08:38:34 +0200 Subject: [PATCH] fix: Allow optional whitespace in header values Optional whitespace in the CacheControl header can also occur before the separating comma. Without this fix, request would fail with a FormatException. --- .../lib/src/model/cache/cache_control.dart | 1 + http_cache_core/test/cache_control_test.dart | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/http_cache_core/lib/src/model/cache/cache_control.dart b/http_cache_core/lib/src/model/cache/cache_control.dart index 225fd28..0fc1337 100644 --- a/http_cache_core/lib/src/model/cache/cache_control.dart +++ b/http_cache_core/lib/src/model/cache/cache_control.dart @@ -110,6 +110,7 @@ class CacheControl { other.add(attribute); } } + scanner.scan(whitespace); } headerValues ??= []; diff --git a/http_cache_core/test/cache_control_test.dart b/http_cache_core/test/cache_control_test.dart index 18e6c54..8a73242 100644 --- a/http_cache_core/test/cache_control_test.dart +++ b/http_cache_core/test/cache_control_test.dart @@ -40,6 +40,29 @@ void main() { expect(cacheControl1, equals(cacheControl3)); }); + test('headers with and without optional whitespace', () { + final cacheControl1 = CacheControl( + maxAge: 1, + noCache: true, + noStore: true, + other: ['unknown', 'unknown2=2'], + privacy: 'public', + maxStale: 2, + minFresh: 3, + mustRevalidate: true, + ); + + final cacheControl2 = CacheControl.fromHeader([ + 'max-age=1 , no-store, no-cache, public,unknown , unknown2=2 , max-stale=2,min-fresh=3,must-revalidate', + ]); + + expect(cacheControl1, equals(cacheControl2)); + + // Redo test with toHeader() + final cacheControl3 = CacheControl.fromHeader([cacheControl2.toHeader()]); + expect(cacheControl1, equals(cacheControl3)); + }); + test('headers splitted', () { final cacheControl1 = CacheControl( maxAge: 1,