Skip to content

Commit

Permalink
Additional work on path normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet committed Aug 20, 2021
1 parent 5062b6c commit 756eccc
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,18 @@ public static URI toURI(String path, boolean trailingSlash) {
* </ul>
*
* @param base URI to resolve relative paths. Use {@link #toURI(String, boolean)} to construct this parameter.
*
*
* @param segment Relative or absolute path
* @param trailingSlash true if resulting URI must end with a '/'
* @throws IllegalArgumentException if the path contains invalid characters or path segments.
*/
public static URI normalizeWithBase(URI base, String segment, boolean trailingSlash) {
if (segment == null || segment.trim().isEmpty()) {
return base;
if ("/".equals(base.getPath())) {
return base;
}
// otherwise, make sure trailingSlash is honored
return toURI(base.getPath(), trailingSlash);
}
URI segmentUri = toURI(segment, trailingSlash);
URI resolvedUri = base.resolve(segmentUri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import io.quarkus.builder.item.SimpleBuildItem;

public final class ResteasyDeploymentBuildItem extends SimpleBuildItem {
private ResteasyDeployment deployment;
private String rootPath;
private ResteasyDeployment deployment;

public ResteasyDeploymentBuildItem(String rootPath, ResteasyDeployment deployment) {
this.rootPath = rootPath.startsWith("/") ? rootPath : "/" + rootPath;
this.deployment = deployment;
this.rootPath = rootPath;
}

public ResteasyDeployment getDeployment() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public final class ResteasyServerConfigBuildItem extends SimpleBuildItem {
*/
public ResteasyServerConfigBuildItem(String rootPath, String path, Map<String, String> initParameters) {
this.rootPath = rootPath;
this.path = path;
this.path = path.startsWith("/") ? path : "/" + path;
this.initParameters = initParameters;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ public void jaxrsConfig(
BuildProducer<io.quarkus.resteasy.server.common.spi.ResteasyJaxrsConfigBuildItem> resteasyJaxrsConfig,
HttpRootPathBuildItem httpRootPathBuildItem) {
if (resteasyServerConfig.isPresent()) {
String rp = resteasyServerConfig.get().getRootPath();
String rootPath = httpRootPathBuildItem.resolvePath(rp.startsWith("/") ? rp.substring(1) : rp);
String defaultPath = httpRootPathBuildItem.resolvePath(resteasyServerConfig.get().getPath());
String rootPath = httpRootPathBuildItem.relativePath(resteasyServerConfig.get().getRootPath());
String defaultPath = resteasyServerConfig.get().getPath();

deprecatedResteasyJaxrsConfig.produce(new ResteasyJaxrsConfigBuildItem(defaultPath));
resteasyJaxrsConfig
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package io.quarkus.undertow.deployment;

import static io.quarkus.runtime.configuration.ConverterSupport.DEFAULT_QUARKUS_CONVERTER_PRIORITY;

import java.util.Optional;

import javax.annotation.Priority;

import org.eclipse.microprofile.config.spi.Converter;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.quarkus.runtime.annotations.ConvertWith;

@ConfigRoot(phase = ConfigPhase.BUILD_TIME)
public class ServletConfig {
Expand All @@ -17,6 +24,7 @@ public class ServletConfig {
* is /bar and the http root is /foo then the actual Servlet path will be /foo/bar.
*/
@ConfigItem
@ConvertWith(ContextPathConverter.class)
Optional<String> contextPath;

/**
Expand All @@ -25,4 +33,33 @@ public class ServletConfig {
@ConfigItem(defaultValue = "UTF-8")
public String defaultCharset;

/**
* This converter adds a / at the beginning of the context path but does not add one at the end, given we want to support
* it.
* <p>
* See ContextPathTestCase for an example.
*/
@Priority(DEFAULT_QUARKUS_CONVERTER_PRIORITY)
public static class ContextPathConverter implements Converter<String> {

private static final String SLASH = "/";

@Override
public String convert(String value) throws IllegalArgumentException, NullPointerException {
if (value == null) {
return SLASH;
}

value = value.trim();
if (SLASH.equals(value)) {
return value;
}
if (!value.startsWith(SLASH)) {
value = SLASH + value;
}

return value;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
import io.quarkus.undertow.runtime.UndertowDeploymentRecorder;
import io.quarkus.undertow.runtime.UndertowHandlersConfServletExtension;
import io.quarkus.vertx.http.deployment.DefaultRouteBuildItem;
import io.quarkus.vertx.http.deployment.HttpRootPathBuildItem;
import io.quarkus.vertx.http.deployment.RouteBuildItem;
import io.quarkus.vertx.http.runtime.HttpBuildTimeConfig;
import io.quarkus.vertx.http.runtime.HttpConfiguration;
Expand Down Expand Up @@ -319,11 +320,7 @@ public ServletContextPathBuildItem contextPath(
WebMetadataBuildItem webMetadataBuildItem) {
String contextPath;
if (servletConfig.contextPath.isPresent()) {
if (!servletConfig.contextPath.get().startsWith("/")) {
contextPath = "/" + servletConfig.contextPath;
} else {
contextPath = servletConfig.contextPath.get();
}
contextPath = servletConfig.contextPath.get();
} else if (webMetadataBuildItem.getWebMetaData().getDefaultContextPath() != null) {
contextPath = webMetadataBuildItem.getWebMetaData().getDefaultContextPath();
} else {
Expand Down Expand Up @@ -351,6 +348,7 @@ public ServletDeploymentManagerBuildItem build(List<ServletBuildItem> servlets,
ShutdownContextBuildItem shutdownContext,
KnownPathsBuildItem knownPaths,
HttpBuildTimeConfig httpBuildTimeConfig,
HttpRootPathBuildItem httpRootPath,
ServletConfig servletConfig) throws Exception {

ObjectSubstitutionBuildItem.Holder holder = new ObjectSubstitutionBuildItem.Holder(ServletSecurityInfo.class,
Expand All @@ -365,7 +363,7 @@ public ServletDeploymentManagerBuildItem build(List<ServletBuildItem> servlets,
String contextPath = servletContextPathBuildItem.getServletContextPath();
RuntimeValue<DeploymentInfo> deployment = recorder.createDeployment("test", knownPaths.knownFiles,
knownPaths.knownDirectories,
launchMode.getLaunchMode(), shutdownContext, contextPath, httpBuildTimeConfig.rootPath,
launchMode.getLaunchMode(), shutdownContext, httpRootPath.relativePath(contextPath),
servletConfig.defaultCharset, webMetaData.getRequestCharacterEncoding(),
webMetaData.getResponseCharacterEncoding(), httpBuildTimeConfig.auth.proactive,
webMetaData.getWelcomeFileList() != null ? webMetaData.getWelcomeFileList().getWelcomeFiles() : null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,26 +156,17 @@ public static void setHotDeploymentResources(List<Path> resources) {
}

public RuntimeValue<DeploymentInfo> createDeployment(String name, Set<String> knownFile, Set<String> knownDirectories,
LaunchMode launchMode, ShutdownContext context, String contextPath, String httpRootPath, String defaultCharset,
LaunchMode launchMode, ShutdownContext context, String mountPoint, String defaultCharset,
String requestCharacterEncoding, String responseCharacterEncoding, boolean proactiveAuth,
List<String> welcomeFiles) {
String realMountPoint;
if (contextPath.equals("/")) {
realMountPoint = httpRootPath;
} else if (httpRootPath.equals("/")) {
realMountPoint = contextPath;
} else {
realMountPoint = httpRootPath + contextPath;
}

DeploymentInfo d = new DeploymentInfo();
d.setDefaultRequestEncoding(requestCharacterEncoding);
d.setDefaultResponseEncoding(responseCharacterEncoding);
d.setDefaultEncoding(defaultCharset);
d.setSessionIdGenerator(new QuarkusSessionIdGenerator());
d.setClassLoader(getClass().getClassLoader());
d.setDeploymentName(name);
d.setContextPath(realMountPoint);
d.setContextPath(mountPoint);
d.setEagerFilterInit(true);
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void testResolvePathWithSlash() {
void testResolvePathWithSlashApp() {
HttpRootPathBuildItem buildItem = new HttpRootPathBuildItem("/app");

Assertions.assertEquals("/app/", buildItem.resolvePath(""));
Assertions.assertEquals("/app", buildItem.resolvePath(""));
Assertions.assertEquals("/app/foo", buildItem.resolvePath("foo"));
Assertions.assertEquals("/foo", buildItem.resolvePath("/foo"));
}
Expand Down

0 comments on commit 756eccc

Please sign in to comment.