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

Improved PathSpec handling for servletName & pathInfo #7947

Merged
merged 17 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
@@ -0,0 +1,51 @@
//
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//

package org.eclipse.jetty.http.pathmap;

public interface MatchedPath
gregw marked this conversation as resolved.
Show resolved Hide resolved
joakime marked this conversation as resolved.
Show resolved Hide resolved
{
MatchedPath EMPTY = new MatchedPath()
{
@Override
public String getPathMatch()
{
return null;
}

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

/**
* Return the portion of the path that matches a path spec.
*
* @return the path name portion of the match.
*/
String getPathMatch();

/**
* Return the portion of the path that is after the path spec.
*
* @return the path info portion of the match, or null if there is no portion after the {@link #getPathMatch()}
*/
String getPathInfo();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//

package org.eclipse.jetty.http.pathmap;

public class MatchedResource<E>
joakime marked this conversation as resolved.
Show resolved Hide resolved
{
private final MappedResource<E> mappedResource;
private final MatchedPath matchedPath;

public MatchedResource(MappedResource<E> resource, MatchedPath matchedPath)
{
this.mappedResource = resource;
this.matchedPath = matchedPath;
}

public MappedResource<E> getMappedResource()
{
return this.mappedResource;
}

public PathSpec getPathSpec()
{
return mappedResource.getPathSpec();
}

public E getResource()
{
return mappedResource.getResource();
}

public MatchedPath getMatchedPath()
{
return matchedPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ public List<MappedResource<E>> getMatches(String path)
return ret;
}

public MappedResource<E> getMatch(String path)
public MatchedResource<E> getMatched(String path)
{
MatchedPath matchedPath = null;
PathSpecGroup lastGroup = null;

// Search all the mappings
Expand All @@ -142,8 +143,10 @@ public MappedResource<E> getMatch(String path)
MappedResource<E> candidate = exact_map.getBest(path, 0, i);
if (candidate == null)
break;
if (candidate.getPathSpec().matches(path))
return candidate;

matchedPath = candidate.getPathSpec().matched(path);
if (matchedPath != null)
return new MatchedResource<>(candidate, matchedPath);
i = candidate.getPathSpec().getPrefix().length() - 1;
}
break;
Expand All @@ -158,8 +161,10 @@ public MappedResource<E> getMatch(String path)
MappedResource<E> candidate = prefix_map.getBest(path, 0, i);
if (candidate == null)
break;
if (candidate.getPathSpec().matches(path))
return candidate;

matchedPath = candidate.getPathSpec().matched(path);
if (matchedPath != null)
return new MatchedResource<>(candidate, matchedPath);
i = candidate.getPathSpec().getPrefix().length() - 1;
}
break;
Expand All @@ -172,8 +177,12 @@ public MappedResource<E> getMatch(String path)
while ((i = path.indexOf('.', i + 1)) > 0)
{
MappedResource<E> candidate = suffix_map.get(path, i + 1, path.length() - i - 1);
if (candidate != null && candidate.getPathSpec().matches(path))
return candidate;
if (candidate == null)
break;

matchedPath = candidate.getPathSpec().matched(path);
if (matchedPath != null)
return new MatchedResource<>(candidate, matchedPath);
}
break;
}
Expand All @@ -182,15 +191,25 @@ public MappedResource<E> getMatch(String path)
}
}

if (mr.getPathSpec().matches(path))
return mr;
matchedPath = mr.getPathSpec().matched(path);
gregw marked this conversation as resolved.
Show resolved Hide resolved
if (matchedPath != null)
return new MatchedResource<>(mr, matchedPath);

lastGroup = group;
}

return null;
}

/**
* @deprecated use {@link #getMatched(String)} instead
*/
@Deprecated
public MappedResource<E> getMatch(String path)
{
return getMatched(path).getMappedResource();
}

@Override
public Iterator<MappedResource<E>> iterator()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,19 @@ public interface PathSpec extends Comparable<PathSpec>
*
* @param path the path to match against
* @return the path info portion of the string
* @deprecated use {@link #matched(String)} instead
*/
@Deprecated
String getPathInfo(String path);

/**
* Return the portion of the path that matches a path spec.
*
* @param path the path to match against
* @return the match, or null if no match at all
* @deprecated use {@link #matched(String)} instead
*/
@Deprecated
String getPathMatch(String path);

/**
Expand Down Expand Up @@ -90,6 +94,15 @@ public interface PathSpec extends Comparable<PathSpec>
*
* @param path the path to test
* @return true if the path matches this path spec, false otherwise
* @deprecated use {@link #matched(String)} instead
*/
@Deprecated
boolean matches(String path);

/**
* Get the complete matched details of the provided path.
* @param path the path to test
* @return the matched details, if a match was possible, or null if not able to be matched.
*/
MatchedPath matched(String path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.function.Predicate;

/**
Expand All @@ -34,7 +35,7 @@ public class PathSpecSet extends AbstractSet<String> implements Predicate<String
@Override
public boolean test(String s)
{
return specs.getMatch(s) != null;
return specs.getMatched(s) != null;
}

@Override
Expand All @@ -53,11 +54,8 @@ private PathSpec asPathSpec(Object o)
{
return (PathSpec)o;
}
if (o instanceof String)
{
return PathMappings.asPathSpec((String)o);
}
return PathMappings.asPathSpec(o.toString());

return PathMappings.asPathSpec(Objects.toString(o));
}

@Override
Expand Down
Loading