Reuse cached version listing parse on 304 Not Modified

Store the cached ETag alongside the parsed version listing in memory so
fetching versions does not re-read, decode, and parse `.cache/<pkg>-versions.json`
a second time when sending `If-None-Match` and handling `304 Not Modified`,
and avoid rescheduling dependency prefetching when `304 Not Modified`
returns the already-prefetched listing.

Corrupted cache files are now discarded before sending `If-None-Match`,
and an unsolicited `304 Not Modified` response to an unconditional request
is rejected as an HTTP error instead of recursing.
diff --git a/lib/src/source/hosted.dart b/lib/src/source/hosted.dart
index e3a641f..968c9fc 100644
--- a/lib/src/source/hosted.dart
+++ b/lib/src/source/hosted.dart
@@ -468,10 +468,11 @@
     // Only attach If-None-Match when running on native platforms (dart:io).
     // In browser environments (e.g. web tests), If-None-Match triggers a CORS
     // preflight OPTIONS request which package repositories may not support.
-    final (cachedEtag, cachedDoc) =
+    final cachedListing =
         const bool.fromEnvironment('dart.library.io')
-            ? _cachedVersionListingDoc(ref, cache)
-            : (null, null);
+            ? _cachedVersionListingResponse(ref, cache)
+            : null;
+    final etag = cachedListing?.etag;
     final String? bodyText;
     final dynamic body;
     final List<HostedVersionInfo> result;
@@ -487,11 +488,14 @@
             () async {
               final request = http.Request('GET', url);
               request.attachPubApiHeaders();
-              if (cachedEtag != null) {
-                request.headers[HttpHeaders.ifNoneMatchHeader] = cachedEtag;
+              if (etag != null) {
+                request.headers[HttpHeaders.ifNoneMatchHeader] = etag;
               }
               final response = await client.fetch(request);
               if (response.statusCode == HttpStatus.notModified) {
+                if (etag == null) {
+                  throw PubHttpResponseException(response);
+                }
                 return (null, null, true);
               }
               return (
@@ -505,25 +509,14 @@
       );
 
       if (is304) {
-        if (cachedDoc != null) {
-          try {
-            final res = _versionInfoFromPackageListing(
-              cachedDoc,
-              ref,
-              Uri.file(_versionListingCachePath(ref, cache)),
-              cache,
-            );
-            cache.hostedCache._responseCache[ref] = (DateTime.now(), res);
-            log.io('Version listing for "$packageName" is not modified (304).');
-            return res;
-          } on FormatException {
-            // Cached document was invalid, fall through to refetch.
-          }
-        }
-        // If the cache was missing/corrupted despite having an ETag, refetch
-        // unconditionally.
-        tryDeleteEntry(_versionListingCachePath(ref, cache));
-        return await _fetchVersionsNoPrefetching(ref, cache);
+        final versions = cachedListing!.versions;
+        cache.hostedCache._responseCache[ref] = _CachedVersionListing(
+          DateTime.now().toUtc(),
+          versions,
+          etag: etag,
+        );
+        log.io('Version listing for "$packageName" is not modified (304).');
+        return versions;
       }
 
       bodyText = responseBody!;
@@ -548,6 +541,9 @@
           cache,
           etag: responseEtag,
         );
+      } else {
+        tryDeleteEntry(_versionListingCachePath(ref, cache));
+        cache.hostedCache._responseCache.remove(ref);
       }
       return result;
     } on Exception catch (error, stackTrace) {
@@ -590,16 +586,15 @@
     }
 
     final cache = refAndCache.cache;
+    List<HostedVersionInfo>? cachedVersions;
     if (preschedule != null) {
       /// If we have a cached response - preschedule dependencies of that.
-      prescheduleDependenciesOfLatest(
-        await _cachedVersionListingResponse(ref, cache),
-        cache,
-      );
+      cachedVersions = _cachedVersionListingResponse(ref, cache)?.versions;
+      prescheduleDependenciesOfLatest(cachedVersions, cache);
     }
     final result = await _fetchVersionsNoPrefetching(ref, cache);
 
-    if (preschedule != null) {
+    if (preschedule != null && !identical(result, cachedVersions)) {
       // Preschedule the dependencies from the actual response.
       // This might overlap with those from the cached response. But the
       // scheduler ensures each listing will be fetched at most once.
@@ -869,31 +864,32 @@
   }
 
   /// If a cached version listing response for [ref] exists on disk and is less
-  /// than [maxAge] old it is parsed and returned.
+  /// than [maxAge] old it is parsed and returned along with its `_etag` (if
+  /// present and valid).
   ///
   /// Otherwise deletes a cached response if it exists and returns `null`.
   ///
   /// If [maxAge] is not given, we will try to get the cached version no matter
   /// how old it is.
-  Future<List<HostedVersionInfo>?> _cachedVersionListingResponse(
+  _CachedVersionListing? _cachedVersionListingResponse(
     PackageRef ref,
     SystemCache cache, {
     Duration? maxAge,
-  }) async {
+  }) {
     final cachedInfo = cache.hostedCache._responseCache[ref];
     if (cachedInfo != null) {
-      final (cacheTimestamp, versionInfo) = cachedInfo;
-      final cacheAge = DateTime.now().difference(cacheTimestamp);
-      if (maxAge == null || maxAge > cacheAge) {
+      final cacheAge = DateTime.now().difference(cachedInfo.timestamp);
+      if (maxAge == null || (!cacheAge.isNegative && maxAge > cacheAge)) {
         // The cached value is not too old.
-        return versionInfo;
+        return cachedInfo;
       }
     }
     final cachePath = _versionListingCachePath(ref, cache);
     final stat = io.File(cachePath).statSync();
     final now = DateTime.now();
     if (stat.type == io.FileSystemEntityType.file) {
-      if (maxAge == null || now.difference(stat.modified) < maxAge) {
+      final fileAge = now.difference(stat.modified);
+      if (maxAge == null || (!fileAge.isNegative && fileAge < maxAge)) {
         try {
           final cachedDoc = jsonDecode(readTextFile(cachePath));
           if (cachedDoc is! Map) {
@@ -909,9 +905,10 @@
           }
           final parsedTimestamp = DateTime.parse(timestamp);
           final cacheAge = DateTime.now().difference(parsedTimestamp);
-          if (maxAge != null && cacheAge > maxAge) {
+          if (maxAge != null && (cacheAge.isNegative || cacheAge > maxAge)) {
             // Too old according to internal timestamp - delete.
             tryDeleteEntry(cachePath);
+            cache.hostedCache._responseCache.remove(ref);
           } else {
             final res = _versionInfoFromPackageListing(
               cachedDoc,
@@ -919,19 +916,32 @@
               Uri.file(cachePath),
               cache,
             );
-            cache.hostedCache._responseCache[ref] = (parsedTimestamp, res);
-            return res;
+            final rawEtag = cachedDoc['_etag'];
+            final validEtag =
+                rawEtag is String && _isValidETag(rawEtag)
+                    ? rawEtag.trim()
+                    : null;
+            final listing = _CachedVersionListing(
+              parsedTimestamp,
+              res,
+              etag: validEtag,
+            );
+            cache.hostedCache._responseCache[ref] = listing;
+            return listing;
           }
         } on io.IOException {
           // Could not read the file. Delete if it exists.
           tryDeleteEntry(cachePath);
+          cache.hostedCache._responseCache.remove(ref);
         } on FormatException {
           // Decoding error - bad file or bad timestamp. Delete the file.
           tryDeleteEntry(cachePath);
+          cache.hostedCache._responseCache.remove(ref);
         }
       } else {
         // File too old
         tryDeleteEntry(cachePath);
+        cache.hostedCache._responseCache.remove(ref);
       }
     }
     return null;
@@ -965,30 +975,6 @@
     return _etagRegExp.hasMatch(trimmed);
   }
 
-  /// Returns the cached ETag and decoded JSON document for [ref] if one is
-  /// stored in `.cache/<pkg>-versions.json`.
-  (String? etag, Map<String, dynamic>? doc) _cachedVersionListingDoc(
-    PackageRef ref,
-    SystemCache cache,
-  ) {
-    final cachePath = _versionListingCachePath(ref, cache);
-    try {
-      final cachedDoc = jsonDecode(readTextFile(cachePath));
-      if (cachedDoc is Map<String, dynamic>) {
-        final etag = cachedDoc['_etag'];
-        final validEtag =
-            etag is String && _isValidETag(etag) ? etag.trim() : null;
-        return (validEtag, cachedDoc);
-      }
-    } on io.IOException {
-      // Ignore read errors (e.g. file does not exist).
-    } on FormatException {
-      // Corrupted file on disk. Delete it so subsequent requests don't trip.
-      tryDeleteEntry(cachePath);
-    }
-    return (null, null);
-  }
-
   /// Saves the (decoded) response from package-listing of [ref].
   Future<void> _cacheVersionListingResponse(
     Map<String, dynamic> body,
@@ -1003,17 +989,19 @@
       await writeTextFileAsync(
         path,
         jsonEncode(<String, dynamic>{
-          ...body,
-          '_fetchedAt': DateTime.now().toIso8601String(),
+          for (final MapEntry(:key, :value) in body.entries)
+            if (key != '_fetchedAt' && key != '_etag') key: value,
+          '_fetchedAt': DateTime.now().toUtc().toIso8601String(),
           if (validEtag != null) '_etag': validEtag,
         }),
       );
-      // Delete the entry in the in-memory cache to maintain the invariant that
-      // cached information in memory is the same as that on the disk.
-      cache.hostedCache._responseCache.remove(ref);
     } on io.IOException catch (e) {
       // Not being able to write this cache is not fatal. Just move on...
       log.fine('Failed writing cache file. $e');
+    } finally {
+      // Delete the entry in the in-memory cache to maintain the invariant that
+      // cached information in memory is the same as that on the disk.
+      cache.hostedCache._responseCache.remove(ref);
     }
   }
 
@@ -1044,7 +1032,8 @@
   }) async {
     if (cache.isOffline) {
       // Do we have a cached version response on disk?
-      final versionListing = await _cachedVersionListingResponse(ref, cache);
+      final versionListing =
+          _cachedVersionListingResponse(ref, cache)?.versions;
 
       if (versionListing == null) {
         return null;
@@ -1059,11 +1048,8 @@
     );
     if (maxAge != null) {
       // Do we have a cached version response on disk?
-      versionListing ??= await _cachedVersionListingResponse(
-        ref,
-        cache,
-        maxAge: maxAge,
-      );
+      versionListing ??=
+          _cachedVersionListingResponse(ref, cache, maxAge: maxAge)?.versions;
     }
     // Otherwise retrieve the info from the host.
     versionListing ??= await cache.hostedCache.scheduler
@@ -1147,11 +1133,8 @@
     );
     if (maxAge != null) {
       // Do we have a cached version response on disk?
-      versionListing ??= await _cachedVersionListingResponse(
-        ref,
-        cache,
-        maxAge: maxAge,
-      );
+      versionListing ??=
+          _cachedVersionListingResponse(ref, cache, maxAge: maxAge)?.versions;
     }
     versionListing ??= await cache.hostedCache.scheduler.schedule(
       HostedRefAndCache(ref, cache),
@@ -2279,8 +2262,7 @@
   /// Invariant: Entries in this cache are the parsed version of the exact same
   /// information cached on disk. I.e. if the entry is present in this cache,
   /// there will not be a newer version on disk.
-  final Map<PackageRef, (DateTime, List<HostedVersionInfo>)> _responseCache =
-      {};
+  final Map<PackageRef, _CachedVersionListing> _responseCache = {};
 
   /// An in-memory cache to store the futures of fetched security advisories.
   final Map<PackageId, Future<List<Advisory>?>> _advisoriesCache = {};
@@ -2293,3 +2275,11 @@
         pool: pool,
       );
 }
+
+final class _CachedVersionListing {
+  final DateTime timestamp;
+  final List<HostedVersionInfo> versions;
+  final String? etag;
+
+  _CachedVersionListing(this.timestamp, this.versions, {this.etag});
+}
diff --git a/test/hosted/conditional_package_listing_test.dart b/test/hosted/conditional_package_listing_test.dart
index 0a0afaf..5ca2872 100644
--- a/test/hosted/conditional_package_listing_test.dart
+++ b/test/hosted/conditional_package_listing_test.dart
@@ -9,6 +9,8 @@
 import 'dart:io';
 
 import 'package:path/path.dart' as p;
+import 'package:pub/src/exit_codes.dart' as exit_codes;
+import 'package:shelf/shelf.dart' as shelf;
 import 'package:test/test.dart';
 
 import '../descriptor.dart' as d;
@@ -89,8 +91,8 @@
     },
   );
 
-  test('pub recovers gracefully if cached listing is corrupted when 304 is '
-      'received', () async {
+  test('pub discards corrupted cached listing before sending If-None-Match and '
+      'refetches', () async {
     final server = await servePackages(serveEtags: true);
     server.serve('foo', '1.0.0');
 
@@ -113,8 +115,8 @@
       cachePath,
     ).writeAsStringSync('{"_etag": "W/\\"invalid\\"", "corrupted": true}');
 
-    // pub upgrade will attempt to validate via ETag, and when it fails to
-    // parse the cached response on 304, it deletes the cache and refetches.
+    // When the cached listing is corrupted on disk, pub discards it before
+    // sending If-None-Match and fetches a fresh listing from the server.
     await pubUpgrade();
 
     final lockFile =
@@ -123,6 +125,26 @@
   });
 
   test(
+    'pub fails cleanly without recursing if a broken server responds 304 Not '
+    'Modified to an unconditional request',
+    () async {
+      final server = await servePackages();
+      var requestCount = 0;
+      server.handle(RegExp(r'/api/packages/foo$'), (request) {
+        requestCount++;
+        return shelf.Response.notModified();
+      });
+
+      await d.appDir(dependencies: {'foo': '^1.0.0'}).create();
+      await pubGet(
+        error: contains('304 Not Modified trying to find package foo'),
+        exitCode: exit_codes.UNAVAILABLE,
+      );
+      expect(requestCount, 1);
+    },
+  );
+
+  test(
     'retracting a package version updates ETag and is observed on pub upgrade',
     () async {
       final server = await servePackages(serveEtags: true);