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 Conflicts: jetty-http/src/main/java/org/eclipse/jetty/http/PathMap.java jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/GzipHandler.java jetty-util/src/main/java/org/eclipse/jetty/util/IncludeExclude.java jetty-util/src/main/java/org/eclipse/jetty/util/RegexSet.java
This commit is contained in:
parent
77d4b54082
commit
6e0ad429d9
|
@ -29,6 +29,7 @@ import java.util.StringTokenizer;
|
|||
|
||||
import org.eclipse.jetty.util.ArrayTernaryTrie;
|
||||
import org.eclipse.jetty.util.IncludeExclude;
|
||||
import org.eclipse.jetty.util.Predicate;
|
||||
import org.eclipse.jetty.util.Trie;
|
||||
import org.eclipse.jetty.util.URIUtil;
|
||||
|
||||
|
@ -573,7 +574,7 @@ public class PathMap<O> extends HashMap<String,O>
|
|||
}
|
||||
}
|
||||
|
||||
public static class PathSet extends AbstractSet<String> implements IncludeExclude.MatchSet<String>
|
||||
public static class PathSet extends AbstractSet<String> implements Predicate<String>
|
||||
{
|
||||
private final PathMap<Boolean> _map = new PathMap<>();
|
||||
|
||||
|
@ -607,13 +608,16 @@ 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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(String item)
|
||||
{
|
||||
return _map.containsMatch(item);
|
||||
|
|
|
@ -0,0 +1,223 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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 org.eclipse.jetty.util.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 void remove()
|
||||
{
|
||||
throw new UnsupportedOperationException("Remove not supported by this Iterator");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
|
@ -36,7 +36,7 @@ import javax.servlet.http.HttpServletResponse;
|
|||
|
||||
import org.eclipse.jetty.http.HttpMethod;
|
||||
import org.eclipse.jetty.http.MimeTypes;
|
||||
import org.eclipse.jetty.http.PathMap;
|
||||
import org.eclipse.jetty.http.pathmap.PathSpecSet;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.server.handler.HandlerWrapper;
|
||||
import org.eclipse.jetty.util.IncludeExclude;
|
||||
|
@ -73,7 +73,7 @@ public class GzipHandler extends HandlerWrapper
|
|||
|
||||
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);
|
||||
private final IncludeExclude<String> _paths = new IncludeExclude<String>(PathSpecSet.class);
|
||||
private final IncludeExclude<String> _mimeTypes = new IncludeExclude<>();
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -136,9 +136,27 @@ public class GzipHandler extends HandlerWrapper
|
|||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
@ -183,12 +201,27 @@ public class GzipHandler extends HandlerWrapper
|
|||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
|
|
|
@ -18,7 +18,10 @@
|
|||
|
||||
package org.eclipse.jetty.servlets.gzip;
|
||||
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
@ -26,6 +29,7 @@ import java.io.ByteArrayOutputStream;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
|
@ -204,4 +208,16 @@ public class GzipHandlerTest
|
|||
|
||||
assertEquals(__icontent, testOut.toString("UTF8"));
|
||||
}
|
||||
|
||||
@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.*$"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,51 +33,81 @@ import java.util.Set;
|
|||
*/
|
||||
public class IncludeExclude<ITEM>
|
||||
{
|
||||
public interface MatchSet<ITEM> extends Set<ITEM>
|
||||
{
|
||||
public boolean matches(ITEM item);
|
||||
}
|
||||
private final Set<ITEM> _includes;
|
||||
private final Predicate<ITEM> _includePredicate;
|
||||
private final Set<ITEM> _excludes;
|
||||
private final Predicate<ITEM> _excludePredicate;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
protected static class ContainsMatchSet<ITEM> extends HashSet<ITEM> implements MatchSet<ITEM>
|
||||
private static class SetContainsPredicate<ITEM> implements Predicate<ITEM>
|
||||
{
|
||||
@Override
|
||||
public boolean matches(ITEM item)
|
||||
private final Set<ITEM> set;
|
||||
|
||||
public SetContainsPredicate(Set<ITEM> set)
|
||||
{
|
||||
return contains(item);
|
||||
this.set = set;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(ITEM item)
|
||||
{
|
||||
return set.contains(item);
|
||||
}
|
||||
}
|
||||
|
||||
private final MatchSet<ITEM> _includes;
|
||||
private final MatchSet<ITEM> _excludes;
|
||||
|
||||
/**
|
||||
* Default constructor over {@link HashSet}
|
||||
*/
|
||||
public IncludeExclude()
|
||||
{
|
||||
_includes = new ContainsMatchSet<ITEM>();
|
||||
_excludes = new ContainsMatchSet<ITEM>();
|
||||
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 IncludeExclude(Class<? extends MatchSet<ITEM>> setClass)
|
||||
public <SET extends Set<ITEM>> IncludeExclude(Class<SET> setClass)
|
||||
{
|
||||
try
|
||||
{
|
||||
_includes = setClass.newInstance();
|
||||
_excludes = setClass.newInstance();
|
||||
|
||||
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);
|
||||
|
@ -102,11 +132,11 @@ public class IncludeExclude<ITEM>
|
|||
|
||||
public boolean matches(ITEM e)
|
||||
{
|
||||
if (_includes.size()>0 && !_includes.matches(e))
|
||||
if (!_includes.isEmpty() && !_includePredicate.test(e))
|
||||
return false;
|
||||
return !_excludes.matches(e);
|
||||
return !_excludePredicate.test(e);
|
||||
}
|
||||
|
||||
|
||||
public int size()
|
||||
{
|
||||
return _includes.size()+_excludes.size();
|
||||
|
@ -131,6 +161,6 @@ public class IncludeExclude<ITEM>
|
|||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return String.format("%s@%x{i=%s,e=%s}",this.getClass().getSimpleName(),hashCode(),_includes,_excludes);
|
||||
return String.format("%s@%x{i=%s,ip=%s,e=%s,ep=%s}",this.getClass().getSimpleName(),hashCode(),_includes,_includePredicate,_excludes,_excludePredicate);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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.util;
|
||||
|
||||
/**
|
||||
* Temporary implementation of Java 8's <code>java.util.function.Predicate</code>
|
||||
* <p>
|
||||
* To be removed for Java 8 only versions of Jetty.
|
||||
*
|
||||
* @param <ITEM> the item to test
|
||||
*/
|
||||
public interface Predicate<ITEM>
|
||||
{
|
||||
boolean test(ITEM item);
|
||||
}
|
|
@ -30,7 +30,7 @@ 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> implements IncludeExclude.MatchSet<String>
|
||||
public class RegexSet extends AbstractSet<String> implements Predicate<String>
|
||||
{
|
||||
private final Set<String> _patterns=new HashSet<String>();
|
||||
private final Set<String> _unmodifiable=Collections.unmodifiableSet(_patterns);
|
||||
|
@ -95,6 +95,12 @@ public class RegexSet extends AbstractSet<String> implements IncludeExclude.Matc
|
|||
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)
|
||||
{
|
||||
|
|
|
@ -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"));
|
||||
|
@ -146,8 +148,5 @@ public class IncludeExcludeTest
|
|||
|
||||
assertEquals(true,ie.matches("foobar"));
|
||||
assertEquals(true,ie.matches("Ant"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue