From 72d3d1d6034cd267e78adb8acc7b02f2312e0089 Mon Sep 17 00:00:00 2001 From: Grzegorz Grzybek Date: Sat, 4 Jul 2020 15:21:56 +0200 Subject: [PATCH] Issue #5025 - wrong welcome file handling with dispatcher.include() and non-default mapping Signed-off-by: Grzegorz Grzybek --- .../eclipse/jetty/server/ResourceService.java | 7 +++- .../jetty/servlet/DefaultServletTest.java | 42 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java index 10d1759a4cf2..7c29325e9c0b 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java @@ -401,8 +401,13 @@ protected void sendWelcome(HttpContent content, String pathInContext, boolean en if (welcome != null) { + String servletPath = included ? (String)request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH) + : request.getServletPath(); + if (servletPath == null) + servletPath = request.getServletPath(); + if (_pathInfoOnly) - welcome = URIUtil.addPaths(request.getServletPath(), welcome); + welcome = URIUtil.addPaths(servletPath, welcome); if (LOG.isDebugEnabled()) LOG.debug("welcome={}", welcome); diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DefaultServletTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DefaultServletTest.java index a49ca551c1b6..dbe7b5c73677 100644 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DefaultServletTest.java +++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DefaultServletTest.java @@ -41,6 +41,8 @@ import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.http.DateGenerator; @@ -813,6 +815,46 @@ public void testWelcomeMultipleBasesBase() throws Exception } } + @Test + public void testIncludedWelcomeDifferentBase() throws Exception + { + Path altRoot = workDir.getPath().resolve("altroot"); + FS.ensureDirExists(altRoot); + Path altIndex = altRoot.resolve("index.html"); + + ServletHolder defholder = context.addServlet(DefaultServlet.class, "/alt/*"); + defholder.setInitParameter("resourceBase", altRoot.toUri().toASCIIString()); + defholder.setInitParameter("dirAllowed", "false"); + defholder.setInitParameter("redirectWelcome", "false"); + defholder.setInitParameter("welcomeServlets", "true"); + defholder.setInitParameter("pathInfoOnly", "true"); + + ServletHolder gwholder = new ServletHolder("gateway", new HttpServlet() { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + req.getRequestDispatcher("/alt/").include(req, resp); + } + }); + context.addServlet(gwholder, "/gateway/*"); + + String rawResponse; + HttpTester.Response response; + + // Test included alt default + rawResponse = connector.getResponse("GET /context/gateway HTTP/1.0\r\n\r\n"); + response = HttpTester.parseResponse(rawResponse); + // 9.3 "The Include Method" - when include() is used, FileNotFoundException (and HTTP 500) + // should be used + assertThat(response.toString(), response.getStatus(), is(HttpStatus.INTERNAL_SERVER_ERROR_500)); + + createFile(altIndex, "

Alt Index

"); + rawResponse = connector.getResponse("GET /context/gateway HTTP/1.0\r\n\r\n"); + response = HttpTester.parseResponse(rawResponse); + assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); + assertThat(response.getContent(), containsString("

Alt Index

")); + } + @Test public void testWelcomeRedirect() throws Exception {