Skip to content

Commit

Permalink
Fixes to backport of #7748
Browse files Browse the repository at this point in the history
+ Fix RegexPathSpec pathInfo
+ Test regression option to 93 behaviour

Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw committed Apr 4, 2022
1 parent 690d294 commit 1b9a3ea
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 13 deletions.
Expand Up @@ -140,7 +140,7 @@ public String getPathMatch(String path)
Matcher matcher = getMatcher(path);
if (matcher.matches())
{
if (matcher.groupCount() >= 1)
if (_group == PathSpecGroup.PREFIX_GLOB && matcher.groupCount() >= 1)
{
int idx = matcher.start(1);
if (idx > 0)
Expand Down
Expand Up @@ -24,7 +24,9 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class RegexPathSpecTest
{
Expand Down Expand Up @@ -76,6 +78,20 @@ public void testMiddleSpec()
assertNotMatches(spec, "/rest/list");
}

@Test
public void testPathInfo()
{
RegexPathSpec spec = new RegexPathSpec("^/test(/.*)?$");
assertTrue(spec.matches("/test/info"));
assertThat(spec.getPathMatch("/test/info"), equalTo("/test"));
assertThat(spec.getPathInfo("/test/info"), equalTo("/info"));

spec = new RegexPathSpec("^/[Tt]est(/.*)?$");
assertTrue(spec.matches("/test/info"));
assertThat(spec.getPathMatch("/test/info"), equalTo("/test/info"));
assertThat(spec.getPathInfo("/test/info"), nullValue());
}

@Test
public void testMiddleSpecNoGrouping()
{
Expand Down
Expand Up @@ -78,9 +78,6 @@ public void testMapping() throws Exception
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
Expand All @@ -95,9 +92,6 @@ public void testForward() throws Exception
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
Expand All @@ -112,9 +106,6 @@ public void testInclude() throws Exception
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
Expand All @@ -127,9 +118,6 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
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());
}
}

Expand Down
@@ -0,0 +1,120 @@
//
// ========================================================================
// 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 org.eclipse.jetty.http.pathmap.AbstractPathSpec;
import org.eclipse.jetty.http.pathmap.PathSpec;
import org.eclipse.jetty.http.pathmap.PathSpecGroup;
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 RegressionServletTest
{
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)
{
if (pathSpec.startsWith("/") && pathSpec.contains("*"))
return new AbstractPathSpec()
{
@Override
public int getSpecLength()
{
return pathSpec.length();
}

@Override
public PathSpecGroup getGroup()
{
return PathSpecGroup.EXACT;
}

@Override
public int getPathDepth()
{
return 0;
}

@Override
public String getPathInfo(String path)
{
return null;
}

@Override
public String getPathMatch(String path)
{
return path;
}

@Override
public String getDeclaration()
{
return pathSpec;
}

@Override
public String getPrefix()
{
return pathSpec;
}

@Override
public String getSuffix()
{
return null;
}

@Override
public boolean matches(String path)
{
return pathSpec.equals(path);
}
};
return super.asPathSpec(pathSpec);
}
});

_server.setHandler(_servletContextHandler);
_server.addConnector(_connector);
}

@Test
public void testHello() throws Exception
{
_servletContextHandler.addServlet(new ServletHolder(new ServletContextHandlerTest.HelloServlet()), "/*.xhtml");
_server.start();

assertThat(_connector.getResponse("GET /ctx/hello HTTP/1.0\r\n\r\n"), containsString(" 404"));
assertThat(_connector.getResponse("GET /ctx/hello.xhtml HTTP/1.0\r\n\r\n"), containsString(" 404"));
assertThat(_connector.getResponse("GET /ctx/*.xhtml HTTP/1.0\r\n\r\n"), containsString("Hello World"));
}
}

0 comments on commit 1b9a3ea

Please sign in to comment.