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

Jetty 12.0.x resource Servlet #11933

Merged
merged 21 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -885,6 +885,12 @@ public Request getWrapped()
{
return _request;
}

@Override
public String toString()
{
return "%s@%x{%s}".formatted(getClass().getSimpleName(), hashCode(), getWrapped());
}
}

@SuppressWarnings("unchecked")
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ public String[] getParameterValues(String name)
return null;
return vals.toArray(new String[0]);
}

@Override
public String toString()
{
return "%s@%x{%s}".formatted(getClass().getSimpleName(), hashCode(), getRequest());
}
}

private class ForwardRequest extends ParameterRequestWrapper
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,12 @@ public Map<String, String> getTrailerFields()
return trailersMap;
}

@Override
public String toString()
{
return "%s@%x{%s}".formatted(getClass().getSimpleName(), hashCode(), _servletContextRequest);
}

static class AmbiguousURI extends ServletApiRequest
{
private final String msg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static Request wrap(HttpServletRequest httpServletRequest)
.authority(request.getServerName(), request.getServerPort());

if (included)
builder.path(addEncodedPaths(request.getContextPath(), encodePath(DefaultServlet.getIncludedPathInContext(request, includedServletPath, false))));
builder.path(addEncodedPaths(request.getContextPath(), encodePath(DefaultServlet.getIncludedPathInContext(request, includedServletPath))));
gregw marked this conversation as resolved.
Show resolved Hide resolved
else if (request.getDispatcherType() != DispatcherType.REQUEST)
builder.path(addEncodedPaths(request.getContextPath(), encodePath(URIUtil.addPaths(_servletRequest.getServletPath(), _servletRequest.getPathInfo()))));
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
Expand All @@ -52,7 +53,6 @@
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.http.UriCompliance;
import org.eclipse.jetty.http.content.ResourceHttpContent;
import org.eclipse.jetty.http.content.ResourceHttpContentFactory;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.IOResources;
import org.eclipse.jetty.logging.StacklessLogging;
Expand Down Expand Up @@ -1383,7 +1383,7 @@ public void testIncludedWelcomeDifferentBase() throws Exception
FS.ensureDirExists(altRoot);

ServletHolder defholder = context.addServlet(DefaultServlet.class, "/alt/*");
defholder.setInitParameter("resourceBase", altRoot.toUri().toASCIIString());
defholder.setInitParameter("baseResource", altRoot.toUri().toASCIIString());
gregw marked this conversation as resolved.
Show resolved Hide resolved
defholder.setInitParameter("dirAllowed", "false");
defholder.setInitParameter("redirectWelcome", "false");
defholder.setInitParameter("welcomeServlets", "true");
Expand All @@ -1394,7 +1394,9 @@ public void testIncludedWelcomeDifferentBase() throws Exception
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
String includeTarget = req.getParameter("includeTarget");
req.getRequestDispatcher(includeTarget).include(req, resp);
RequestDispatcher requestDispatcher = req.getRequestDispatcher(includeTarget);
System.err.printf("include %s -> %s\n", includeTarget, requestDispatcher);
gregw marked this conversation as resolved.
Show resolved Hide resolved
requestDispatcher.include(req, resp);
}
});
context.addServlet(gwholder, "/gateway");
Expand Down Expand Up @@ -2001,10 +2003,9 @@ public void testDirectFromResourceHttpContent() throws Exception
assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), containsString("<h1>Hello World</h1>"));

ResourceHttpContentFactory factory = (ResourceHttpContentFactory)context.getServletContext().getAttribute("resourceCache");

/*
TODO: fix after HttpContent changes.
ResourceHttpContentFactory factory = (ResourceHttpContentFactory)context.getServletContext().getAttribute("resourceCache");
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this test still @Disabled, but yet is enabled for the ResourceServlet?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because I started fixing it in ResourceServlet, but then saw that the commented out section was beyond the scope of this PR. So leaving DefaultServletTest as it was, but incremental improvement to ResourceServletTest (but more to do).

HttpContent content = factory.getContent("/index.html", 200);
ByteBuffer buffer = content.getDirectBuffer();
assertThat("Buffer is direct", buffer.isDirect(), is(true));
Expand Down Expand Up @@ -3425,22 +3426,17 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
response.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}

@Override
public void destroy()
{
}
}

@Test
public void testPathInfoOnly() throws Exception
public void testNotPathInfoOnly() throws Exception
{
ServletContextHandler context = new ServletContextHandler("/c1", ServletContextHandler.NO_SESSIONS);
context.setWelcomeFiles(new String[]{"index.y", "index.x"});
ServletHolder indexServlet = new ServletHolder("index-servlet", new HttpServlet()
{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
Expand All @@ -3456,35 +3452,35 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
context.getServletHandler().addServlet(indexServlet);
context.getServletHandler().addServletMapping(indexMapping);

Path pathTest = MavenTestingUtils.getTestResourcePath("pathTest");
Path docroot = MavenTestingUtils.getTestResourcePath("docroot");
janbartel marked this conversation as resolved.
Show resolved Hide resolved

Path defaultDir = pathTest.resolve("default");
ServletHolder slashHolder = new ServletHolder("default", new DefaultServlet());
slashHolder.setInitParameter("redirectWelcome", "false");
slashHolder.setInitParameter("welcomeServlets", "true");
slashHolder.setInitParameter("baseResource", defaultDir.toAbsolutePath().toString());
slashHolder.setInitParameter("baseResource", docroot.toAbsolutePath().toString());
context.addServlet(slashHolder, "/");

Path rDir = pathTest.resolve("rdir");
ServletHolder rHolder = new ServletHolder("rdefault", new DefaultServlet());
Path altroot = MavenTestingUtils.getTestResourcePath("altroot");
ServletHolder rHolder = new ServletHolder("alt", new DefaultServlet());
rHolder.setInitParameter("redirectWelcome", "false");
rHolder.setInitParameter("welcomeServlets", "true");
rHolder.setInitParameter("baseResource", rDir.toAbsolutePath().toString());
context.addServlet(rHolder, "/r/*");
rHolder.setInitParameter("pathInfoOnly", "false");
rHolder.setInitParameter("baseResource", altroot.toAbsolutePath().toString());
context.addServlet(rHolder, "/all/*");

server.stop();
server.setHandler(context);
server.start();
String rawRequest = """
GET /c1/r/ HTTP/1.1\r
GET /c1/all/index.html HTTP/1.1\r
Host: localhost\r
Connection: close\r
\r
""";

String rawResponse = connector.getResponse(rawRequest);
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
assertThat(response.getContent(), containsString("testPathInfoOnly-OK"));
assertThat(response.getContent(), containsString("this is alternate index content"));
}

@Test
Expand Down Expand Up @@ -3652,11 +3648,6 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
response.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}

@Override
public void destroy()
{
}
}

private boolean deleteFile(Path file) throws IOException
Expand Down
Loading
Loading