ivoleitao / stash

Key-value store abstraction with plain and cache driven semantics and a pluggable backend architecture.
MIT License
87 stars 16 forks source link

stash_dio: Null check operator used on a null value #8

Closed tjarvstrand closed 3 years ago

tjarvstrand commented 3 years ago

Describe the bug Caching crashes when responses do not have cache-control or expires headers set

To Reproduce Make a request to any URL that does not return any of the cache-control or expires headers set.

Expected behavior The response should be successfully cached.

Version stash_dio 2.0.2

Additional context I believe there is a typo here: https://github.com/ivoleitao/stash/blob/de443ce6bb0fc203677d66d4f581db21d47b4af1/packages/stash_dio/lib/src/dio/cache_value.dart#L52

This line looks like it should be ?. rather than !.

tjarvstrand commented 3 years ago

In case anyone else runs into this I have been able to work around it by adding the below dio interceptor before stash_dio:

...
import 'package:http_parser/http_parser.dart';
InterceptorsWrapper(
    onResponse: (response, handler) {
      final expires = response.headers.value('expires');
      final hasValidExpiry = expires != null &&
          expires.length > 4 &&
          parseHttpDate(expires).toLocal().compareTo(DateTime.now()) >= 0;
      final hasMaxStale =
          response.headers.value("cache-control")?.contains("max-stale") ??
              false;
      if (!hasValidExpiry && !hasMaxStale) {
        response.headers.set("cache-control", "max-stale=0");
      }
      handler.next(response);
    },
  );
ivoleitao commented 3 years ago

Hi thank you for your report. I’m preparing a new version for the next weekend and I intend to fix this issue then.

ivoleitao commented 3 years ago

I've published a new version that fixes this issue. I was a bit surprised that this was not caught on the unit tests but later understood that this was only happen when there's a need to serialise data. I've now added a unit test to make sure this does not happen again