replaced getPathInContext with static method

This commit is contained in:
Greg Wilkins 2022-10-24 21:02:18 +11:00
parent 465ddf0932
commit 14d912c654
5 changed files with 31 additions and 3 deletions

View File

@ -894,6 +894,17 @@ public interface HttpURI
_uri = null;
_path = path;
_canonicalPath = null;
// If the passed path does not have a parameter, then keep the current parameter
// else delete the current parameter
if (_param != null)
{
if (path.indexOf(';') >= 0)
_param = null;
else
_path = _path + ';' + _param;
}
return this;
}

View File

@ -916,4 +916,13 @@ public class HttpURITest
.path("");
assertEquals("//host", uri.asString());
}
@Test
public void testKeepParam()
{
HttpURI orig = HttpURI.from("http://localhost/context/info;param=value");
HttpURI built = HttpURI.build(orig).path("/context/info").asImmutable();
assertThat(built.getParam(), is(orig.getParam()));
assertThat(built.toString(), is(orig.toString()));
}
}

View File

@ -705,6 +705,8 @@ public interface Request extends Attributes, Content.Source
static HttpURI updateHttpURI(Request request, String newPathInContext)
{
return HttpURI.build(request.getHttpURI()).path(URIUtil.addPaths(getContextPath(request), newPathInContext)).asImmutable();
return HttpURI.build(request.getHttpURI())
.path(URIUtil.addPaths(getContextPath(request), newPathInContext))
.asImmutable();
}
}

View File

@ -34,6 +34,7 @@ import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import jakarta.servlet.DispatcherType;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
@ -426,6 +427,7 @@ public class DefaultServlet extends HttpServlet
private final HttpServletRequest _servletRequest;
private final Request _request;
private final HttpURI _uri;
private final HttpFields _httpFields;
ServletCoreRequest(HttpServletRequest request)
@ -447,6 +449,10 @@ public class DefaultServlet extends HttpServlet
}
}
_httpFields = fields.asImmutable();
_uri = (request.getDispatcherType() == DispatcherType.REQUEST)
? _request.getHttpURI()
: Request.updateHttpURI(_request, URIUtil.addPaths(_servletRequest.getServletPath(), _servletRequest.getPathInfo()));
}
@Override
@ -464,7 +470,7 @@ public class DefaultServlet extends HttpServlet
@Override
public HttpURI getHttpURI()
{
return Request.updateHttpURI(_request, URIUtil.addPaths(_servletRequest.getServletPath(), _servletRequest.getPathInfo()));
return _uri;
}
@Override

View File

@ -358,7 +358,7 @@ public class DefaultServletTest
FS.ensureDirExists(docRoot.resolve("three"));
String resBasePath = docRoot.toAbsolutePath().toString();
defholder.setInitParameter("resourceBase", resBasePath);
defholder.setInitParameter("baseResource", resBasePath);
String req1 = """
GET /context/one/deep/ HTTP/1.1\r