Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Disable Jetty's webapp configuration #6050

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlets.CrossOriginFilter;
import org.eclipse.jetty.util.MultiException;
import org.eclipse.jetty.util.component.Graceful;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.jakarta.common.SessionTracker;
import org.eclipse.jetty.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer;
import org.eclipse.jetty.websocket.jakarta.server.internal.JakartaWebSocketServerContainer;
import org.jetbrains.annotations.Nullable;

import javax.inject.Inject;
import javax.inject.Singleton;
Expand Down Expand Up @@ -93,17 +94,20 @@ public JettyBackedGrpcServer(
jetty = new Server();
jetty.addConnector(createConnector(jetty, config));

final WebAppContext context =
new WebAppContext(null, "/", null, null, null, new ErrorPageErrorHandler(), NO_SESSIONS);
final ServletContextHandler context =
new ServletContextHandler(null, "/", null, null, null, new ErrorPageErrorHandler(), NO_SESSIONS);
try {
// Build a URL to a known file on the classpath, so Jetty can load resources from that jar to serve as
// static content
String knownFile = "/ide/index.html";
URL ide = JettyBackedGrpcServer.class.getResource(knownFile);
Resource jarContents = Resource.newResource(ide.toExternalForm().replace("!" + knownFile, "!/"));
context.setBaseResource(ControlledCacheResource.wrap(jarContents));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we don't use io.deephaven.server.jetty.ControlledCacheResource; is that correct? If so, should we delete it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, same problem that js-plugins has... I found that the ServletContextHandler also can be given a base resource and will guide DefaultServlet to provide content from there. We still want to share other settings though - it turns out our DefaultServlet configuration was coming from a jetty xml file, webdefault.xml. Now we explicitly configure it (and don't end up with other annotation scanning from the classpath or service loaders).

} catch (IOException ioException) {
throw new UncheckedIOException(ioException);
}
context.setInitParameter(DefaultServlet.CONTEXT_INIT + "dirAllowed", "false");
// Register a DefaultServlet to serve our custom resources
context.addServlet(servletHolder("default", null), "/*");

// Cache all of the appropriate assets folders
for (String appRoot : List.of("/ide/", "/iframe/table/", "/iframe/chart/", "/iframe/widget/")) {
Expand Down Expand Up @@ -380,12 +384,14 @@ public void onClosed(Connection connection) {
return serverConnector;
}

private static ServletHolder servletHolder(String name, URI filesystemUri) {
private static ServletHolder servletHolder(String name, @Nullable URI filesystemUri) {
final ServletHolder jsPlugins = new ServletHolder(name, DefaultServlet.class);
// Note, the URI needs explicitly be parseable as a directory URL ending in "!/", a requirement of the jetty
// resource creation implementation, see
// org.eclipse.jetty.util.resource.Resource.newResource(java.lang.String, boolean)
jsPlugins.setInitParameter("resourceBase", filesystemUri.toString());
if (filesystemUri != null) {
// Note, the URI needs explicitly be parseable as a directory URL ending in "!/", a requirement of the jetty
// resource creation implementation, see
// org.eclipse.jetty.util.resource.Resource.newResource(java.lang.String, boolean)
jsPlugins.setInitParameter("resourceBase", filesystemUri.toString());
}
jsPlugins.setInitParameter("pathInfoOnly", "true");
jsPlugins.setInitParameter("dirAllowed", "false");
jsPlugins.setAsyncSupported(true);
Expand Down