Skip to content

Commit e3cf531

Browse files
Fix redirect loop bug in ResourceFileServlet
Signed-off-by: Lachlan Roberts <lachlan.p.roberts@gmail.com>
1 parent 48be4eb commit e3cf531

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

runtime/runtime_impl_jetty12/src/main/java/com/google/apphosting/runtime/jetty/ee8/ResourceFileServlet.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

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

19-
import com.google.apphosting.runtime.AppVersion;
2019
import com.google.apphosting.runtime.AppEngineConstants;
20+
import com.google.apphosting.runtime.AppVersion;
2121
import com.google.apphosting.utils.config.AppYaml;
2222
import com.google.common.base.Ascii;
2323
import com.google.common.flogger.GoogleLogger;
@@ -30,8 +30,9 @@
3030
import javax.servlet.http.HttpServletRequest;
3131
import javax.servlet.http.HttpServletResponse;
3232
import org.eclipse.jetty.ee8.nested.ContextHandler;
33+
import org.eclipse.jetty.ee8.servlet.ServletContextHandler;
3334
import org.eclipse.jetty.ee8.servlet.ServletHandler;
34-
import org.eclipse.jetty.http.pathmap.MappedResource;
35+
import org.eclipse.jetty.ee8.servlet.ServletMapping;
3536
import org.eclipse.jetty.util.StringUtil;
3637
import org.eclipse.jetty.util.URIUtil;
3738
import org.eclipse.jetty.util.resource.Resource;
@@ -57,8 +58,9 @@ public class ResourceFileServlet extends HttpServlet {
5758
private Resource resourceBase;
5859
private String[] welcomeFiles;
5960
private FileSender fSender;
60-
ContextHandler chandler;
61+
ServletContextHandler chandler;
6162
ServletContext context;
63+
String defaultServletName;
6264

6365
/**
6466
* Initialize the servlet by extracting some useful configuration data from the current {@link
@@ -69,7 +71,7 @@ public void init() throws ServletException {
6971
context = getServletContext();
7072
AppVersion appVersion =
7173
(AppVersion) context.getAttribute(AppEngineConstants.APP_VERSION_CONTEXT_ATTR);
72-
chandler = ContextHandler.getContextHandler(context);
74+
chandler = ServletContextHandler.getServletContextHandler(context);
7375

7476
AppYaml appYaml =
7577
(AppYaml) chandler.getServer().getAttribute(AppEngineConstants.APP_YAML_ATTRIBUTE_TARGET);
@@ -78,6 +80,12 @@ public void init() throws ServletException {
7880
// we access Jetty's internal state.
7981
welcomeFiles = chandler.getWelcomeFiles();
8082

83+
ServletMapping servletMapping = chandler.getServletHandler().getServletMapping("/");
84+
if (servletMapping == null) {
85+
throw new ServletException("No servlet mapping found");
86+
}
87+
defaultServletName = servletMapping.getServletName();
88+
8189
try {
8290
// TODO: review use of root factory.
8391
resourceBase = ResourceFactory.root().newResource(context.getResource("/" + appVersion.getPublicRoot()));
@@ -254,13 +262,12 @@ private boolean maybeServeWelcomeFile(
254262
(AppVersion) getServletContext().getAttribute(AppEngineConstants.APP_VERSION_CONTEXT_ATTR);
255263
ServletHandler handler = chandler.getChildHandlerByClass(ServletHandler.class);
256264

257-
MappedResource<ServletHandler.MappedServlet> defaultEntry = handler.getHolderEntry("/");
258-
259265
for (String welcomeName : welcomeFiles) {
260266
String welcomePath = path + welcomeName;
261267
String relativePath = welcomePath.substring(1);
262268

263-
if (!Objects.equals(handler.getHolderEntry(welcomePath), defaultEntry)) {
269+
ServletHandler.MappedServlet mappedServlet = handler.getMappedServlet(welcomePath);
270+
if (!Objects.equals(mappedServlet.getServletHolder().getName(), defaultServletName)) {
264271
// It's a path mapped to a servlet. Forward to it.
265272
RequestDispatcher dispatcher = request.getRequestDispatcher(path + welcomeName);
266273
return serveWelcomeFileAsForward(dispatcher, included, request, response);

0 commit comments

Comments
 (0)