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..f5b9addebcce 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,11 @@ 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 (_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..73076a0de1f0 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,48 @@ 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 {