Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tjarvstrand opened this issue May 8, 2021 · 3 comments
Closed

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

tjarvstrand opened this issue May 8, 2021 · 3 comments
Assignees
Labels
bug Something isn't working
Projects

Comments

@tjarvstrand
Copy link
Contributor

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:

'staleDate': staleDate!.microsecondsSinceEpoch,

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

@tjarvstrand
Copy link
Contributor Author

tjarvstrand commented May 8, 2021

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 ivoleitao self-assigned this May 9, 2021
@ivoleitao
Copy link
Owner

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 ivoleitao added the bug Something isn't working label May 12, 2021
@ivoleitao ivoleitao added this to To do in stash via automation May 12, 2021
@ivoleitao ivoleitao moved this from To do to In progress in stash May 15, 2021
@ivoleitao
Copy link
Owner

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

stash automation moved this from In progress to Done May 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
stash
Done
Development

No branches or pull requests

2 participants