Skip to content

Commit

Permalink
Fix redirect loop bug in ResourceFileServlet
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <[email protected]>
  • Loading branch information
lachlan-roberts committed Jan 24, 2025
1 parent 48be4eb commit e3cf531
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.google.apphosting.runtime.jetty.ee8;

import com.google.apphosting.runtime.AppVersion;
import com.google.apphosting.runtime.AppEngineConstants;
import com.google.apphosting.runtime.AppVersion;
import com.google.apphosting.utils.config.AppYaml;
import com.google.common.base.Ascii;
import com.google.common.flogger.GoogleLogger;
Expand All @@ -30,8 +30,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 @@ -57,8 +58,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 @@ -69,7 +71,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 @@ -78,6 +80,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 = ResourceFactory.root().newResource(context.getResource("/" + appVersion.getPublicRoot()));
Expand Down Expand Up @@ -254,13 +262,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 e3cf531

Please sign in to comment.