From 7365214fb6a703a21134da06cc6fb16f89d31024 Mon Sep 17 00:00:00 2001 From: Jeremy Long Date: Sat, 3 Aug 2013 12:09:44 -0400 Subject: [PATCH] updated getLastModified() to support the file:// protocol Former-commit-id: 75604221f3e38de0c877fa6e8b5048da593a2a56 --- .../dependencycheck/utils/Downloader.java | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/Downloader.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/Downloader.java index bf83b520d..fead3043b 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/Downloader.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/Downloader.java @@ -27,6 +27,7 @@ import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.SocketAddress; +import java.net.URISyntaxException; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; @@ -162,7 +163,8 @@ public final class Downloader { /** * Makes an HTTP Head request to retrieve the last modified date of the - * given URL. + * given URL. If the file:// protocol is specified, then the lastTimestamp + * of the file is returned. * * @param url the URL to retrieve the timestamp from * @return an epoch timestamp @@ -170,21 +172,33 @@ public final class Downloader { * the HTTP request */ public static long getLastModified(URL url) throws DownloadFailedException { - HttpURLConnection conn = null; long timestamp = 0; - try { - conn = Downloader.getConnection(url); - conn.setRequestMethod("HEAD"); - conn.connect(); - timestamp = conn.getLastModified(); - } catch (Exception ex) { - throw new DownloadFailedException("Error making HTTP HEAD request.", ex); - } finally { - if (conn != null) { - try { - conn.disconnect(); - } finally { - conn = null; + //TODO add the FPR protocol? + if ("file:".equalsIgnoreCase(url.getProtocol())) { + File f; + try { + f = new File(url.toURI()); + } catch (URISyntaxException ex) { + final String msg = String.format("Unable to locate '%s'; is the cve.url-2.0.modified property set correctly?", url.toString()); + throw new DownloadFailedException(msg); + } + timestamp = f.lastModified(); + } else { + HttpURLConnection conn = null; + try { + conn = Downloader.getConnection(url); + conn.setRequestMethod("HEAD"); + conn.connect(); + timestamp = conn.getLastModified(); + } catch (Exception ex) { + throw new DownloadFailedException("Error making HTTP HEAD request.", ex); + } finally { + if (conn != null) { + try { + conn.disconnect(); + } finally { + conn = null; + } } } }