Downloader: Use Headers.getDate() rather than parsing the date manually.

This commit is contained in:
Andreas Schildbach 2019-10-10 22:58:59 +02:00
parent 759afdd5ca
commit 93e4e23f60

View file

@ -50,7 +50,6 @@ import okhttp3.HttpUrl;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import okhttp3.internal.http.HttpDate;
public class Downloader { public class Downloader {
private final File cacheDir; private final File cacheDir;
@ -84,15 +83,12 @@ public class Downloader {
final Request.Builder request = new Request.Builder(); final Request.Builder request = new Request.Builder();
request.url(remoteUrl); request.url(remoteUrl);
if (meta != null) { if (meta != null) {
final String expiresHeader = meta.get("Expires"); final Date expires = meta.getDate("Expires");
if (expiresHeader != null) { if (expires != null && System.currentTimeMillis() < expires.getTime()) {
final Date expires = HttpDate.parse(expiresHeader); log.info("Download '{}' skipped; using cached copy.", remoteUrl);
if (expires != null && System.currentTimeMillis() < expires.getTime()) { future.set(HttpURLConnection.HTTP_NOT_MODIFIED);
log.info("Download '{}' skipped; using cached copy.", remoteUrl); semaphore.release();
future.set(HttpURLConnection.HTTP_NOT_MODIFIED); return future;
semaphore.release();
return future;
}
} }
final String lastModified = meta.get("Last-Modified"); final String lastModified = meta.get("Last-Modified");