Skip to content

Commit

Permalink
Issue #7748 - allow override of path mapping behavior in ServletConte…
Browse files Browse the repository at this point in the history
…xtHandler

+ needed to open up ServletHandler to allow regex mappings

Signed-off-by: Greg Wilkins <gregw@webtide.com>
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
gregw committed Apr 4, 2022
1 parent 24b58ed commit 690d294
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 6 deletions.
Expand Up @@ -303,7 +303,7 @@ protected synchronized void doStop()
}
}
else
servletHolders.add(_servlets[i]); //only retain embedded
servletHolders.add(_servlets[i]); //only retain embedded
}
}

Expand All @@ -314,7 +314,7 @@ protected synchronized void doStop()
ServletMapping[] sms = servletMappings.toArray(new ServletMapping[0]);
updateBeans(_servletMappings, sms);
_servletMappings = sms;

if (_contextHandler != null)
_contextHandler.contextDestroyed();

Expand Down Expand Up @@ -1151,7 +1151,7 @@ public void addFilterMapping(FilterMapping mapping)
else
{
//there are existing entries. If this is a programmatic filtermapping, it is added at the end of the list.
//If this is a normal filtermapping, it is inserted after all the other filtermappings (matchBefores and normals),
//If this is a normal filtermapping, it is inserted after all the other filtermappings (matchBefores and normals),
//but before the first matchAfter filtermapping.
if (source == Source.JAVAX_API)
{
Expand Down Expand Up @@ -1299,7 +1299,13 @@ protected synchronized void updateNameMappings()
}
}

protected synchronized void updateMappings()
protected PathSpec asPathSpec(String pathSpec)
{
// By default only allow servlet path specs
return new ServletPathSpec(pathSpec);
}

protected void updateMappings()
{
// update filter mappings
if (_filterMappings == null)
Expand Down Expand Up @@ -1388,7 +1394,7 @@ protected synchronized void updateMappings()
finalMapping = mapping;
else
{
//already have a candidate - only accept another one
//already have a candidate - only accept another one
//if the candidate is a default, or we're allowing duplicate mappings
if (finalMapping.isDefault())
finalMapping = mapping;
Expand Down Expand Up @@ -1421,7 +1427,7 @@ else if (isAllowDuplicateMappings())
finalMapping.getServletName(),
_servletNameMap.get(finalMapping.getServletName()).getSource());

pm.put(new ServletPathSpec(pathSpec), _servletNameMap.get(finalMapping.getServletName()));
pm.put(asPathSpec(pathSpec), _servletNameMap.get(finalMapping.getServletName()));
}

_servletPathMap = pm;
Expand Down
@@ -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);
}
}
}

0 comments on commit 690d294

Please sign in to comment.