From 5b91e70c1b36f5f2f8fe3a5148ce9c8282604444 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Thu, 20 Oct 2022 06:22:21 -0500 Subject: [PATCH] Simplify resolve(String) a bit (#8741) --- .../eclipse/jetty/util/resource/PathResource.java | 11 ----------- .../eclipse/jetty/util/resource/ResourceTest.java | 13 +++++++++++++ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/PathResource.java b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/PathResource.java index 45283ccfbe3..dfeb444212d 100644 --- a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/PathResource.java +++ b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/PathResource.java @@ -285,17 +285,6 @@ public class PathResource extends Resource if (URIUtil.SLASH.equals(subUriPath)) return this; - // Sub-paths are always resolved under the given URI, - // we compensate for input sub-paths like "/subdir" - // where default resolve behavior would be to treat - // that like an absolute path. - while (subUriPath.startsWith(URIUtil.SLASH)) - { - // TODO XXX this appears entirely unnecessary and inefficient. We already have utilities - // to handle appending path strings with/without slashes. - subUriPath = subUriPath.substring(1); - } - URI uri = getURI(); URI resolvedUri = URIUtil.addPath(uri, subUriPath); Path path = Paths.get(resolvedUri); diff --git a/jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceTest.java b/jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceTest.java index 8246746e691..88ace644e61 100644 --- a/jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceTest.java +++ b/jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceTest.java @@ -378,6 +378,19 @@ public class ResourceTest assertThrows(IllegalArgumentException.class, () -> resource.resolve("./../bar")); } + @Test + public void testResolveStartsWithSlash(WorkDir workDir) + { + Path testdir = workDir.getEmptyPathDir(); + Path foo = testdir.resolve("foo"); + FS.ensureDirExists(foo); + Resource resourceBase = resourceFactory.newResource(testdir); + // test that a resolve starting with `/` works. + Resource resourceDir = resourceBase.resolve("/foo"); + assertTrue(Resources.exists(resourceDir)); + assertThat(resourceDir.getURI(), is(foo.toUri())); + } + @Test public void testNewResourcePathDoesNotExist(WorkDir workDir) {