Skip to content

Commit

Permalink
Fix #6305 Optimise isProtectedTarget
Browse files Browse the repository at this point in the history
updates from review

Signed-off-by: Greg Wilkins <[email protected]>
  • Loading branch information
gregw committed May 21, 2021
1 parent ad77b59 commit 9ae227a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ public enum ContextStatus
DESTROYED
}

/**
* The type of protected target match
* @see #_protectedTargets
*/
private enum ProtectedTargetType
{
EXACT,
PREFIX
}

protected ContextStatus _contextStatus = ContextStatus.NOTSET;
protected Context _scontext;
private final AttributesMap _attributes;
Expand Down Expand Up @@ -215,7 +225,7 @@ public enum ContextStatus
private final List<ServletRequestAttributeListener> _servletRequestAttributeListeners = new CopyOnWriteArrayList<>();
private final List<ContextScopeListener> _contextListeners = new CopyOnWriteArrayList<>();
private final Set<EventListener> _durableListeners = new HashSet<>();
private Index<Boolean> _protectedTargets = Index.empty(false);
private Index<ProtectedTargetType> _protectedTargets = Index.empty(false);
private final CopyOnWriteArrayList<AliasCheck> _aliasChecks = new CopyOnWriteArrayList<>();

public enum Availability
Expand Down Expand Up @@ -1482,36 +1492,30 @@ public boolean isProtectedTarget(String target)
// ignore empty segments which may be discard by file system
target = URIUtil.compactPath(target);

Boolean exact = _protectedTargets.getBest(target);
ProtectedTargetType type = _protectedTargets.getBest(target);

if (exact == null)
// Not protected
return false;
if (exact)
// protected if exact length
return _protectedTargets.get(target) == Boolean.TRUE;
// protected prefix
return true;
return type == ProtectedTargetType.PREFIX ||
type == ProtectedTargetType.EXACT && _protectedTargets.get(target) == ProtectedTargetType.EXACT;
}

/**
* @param targets Array of URL prefix. Each prefix is in the form /path and will match either /path exactly or /path/anything
*/
public void setProtectedTargets(String[] targets)
{
Index.Builder<Boolean> builder = new Index.Builder<>();
Index.Builder<ProtectedTargetType> builder = new Index.Builder<>();
if (targets != null)
{
for (String t : targets)
{
if (!t.startsWith("/") || t.indexOf('/', 2) > 0)
if (!t.startsWith("/"))
throw new IllegalArgumentException("Bad protected target: " + t);
// Index to TRUE if exact match required
builder.with(t, Boolean.TRUE);

// Index to FALSE for prefix matches
for (String s : new String[] {"/", "?", "#", ";"})
builder.with(t + s, Boolean.FALSE);
builder.with(t, ProtectedTargetType.EXACT);
builder.with(t + "/", ProtectedTargetType.PREFIX);
builder.with(t + "?", ProtectedTargetType.PREFIX);
builder.with(t + "#", ProtectedTargetType.PREFIX);
builder.with(t + ";", ProtectedTargetType.PREFIX);
}
}
_protectedTargets = builder.caseSensitive(false).build();
Expand All @@ -1523,7 +1527,7 @@ public String[] getProtectedTargets()
return null;

return _protectedTargets.keySet().stream()
.filter(_protectedTargets::get)
.filter(s -> _protectedTargets.get(s) == ProtectedTargetType.EXACT)
.toArray(String[]::new);
}

Expand Down
3 changes: 2 additions & 1 deletion jetty-util/src/main/java/org/eclipse/jetty/util/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public interface Index<V>
V getBest(String s, int offset, int len);

/**
* Get the best match from key in a String.
* Get the best match from key in a String, which may be
* a prefix match or an exact match.
*
* @param s The string
* @return The value or null if not found
Expand Down

0 comments on commit 9ae227a

Please sign in to comment.