RFR: JDK-6956385: URLConnection.getLastModified() leaks file handles for jar:file and file: URLs [v3]

Jesse Glick duke at openjdk.org
Mon May 22 17:51:51 UTC 2023


On Mon, 22 May 2023 17:43:51 GMT, Jesse Glick <duke at openjdk.org> wrote:

>> That seems like it could cause other issues down the road though. `FileURLConnection::connect` has this comment:
>> 
>> 
>>     /*
>>      * Note: the semantics of FileURLConnection object is that the
>>      * results of the various URLConnection calls, such as
>>      * getContentType, getInputStream or getContentLength reflect
>>      * whatever was true when connect was called.
>>      */
>>     public void connect() throws IOException {
>
> This patch appears to be work and may be preferable to what is currently proposed:
> 
> 
> diff --git src/java.base/share/classes/sun/net/www/protocol/file/FileURLConnection.java src/java.base/share/classes/sun/net/www/protocol/file/FileURLConnection.java
> index a9ce7cee9a1..55783474c22 100644
> --- src/java.base/share/classes/sun/net/www/protocol/file/FileURLConnection.java
> +++ src/java.base/share/classes/sun/net/www/protocol/file/FileURLConnection.java
> @@ -53,7 +53,6 @@ public class FileURLConnection extends URLConnection {
>      File file;
>      String filename;
>      boolean isDirectory = false;
> -    boolean exists = false;
>      List<String> files;
>  
>      long length = -1;
> @@ -93,16 +92,12 @@ public class FileURLConnection extends URLConnection {
>      private boolean initializedHeaders = false;
>  
>      private void initializeHeaders() {
> -        try {
> -            connect();
> -            exists = file.exists();
> -        } catch (IOException e) {
> -        }
> -        if (!initializedHeaders || !exists) {
> +        if (!initializedHeaders || !file.exists()) {
>              length = file.length();
>              lastModified = file.lastModified();
> +            filename = file.toString();
>  
> -            if (!isDirectory) {
> +            if (!file.isDirectory()) {
>                  FileNameMap map = java.net.URLConnection.getFileNameMap();
>                  contentType = map.getContentTypeFor(filename);
>                  if (contentType != null) {
> @@ -131,17 +126,17 @@ public class FileURLConnection extends URLConnection {
>  
>      public Map<String,List<String>> getHeaderFields() {
>          initializeHeaders();
> -        return super.getHeaderFields();
> +        return properties.getHeaders();
>      }
>  
>      public String getHeaderField(String name) {
>          initializeHeaders();
> -        return super.getHeaderField(name);
> +        return properties.findValue(name);
>      }
>  
>      public String getHeaderField(int n) {
>          initializeHeaders();
> -        return super.getHeaderField(n);
> +        return properties.getKey(n);
>      }
>  
>      public int getContentLength() {

> has this comment

Interesting. And yet that does not appear to be true in `master`: `getContentType` and `getContentLength` will return whatever was in effect at the time `initializeHeaders()` was first called, which may have been before any explicit call to `connect()`, or hours after an explicit call.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/12871#discussion_r1200840711


More information about the net-dev mailing list