Merge remote-tracking branch 'origin/jetty-10.0.x' into jetty-11.0.x

This commit is contained in:
Greg Wilkins 2022-03-22 09:51:58 +01:00
commit 43cfbe8084
4 changed files with 222 additions and 9 deletions

View File

@ -85,18 +85,13 @@ public class ServletPathMapping implements HttpServletMapping
break;
case MIDDLE_GLOB:
_mappingMatch = null;
_matchValue = "";
_servletPath = pathInContext;
_pathInfo = null;
break;
default:
throw new IllegalStateException();
}
}
else
{
// TODO can we do better for RegexPathSpec
_mappingMatch = null;
_matchValue = "";
_servletPath = pathInContext;

View File

@ -1246,6 +1246,12 @@ public class ServletHandler extends ScopedHandler
}
}
protected PathSpec asPathSpec(String pathSpec)
{
// By default only allow servlet path specs
return new ServletPathSpec(pathSpec);
}
protected void updateMappings()
{
try (AutoLock ignored = lock())
@ -1354,9 +1360,9 @@ public class ServletHandler extends ScopedHandler
finalMapping.getServletName(),
getServlet(finalMapping.getServletName()).getSource());
ServletPathSpec servletPathSpec = new ServletPathSpec(pathSpec);
MappedServlet mappedServlet = new MappedServlet(servletPathSpec, getServlet(finalMapping.getServletName()));
pm.put(servletPathSpec, mappedServlet);
PathSpec ps = asPathSpec(pathSpec);
MappedServlet mappedServlet = new MappedServlet(ps, getServlet(finalMapping.getServletName()));
pm.put(ps, mappedServlet);
}
_servletPathMap = pm;

View File

@ -0,0 +1,153 @@
//
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.pathmap.PathMappings;
import org.eclipse.jetty.http.pathmap.PathSpec;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Server;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
public class RegexServletTest
{
private Server _server;
private LocalConnector _connector;
private ServletContextHandler _servletContextHandler;
@BeforeEach
public void beforeEach()
{
_server = new Server();
_connector = new LocalConnector(_server);
_servletContextHandler = new ServletContextHandler(_server, "/ctx");
_servletContextHandler.setServletHandler(new ServletHandler()
{
@Override
protected PathSpec asPathSpec(String pathSpec)
{
return PathMappings.asPathSpec(pathSpec);
}
});
_server.setHandler(_servletContextHandler);
_server.addConnector(_connector);
}
@Test
public void testHello() throws Exception
{
_servletContextHandler.addServlet(new ServletHolder(new ServletContextHandlerTest.HelloServlet()), "^/[Hh]ello");
_server.start();
assertThat(_connector.getResponse("GET /ctx/hello HTTP/1.0\r\n\r\n"), containsString("Hello World"));
assertThat(_connector.getResponse("GET /ctx/Hello HTTP/1.0\r\n\r\n"), containsString("Hello World"));
assertThat(_connector.getResponse("GET /ctx/HELLO HTTP/1.0\r\n\r\n"), containsString(" 404"));
}
@Test
public void testMapping() throws Exception
{
_servletContextHandler.addServlet(new ServletHolder(new TestServlet()), "^/test/.*$");
_server.start();
String response = _connector.getResponse("GET /ctx/test/info HTTP/1.0\r\n\r\n");
assertThat(response, containsString(" 200 OK"));
assertThat(response, containsString("contextPath='/ctx'"));
assertThat(response, containsString("servletPath='/test/info'"));
assertThat(response, containsString("pathInfo='null'"));
assertThat(response, containsString("mapping.mappingMatch='null'"));
assertThat(response, containsString("mapping.matchValue=''"));
assertThat(response, containsString("mapping.pattern='^/test/.*$'"));
}
@Test
public void testForward() throws Exception
{
_servletContextHandler.addServlet(new ServletHolder(new ForwardServlet()), "^/forward(/.*)?");
_servletContextHandler.addServlet(new ServletHolder(new TestServlet()), "^/[Tt]est(/.*)?");
_server.start();
String response = _connector.getResponse("GET /ctx/forward/ignore HTTP/1.0\r\n\r\n");
assertThat(response, containsString(" 200 OK"));
assertThat(response, containsString("contextPath='/ctx'"));
assertThat(response, containsString("servletPath='/Test/info'"));
assertThat(response, containsString("pathInfo='null'"));
assertThat(response, containsString("mapping.mappingMatch='null'"));
assertThat(response, containsString("mapping.matchValue=''"));
assertThat(response, containsString("mapping.pattern='^/[Tt]est(/.*)?'"));
}
@Test
public void testInclude() throws Exception
{
_servletContextHandler.addServlet(new ServletHolder(new IncludeServlet()), "^/include$");
_servletContextHandler.addServlet(new ServletHolder(new TestServlet()), "^/[Tt]est(/.*)?");
_server.start();
String response = _connector.getResponse("GET /ctx/include HTTP/1.0\r\n\r\n");
assertThat(response, containsString(" 200 OK"));
assertThat(response, containsString("contextPath='/ctx'"));
assertThat(response, containsString("servletPath='/include'"));
assertThat(response, containsString("pathInfo='null'"));
assertThat(response, containsString("mapping.mappingMatch='null'"));
assertThat(response, containsString("mapping.matchValue=''"));
assertThat(response, containsString("mapping.pattern='^/include$'"));
}
static class TestServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setStatus(200);
PrintWriter out = resp.getWriter();
out.printf("contextPath='%s'%n", req.getContextPath());
out.printf("servletPath='%s'%n", req.getServletPath());
out.printf("pathInfo='%s'%n", req.getPathInfo());
out.printf("mapping.mappingMatch='%s'%n", req.getHttpServletMapping().getMappingMatch());
out.printf("mapping.matchValue='%s'%n", req.getHttpServletMapping().getMatchValue());
out.printf("mapping.pattern='%s'%n", req.getHttpServletMapping().getPattern());
}
}
static class ForwardServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
req.getServletContext().getRequestDispatcher("/Test/info").forward(req, resp);
}
}
static class IncludeServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
req.getServletContext().getRequestDispatcher("/Test/info").include(req, resp);
}
}
}

View File

@ -24,6 +24,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
@ -50,6 +51,7 @@ import jakarta.servlet.ServletRequestEvent;
import jakarta.servlet.ServletRequestListener;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.SessionTrackingMode;
import jakarta.servlet.WriteListener;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@ -92,6 +94,7 @@ import org.slf4j.LoggerFactory;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
@ -994,6 +997,62 @@ public class ServletContextHandlerTest
assertEquals(1, MySCAListener.removes);
}
@Test
public void testAsyncServletRequestListenerListener() throws Exception
{
ContextHandlerCollection contexts = new ContextHandlerCollection();
_server.setHandler(contexts);
ServletContextHandler root = new ServletContextHandler(contexts, "/", ServletContextHandler.SESSIONS);
root.addEventListener(new MyRequestListener());
CountDownLatch latch = new CountDownLatch(1);
root.addServlet(new ServletHolder(new HttpServlet()
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
System.err.println("GET " + req.getDispatcherType());
if (req.getDispatcherType() == DispatcherType.REQUEST)
{
req.startAsync();
resp.getOutputStream().setWriteListener(new WriteListener()
{
@Override
public void onWritePossible() throws IOException
{
System.err.println("WRITE POSSIBLE");
if (resp.getOutputStream().isReady())
{
resp.getOutputStream().print("OK");
req.getAsyncContext().dispatch();
}
}
@Override
public void onError(Throwable t)
{
t.printStackTrace();
}
});
}
else
{
if (resp.getOutputStream().isReady())
resp.flushBuffer();
latch.countDown();
}
}
}), "/test");
_server.start();
//test ServletRequestAttributeListener
String response = _connector.getResponse("GET /test HTTP/1.0\r\n\r\n");
latch.await(5, TimeUnit.SECONDS);
assertThat(response, Matchers.containsString("200 OK"));
assertThat(MyRequestListener.inits, equalTo(2));
assertThat(MyRequestListener.destroys, equalTo(2));
}
@Test
public void testFindContainer() throws Exception
{