Issue #1676 removed deprecated PathMap

This commit is contained in:
Greg Wilkins 2017-07-18 19:59:07 +02:00
parent 3a4da94e1a
commit 34b25ff385
10 changed files with 209 additions and 915 deletions

View File

@ -43,7 +43,7 @@ public class ManyConnectors
// to get access to a keystore that we use in many unit tests and should
// probably be a direct path to your own keystore.
String jettyDistKeystore = "../../jetty-distribution/target/distribution/demo-base/etc/keystore";
String jettyDistKeystore = "../../jetty-distribution/target/distribution/demo-base/etc/test-keystore";
String keystorePath = System.getProperty(
"example.keystore", jettyDistKeystore);
File keystoreFile = new File(keystorePath);

View File

@ -1,642 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// 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;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.function.Predicate;
import java.util.function.Predicate;
import org.eclipse.jetty.util.ArrayTernaryTrie;
import org.eclipse.jetty.util.Trie;
import org.eclipse.jetty.util.URIUtil;
/**
* URI path map to Object.
* <p>
* This mapping implements the path specification recommended
* in the 2.2 Servlet API.
* </p>
*
* <p>
* Path specifications can be of the following forms:
* </p>
* <pre>
* /foo/bar - an exact path specification.
* /foo/* - a prefix path specification (must end '/*').
* *.ext - a suffix path specification.
* / - the default path specification.
* "" - the / path specification
* </pre>
*
* Matching is performed in the following order
* <ol>
* <li>Exact match.</li>
* <li>Longest prefix match.</li>
* <li>Longest suffix match.</li>
* <li>default.</li>
* </ol>
*
* <p>
* Multiple path specifications can be mapped by providing a list of
* specifications. By default this class uses characters ":," as path
* separators, unless configured differently by calling the static
* method @see PathMap#setPathSpecSeparators(String)
* <p>
* Special characters within paths such as '?<EFBFBD> and ';' are not treated specially
* as it is assumed they would have been either encoded in the original URL or
* stripped from the path.
* <p>
* This class is not synchronized. If concurrent modifications are
* possible then it should be synchronized at a higher level.
*
* @param <O> the Map.Entry value type
* @deprecated replaced with {@link org.eclipse.jetty.http.pathmap.PathMappings} (this class will be removed in Jetty 10)
*/
@Deprecated
public class PathMap<O> extends HashMap<String,O>
{
/* ------------------------------------------------------------ */
private static String __pathSpecSeparators = ":,";
/* ------------------------------------------------------------ */
/** Set the path spec separator.
* Multiple path specification may be included in a single string
* if they are separated by the characters set in this string.
* By default this class uses ":," characters as path separators.
* @param s separators
*/
public static void setPathSpecSeparators(String s)
{
__pathSpecSeparators=s;
}
/* --------------------------------------------------------------- */
Trie<MappedEntry<O>> _prefixMap=new ArrayTernaryTrie<>(false);
Trie<MappedEntry<O>> _suffixMap=new ArrayTernaryTrie<>(false);
final Map<String,MappedEntry<O>> _exactMap=new HashMap<>();
List<MappedEntry<O>> _defaultSingletonList=null;
MappedEntry<O> _prefixDefault=null;
MappedEntry<O> _default=null;
boolean _nodefault=false;
/* --------------------------------------------------------------- */
public PathMap()
{
this(11);
}
/* --------------------------------------------------------------- */
public PathMap(boolean noDefault)
{
this(11, noDefault);
}
/* --------------------------------------------------------------- */
public PathMap(int capacity)
{
this(capacity, false);
}
/* --------------------------------------------------------------- */
private PathMap(int capacity, boolean noDefault)
{
super(capacity);
_nodefault=noDefault;
}
/* --------------------------------------------------------------- */
/**
* Construct from dictionary PathMap.
* @param dictMap the map representing the dictionary to build this PathMap from
*/
public PathMap(Map<String, ? extends O> dictMap)
{
putAll(dictMap);
}
/* --------------------------------------------------------------- */
/** Add a single path match to the PathMap.
* @param pathSpec The path specification, or comma separated list of
* path specifications.
* @param object The object the path maps to
*/
@Override
public O put(String pathSpec, O object)
{
if ("".equals(pathSpec.trim()))
{
MappedEntry<O> entry = new MappedEntry<>("",object);
entry.setMapped("");
_exactMap.put("", entry);
return super.put("", object);
}
StringTokenizer tok = new StringTokenizer(pathSpec,__pathSpecSeparators);
O old =null;
while (tok.hasMoreTokens())
{
String spec=tok.nextToken();
if (!spec.startsWith("/") && !spec.startsWith("*."))
throw new IllegalArgumentException("PathSpec "+spec+". must start with '/' or '*.'");
old = super.put(spec,object);
// Make entry that was just created.
MappedEntry<O> entry = new MappedEntry<>(spec,object);
if (entry.getKey().equals(spec))
{
if (spec.equals("/*"))
_prefixDefault=entry;
else if (spec.endsWith("/*"))
{
String mapped=spec.substring(0,spec.length()-2);
entry.setMapped(mapped);
while (!_prefixMap.put(mapped,entry))
_prefixMap=new ArrayTernaryTrie<>((ArrayTernaryTrie<MappedEntry<O>>)_prefixMap,1.5);
}
else if (spec.startsWith("*."))
{
String suffix=spec.substring(2);
while(!_suffixMap.put(suffix,entry))
_suffixMap=new ArrayTernaryTrie<>((ArrayTernaryTrie<MappedEntry<O>>)_suffixMap,1.5);
}
else if (spec.equals(URIUtil.SLASH))
{
if (_nodefault)
_exactMap.put(spec,entry);
else
{
_default=entry;
_defaultSingletonList=Collections.singletonList(_default);
}
}
else
{
entry.setMapped(spec);
_exactMap.put(spec,entry);
}
}
}
return old;
}
/* ------------------------------------------------------------ */
/** Get object matched by the path.
* @param path the path.
* @return Best matched object or null.
*/
public O match(String path)
{
MappedEntry<O> entry = getMatch(path);
if (entry!=null)
return entry.getValue();
return null;
}
/* --------------------------------------------------------------- */
/** Get the entry mapped by the best specification.
* @param path the path.
* @return Map.Entry of the best matched or null.
*/
public MappedEntry<O> getMatch(String path)
{
if (path==null)
return null;
int l=path.length();
MappedEntry<O> entry=null;
//special case
if (l == 1 && path.charAt(0)=='/')
{
entry = _exactMap.get("");
if (entry != null)
return entry;
}
// try exact match
entry=_exactMap.get(path);
if (entry!=null)
return entry;
// prefix search
int i=l;
final Trie<PathMap.MappedEntry<O>> prefix_map=_prefixMap;
while(i>=0)
{
entry=prefix_map.getBest(path,0,i);
if (entry==null)
break;
String key = entry.getKey();
if (key.length()-2>=path.length() || path.charAt(key.length()-2)=='/')
return entry;
i=key.length()-3;
}
// Prefix Default
if (_prefixDefault!=null)
return _prefixDefault;
// Extension search
i=0;
final Trie<PathMap.MappedEntry<O>> suffix_map=_suffixMap;
while ((i=path.indexOf('.',i+1))>0)
{
entry=suffix_map.get(path,i+1,l-i-1);
if (entry!=null)
return entry;
}
// Default
return _default;
}
/* --------------------------------------------------------------- */
/** Get all entries matched by the path.
* Best match first.
* @param path Path to match
* @return List of Map.Entry instances key=pathSpec
*/
public List<? extends Map.Entry<String,O>> getMatches(String path)
{
MappedEntry<O> entry;
List<MappedEntry<O>> entries=new ArrayList<>();
if (path==null)
return entries;
if (path.length()==0)
return _defaultSingletonList;
// try exact match
entry=_exactMap.get(path);
if (entry!=null)
entries.add(entry);
// prefix search
int l=path.length();
int i=l;
final Trie<PathMap.MappedEntry<O>> prefix_map=_prefixMap;
while(i>=0)
{
entry=prefix_map.getBest(path,0,i);
if (entry==null)
break;
String key = entry.getKey();
if (key.length()-2>=path.length() || path.charAt(key.length()-2)=='/')
entries.add(entry);
i=key.length()-3;
}
// Prefix Default
if (_prefixDefault!=null)
entries.add(_prefixDefault);
// Extension search
i=0;
final Trie<PathMap.MappedEntry<O>> suffix_map=_suffixMap;
while ((i=path.indexOf('.',i+1))>0)
{
entry=suffix_map.get(path,i+1,l-i-1);
if (entry!=null)
entries.add(entry);
}
// root match
if ("/".equals(path))
{
entry=_exactMap.get("");
if (entry!=null)
entries.add(entry);
}
// Default
if (_default!=null)
entries.add(_default);
return entries;
}
/* --------------------------------------------------------------- */
/** Return whether the path matches any entries in the PathMap,
* excluding the default entry
* @param path Path to match
* @return Whether the PathMap contains any entries that match this
*/
public boolean containsMatch(String path)
{
MappedEntry<?> match = getMatch(path);
return match!=null && !match.equals(_default);
}
/* --------------------------------------------------------------- */
@Override
public O remove(Object pathSpec)
{
if (pathSpec!=null)
{
String spec=(String) pathSpec;
if (spec.equals("/*"))
_prefixDefault=null;
else if (spec.endsWith("/*"))
_prefixMap.remove(spec.substring(0,spec.length()-2));
else if (spec.startsWith("*."))
_suffixMap.remove(spec.substring(2));
else if (spec.equals(URIUtil.SLASH))
{
_default=null;
_defaultSingletonList=null;
}
else
_exactMap.remove(spec);
}
return super.remove(pathSpec);
}
/* --------------------------------------------------------------- */
@Override
public void clear()
{
_exactMap.clear();
_prefixMap=new ArrayTernaryTrie<>(false);
_suffixMap=new ArrayTernaryTrie<>(false);
_default=null;
_defaultSingletonList=null;
_prefixDefault=null;
super.clear();
}
/* --------------------------------------------------------------- */
/**
* @param pathSpec the path spec
* @param path the path
* @return true if match.
*/
public static boolean match(String pathSpec, String path)
{
return match(pathSpec, path, false);
}
/* --------------------------------------------------------------- */
/**
* @param pathSpec the path spec
* @param path the path
* @param noDefault true to not handle the default path "/" special, false to allow matcher rules to run
* @return true if match.
*/
public static boolean match(String pathSpec, String path, boolean noDefault)
{
if (pathSpec.length()==0)
return "/".equals(path);
char c = pathSpec.charAt(0);
if (c=='/')
{
if (!noDefault && pathSpec.length()==1 || pathSpec.equals(path))
return true;
if(isPathWildcardMatch(pathSpec, path))
return true;
}
else if (c=='*')
return path.regionMatches(path.length()-pathSpec.length()+1,
pathSpec,1,pathSpec.length()-1);
return false;
}
/* --------------------------------------------------------------- */
private static boolean isPathWildcardMatch(String pathSpec, String path)
{
// For a spec of "/foo/*" match "/foo" , "/foo/..." but not "/foobar"
int cpl=pathSpec.length()-2;
if (pathSpec.endsWith("/*") && path.regionMatches(0,pathSpec,0,cpl))
{
if (path.length()==cpl || '/'==path.charAt(cpl))
return true;
}
return false;
}
/* --------------------------------------------------------------- */
/** Return the portion of a path that matches a path spec.
* @param pathSpec the path spec
* @param path the path
* @return null if no match at all.
*/
public static String pathMatch(String pathSpec, String path)
{
char c = pathSpec.charAt(0);
if (c=='/')
{
if (pathSpec.length()==1)
return path;
if (pathSpec.equals(path))
return path;
if (isPathWildcardMatch(pathSpec, path))
return path.substring(0,pathSpec.length()-2);
}
else if (c=='*')
{
if (path.regionMatches(path.length()-(pathSpec.length()-1),
pathSpec,1,pathSpec.length()-1))
return path;
}
return null;
}
/* --------------------------------------------------------------- */
/** Return the portion of a path that is after a path spec.
* @param pathSpec the path spec
* @param path the path
* @return The path info string
*/
public static String pathInfo(String pathSpec, String path)
{
if ("".equals(pathSpec))
return path; //servlet 3 spec sec 12.2 will be '/'
char c = pathSpec.charAt(0);
if (c=='/')
{
if (pathSpec.length()==1)
return null;
boolean wildcard = isPathWildcardMatch(pathSpec, path);
// handle the case where pathSpec uses a wildcard and path info is "/*"
if (pathSpec.equals(path) && !wildcard)
return null;
if (wildcard)
{
if (path.length()==pathSpec.length()-2)
return null;
return path.substring(pathSpec.length()-2);
}
}
return null;
}
/* ------------------------------------------------------------ */
/** Relative path.
* @param base The base the path is relative to.
* @param pathSpec The spec of the path segment to ignore.
* @param path the additional path
* @return base plus path with pathspec removed
*/
public static String relativePath(String base,
String pathSpec,
String path )
{
String info=pathInfo(pathSpec,path);
if (info==null)
info=path;
if( info.startsWith( "./"))
info = info.substring( 2);
if( base.endsWith( URIUtil.SLASH))
if( info.startsWith( URIUtil.SLASH))
path = base + info.substring(1);
else
path = base + info;
else
if( info.startsWith( URIUtil.SLASH))
path = base + info;
else
path = base + URIUtil.SLASH + info;
return path;
}
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
public static class MappedEntry<O> implements Map.Entry<String,O>
{
private final String key;
private final O value;
private String mapped;
MappedEntry(String key, O value)
{
this.key=key;
this.value=value;
}
@Override
public String getKey()
{
return key;
}
@Override
public O getValue()
{
return value;
}
@Override
public O setValue(O o)
{
throw new UnsupportedOperationException();
}
@Override
public String toString()
{
return key+"="+value;
}
public String getMapped()
{
return mapped;
}
void setMapped(String mapped)
{
this.mapped = mapped;
}
}
public static class PathSet extends AbstractSet<String> implements Predicate<String>
{
private final PathMap<Boolean> _map = new PathMap<>();
@Override
public Iterator<String> iterator()
{
return _map.keySet().iterator();
}
@Override
public int size()
{
return _map.size();
}
@Override
public boolean add(String item)
{
return _map.put(item,Boolean.TRUE)==null;
}
@Override
public boolean remove(Object item)
{
return _map.remove(item)!=null;
}
@Override
public boolean contains(Object o)
{
return _map.containsKey(o);
}
@Override
public boolean test(String s)
{
return _map.containsMatch(s);
}
public boolean containsMatch(String s)
{
return _map.containsMatch(s);
}
}
}

View File

@ -36,7 +36,146 @@ public class ServletPathSpec extends PathSpec
return "/" + pathSpec;
return pathSpec;
}
/**
* @param pathSpec the path spec
* @param path the path
* @return true if match.
*/
public static boolean match(String pathSpec, String path)
{
return match(pathSpec, path, false);
}
/**
* @param pathSpec the path spec
* @param path the path
* @param noDefault true to not handle the default path "/" special, false to allow matcher rules to run
* @return true if match.
*/
public static boolean match(String pathSpec, String path, boolean noDefault)
{
if (pathSpec.length()==0)
return "/".equals(path);
char c = pathSpec.charAt(0);
if (c=='/')
{
if (!noDefault && pathSpec.length()==1 || pathSpec.equals(path))
return true;
if(isPathWildcardMatch(pathSpec, path))
return true;
}
else if (c=='*')
return path.regionMatches(path.length()-pathSpec.length()+1,
pathSpec,1,pathSpec.length()-1);
return false;
}
private static boolean isPathWildcardMatch(String pathSpec, String path)
{
// For a spec of "/foo/*" match "/foo" , "/foo/..." but not "/foobar"
int cpl=pathSpec.length()-2;
if (pathSpec.endsWith("/*") && path.regionMatches(0,pathSpec,0,cpl))
{
if (path.length()==cpl || '/'==path.charAt(cpl))
return true;
}
return false;
}
/** Return the portion of a path that matches a path spec.
* @param pathSpec the path spec
* @param path the path
* @return null if no match at all.
*/
public static String pathMatch(String pathSpec, String path)
{
char c = pathSpec.charAt(0);
if (c=='/')
{
if (pathSpec.length()==1)
return path;
if (pathSpec.equals(path))
return path;
if (isPathWildcardMatch(pathSpec, path))
return path.substring(0,pathSpec.length()-2);
}
else if (c=='*')
{
if (path.regionMatches(path.length()-(pathSpec.length()-1),
pathSpec,1,pathSpec.length()-1))
return path;
}
return null;
}
/** Return the portion of a path that is after a path spec.
* @param pathSpec the path spec
* @param path the path
* @return The path info string
*/
public static String pathInfo(String pathSpec, String path)
{
if ("".equals(pathSpec))
return path; //servlet 3 spec sec 12.2 will be '/'
char c = pathSpec.charAt(0);
if (c=='/')
{
if (pathSpec.length()==1)
return null;
boolean wildcard = isPathWildcardMatch(pathSpec, path);
// handle the case where pathSpec uses a wildcard and path info is "/*"
if (pathSpec.equals(path) && !wildcard)
return null;
if (wildcard)
{
if (path.length()==pathSpec.length()-2)
return null;
return path.substring(pathSpec.length()-2);
}
}
return null;
}
/** Relative path.
* @param base The base the path is relative to.
* @param pathSpec The spec of the path segment to ignore.
* @param path the additional path
* @return base plus path with pathspec removed
*/
public static String relativePath(String base,
String pathSpec,
String path )
{
String info = pathInfo(pathSpec, path);
if (info == null)
info = path;
if (info.startsWith("./"))
info = info.substring(2);
if (base.endsWith(URIUtil.SLASH))
if (info.startsWith(URIUtil.SLASH))
path = base + info.substring(1);
else
path = base + info;
else if (info.startsWith(URIUtil.SLASH))
path = base + info;
else
path = base + URIUtil.SLASH + info;
return path;
}
public ServletPathSpec(String servletPathSpec)
{

View File

@ -1,204 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// 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;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
*
*/
public class PathMapTest
{
@Test
public void testPathMap() throws Exception
{
PathMap<String> p = new PathMap<>();
p.put("/abs/path", "1");
p.put("/abs/path/longer", "2");
p.put("/animal/bird/*", "3");
p.put("/animal/fish/*", "4");
p.put("/animal/*", "5");
p.put("*.tar.gz", "6");
p.put("*.gz", "7");
p.put("/", "8");
p.put("/XXX:/YYY", "9");
p.put("", "10");
p.put("/\u20ACuro/*", "11");
String[][] tests = {
{ "/abs/path", "1"},
{ "/abs/path/xxx", "8"},
{ "/abs/pith", "8"},
{ "/abs/path/longer", "2"},
{ "/abs/path/", "8"},
{ "/abs/path/xxx", "8"},
{ "/animal/bird/eagle/bald", "3"},
{ "/animal/fish/shark/grey", "4"},
{ "/animal/insect/bug", "5"},
{ "/animal", "5"},
{ "/animal/", "5"},
{ "/animal/x", "5"},
{ "/animal/*", "5"},
{ "/suffix/path.tar.gz", "6"},
{ "/suffix/path.gz", "7"},
{ "/animal/path.gz", "5"},
{ "/Other/path", "8"},
{ "/\u20ACuro/path", "11"},
{ "/", "10"},
};
for (String[] test : tests)
{
assertEquals(test[0], test[1], p.getMatch(test[0]).getValue());
}
assertEquals("Get absolute path", "1", p.get("/abs/path"));
assertEquals("Match absolute path", "/abs/path", p.getMatch("/abs/path").getKey());
assertEquals("all matches", "[/animal/bird/*=3, /animal/*=5, *.tar.gz=6, *.gz=7, /=8]",
p.getMatches("/animal/bird/path.tar.gz").toString());
assertEquals("Dir matches", "[/animal/fish/*=4, /animal/*=5, /=8]", p.getMatches("/animal/fish/").toString());
assertEquals("Dir matches", "[/animal/fish/*=4, /animal/*=5, /=8]", p.getMatches("/animal/fish").toString());
assertEquals("Root matches", "[=10, /=8]",p.getMatches("/").toString());
assertEquals("Dir matches", "[/=8]", p.getMatches("").toString());
assertEquals("pathMatch exact", "/Foo/bar", PathMap.pathMatch("/Foo/bar", "/Foo/bar"));
assertEquals("pathMatch prefix", "/Foo", PathMap.pathMatch("/Foo/*", "/Foo/bar"));
assertEquals("pathMatch prefix", "/Foo", PathMap.pathMatch("/Foo/*", "/Foo/"));
assertEquals("pathMatch prefix", "/Foo", PathMap.pathMatch("/Foo/*", "/Foo"));
assertEquals("pathMatch suffix", "/Foo/bar.ext", PathMap.pathMatch("*.ext", "/Foo/bar.ext"));
assertEquals("pathMatch default", "/Foo/bar.ext", PathMap.pathMatch("/", "/Foo/bar.ext"));
assertEquals("pathInfo exact", null, PathMap.pathInfo("/Foo/bar", "/Foo/bar"));
assertEquals("pathInfo prefix", "/bar", PathMap.pathInfo("/Foo/*", "/Foo/bar"));
assertEquals("pathInfo prefix", "/*", PathMap.pathInfo("/Foo/*", "/Foo/*"));
assertEquals("pathInfo prefix", "/", PathMap.pathInfo("/Foo/*", "/Foo/"));
assertEquals("pathInfo prefix", null, PathMap.pathInfo("/Foo/*", "/Foo"));
assertEquals("pathInfo suffix", null, PathMap.pathInfo("*.ext", "/Foo/bar.ext"));
assertEquals("pathInfo default", null, PathMap.pathInfo("/", "/Foo/bar.ext"));
assertEquals("multi paths", "9", p.getMatch("/XXX").getValue());
assertEquals("multi paths", "9", p.getMatch("/YYY").getValue());
p.put("/*", "0");
assertEquals("Get absolute path", "1", p.get("/abs/path"));
assertEquals("Match absolute path", "/abs/path", p.getMatch("/abs/path").getKey());
assertEquals("Match absolute path", "1", p.getMatch("/abs/path").getValue());
assertEquals("Mismatch absolute path", "0", p.getMatch("/abs/path/xxx").getValue());
assertEquals("Mismatch absolute path", "0", p.getMatch("/abs/pith").getValue());
assertEquals("Match longer absolute path", "2", p.getMatch("/abs/path/longer").getValue());
assertEquals("Not exact absolute path", "0", p.getMatch("/abs/path/").getValue());
assertEquals("Not exact absolute path", "0", p.getMatch("/abs/path/xxx").getValue());
assertEquals("Match longest prefix", "3", p.getMatch("/animal/bird/eagle/bald").getValue());
assertEquals("Match longest prefix", "4", p.getMatch("/animal/fish/shark/grey").getValue());
assertEquals("Match longest prefix", "5", p.getMatch("/animal/insect/bug").getValue());
assertEquals("mismatch exact prefix", "5", p.getMatch("/animal").getValue());
assertEquals("mismatch exact prefix", "5", p.getMatch("/animal/").getValue());
assertEquals("Match longest suffix", "0", p.getMatch("/suffix/path.tar.gz").getValue());
assertEquals("Match longest suffix", "0", p.getMatch("/suffix/path.gz").getValue());
assertEquals("prefix rather than suffix", "5", p.getMatch("/animal/path.gz").getValue());
assertEquals("default", "0", p.getMatch("/Other/path").getValue());
assertEquals("pathMatch /*", "", PathMap.pathMatch("/*", "/xxx/zzz"));
assertEquals("pathInfo /*", "/xxx/zzz", PathMap.pathInfo("/*", "/xxx/zzz"));
assertTrue("match /", PathMap.match("/", "/anything"));
assertTrue("match /*", PathMap.match("/*", "/anything"));
assertTrue("match /foo", PathMap.match("/foo", "/foo"));
assertTrue("!match /foo", !PathMap.match("/foo", "/bar"));
assertTrue("match /foo/*", PathMap.match("/foo/*", "/foo"));
assertTrue("match /foo/*", PathMap.match("/foo/*", "/foo/"));
assertTrue("match /foo/*", PathMap.match("/foo/*", "/foo/anything"));
assertTrue("!match /foo/*", !PathMap.match("/foo/*", "/bar"));
assertTrue("!match /foo/*", !PathMap.match("/foo/*", "/bar/"));
assertTrue("!match /foo/*", !PathMap.match("/foo/*", "/bar/anything"));
assertTrue("match *.foo", PathMap.match("*.foo", "anything.foo"));
assertTrue("!match *.foo", !PathMap.match("*.foo", "anything.bar"));
assertEquals("match / with ''", "10", p.getMatch("/").getValue());
assertTrue("match \"\"", PathMap.match("", "/"));
}
/**
* See JIRA issue: JETTY-88.
* @throws Exception failed test
*/
@Test
public void testPathMappingsOnlyMatchOnDirectoryNames() throws Exception
{
String spec = "/xyz/*";
assertMatch(spec, "/xyz");
assertMatch(spec, "/xyz/");
assertMatch(spec, "/xyz/123");
assertMatch(spec, "/xyz/123/");
assertMatch(spec, "/xyz/123.txt");
assertNotMatch(spec, "/xyz123");
assertNotMatch(spec, "/xyz123;jessionid=99");
assertNotMatch(spec, "/xyz123/");
assertNotMatch(spec, "/xyz123/456");
assertNotMatch(spec, "/xyz.123");
assertNotMatch(spec, "/xyz;123"); // as if the ; was encoded and part of the path
assertNotMatch(spec, "/xyz?123"); // as if the ? was encoded and part of the path
}
@Test
public void testPrecidenceVsOrdering() throws Exception
{
PathMap<String> p = new PathMap<>();
p.put("/dump/gzip/*","prefix");
p.put("*.txt","suffix");
assertEquals(null,p.getMatch("/foo/bar"));
assertEquals("prefix",p.getMatch("/dump/gzip/something").getValue());
assertEquals("suffix",p.getMatch("/foo/something.txt").getValue());
assertEquals("prefix",p.getMatch("/dump/gzip/something.txt").getValue());
p = new PathMap<>();
p.put("*.txt","suffix");
p.put("/dump/gzip/*","prefix");
assertEquals(null,p.getMatch("/foo/bar"));
assertEquals("prefix",p.getMatch("/dump/gzip/something").getValue());
assertEquals("suffix",p.getMatch("/foo/something.txt").getValue());
assertEquals("prefix",p.getMatch("/dump/gzip/something.txt").getValue());
}
private void assertMatch(String spec, String path)
{
boolean match = PathMap.match(spec, path);
assertTrue("PathSpec '" + spec + "' should match path '" + path + "'", match);
}
private void assertNotMatch(String spec, String path)
{
boolean match = PathMap.match(spec, path);
assertFalse("PathSpec '" + spec + "' should not match path '" + path + "'", match);
}
}

View File

@ -23,10 +23,10 @@ import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.PathMap;
import org.eclipse.jetty.http.pathmap.ServletPathSpec;
/**
* Abstract rule that use a {@link PathMap} for pattern matching. It uses the
* Abstract rule that use a {@link ServletPathSpec} for pattern matching. It uses the
* servlet pattern syntax.
*/
public abstract class PatternRule extends Rule
@ -69,7 +69,7 @@ public abstract class PatternRule extends Rule
@Override
public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
{
if (PathMap.match(_pattern, target))
if (ServletPathSpec.match(_pattern, target))
{
return apply(target,request, response);
}

View File

@ -37,7 +37,7 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
* There is also handling for cookies, headers, redirection, setting status or error codes
* whenever the rule finds a match.
*
* <p> The rules can be matched by the either: pattern matching of PathMap
* <p> The rules can be matched by the either: pattern matching of @{@link org.eclipse.jetty.http.pathmap.ServletPathSpec}
* (eg {@link PatternRule}), regular expressions (eg {@link RegexRule}) or certain conditions set
* (eg {@link MsieSslRule} - the requests must be in SSL mode).
*

View File

@ -23,13 +23,13 @@ import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.PathMap;
import org.eclipse.jetty.http.pathmap.ServletPathSpec;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.annotation.Name;
/**
* Rewrite the URI by replacing the matched {@link PathMap} path with a fixed string.
* Rewrite the URI by replacing the matched {@link ServletPathSpec} path with a fixed string.
*/
public class RewritePatternRule extends PatternRule implements Rule.ApplyURI
{
@ -77,7 +77,7 @@ public class RewritePatternRule extends PatternRule implements Rule.ApplyURI
@Override
public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
{
target = URIUtil.addPaths(_replacement, PathMap.pathInfo(_pattern, target));
target = URIUtil.addPaths(_replacement, ServletPathSpec.pathInfo(_pattern, target));
return target;
}

View File

@ -39,7 +39,8 @@ import javax.servlet.annotation.ServletSecurity.EmptyRoleSemantic;
import javax.servlet.annotation.ServletSecurity.TransportGuarantee;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.PathMap;
import org.eclipse.jetty.http.pathmap.MappedResource;
import org.eclipse.jetty.http.pathmap.PathMappings;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
@ -65,7 +66,7 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr
private static final String ALL_METHODS = "*";
private final List<ConstraintMapping> _constraintMappings= new CopyOnWriteArrayList<>();
private final Set<String> _roles = new CopyOnWriteArraySet<>();
private final PathMap<Map<String, RoleInfo>> _constraintMap = new PathMap<>();
private final PathMappings<Map<String, RoleInfo>> _constraintRoles = new PathMappings<>();
private boolean _denyUncoveredMethods = false;
@ -415,9 +416,9 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr
if (isStarted() && modified)
{
// Add the new role to currently defined any role role infos
for (Map<String,RoleInfo> map : _constraintMap.values())
for (MappedResource<Map<String, RoleInfo >> map : _constraintRoles)
{
for (RoleInfo info : map.values())
for (RoleInfo info : map.getResource().values())
{
if (info.isAnyRole())
info.addRole(role);
@ -433,7 +434,7 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr
@Override
protected void doStart() throws Exception
{
_constraintMap.clear();
_constraintRoles.reset();
if (_constraintMappings!=null)
{
for (ConstraintMapping mapping : _constraintMappings)
@ -454,7 +455,7 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr
protected void doStop() throws Exception
{
super.doStop();
_constraintMap.clear();
_constraintRoles.reset();
}
@ -467,11 +468,11 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr
*/
protected void processConstraintMapping(ConstraintMapping mapping)
{
Map<String, RoleInfo> mappings = _constraintMap.get(mapping.getPathSpec());
Map<String, RoleInfo> mappings = _constraintRoles.get(PathMappings.asPathSpec(mapping.getPathSpec()));
if (mappings == null)
{
mappings = new HashMap<String,RoleInfo>();
_constraintMap.put(mapping.getPathSpec(),mappings);
_constraintRoles.put(mapping.getPathSpec(), mappings);
}
RoleInfo allMethodsRoleInfo = mappings.get(ALL_METHODS);
if (allMethodsRoleInfo != null && allMethodsRoleInfo.isForbidden())
@ -612,53 +613,53 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr
@Override
protected RoleInfo prepareConstraintInfo(String pathInContext, Request request)
{
Map<String, RoleInfo> mappings = _constraintMap.match(pathInContext);
MappedResource<Map<String, RoleInfo>> resource = _constraintRoles.getMatch(pathInContext);
if (resource==null)
return null;
if (mappings != null)
Map<String, RoleInfo> mappings = resource.getResource();
if (mappings==null)
return null;
String httpMethod = request.getMethod();
RoleInfo roleInfo = mappings.get(httpMethod);
if (roleInfo == null)
{
String httpMethod = request.getMethod();
RoleInfo roleInfo = mappings.get(httpMethod);
if (roleInfo == null)
//No specific http-method names matched
List<RoleInfo> applicableConstraints = new ArrayList<RoleInfo>();
//Get info for constraint that matches all methods if it exists
RoleInfo all = mappings.get(ALL_METHODS);
if (all != null)
applicableConstraints.add(all);
//Get info for constraints that name method omissions where target method name is not omitted
//(ie matches because target method is not omitted, hence considered covered by the constraint)
for (Entry<String, RoleInfo> entry: mappings.entrySet())
{
//No specific http-method names matched
List<RoleInfo> applicableConstraints = new ArrayList<RoleInfo>();
//Get info for constraint that matches all methods if it exists
RoleInfo all = mappings.get(ALL_METHODS);
if (all != null)
applicableConstraints.add(all);
//Get info for constraints that name method omissions where target method name is not omitted
//(ie matches because target method is not omitted, hence considered covered by the constraint)
for (Entry<String, RoleInfo> entry: mappings.entrySet())
{
if (entry.getKey() != null && entry.getKey().endsWith(OMISSION_SUFFIX) && ! entry.getKey().contains(httpMethod))
applicableConstraints.add(entry.getValue());
}
if (applicableConstraints.size() == 0 && isDenyUncoveredHttpMethods())
{
roleInfo = new RoleInfo();
roleInfo.setForbidden(true);
}
else if (applicableConstraints.size() == 1)
roleInfo = applicableConstraints.get(0);
else
{
roleInfo = new RoleInfo();
roleInfo.setUserDataConstraint(UserDataConstraint.None);
for (RoleInfo r:applicableConstraints)
roleInfo.combine(r);
}
if (entry.getKey() != null && entry.getKey().endsWith(OMISSION_SUFFIX) && ! entry.getKey().contains(httpMethod))
applicableConstraints.add(entry.getValue());
}
return roleInfo;
if (applicableConstraints.size() == 0 && isDenyUncoveredHttpMethods())
{
roleInfo = new RoleInfo();
roleInfo.setForbidden(true);
}
else if (applicableConstraints.size() == 1)
roleInfo = applicableConstraints.get(0);
else
{
roleInfo = new RoleInfo();
roleInfo.setUserDataConstraint(UserDataConstraint.None);
for (RoleInfo r:applicableConstraints)
roleInfo.combine(r);
}
}
return null;
return roleInfo;
}
@Override
@ -766,12 +767,12 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr
public void dump(Appendable out,String indent) throws IOException
{
// TODO these should all be beans
dumpBeans(out,indent,
Collections.singleton(getLoginService()),
Collections.singleton(getIdentityService()),
Collections.singleton(getAuthenticator()),
Collections.singleton(_roles),
_constraintMap.entrySet());
dumpBeans(out, indent,
Collections.singleton(getLoginService()),
Collections.singleton(getIdentityService()),
Collections.singleton(getAuthenticator()),
Collections.singleton(_roles),
_constraintRoles.getMappings());
}
/* ------------------------------------------------------------ */
@ -829,9 +830,10 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr
Set<String> uncoveredPaths = new HashSet<String>();
for (String path:_constraintMap.keySet())
for (MappedResource<Map<String, RoleInfo>> resource: _constraintRoles)
{
Map<String, RoleInfo> methodMappings = _constraintMap.get(path);
String path = resource.getPathSpec().getDeclaration();
Map<String, RoleInfo> methodMappings = resource.getResource();
//Each key is either:
// : an exact method name
// : * which means that the constraint applies to every method

View File

@ -23,7 +23,7 @@ import java.util.EnumSet;
import javax.servlet.DispatcherType;
import org.eclipse.jetty.http.PathMap;
import org.eclipse.jetty.http.pathmap.ServletPathSpec;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
@ -112,7 +112,7 @@ public class FilterMapping implements Dumpable
if (appliesTo(type))
{
for (int i=0;i<_pathSpecs.length;i++)
if (_pathSpecs[i]!=null && PathMap.match(_pathSpecs[i], path,true))
if (_pathSpecs[i]!=null && ServletPathSpec.match(_pathSpecs[i], path, true))
return true;
}

View File

@ -113,7 +113,6 @@ public class ServletHandler extends ScopedHandler
private MultiMap<FilterMapping> _filterNameMappings;
private final Map<String,ServletHolder> _servletNameMap=new HashMap<>();
// private PathMap<ServletHolder> _servletPathMap;
private PathMappings<ServletHolder> _servletPathMap;
private ListenerHolder[] _listeners=new ListenerHolder[0];