replaced getPathInContext with static method

updates from review
This commit is contained in:
Greg Wilkins 2022-10-25 21:11:10 +11:00
parent 9dc9eaa711
commit 3b2d4048fd
19 changed files with 71 additions and 51 deletions

View File

@ -898,7 +898,7 @@ public interface HttpURI
{
if (hasAuthority() && !isPathValidForAuthority(path))
throw new IllegalArgumentException("Relative path with authority");
if (!URIUtil.isLegalPath(path))
if (!URIUtil.isValidPath(path))
throw new IllegalArgumentException("Path not correctly encoded: " + path);
_uri = null;
_path = path;

View File

@ -26,17 +26,17 @@ import org.eclipse.jetty.util.URIUtil;
public class CompactPathRule extends Rule
{
@Override
public Request.WrapperProcessor matchAndApply(Request.WrapperProcessor input) throws IOException
public Request.WrapperProcessor matchAndApply(Request.WrapperProcessor request) throws IOException
{
String path = Request.getPathInContext(input);
String path = request.getHttpURI().getCanonicalPath();
String compacted = URIUtil.compactPath(path);
if (path.equals(compacted))
return null;
HttpURI uri = Request.updateHttpURI(input, compacted);
HttpURI uri = Request.newHttpURIFrom(request, compacted);
return new Request.WrapperProcessor(input)
return new Request.WrapperProcessor(request)
{
@Override
public HttpURI getHttpURI()

View File

@ -57,6 +57,12 @@ public interface Context extends Attributes, Decorator, Executor
/** scope the calling thread to the context and request and run the runnable. */
void run(Runnable runnable, Request request);
/** Return a URI path scoped to this context **/
String pathInContext(String path);
/**
* <p>Get a URI path scoped to a context. For example if the context path is "/ctx" then a
* full path of "/ctx/foo/bar" will return "/foo/bar".</p>
* @param fullPath A full URI path
* @return The URI path scoped to the context, or null if the fullPath is not in the context.
* The empty string is returned if the fullPath is exactly the context path.
*/
String getPathInContext(String fullPath);
}

View File

@ -166,13 +166,15 @@ public interface Request extends Attributes, Content.Source
/**
* <p>Get the canonically encoded path of the URI, scoped to the current context.</p>
* <p>The <code>pathInContext</code> represents the targeted resource within the current content for the request.</p>
* <p>For example, when the request has a {@link Context} with {@code contextPath=/ctx} and the requests
* {@link HttpURI} has{@code canonicalPath=/ctx/foo}, then the path in context is {@code /foo}.</p>
* @return The part of the canonically encoded path of the URI after any context path prefix has been removed.
* @see HttpURI#getCanonicalPath()
* @see Context#getContextPath()
*/
static String getPathInContext(Request request)
{
return request.getContext().pathInContext(request.getHttpURI().getCanonicalPath());
return request.getContext().getPathInContext(request.getHttpURI().getCanonicalPath());
}
/**
@ -703,7 +705,13 @@ public interface Request extends Attributes, Content.Source
}
}
static HttpURI updateHttpURI(Request request, String newPathInContext)
/**
* <p>Create a new {@link HttpURI} for a request by replacing the path in context</p>
* @param request The request to base the HttpURI on.
* @param newPathInContext The new path in context for the URI
* @return A new immutable HttpURI for the request with the path replaced (but parameters and query string retained).
*/
static HttpURI newHttpURIFrom(Request request, String newPathInContext)
{
return HttpURI.build(request.getHttpURI())
.path(URIUtil.addPaths(getContextPath(request), newPathInContext))

View File

@ -536,7 +536,6 @@ public class ResourceService
{
// Redirect to the index
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, 0);
// TODO need URI util that handles param and query without reconstructing entire URI with scheme and authority
HttpURI.Mutable uri = HttpURI.build(request.getHttpURI());
uri.path(URIUtil.addPaths(contextPath, welcomeTarget));
return new WelcomeAction(WelcomeActionType.REDIRECT, uri.getPathQuery());

View File

@ -787,9 +787,9 @@ public class Server extends Handler.Wrapper implements Attributes
}
@Override
public String pathInContext(String path)
public String getPathInContext(String fullPath)
{
return path;
return fullPath;
}
}

View File

@ -1168,17 +1168,17 @@ public class ContextHandler extends Handler.Wrapper implements Attributes, Grace
}
@Override
public String pathInContext(String path)
public String getPathInContext(String fullPath)
{
if (_rootContext)
return path;
if (!path.startsWith(_contextPath))
return fullPath;
if (!fullPath.startsWith(_contextPath))
return null;
if (path.length() == _contextPath.length())
if (fullPath.length() == _contextPath.length())
return "";
if (path.charAt(_contextPath.length()) != '/')
if (fullPath.charAt(_contextPath.length()) != '/')
return null;
return path.substring(_contextPath.length());
return fullPath.substring(_contextPath.length());
}
}

View File

@ -102,8 +102,9 @@ public class MovedContextHandler extends ContextHandler
return;
String path = _newContextURL;
if (!_discardPathInfo && Request.getPathInContext(request) != null)
path = URIUtil.addPaths(path, Request.getPathInContext(request));
String pathInContext = Request.getPathInContext(request);
if (!_discardPathInfo && pathInContext != null)
path = URIUtil.addPaths(path, pathInContext);
HttpURI uri = request.getHttpURI();
StringBuilder location = new StringBuilder();

View File

@ -56,7 +56,7 @@ public abstract class ReHandlingErrorProcessor extends ErrorProcessor
if (pathInContext != null)
{
request.setAttribute(ReHandlingErrorProcessor.class.getName(), pathInContext);
HttpURI uri = Request.updateHttpURI(request, pathInContext);
HttpURI uri = Request.newHttpURIFrom(request, pathInContext);
Request.Wrapper wrapper = new ReHandleRequestWrapper(request, uri);
try

View File

@ -92,8 +92,9 @@ public class ResourceHandler extends Handler.Wrapper
for (String welcome : _welcomes)
{
String welcomeInContext = URIUtil.addPaths(Request.getPathInContext(request), welcome);
Resource welcomePath = _resourceBase.resolve(Request.getPathInContext(request)).resolve(welcome);
String pathInContext = Request.getPathInContext(request);
String welcomeInContext = URIUtil.addPaths(pathInContext, welcome);
Resource welcomePath = _resourceBase.resolve(pathInContext).resolve(welcome);
if (Resources.isReadableFile(welcomePath))
return welcomeInContext;
}

View File

@ -90,7 +90,7 @@ public class TryPathsHandler extends Handler.Wrapper
public TryPathsRequest(Request wrapped, String pathInContext)
{
super(wrapped);
_uri = Request.updateHttpURI(wrapped, URIUtil.canonicalPath(pathInContext));
_uri = Request.newHttpURIFrom(wrapped, URIUtil.canonicalPath(pathInContext));
}
@Override

View File

@ -73,21 +73,22 @@ public class ErrorProcessorTest
@Override
public void process(Request request, Response response, Callback callback)
{
if (Request.getPathInContext(request).startsWith("/badmessage/"))
String pathInContext = Request.getPathInContext(request);
if (pathInContext.startsWith("/badmessage/"))
{
int code = Integer.parseInt(Request.getPathInContext(request).substring(Request.getPathInContext(request).lastIndexOf('/') + 1));
int code = Integer.parseInt(pathInContext.substring(pathInContext.lastIndexOf('/') + 1));
throw new BadMessageException(code);
}
// produce an exception with an JSON formatted cause message
if (Request.getPathInContext(request).startsWith("/jsonmessage/"))
if (pathInContext.startsWith("/jsonmessage/"))
{
String message = "\"}, \"glossary\": {\n \"title\": \"example\"\n }\n {\"";
throw new TestException(message);
}
// produce an exception with an XML cause message
if (Request.getPathInContext(request).startsWith("/xmlmessage/"))
if (pathInContext.startsWith("/xmlmessage/"))
{
String message =
"<!DOCTYPE glossary PUBLIC \"-//OASIS//DTD DocBook V3.1//EN\">\n" +
@ -98,14 +99,14 @@ public class ErrorProcessorTest
}
// produce an exception with an HTML cause message
if (Request.getPathInContext(request).startsWith("/htmlmessage/"))
if (pathInContext.startsWith("/htmlmessage/"))
{
String message = "<hr/><script>alert(42)</script>%3Cscript%3E";
throw new TestException(message);
}
// produce an exception with a UTF-8 cause message
if (Request.getPathInContext(request).startsWith("/utf8message/"))
if (pathInContext.startsWith("/utf8message/"))
{
// @checkstyle-disable-check : AvoidEscapedUnicodeCharacters
String message = "Euro is &euro; and \u20AC and %E2%82%AC";
@ -114,12 +115,12 @@ public class ErrorProcessorTest
}
// 200 response
if (Request.getPathInContext(request).startsWith("/ok/"))
if (pathInContext.startsWith("/ok/"))
{
Content.Sink.write(
response,
true,
"%s Error %s : %s%n".formatted(Request.getPathInContext(request), request.getAttribute(ErrorProcessor.ERROR_STATUS), request.getAttribute(ErrorProcessor.ERROR_MESSAGE)),
"%s Error %s : %s%n".formatted(pathInContext, request.getAttribute(ErrorProcessor.ERROR_STATUS), request.getAttribute(ErrorProcessor.ERROR_MESSAGE)),
callback);
return;
}

View File

@ -185,45 +185,45 @@ public interface Attributes
@Override
public Object removeAttribute(String name)
{
return _wrapped.removeAttribute(name);
return getWrapped().removeAttribute(name);
}
@Override
public Object setAttribute(String name, Object attribute)
{
return _wrapped.setAttribute(name, attribute);
return getWrapped().setAttribute(name, attribute);
}
@Override
public Object getAttribute(String name)
{
return _wrapped.getAttribute(name);
return getWrapped().getAttribute(name);
}
@Override
public Set<String> getAttributeNameSet()
{
return _wrapped.getAttributeNameSet();
return getWrapped().getAttributeNameSet();
}
@Override
public void clearAttributes()
{
_wrapped.clearAttributes();
getWrapped().clearAttributes();
}
// TODO: remove? or fix (don't want the wrapped and wrapper to match)
@Override
public int hashCode()
{
return _wrapped.hashCode();
return getWrapped().hashCode();
}
// TODO: remove? or fix (don't want the wrapped and wrapper to match)
@Override
public boolean equals(Object obj)
{
return _wrapped.equals(obj);
return getWrapped().equals(obj);
}
}

View File

@ -698,10 +698,10 @@ public final class URIUtil
}
/**
* @param path The path to check for legality
* @return True if the path does not contain any illegal path characters
* @param path The path to check for validity
* @return True if the path does not contain any invalid path characters
*/
public static boolean isLegalPath(String path)
public static boolean isValidPath(String path)
{
if (path == null)
return true;

View File

@ -444,7 +444,7 @@ public class DefaultServlet extends HttpServlet
_httpFields = fields.asImmutable();
_uri = (request.getDispatcherType() == DispatcherType.REQUEST)
? getWrapped().getHttpURI()
: Request.updateHttpURI(getWrapped(), URIUtil.addPaths(_servletRequest.getServletPath(), _servletRequest.getPathInfo()));
: Request.newHttpURIFrom(getWrapped(), URIUtil.addPaths(_servletRequest.getServletPath(), _servletRequest.getPathInfo()));
}
@Override

View File

@ -1187,7 +1187,9 @@ public class ServletContextHandler extends ContextHandler implements Graceful
@Override
protected ServletContextRequest wrap(Request request)
{
String pathInContext = getContext().pathInContext(request.getHttpURI().getCanonicalPath());
// we need to directly ask the context for the pathInContext rather than use the utility static method, as
// we have not yet wrapped the request in this context.
String pathInContext = getContext().getPathInContext(request.getHttpURI().getCanonicalPath());
MatchedResource<ServletHandler.MappedServlet> matchedResource = _servletHandler.getMatchedServlet(pathInContext);
if (matchedResource == null)
return null;

View File

@ -242,8 +242,8 @@ public class ServletContextRequest extends ContextRequest implements Runnable
{
case "o.e.j.s.s.ServletScopedRequest.request" -> _httpServletRequest;
case "o.e.j.s.s.ServletScopedRequest.response" -> _response.getHttpServletResponse();
case "o.e.j.s.s.ServletScopedRequest.servlet" -> _mappedServlet.getServletPathMapping(_pathInContext).getServletName();
case "o.e.j.s.s.ServletScopedRequest.url-pattern" -> _mappedServlet.getServletPathMapping(_pathInContext).getPattern();
case "o.e.j.s.s.ServletScopedRequest.servlet" -> _mappedServlet.getServletPathMapping(getPathInContext()).getServletName();
case "o.e.j.s.s.ServletScopedRequest.url-pattern" -> _mappedServlet.getServletPathMapping(getPathInContext()).getPattern();
default -> super.getAttribute(name);
};
}

View File

@ -223,12 +223,13 @@ public class FormAuthenticator extends LoginAuthenticator
ServletContextRequest servletContextRequest = Request.as(req, ServletContextRequest.class);
ServletContextRequest.ServletApiRequest servletApiRequest = servletContextRequest.getServletApiRequest();
boolean jSecurityCheck = isJSecurityCheck(Request.getPathInContext(req));
String pathInContext = servletContextRequest.getPathInContext();
boolean jSecurityCheck = isJSecurityCheck(pathInContext);
mandatory |= jSecurityCheck;
if (!mandatory)
return new DeferredAuthentication(this);
if (isLoginOrErrorPage(Request.getPathInContext(req)) && !DeferredAuthentication.isDeferred(res))
if (isLoginOrErrorPage(pathInContext) && !DeferredAuthentication.isDeferred(res))
return new DeferredAuthentication(this);
// Handle a request for authentication.

View File

@ -1545,9 +1545,10 @@ public class Request implements HttpServletRequest
_method = coreRequest.getMethod();
_uri = coreRequest.getHttpURI();
String pathInContext = org.eclipse.jetty.server.Request.getPathInContext(coreRequest);
_pathInContext = _context.getContextHandler().isCanonicalEncodingURIs()
? org.eclipse.jetty.server.Request.getPathInContext(coreRequest)
: URIUtil.decodePath(org.eclipse.jetty.server.Request.getPathInContext(coreRequest));
? pathInContext
: URIUtil.decodePath(pathInContext);
_httpFields = coreRequest.getHeaders();
setSecure(coreRequest.isSecure());