Fixed MultiProtocolURL.resolveBackpath to handle remaining '..' segments

pull/91/head
luccioman 8 years ago
parent f1f4459f88
commit 3ccd89e274

@ -474,7 +474,17 @@ public class MultiProtocolURL implements Serializable, Comparable<MultiProtocolU
escape();
}
// resolve '..'
/**
* Resolve '..' segments in the path.
* For standard pseudo algorithms, see :
* <ul>
* <li>https://tools.ietf.org/html/rfc3986#section-5.2.4</li>
* <li>https://url.spec.whatwg.org/#path-state</li>
* <li>https://www.w3.org/TR/url/#relative-path-state</li>
* </ul>
* @param path URL path part : must not be null
* @return the path with '..' segments resolved
*/
private static final String resolveBackpath(final String path) {
String p = path;
if (p.isEmpty() || p.charAt(0) != '/') { p = "/" + p; }
@ -486,6 +496,14 @@ public class MultiProtocolURL implements Serializable, Comparable<MultiProtocolU
p = matcher.replaceAll("");
matcher.reset(p);
}
/* Let's remove any eventual remaining but inappropriate '..' segments at the beginning.
* See https://tools.ietf.org/html/rfc3986#section-5.2.4 -> parts 2.C and 2.D */
while(p.startsWith("/../")) {
p = p.substring(3);
}
if(p.equals("/..")) {
p = "/";
}
return p.equals("") ? "/" : p;
}

@ -38,15 +38,17 @@ public class MultiProtocolURLTest {
String[][] testStrings = new String[][]{
new String[]{"/..home", "/..home"},
new String[]{"/test/..home/test.html", "/test/..home/test.html"},
new String[]{"/../", "/../"},
new String[]{"/..", "/.."},
new String[]{"/test/..", "/"},
new String[]{"/test/../", "/"},
new String[]{"/test/test2/..", "/test"},
new String[]{"/test/test2/../", "/test/"},
new String[]{"/test/test2/../hallo", "/test/hallo"},
new String[]{"/test/test2/../hallo/", "/test/hallo/"},
new String[]{"/home/..test/../hallo/../", "/home/"}
new String[]{"/home/..test/../hallo/../", "/home/"},
/* No path segments prior to the '..' segment : '..' still has to be removed (See https://tools.ietf.org/html/rfc3986#section-5.2.4 -> parts 2.C and 2.D )*/
new String[]{"/../", "/"},
new String[]{"/..", "/"},
new String[]{"/../../../image.jpg", "/image.jpg"}
};
String testhost = "http://localhost";
for (int i = 0; i < testStrings.length; i++) {
@ -223,7 +225,7 @@ public class MultiProtocolURLTest {
* Test of getFileExtension method, of class MultiProtocolURL.
*/
@Test
public void testGetFileExtension() throws MalformedURLException {
public void testGetFileExtension() {
Map<String, String> testurls = new HashMap<String, String>();
// key=testurl, value=result
testurls.put("path/file.xml","xml"); // easiest

Loading…
Cancel
Save