You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by jw...@apache.org on 2017/06/03 15:44:40 UTC

groovy git commit: GROOVY-8056: GroovyCodeSource(URL) can leak a file handler

Repository: groovy
Updated Branches:
  refs/heads/master 2d47ece85 -> 189d49117


GROOVY-8056: GroovyCodeSource(URL) can leak a file handler


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/189d4911
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/189d4911
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/189d4911

Branch: refs/heads/master
Commit: 189d49117005acd163525ac71cac666ef6a69772
Parents: 2d47ece
Author: John Wagenleitner <jw...@apache.org>
Authored: Sat Jun 3 07:50:41 2017 -0700
Committer: John Wagenleitner <jw...@apache.org>
Committed: Sat Jun 3 08:37:43 2017 -0700

----------------------------------------------------------------------
 src/main/groovy/lang/GroovyCodeSource.java | 30 +++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/189d4911/src/main/groovy/lang/GroovyCodeSource.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/GroovyCodeSource.java b/src/main/groovy/lang/GroovyCodeSource.java
index 13660c0..4c5c57b 100644
--- a/src/main/groovy/lang/GroovyCodeSource.java
+++ b/src/main/groovy/lang/GroovyCodeSource.java
@@ -21,10 +21,14 @@ package groovy.lang;
 import groovy.security.GroovyCodeSourcePermission;
 import groovy.util.CharsetToolkit;
 
-import java.io.*;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.Reader;
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URL;
+import java.net.URLConnection;
 import java.security.AccessController;
 import java.security.CodeSource;
 import java.security.PrivilegedActionException;
@@ -173,7 +177,7 @@ public class GroovyCodeSource {
         this.name = url.toExternalForm();
         this.codeSource = new CodeSource(url, (java.security.cert.Certificate[]) null);
         try {
-            String contentEncoding = url.openConnection().getContentEncoding();
+            String contentEncoding = getContentEncoding(url);
             if (contentEncoding != null) {
                 this.scriptText = ResourceGroovyMethods.getText(url, contentEncoding);
             } else {
@@ -184,6 +188,28 @@ public class GroovyCodeSource {
         }
     }
 
+    /**
+     * TODO(jwagenleitner): remove or fix in future release
+     *
+     * According to the spec getContentEncoding() returns the Content-Encoding
+     * HTTP Header which typically carries values such as 'gzip' or 'deflate'
+     * and is not the character set encoding. For compatibility in 2.4.x,
+     * this behavior is retained but should be removed or fixed (parse
+     * charset from Content-Type header) in future releases.
+     *
+     * see GROOVY-8056 and https://github.com/apache/groovy/pull/500
+     */
+    private static String getContentEncoding(URL url) throws IOException {
+        URLConnection urlConnection = url.openConnection();
+        String encoding = urlConnection.getContentEncoding();
+        try {
+            IOGroovyMethods.closeQuietly(urlConnection.getInputStream());
+        } catch (IOException ignore) {
+            // For compatibility, ignore exceptions from getInputStream() call
+        }
+        return encoding;
+    }
+
     CodeSource getCodeSource() {
         return codeSource;
     }