@@ -187,7 +187,9 @@ private void downloadHttp(URI uri, boolean checkETag) throws DownloadException,
187187
188188 ArrayList <IOException > exceptions = null ;
189189
190- for (int retryTime = 0 ; retryTime < retry ; retryTime ++) {
190+ // If loading the cache fails, the cache should not be loaded again.
191+ boolean useCachedResult = true ;
192+ for (int retryTime = 0 , retryLimit = retry ; retryTime < retryLimit ; retryTime ++) {
191193 if (isCancelled ()) {
192194 throw new InterruptedException ();
193195 }
@@ -204,7 +206,7 @@ private void downloadHttp(URI uri, boolean checkETag) throws DownloadException,
204206
205207 LinkedHashMap <String , String > headers = new LinkedHashMap <>();
206208 headers .put ("accept-encoding" , "gzip" );
207- if (checkETag )
209+ if (useCachedResult && checkETag )
208210 headers .putAll (repository .injectConnection (uri ));
209211
210212 do {
@@ -248,7 +250,7 @@ private void downloadHttp(URI uri, boolean checkETag) throws DownloadException,
248250 } while (true );
249251
250252 int responseCode = response .statusCode ();
251- if (responseCode == HttpURLConnection .HTTP_NOT_MODIFIED ) {
253+ if (useCachedResult && responseCode == HttpURLConnection .HTTP_NOT_MODIFIED ) {
252254 // Handle cache
253255 try {
254256 Path cache = repository .getCachedRemoteFile (currentURI , false );
@@ -260,9 +262,10 @@ private void downloadHttp(URI uri, boolean checkETag) throws DownloadException,
260262 } catch (IOException e ) {
261263 LOG .warning ("Unable to use cached file, redownload " + NetworkUtils .dropQuery (uri ), e );
262264 repository .removeRemoteEntry (currentURI );
265+ useCachedResult = false ;
263266 // Now we must reconnect the server since 304 may result in empty content,
264267 // if we want to redownload the file, we must reconnect the server without etag settings.
265- retryTime -- ;
268+ retryLimit ++ ;
266269 continue ;
267270 }
268271 } else if (responseCode / 100 == 4 ) {
0 commit comments