From 6fb1d451bd6716ee0c09117efc50f4c6e46de075 Mon Sep 17 00:00:00 2001 From: Lachlan Date: Sun, 28 Apr 2024 20:46:26 -0700 Subject: [PATCH] Copybara import of the project: -- 1d008e7ada0e125d3bce0f3aa944e84a57a124cb by Lachlan Roberts : fix infinite redirect bug in ResourceFileServlet for EE10 Signed-off-by: Lachlan Roberts COPYBARA_INTEGRATE_REVIEW=https://github.com/GoogleCloudPlatform/appengine-java-standard/pull/113 from GoogleCloudPlatform:ResourceFileServlet-infiniteRedirect 1d008e7ada0e125d3bce0f3aa944e84a57a124cb PiperOrigin-RevId: 628941837 Change-Id: I7d82a749da482858210818a584b69b577b38ed57 --- .../runtime/jetty/ee10/ResourceFileServlet.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/runtime/runtime_impl_jetty12/src/main/java/com/google/apphosting/runtime/jetty/ee10/ResourceFileServlet.java b/runtime/runtime_impl_jetty12/src/main/java/com/google/apphosting/runtime/jetty/ee10/ResourceFileServlet.java index c0acd687..12919074 100644 --- a/runtime/runtime_impl_jetty12/src/main/java/com/google/apphosting/runtime/jetty/ee10/ResourceFileServlet.java +++ b/runtime/runtime_impl_jetty12/src/main/java/com/google/apphosting/runtime/jetty/ee10/ResourceFileServlet.java @@ -29,6 +29,7 @@ import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jetty.ee10.servlet.ServletContextHandler; import org.eclipse.jetty.ee10.servlet.ServletHandler; +import org.eclipse.jetty.ee10.servlet.ServletMapping; import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.URIUtil; @@ -60,6 +61,7 @@ public class ResourceFileServlet extends HttpServlet { private FileSender fSender; ServletContextHandler chandler; ServletContext context; + String defaultServletName; /** * Initialize the servlet by extracting some useful configuration data from the current {@link @@ -79,6 +81,12 @@ public void init() throws ServletException { // we access Jetty's internal state. welcomeFiles = chandler.getWelcomeFiles(); + ServletMapping servletMapping = chandler.getServletHandler().getServletMapping("/"); + if (servletMapping == null) { + throw new ServletException("No servlet mapping found"); + } + defaultServletName = servletMapping.getServletName(); + try { // TODO: review use of root factory. resourceBase = @@ -256,13 +264,12 @@ private boolean maybeServeWelcomeFile( (AppVersion) getServletContext().getAttribute(JettyConstants.APP_VERSION_CONTEXT_ATTR); ServletHandler handler = chandler.getServletHandler(); - ServletHandler.MappedServlet defaultEntry = handler.getMappedServlet("/"); - for (String welcomeName : welcomeFiles) { String welcomePath = path + welcomeName; String relativePath = welcomePath.substring(1); - if (!Objects.equals(handler.getMappedServlet(welcomePath), defaultEntry)) { + ServletHandler.MappedServlet mappedServlet = handler.getMappedServlet(welcomePath); + if (!Objects.equals(mappedServlet.getServletHolder().getName(), defaultServletName)) { // It's a path mapped to a servlet. Forward to it. RequestDispatcher dispatcher = request.getRequestDispatcher(path + welcomeName); return serveWelcomeFileAsForward(dispatcher, included, request, response);