Skip to content

Commit

Permalink
Copybara import of the project:
Browse files Browse the repository at this point in the history
--
1d008e7 by Lachlan Roberts <[email protected]>:

fix infinite redirect bug in ResourceFileServlet for EE10

Signed-off-by: Lachlan Roberts <[email protected]>
COPYBARA_INTEGRATE_REVIEW=#113 from GoogleCloudPlatform:ResourceFileServlet-infiniteRedirect 1d008e7
PiperOrigin-RevId: 628941837
Change-Id: I7d82a749da482858210818a584b69b577b38ed57
  • Loading branch information
lachlan-roberts authored and gae-java-bot committed Apr 29, 2024
1 parent 61a8e51 commit 6fb1d45
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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 =
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6fb1d45

Please sign in to comment.