Applying suggestions from @gregw in issue #8749

This commit is contained in:
Joakim Erdfelt 2022-10-20 16:30:58 -05:00
parent f359023f06
commit 0b542dc29b
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
3 changed files with 41 additions and 2 deletions

View File

@ -150,6 +150,12 @@ public interface Request extends Attributes, Content.Source
*/
Context getContext();
default String getContextPath()
{
Context context = getContext();
return context == null ? null : context.getContextPath();
}
/**
* TODO see discussion in #7713, as this path should probably be canonically encoded - ie everything but %25 and %2F decoded
* @return The part of the decoded path of the URI after any context path prefix has been removed.
@ -509,6 +515,35 @@ public interface Request extends Attributes, Content.Source
return getWrapped().getContext();
}
@Override
public String getContextPath()
{
Context context = getContext();
if (context == null)
return null;
Request.Wrapper wrapper = this;
String contextPath = context.getContextPath();
while (wrapper != null)
{
Request wrapped = wrapper.getWrapped();
Context outer = wrapped.getContext();
if (context != outer)
{
if (outer == null || outer instanceof Server.ServerContext)
return contextPath;
contextPath = URIUtil.addPaths(outer.getContextPath(), contextPath);
context = outer;
}
wrapper = wrapped instanceof Request.Wrapper w ? w : null;
}
return contextPath;
}
@Override
public String getPathInContext()
{

View File

@ -712,7 +712,7 @@ public class Server extends Handler.Wrapper implements Attributes
private static class DynamicErrorProcessor extends ErrorProcessor {}
private class ServerContext extends Attributes.Wrapper implements Context
public class ServerContext extends Attributes.Wrapper implements Context
{
private ServerContext()
{

View File

@ -981,7 +981,11 @@ public class ContextHandler extends Handler.Wrapper implements Attributes, Grace
@Override
public String getContextPath()
{
return _contextPath;
Context context = getContext();
Request.Wrapper wrapper = this;
String contextPath = context.getContextPath();
Context outer = wrapper.getWrapped().getContext();
return (outer instanceof Server.ServerContext) ? contextPath : null;
}
@Override