484350 - Allow GzipHandler path include/exclude to use regex

+ Overhauled IncludeExclude to use java 8 predicate
+ Introduced PathSpecSet to standardize path IncludeExclude
+ GzipHandler now uses PathSpecSet for paths
This commit is contained in:
Joakim Erdfelt 2015-12-14 14:58:12 -07:00
parent 1df5a05ee1
commit b6df9508c6
7 changed files with 378 additions and 43 deletions

View File

@ -27,6 +27,7 @@ import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import org.eclipse.jetty.util.ArrayTernaryTrie;
import org.eclipse.jetty.util.RegexSet;
@ -592,9 +593,8 @@ public class PathMap<O> extends HashMap<String,O>
}
}
public static class PathSet extends AbstractSet<String>
public static class PathSet extends AbstractSet<String> implements Predicate<String>
{
public static final BiFunction<PathSet,String,Boolean> MATCHER=(s,e)->{return s.containsMatch(e);};
private final PathMap<Boolean> _map = new PathMap<>();
@Override
@ -627,12 +627,15 @@ public class PathMap<O> extends HashMap<String,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

@ -0,0 +1,216 @@
//
// ========================================================================
// Copyright (c) 1995-2015 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.pathmap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Predicate;
/**
* A Set of PathSpec strings.
* <p>
* Used by {@link org.eclipse.jetty.util.IncludeExclude} logic
*/
public class PathSpecSet implements Set<String>, Predicate<String>
{
private final Set<PathSpec> specs = new TreeSet<>();
@Override
public boolean test(String s)
{
for (PathSpec spec : specs)
{
if (spec.matches(s))
{
return true;
}
}
return false;
}
@Override
public boolean isEmpty()
{
return specs.isEmpty();
}
@Override
public Iterator<String> iterator()
{
return new Iterator<String>()
{
private Iterator<PathSpec> iter = specs.iterator();
@Override
public boolean hasNext()
{
return iter.hasNext();
}
@Override
public String next()
{
PathSpec spec = iter.next();
if (spec == null)
{
return null;
}
return spec.getDeclaration();
}
};
}
@Override
public int size()
{
return specs.size();
}
@Override
public boolean contains(Object o)
{
if (o instanceof PathSpec)
{
return specs.contains(o);
}
if (o instanceof String)
{
return specs.contains(toPathSpec((String)o));
}
return false;
}
private PathSpec asPathSpec(Object o)
{
if (o == null)
{
return null;
}
if (o instanceof PathSpec)
{
return (PathSpec)o;
}
if (o instanceof String)
{
return toPathSpec((String)o);
}
return toPathSpec(o.toString());
}
private PathSpec toPathSpec(String rawSpec)
{
if ((rawSpec == null) || (rawSpec.length() < 1))
{
throw new RuntimeException("Path Spec String must start with '^', '/', or '*.': got [" + rawSpec + "]");
}
if (rawSpec.charAt(0) == '^')
{
return new RegexPathSpec(rawSpec);
}
else
{
return new ServletPathSpec(rawSpec);
}
}
@Override
public Object[] toArray()
{
return toArray(new String[specs.size()]);
}
@Override
public <T> T[] toArray(T[] a)
{
int i = 0;
for (PathSpec spec : specs)
{
a[i++] = (T)spec.getDeclaration();
}
return a;
}
@Override
public boolean add(String e)
{
return specs.add(toPathSpec(e));
}
@Override
public boolean remove(Object o)
{
return specs.remove(asPathSpec(o));
}
@Override
public boolean containsAll(Collection<?> coll)
{
for (Object o : coll)
{
if (!specs.contains(asPathSpec(o)))
return false;
}
return true;
}
@Override
public boolean addAll(Collection<? extends String> coll)
{
boolean ret = false;
for (String s : coll)
{
ret |= add(s);
}
return ret;
}
@Override
public boolean retainAll(Collection<?> coll)
{
List<PathSpec> collSpecs = new ArrayList<>();
for (Object o : coll)
{
collSpecs.add(asPathSpec(o));
}
return specs.retainAll(collSpecs);
}
@Override
public boolean removeAll(Collection<?> coll)
{
List<PathSpec> collSpecs = new ArrayList<>();
for (Object o : coll)
{
collSpecs.add(asPathSpec(o));
}
return specs.removeAll(collSpecs);
}
@Override
public void clear()
{
specs.clear();
}
}

View File

@ -36,7 +36,7 @@ import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.http.PathMap;
import org.eclipse.jetty.http.pathmap.PathSpecSet;
import org.eclipse.jetty.server.HttpOutput;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.HandlerWrapper;
@ -72,9 +72,9 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
// non-static, as other GzipHandler instances may have different configurations
private final ThreadLocal<Deflater> _deflater = new ThreadLocal<Deflater>();
private final IncludeExclude<String> _agentPatterns=new IncludeExclude<>(RegexSet.class,RegexSet.MATCHER);
private final IncludeExclude<String> _agentPatterns=new IncludeExclude<>(RegexSet.class);
private final IncludeExclude<String> _methods = new IncludeExclude<>();
private final IncludeExclude<String> _paths = new IncludeExclude<>(PathMap.PathSet.class,PathMap.PathSet.MATCHER);
private final IncludeExclude<String> _paths = new IncludeExclude<String>(PathSpecSet.class);
private final IncludeExclude<String> _mimeTypes = new IncludeExclude<>();
private HttpField _vary;
@ -144,9 +144,27 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
/* ------------------------------------------------------------ */
/**
* Add path to excluded paths list.
* <p>
* There are 2 syntaxes supported, Servlet <code>url-pattern</code> based, and
* Regex based. This means that the initial characters on the path spec
* line are very strict, and determine the behavior of the path matching.
* <ul>
* <li>If the spec starts with <code>'^'</code> the spec is assumed to be
* a regex based path spec and will match with normal Java regex rules.</li>
* <li>If the spec starts with <code>'/'</code> then spec is assumed to be
* a Servlet url-pattern rules path spec for either an exact match
* or prefix based match.</li>
* <li>If the spec starts with <code>'*.'</code> then spec is assumed to be
* a Servlet url-pattern rules path spec for a suffix based match.</li>
* <li>All other syntaxes are unsupported</li>
* </ul>
* <p>
* Note: inclusion takes precedence over exclude.
*
* @param pathspecs Path specs (as per servlet spec) to exclude. If a
* ServletContext is available, the paths are relative to the context path,
* otherwise they are absolute.
* otherwise they are absolute.<br>
* For backward compatibility the pathspecs may be comma separated strings, but this
* will not be supported in future versions.
*/
@ -191,12 +209,27 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
/* ------------------------------------------------------------ */
/**
* Add path specs to include. Inclusion takes precedence over exclusion.
* Add path specs to include.
* <p>
* There are 2 syntaxes supported, Servlet <code>url-pattern</code> based, and
* Regex based. This means that the initial characters on the path spec
* line are very strict, and determine the behavior of the path matching.
* <ul>
* <li>If the spec starts with <code>'^'</code> the spec is assumed to be
* a regex based path spec and will match with normal Java regex rules.</li>
* <li>If the spec starts with <code>'/'</code> then spec is assumed to be
* a Servlet url-pattern rules path spec for either an exact match
* or prefix based match.</li>
* <li>If the spec starts with <code>'*.'</code> then spec is assumed to be
* a Servlet url-pattern rules path spec for a suffix based match.</li>
* <li>All other syntaxes are unsupported</li>
* </ul>
* <p>
* Note: inclusion takes precedence over exclude.
*
* @param pathspecs Path specs (as per servlet spec) to include. If a
* ServletContext is available, the paths are relative to the context path,
* otherwise they are absolute
* For backward compatibility the pathspecs may be comma separated strings, but this
* will not be supported in future versions.
*/
public void addIncludedPaths(String... pathspecs)
{
@ -334,9 +367,9 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
/* ------------------------------------------------------------ */
/**
* Get the minimum reponse size.
* Get the minimum response size.
*
* @return minimum reponse size
* @return minimum response size
*/
public int getMinGzipSize()
{

View File

@ -0,0 +1,42 @@
//
// ========================================================================
// Copyright (c) 1995-2015 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.server.handler.gzip;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import org.junit.Test;
public class GzipHandlerTest
{
@Test
public void testAddGetPaths()
{
GzipHandler gzip = new GzipHandler();
gzip.addIncludedPaths("/foo");
gzip.addIncludedPaths("^/bar.*$");
String[] includedPaths = gzip.getIncludedPaths();
assertThat("Included Paths.size", includedPaths.length, is(2));
assertThat("Included Paths", Arrays.asList(includedPaths), contains("/foo","^/bar.*$"));
}
}

View File

@ -20,7 +20,7 @@ package org.eclipse.jetty.util;
import java.util.HashSet;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Predicate;
/** Utility class to maintain a set of inclusions and exclusions.
@ -36,35 +36,77 @@ public class IncludeExclude<ITEM>
{
private final Set<ITEM> _includes;
private final Set<ITEM> _excludes;
private final BiFunction<Set<ITEM>,ITEM, Boolean> _matcher;
private final Predicate<ITEM> _includePredicate;
private final Predicate<ITEM> _excludePredicate;
private static class SetContainsPredicate<ITEM> implements Predicate<ITEM>
{
private final Set<ITEM> set;
public SetContainsPredicate(Set<ITEM> set)
{
this.set = set;
}
@Override
public boolean test(ITEM item)
{
return set.contains(item);
}
}
/**
* Default constructor over {@link HashSet}
*/
public IncludeExclude()
{
this(HashSet.class,null);
this(HashSet.class);
}
/**
* Construct an IncludeExclude
* @param setClass The type of {@link Set} to using internally
* @param matcher A function to test if a passed ITEM is matched by the passed SET, or null to use {@link Set#contains(Object)}
* @param predicate A predicate function to test if a passed ITEM is matched by the passed SET}
*/
public <SET extends Set<ITEM>> IncludeExclude(Class<SET> setClass, BiFunction<SET,ITEM, Boolean> matcher)
public <SET extends Set<ITEM>> IncludeExclude(Class<SET> setClass)
{
try
{
_includes = setClass.newInstance();
_excludes = setClass.newInstance();
_matcher = (BiFunction<Set<ITEM>,ITEM, Boolean>)matcher;
if(_includes instanceof Predicate) {
_includePredicate = (Predicate<ITEM>)_includes;
} else {
_includePredicate = new SetContainsPredicate<>(_includes);
}
if(_excludes instanceof Predicate) {
_excludePredicate = (Predicate<ITEM>)_excludes;
} else {
_excludePredicate = new SetContainsPredicate<>(_excludes);
}
}
catch (InstantiationException | IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
/**
* Construct an IncludeExclude
*
* @param includeSet the Set of items that represent the included space
* @param includePredicate the Predicate for included item testing (null for simple {@link Set#contains(Object)} test)
* @param excludeSet the Set of items that represent the excluded space
* @param excludePredicate the Predicate for excluded item testing (null for simple {@link Set#contains(Object)} test)
*/
public <SET extends Set<ITEM>> IncludeExclude(Set<ITEM> includeSet, Predicate<ITEM> includePredicate, Set<ITEM> excludeSet, Predicate<ITEM> excludePredicate)
{
_includes = includeSet;
_includePredicate = includePredicate;
_excludes = excludeSet;
_excludePredicate = excludePredicate;
}
public void include(ITEM element)
{
_includes.add(element);
@ -89,17 +131,11 @@ public class IncludeExclude<ITEM>
public boolean matches(ITEM e)
{
if (_matcher==null)
{
if (_includes.size()>0 && !_includes.contains(e))
return false;
return !_excludes.contains(e);
}
if (_includes.size()>0 && !_matcher.apply(_includes,e))
if (!_includes.isEmpty() && !_includePredicate.test(e))
return false;
return !_matcher.apply(_excludes,e);
return !_excludePredicate.test(e);
}
public int size()
{
return _includes.size()+_excludes.size();
@ -124,6 +160,6 @@ public class IncludeExclude<ITEM>
@Override
public String toString()
{
return String.format("%s@%x{i=%s,e=%s,m=%s}",this.getClass().getSimpleName(),hashCode(),_includes,_excludes,_matcher);
return String.format("%s@%x{i=%s,ip=%s,e=%s,ep=%s}",this.getClass().getSimpleName(),hashCode(),_includes,_includePredicate,_excludes,_excludePredicate);
}
}

View File

@ -24,6 +24,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.regex.Pattern;
@ -32,9 +33,8 @@ import java.util.regex.Pattern;
* <p>
* Provides the efficient {@link #matches(String)} method to check for a match against all the combined Regex's
*/
public class RegexSet extends AbstractSet<String>
public class RegexSet extends AbstractSet<String> implements Predicate<String>
{
public static final BiFunction<RegexSet,String,Boolean> MATCHER=(rs,p)->{return rs.matches(p);};
private final Set<String> _patterns=new HashSet<String>();
private final Set<String> _unmodifiable=Collections.unmodifiableSet(_patterns);
private Pattern _pattern;
@ -98,6 +98,12 @@ public class RegexSet extends AbstractSet<String>
builder.append(")$");
_pattern = Pattern.compile(builder.toString());
}
@Override
public boolean test(String s)
{
return _pattern!=null && _pattern.matcher(s).matches();
}
public boolean matches(String s)
{

View File

@ -20,7 +20,9 @@ package org.eclipse.jetty.util;
import org.junit.Test;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class IncludeExcludeTest
{
@ -29,8 +31,8 @@ public class IncludeExcludeTest
{
IncludeExclude<String> ie = new IncludeExclude<>();
assertEquals(0,ie.size());
assertEquals(true,ie.matches("foo"));
assertThat("Empty IncludeExclude", ie.size(), is(0));
assertThat("Matches 'foo'",ie.matches("foo"),is(true));
}
@Test
@ -40,7 +42,7 @@ public class IncludeExcludeTest
ie.include("foo");
ie.include("bar");
assertEquals(2,ie.size());
assertThat("IncludeExclude.size", ie.size(), is(2));
assertEquals(false,ie.matches(""));
assertEquals(true,ie.matches("foo"));
assertEquals(true,ie.matches("bar"));
@ -86,7 +88,7 @@ public class IncludeExcludeTest
@Test
public void testEmptyRegex()
{
IncludeExclude<String> ie = new IncludeExclude<>(RegexSet.class,RegexSet.MATCHER);
IncludeExclude<String> ie = new IncludeExclude<>(RegexSet.class);
assertEquals(0,ie.size());
assertEquals(true,ie.matches("foo"));
@ -95,7 +97,7 @@ public class IncludeExcludeTest
@Test
public void testIncludeRegex()
{
IncludeExclude<String> ie = new IncludeExclude<>(RegexSet.class,RegexSet.MATCHER);
IncludeExclude<String> ie = new IncludeExclude<>(RegexSet.class);
ie.include("f..");
ie.include("b((ar)|(oo))");
@ -112,7 +114,7 @@ public class IncludeExcludeTest
@Test
public void testExcludeRegex()
{
IncludeExclude<String> ie = new IncludeExclude<>(RegexSet.class,RegexSet.MATCHER);
IncludeExclude<String> ie = new IncludeExclude<>(RegexSet.class);
ie.exclude("f..");
ie.exclude("b((ar)|(oo))");
@ -130,7 +132,7 @@ public class IncludeExcludeTest
@Test
public void testIncludeExcludeRegex()
{
IncludeExclude<String> ie = new IncludeExclude<>(RegexSet.class,RegexSet.MATCHER);
IncludeExclude<String> ie = new IncludeExclude<>(RegexSet.class);
ie.include(".*[aeiou].*");
ie.include("[AEIOU].*");
ie.exclude("f..");
@ -146,8 +148,5 @@ public class IncludeExcludeTest
assertEquals(true,ie.matches("foobar"));
assertEquals(true,ie.matches("Ant"));
}
}