Navigation Menu

Skip to content

Commit

Permalink
Normalize resource paths before looking them up.
Browse files Browse the repository at this point in the history
This should fix lookups of classpath resources failing with
non-normalized paths.
  • Loading branch information
hns committed Jun 27, 2011
1 parent c4deb3d commit 9d08bc1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/org/ringojs/engine/RhinoEngine.java
Expand Up @@ -749,7 +749,7 @@ public Resource findResource(String path, Repository localRoot)
&& (path.startsWith("./") || path.startsWith("../"))) {
return findResource(localRoot.getRelativePath() + path, null);
} else {
return config.getResource(path);
return config.getResource(normalizePath(path));
}
}

Expand All @@ -774,7 +774,38 @@ public Repository findRepository(String path, Repository localPath) throws IOExc
return repository;
}
}
return config.getRepository(path);
return config.getRepository(normalizePath(path));
}

public static String normalizePath(String path) {
if (path.indexOf('.') == -1) {
return path;
}
boolean absolute = path.startsWith("/");
String[] elements = path.split(Repository.SEPARATOR);
LinkedList<String> list = new LinkedList<String>();
for (String e : elements) {
if ("..".equals(e)) {
if (list.isEmpty() || "..".equals(list.getLast())) {
list.add(e);
} else {
list.removeLast();
}
} else if (!".".equals(e) && e.length() > 0) {
list.add(e);
}
}
StringBuilder sb = new StringBuilder(path.length());
if (absolute) {
sb.append("/");
}
int count = 0, last = list.size() - 1;
for (String e : list) {
sb.append(e);
if (count++ < last)
sb.append("/");
}
return sb.toString();
}

public void addToClasspath(Trackable path) throws MalformedURLException {
Expand Down
38 changes: 38 additions & 0 deletions src/org/ringojs/test/NormalizePathTest.java
@@ -0,0 +1,38 @@
package org.ringojs.test;

import junit.framework.TestCase;

import static org.ringojs.engine.RhinoEngine.normalizePath;

public class NormalizePathTest extends TestCase {

public void test1() {
String path = normalizePath("../bar/../foo/baz");
assertEquals(path, "../foo/baz");
}

public void test2() {
String path = normalizePath("./bar/.//../foo/baz");
assertEquals(path, "foo/baz");
}

public void test3() {
String path = normalizePath("bar/foo/baz");
assertEquals(path, "bar/foo/baz");
}

public void test4() {
String path = normalizePath("/bar/foo/baz/../BAZ");
assertEquals(path, "/bar/foo/BAZ");
}

public void test5() {
String path = normalizePath("bar//foo/./../baz/../../");
assertEquals(path, "");
}

public void test6() {
String path = normalizePath("bar/../foo/./../baz/../../../");
assertEquals(path, "../..");
}
}

0 comments on commit 9d08bc1

Please sign in to comment.