Skip to content

Commit

Permalink
use Request.setContext in all places done in Jetty-11
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <[email protected]>
  • Loading branch information
lachlan-roberts committed Jun 15, 2023
1 parent e4c79c5 commit 5dcf022
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -865,15 +865,52 @@ public void doScope(String target, Request baseRequest, HttpServletRequest reque
if (LOG.isDebugEnabled())
LOG.debug("scope {}|{}|{} @ {}", baseRequest.getContextPath(), baseRequest.getServletPath(), baseRequest.getPathInfo(), this);

APIContext oldApiContext = baseRequest.getContext();
APIContext oldContext = baseRequest.getContext();
String oldPathInContext = baseRequest.getPathInContext();
String pathInContext = target;
DispatcherType dispatch = baseRequest.getDispatcherType();

// Are we already in this context?
if (oldContext != _apiContext)
{
// check the target.
String contextPath = getContextPath();
if (DispatcherType.REQUEST.equals(dispatch) || DispatcherType.ASYNC.equals(dispatch))
{
if (target.length() > contextPath.length())
{
if (contextPath.length() > 1)
target = target.substring(contextPath.length());
pathInContext = target;
}
else if (contextPath.length() == 1)
{
target = "/";
pathInContext = "/";
}
else
{
target = "/";
pathInContext = null;
}
}
}

try
{
baseRequest.setContext(_apiContext);
baseRequest.setContext(_apiContext,
(DispatcherType.INCLUDE.equals(dispatch) || !target.startsWith("/")) ? oldPathInContext : pathInContext);

org.eclipse.jetty.server.handler.ContextHandler.ScopedContext context = getCoreContextHandler().getContext();
if (context == org.eclipse.jetty.server.handler.ContextHandler.getCurrentContext())
{
nextScope(target, baseRequest, request, response);
}
else
context.call(() -> nextScope(target, baseRequest, request, response), baseRequest.getCoreRequest());
{
String t = target;
context.call(() -> nextScope(t, baseRequest, request, response), baseRequest.getCoreRequest());
}
}
catch (IOException | ServletException | RuntimeException e)
{
Expand All @@ -885,7 +922,7 @@ public void doScope(String target, Request baseRequest, HttpServletRequest reque
}
finally
{
baseRequest.setContext(oldApiContext);
baseRequest.setContext(oldContext, oldPathInContext);
}
}

Expand Down Expand Up @@ -1660,14 +1697,12 @@ public void handleAsync(HttpChannel channel) throws IOException, ServletExceptio
// this is a dispatch with either a provided URI and/or a dispatched path
// We will have to modify the request and then revert
final HttpURI oldUri = baseRequest.getHttpURI();
final String oldPathInContext = baseRequest.getPathInContext();
final ServletPathMapping oldServletPathMapping = baseRequest.getServletPathMapping();
final MultiMap<String> oldQueryParams = baseRequest.getQueryParameters();
try
{
if (encodedPathQuery == null)
{
baseRequest.onDispatch(baseUri, oldPathInContext);
baseRequest.setHttpURI(baseUri);
}
else
{
Expand All @@ -1692,23 +1727,18 @@ public void handleAsync(HttpChannel channel) throws IOException, ServletExceptio
builder.param(baseUri.getParam());
if (StringUtil.isEmpty(builder.getQuery()))
builder.query(baseUri.getQuery());
baseRequest.setHttpURI(builder);

HttpURI uri = builder.asImmutable();
String pathInContext = uri.getDecodedPath();
if (baseRequest.getContextPath().length() > 1)
pathInContext = pathInContext.substring(baseRequest.getContextPath().length());

baseRequest.onDispatch(uri, pathInContext);
if (baseUri.getQuery() != null && baseRequest.getQueryString() != null)
baseRequest.mergeQueryParameters(oldUri.getQuery(), baseRequest.getQueryString());
}

baseRequest.setContext(null, baseRequest.getHttpURI().getDecodedPath());
handleAsync(channel, event, baseRequest);
}
finally
{
baseRequest.onDispatch(oldUri, oldPathInContext);
baseRequest.setServletPathMapping(oldServletPathMapping);
baseRequest.setHttpURI(oldUri);
baseRequest.setQueryParameters(oldQueryParams);
baseRequest.resetParameters();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ protected void forward(ServletRequest request, ServletResponse response, Dispatc
query = old_uri.getQuery();

String decodedPathInContext = URIUtil.decodePath(_pathInContext);
baseRequest.onDispatch(HttpURI.build(old_uri, _uri.getPath(), _uri.getParam(), query), decodedPathInContext);
baseRequest.setHttpURI(HttpURI.build(old_uri, _uri.getPath(), _uri.getParam(), query));
baseRequest.setContext(_contextHandler.getServletContext(), decodedPathInContext);

if (_uri.getQuery() != null || old_uri.getQuery() != null)
{
Expand Down Expand Up @@ -229,7 +230,8 @@ protected void forward(ServletRequest request, ServletResponse response, Dispatc
}
finally
{
baseRequest.onDispatch(old_uri, old_path_in_context);
baseRequest.setHttpURI(old_uri);
baseRequest.setContext(old_context, old_path_in_context);
baseRequest.setServletPathMapping(old_mapping);
baseRequest.setQueryParameters(old_query_params);
baseRequest.resetParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -696,9 +696,10 @@ public ContextHandler.APIContext getContext()
return _context;
}

public void setContext(ContextHandler.APIContext context)
public void setContext(ContextHandler.APIContext context, String pathInContext)
{
_context = context;
_pathInContext = pathInContext;
}

/**
Expand Down Expand Up @@ -1344,13 +1345,11 @@ public HttpURI getHttpURI()
return _uri;
}

public void onDispatch(HttpURI uri, String decodedPathInContext)
public void setHttpURI(HttpURI uri)
{
if (_uri != null && !Objects.equals(_uri.getQuery(), uri.getQuery()) && _queryParameters != BAD_PARAMS)
_parameters = _queryParameters = null;
_uri = uri.asImmutable();
_pathInContext = decodedPathInContext;
_servletPathMapping = null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.NanoTime;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.thread.Scheduler;
import org.eclipse.jetty.util.thread.TimerScheduler;
import org.hamcrest.Matchers;
Expand Down Expand Up @@ -1605,7 +1606,8 @@ public void testEncodeRedirect() throws Exception

Response response = getResponse();
Request request = response.getHttpChannel().getRequest();
request.onDispatch(HttpURI.build(request.getHttpURI()).host("myhost").port(8888), "/path/info");
request.setHttpURI(HttpURI.build(request.getHttpURI()).host("myhost").port(8888));
request.setContext(_context._apiContext, "/path/info");

assertEquals("http://myhost:8888/path/info;param?query=0&more=1#target", response.encodeURL("http://myhost:8888/path/info;param?query=0&more=1#target"));

Expand Down Expand Up @@ -1716,7 +1718,8 @@ public void testSendRedirect(String destination, String expected, boolean cookie
uri.scheme("http");
if (host != null)
uri.host(host).port(port);
request.onDispatch(uri, "/path/info");
request.setHttpURI(uri);
request.setContext(_context._apiContext, "/path/info");

ContextHandler.CoreContextRequest coreRequest = response.getHttpChannel().getCoreRequest();
coreRequest.setSessionManager(sessionHandler.getSessionManager());
Expand Down Expand Up @@ -1786,7 +1789,8 @@ public void testSendRedirectRelative()
if (host != null)
uri.authority(host, port);
uri.pathQuery("/path/info;param;jsessionid=12345?query=0&more=1#target");
request.onDispatch(uri, "/info");
request.setHttpURI(uri);
request.setContext(_context._apiContext, "/info");

ContextHandler.CoreContextRequest coreRequest = response.getHttpChannel().getCoreRequest();
coreRequest.setRequestedSession(new AbstractSessionManager.RequestedSession(null, "12345", i > 2));
Expand Down Expand Up @@ -2268,6 +2272,7 @@ private Response getResponse(HttpVersion version)
org.eclipse.jetty.server.Response coreResponse = new MockResponse(coreRequest);

_channel.onRequest(new ContextHandler.CoreContextRequest(coreRequest, _context.getCoreContextHandler().getContext(), _channel));
_channel.getRequest().setContext(_context._apiContext, URIUtil.decodePath(org.eclipse.jetty.server.Request.getPathInContext(coreRequest)));
_channel.onProcess(coreResponse, Callback.NOOP);

BufferUtil.clear(_content);
Expand Down

0 comments on commit 5dcf022

Please sign in to comment.