Skip to content

Commit afe26bc

Browse files
committed
Merge pull request #333 from GoogleCloudPlatform:ee8-ResourceFileServlet-RedirectLoop
PiperOrigin-RevId: 719193682 Change-Id: I6ac67bc7eec357d4f1dc20353ae156e28c01f5cd
2 parents 546cbc0 + e3cf531 commit afe26bc

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
import javax.servlet.http.HttpServletRequest;
3232
import javax.servlet.http.HttpServletResponse;
3333
import org.eclipse.jetty.ee8.nested.ContextHandler;
34+
import org.eclipse.jetty.ee8.servlet.ServletContextHandler;
3435
import org.eclipse.jetty.ee8.servlet.ServletHandler;
35-
import org.eclipse.jetty.http.pathmap.MappedResource;
36+
import org.eclipse.jetty.ee8.servlet.ServletMapping;
3637
import org.eclipse.jetty.util.StringUtil;
3738
import org.eclipse.jetty.util.URIUtil;
3839
import org.eclipse.jetty.util.resource.Resource;
@@ -58,8 +59,9 @@ public class ResourceFileServlet extends HttpServlet {
5859
private Resource resourceBase;
5960
private String[] welcomeFiles;
6061
private FileSender fSender;
61-
ContextHandler chandler;
62+
ServletContextHandler chandler;
6263
ServletContext context;
64+
String defaultServletName;
6365

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

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

84+
ServletMapping servletMapping = chandler.getServletHandler().getServletMapping("/");
85+
if (servletMapping == null) {
86+
throw new ServletException("No servlet mapping found");
87+
}
88+
defaultServletName = servletMapping.getServletName();
89+
8290
try {
8391
URL resourceBaseUrl = context.getResource("/" + appVersion.getPublicRoot());
8492
resourceBase = (resourceBaseUrl == null) ? null : ResourceFactory.of(chandler).newResource(resourceBaseUrl);
@@ -255,13 +263,12 @@ private boolean maybeServeWelcomeFile(
255263
(AppVersion) getServletContext().getAttribute(AppEngineConstants.APP_VERSION_CONTEXT_ATTR);
256264
ServletHandler handler = chandler.getChildHandlerByClass(ServletHandler.class);
257265

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

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

0 commit comments

Comments
 (0)