Skip to content

Commit

Permalink
Normalize the quarkus.http.root-path at config level
Browse files Browse the repository at this point in the history
Make sure the quarkus.http.root-path always starts and ends with a '/'
so that we don't have any risk of having logic working with '/test' and
not with '/test/'.

Fixes #19492
  • Loading branch information
gsmet committed Aug 19, 2021
1 parent 0770982 commit a5d46b1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import java.time.Duration;

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;
import io.vertx.core.http.ClientAuth;

@ConfigRoot(name = "http", phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
Expand All @@ -14,6 +17,7 @@ public class HttpBuildTimeConfig {
* The HTTP root path. All web content will be served relative to this root path.
*/
@ConfigItem(defaultValue = "/")
@ConvertWith(NormalizeHttpRootPathConverter.class)
public String rootPath;

public AuthConfig auth;
Expand Down Expand Up @@ -43,7 +47,7 @@ public class HttpBuildTimeConfig {
* Non-application endpoints will be served from the specified path.
* * `${quarkus.http.root-path}` -> Setting this path to the same value as HTTP root path disables
* this root path. All extension-provided endpoints will be served from `${quarkus.http.root-path}`.
*
*
* @asciidoclet
*/
@ConfigItem(defaultValue = "q")
Expand All @@ -54,4 +58,32 @@ public class HttpBuildTimeConfig {
*/
@ConfigItem(defaultValue = "30s")
public Duration testTimeout;

/**
* Make sure the HTTP root path is fully normalized and always starts and ends with a '/'.
*/
public static class NormalizeHttpRootPathConverter 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;
}
if (!value.endsWith(SLASH)) {
value = value + SLASH;
}

return value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Consumer<Route> start() {
StaticHandler staticHandler = StaticHandler.create(META_INF_RESOURCES).setDefaultContentEncoding("UTF-8");
handlers.add(ctx -> {
String rel = ctx.mountPoint() == null ? ctx.normalisedPath()
: ctx.normalisedPath().substring(ctx.mountPoint().length());
: ctx.normalisedPath().substring(ctx.mountPoint().length() - 1);
if (knownPaths.contains(rel)) {
staticHandler.handle(ctx);
} else {
Expand Down

0 comments on commit a5d46b1

Please sign in to comment.