Skip to content

Commit

Permalink
Merge pull request #333 from GoogleCloudPlatform:ee8-ResourceFileServ…
Browse files Browse the repository at this point in the history
…let-RedirectLoop

PiperOrigin-RevId: 719193682
Change-Id: I6ac67bc7eec357d4f1dc20353ae156e28c01f5cd
  • Loading branch information
gae-java-bot committed Jan 24, 2025
2 parents 546cbc0 + e3cf531 commit afe26bc
Showing 1 changed file with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.ee8.nested.ContextHandler;
import org.eclipse.jetty.ee8.servlet.ServletContextHandler;
import org.eclipse.jetty.ee8.servlet.ServletHandler;
import org.eclipse.jetty.http.pathmap.MappedResource;
import org.eclipse.jetty.ee8.servlet.ServletMapping;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.resource.Resource;
Expand All @@ -58,8 +59,9 @@ public class ResourceFileServlet extends HttpServlet {
private Resource resourceBase;
private String[] welcomeFiles;
private FileSender fSender;
ContextHandler chandler;
ServletContextHandler chandler;
ServletContext context;
String defaultServletName;

/**
* Initialize the servlet by extracting some useful configuration data from the current {@link
Expand All @@ -70,7 +72,7 @@ public void init() throws ServletException {
context = getServletContext();
AppVersion appVersion =
(AppVersion) context.getAttribute(AppEngineConstants.APP_VERSION_CONTEXT_ATTR);
chandler = ContextHandler.getContextHandler(context);
chandler = ServletContextHandler.getServletContextHandler(context);

AppYaml appYaml =
(AppYaml) chandler.getServer().getAttribute(AppEngineConstants.APP_YAML_ATTRIBUTE_TARGET);
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 {
URL resourceBaseUrl = context.getResource("/" + appVersion.getPublicRoot());
resourceBase = (resourceBaseUrl == null) ? null : ResourceFactory.of(chandler).newResource(resourceBaseUrl);
Expand Down Expand Up @@ -255,13 +263,12 @@ private boolean maybeServeWelcomeFile(
(AppVersion) getServletContext().getAttribute(AppEngineConstants.APP_VERSION_CONTEXT_ATTR);
ServletHandler handler = chandler.getChildHandlerByClass(ServletHandler.class);

MappedResource<ServletHandler.MappedServlet> defaultEntry = handler.getHolderEntry("/");

for (String welcomeName : welcomeFiles) {
String welcomePath = path + welcomeName;
String relativePath = welcomePath.substring(1);

if (!Objects.equals(handler.getHolderEntry(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 afe26bc

Please sign in to comment.