Skip to content

Commit

Permalink
484350 - Allow GzipHandler path include/exclude to use regex
Browse files Browse the repository at this point in the history
+ 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
  • Loading branch information
joakime committed Dec 15, 2015
1 parent 77d4b54 commit 6e0ad42
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 36 deletions.
10 changes: 7 additions & 3 deletions jetty-http/src/main/java/org/eclipse/jetty/http/PathMap.java
Expand Up @@ -29,6 +29,7 @@

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;

Expand Down Expand Up @@ -573,7 +574,7 @@ void setMapped(String mapped)
}
}

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<>();

Expand Down Expand Up @@ -607,13 +608,16 @@ 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);
}

@Override
public boolean matches(String item)
{
return _map.containsMatch(item);
Expand Down
@@ -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();
}
}
Expand Up @@ -36,7 +36,7 @@

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;
Expand Down Expand Up @@ -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<>();

/* ------------------------------------------------------------ */
Expand Down Expand Up @@ -136,9 +136,27 @@ public void addExcludedMimeTypes(String... types)

/* ------------------------------------------------------------ */
/**
* 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.
*/
Expand Down Expand Up @@ -183,12 +201,27 @@ public void addIncludedMimeTypes(String... types)

/* ------------------------------------------------------------ */
/**
* 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)
{
Expand Down
Expand Up @@ -18,14 +18,18 @@

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;
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;
Expand Down Expand Up @@ -204,4 +208,16 @@ public void testIncludeGzipHandler() throws Exception

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.*$"));
}
}

0 comments on commit 6e0ad42

Please sign in to comment.