diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java index 0d8789372fb..5292df222bf 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java @@ -443,7 +443,7 @@ public class ResourceService response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, 0); // TODO need helper code to edit URIs - String uri = URIUtil.encodePath(URIUtil.addPaths(request.getContextPath(), welcome)); + String uri = URIUtil.addPaths(request.getContextPath(), welcome); String q = request.getHttpURI().getQuery(); if (q != null && !q.isEmpty()) uri += "?" + q; diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java index 8497d75ad5c..2a18fb6b094 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java @@ -525,6 +525,39 @@ public class ResourceHandlerTest assertThat(response.getContent(), containsString("Hello")); } + @Test + public void testWelcomeDirWithQuestion() throws Exception + { + Path dir = TEST_PATH.resolve("dir?"); + Files.createDirectories(dir); + Path welcome = dir.resolve("welcome.txt"); + try (FileOutputStream out = new FileOutputStream(welcome.toFile())) + { + out.write("Hello".getBytes()); + } + welcome.toFile().deleteOnExit(); + + HttpTester.Response response = HttpTester.parseResponse( + _local.getResponse("GET /resource/dir? HTTP/1.0\r\n\r\n")); + assertThat(response.getStatus(), is(HttpStatus.NOT_FOUND_404)); + + response = HttpTester.parseResponse( + _local.getResponse("GET /resource/dir%3F HTTP/1.0\r\n\r\n")); + assertThat(response.getStatus(), is(HttpStatus.FOUND_302)); + assertThat(response.getField(LOCATION).getValue(), endsWith("/resource/dir%3F/")); + + response = HttpTester.parseResponse( + _local.getResponse("GET /resource/dir%3F/ HTTP/1.0\r\n\r\n")); + assertThat(response.getStatus(), is(HttpStatus.OK_200)); + assertThat(response.getContent(), containsString("Hello")); + + _resourceHandler.setRedirectWelcome(true); + response = HttpTester.parseResponse( + _local.getResponse("GET /resource/dir%3F/ HTTP/1.0\r\n\r\n")); + assertThat(response.getStatus(), is(HttpStatus.FOUND_302)); + assertThat(response.getField(LOCATION).getValue(), endsWith("/resource/dir%3F/welcome.txt")); + } + @Test public void testWelcomeRedirect() throws Exception { diff --git a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/DefaultServlet.java b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/DefaultServlet.java index ec530db3534..33ad2483adc 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/DefaultServlet.java +++ b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/DefaultServlet.java @@ -711,7 +711,7 @@ public class DefaultServlet extends HttpServlet implements ResourceService.Welco // Redirect to the index response.setContentLength(0); - String uri = URIUtil.encodePath(URIUtil.addPaths(request.getContextPath(), welcome)); + String uri = URIUtil.addPaths(request.getContextPath(), welcome); String q = request.getQueryString(); if (q != null && !q.isEmpty()) uri += "?" + q; diff --git a/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/DefaultServletTest.java b/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/DefaultServletTest.java index a279f41753c..f07325255ff 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/DefaultServletTest.java +++ b/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/DefaultServletTest.java @@ -880,7 +880,6 @@ public class DefaultServletTest } @Test - @Disabled public void testWelcomeRedirect() throws Exception { Path dir = docRoot.resolve("dir"); @@ -942,7 +941,6 @@ public class DefaultServletTest } @Test - @Disabled public void testRelativeRedirect() throws Exception { Path dir = docRoot.resolve("dir"); @@ -986,7 +984,6 @@ public class DefaultServletTest * Ensure that oddball directory names are served with proper escaping */ @Test - @Disabled public void testWelcomeRedirectDirWithQuestion() throws Exception { FS.ensureDirExists(docRoot); @@ -1004,6 +1001,10 @@ public class DefaultServletTest String rawResponse; HttpTester.Response response; + rawResponse = connector.getResponse("GET /context/dir%3F/index.html HTTP/1.0\r\n\r\n"); + response = HttpTester.parseResponse(rawResponse); + assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); + rawResponse = connector.getResponse("GET /context/dir%3F HTTP/1.0\r\n\r\n"); response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.MOVED_TEMPORARILY_302));