Skip to content

Commit

Permalink
improve the case when the path for a jar-file needs to be URL-decoded
Browse files Browse the repository at this point in the history
it removes the case my.jar!/f%20o.rb which matches the behaviour of
file:f%20o.rb

Sponsored by Lookout Inc.
  • Loading branch information
mkristian committed Feb 10, 2015
1 parent d220103 commit f3d806d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
31 changes: 16 additions & 15 deletions core/src/main/java/org/jruby/util/JarResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,6 @@ public static JarResource create(String pathname) {
Matcher matcher = PREFIX_MATCH.matcher(pathname);
String sanitized = matcher.matches() ? matcher.group(1) : pathname;

try {
// since pathname is actually an uri we need to decode any url decoded characters like %20
// which happens when directory names contain spaces
// but do not to decode '+' to allow '+' inside a filenames
sanitized = sanitized.replace("+", "%2B");
sanitized = URLDecoder.decode(sanitized, "UTF-8");
} catch (IllegalArgumentException iae) {
// something in the path did not decode, so it's probably not a URI
// See jruby/jruby#2264.
return null;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException( "hmm - system does not know UTF-8 string encoding :(" );
}

int bang = sanitized.indexOf('!');
String jarPath = sanitized.substring(0, bang);
String entryPath = sanitized.substring(bang + 1);
Expand All @@ -54,7 +40,22 @@ private static JarResource createJarResource(String jarPath, String entryPath, b

if (index == null) {
// Jar doesn't exist
return null;
try {
jarPath = URLDecoder.decode(jarPath, "UTF-8");
entryPath = URLDecoder.decode(entryPath, "UTF-8");
} catch (IllegalArgumentException iae) {
// something in the path did not decode, so it's probably not a URI
// See jruby/jruby#2264.
return null;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException( "hmm - system does not know UTF-8 string encoding :(" );
}
index = jarCache.getIndex(jarPath);

if (index == null) {
// Jar doesn't exist
return null;
}
}

// Try it as directory first, because jars tend to have foo/ entries
Expand Down
4 changes: 0 additions & 4 deletions core/src/test/java/org/jruby/util/JarResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public void testCreateJarResource(){
assertNotNull( resource );
resource = JarResource.create( jar + "!/f o.rb" );
assertNotNull( resource );
resource = JarResource.create( jar + "!/f%20o.rb" );
assertNotNull( resource );
resource = JarResource.create( jar + "!/doesnotexist.rb" );
assertNull( resource );
resource = JarResource.create( jar.replace( ".jar", ".zip" ) + "!/foo.rb" );
Expand All @@ -24,8 +22,6 @@ public void testCreateJarResourceWithSpaceCharInPath(){
assertNotNull( resource );
resource = JarResource.create( jar + "!/f o.rb" );
assertNotNull( resource );
resource = JarResource.create( jar + "!/f%20o.rb" );
assertNotNull( resource );
resource = JarResource.create( jar + "!/doesnotexist.rb" );
assertNull( resource );
resource = JarResource.create( jar.replace( ".jar", ".zip" ) + "!/foo.rb" );
Expand Down

0 comments on commit f3d806d

Please sign in to comment.