diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpServerContext.java b/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpServerContext.java index bb1c3d8..9f28870 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpServerContext.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpServerContext.java @@ -213,7 +213,7 @@ public class BaseHttpServerContext implements HttpServerContext { .charset(charset, CodingErrorAction.REPLACE) .path(httpRequestBuilder.getRequestURI()) .build(); - ParameterBuilder formParameterBuilder = Parameter.builder().domain("FORM"); + ParameterBuilder formParameterBuilder = Parameter.builder().domain(Parameter.Domain.FORM); // https://www.w3.org/TR/html4/interact/forms.html#h-17.13.4 if (HttpMethod.POST.equals(httpRequestBuilder.getMethod()) && (mimeType != null && mimeType.contains(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED))) { @@ -224,7 +224,7 @@ public class BaseHttpServerContext implements HttpServerContext { } } CookieBox cookieBox = attributes.get(CookieBox.class, "incomingcookies"); - ParameterBuilder cookieParameterBuilder = Parameter.builder().domain("COOKIE"); + ParameterBuilder cookieParameterBuilder = Parameter.builder().domain(Parameter.Domain.COOKIE); if (cookieBox != null) { cookieBox.forEach(c -> cookieParameterBuilder.add(c.name(), c.value())); } diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpService.java b/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpService.java index ff2dd08..13b4f72 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpService.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpService.java @@ -88,7 +88,7 @@ public class BaseHttpService implements HttpService { @Override public String toString() { - return "BaseHttpService[methods=" + builder.methods + ",prefix" + builder.prefix + ",path=" + builder.pathSpec + ",handler=" + builder.handlers + "]"; + return "BaseHttpService[methods=" + builder.methods + ",prefix=" + builder.prefix + ",path=" + builder.pathSpec + ",handler=" + builder.handlers + "]"; } } diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/auth/FormAuthenticationHandler.java b/net-http-server/src/main/java/org/xbib/net/http/server/auth/FormAuthenticationHandler.java index e9b199b..d16f744 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/auth/FormAuthenticationHandler.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/auth/FormAuthenticationHandler.java @@ -49,18 +49,18 @@ public class FormAuthenticationHandler extends LoginAuthenticationHandler implem userProfile = new BaseUserProfile(); context.attributes().put("userprofile", userProfile); Parameter parameter = context.httpRequest().getParameter(); - if (!parameter.containsKey("FORM", usernameParameter)) { + if (!parameter.containsKey(usernameParameter, Parameter.Domain.FORM)) { logger.log(Level.WARNING, "usernameParameter not set, unable to authenticate"); prepareFormAuthentication(context); return; } - if (!parameter.containsKey("FORM", passwordParameter)) { + if (!parameter.containsKey(passwordParameter, Parameter.Domain.FORM)) { logger.log(Level.WARNING, "passwordParameter not set, unable to authenticate"); prepareFormAuthentication(context); return; } - String username = parameter.getAsString("FORM", usernameParameter); - String password = parameter.getAsString("FORM", passwordParameter); + String username = parameter.getAsString(usernameParameter, Parameter.Domain.FORM); + String password = parameter.getAsString(passwordParameter, Parameter.Domain.FORM); logger.log(Level.FINE, "username and password found, ready for authentication"); try { authenticate(userProfile, username, password, context.httpRequest()); diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/auth/LoginAuthenticationHandler.java b/net-http-server/src/main/java/org/xbib/net/http/server/auth/LoginAuthenticationHandler.java index 62e1b3b..5834c64 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/auth/LoginAuthenticationHandler.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/auth/LoginAuthenticationHandler.java @@ -3,6 +3,7 @@ package org.xbib.net.http.server.auth; import java.util.Collection; import org.xbib.net.Authenticator; import org.xbib.net.GroupsProvider; +import org.xbib.net.Parameter; import org.xbib.net.Request; import org.xbib.net.SecurityRealm; import org.xbib.net.UserDetails; @@ -42,8 +43,8 @@ public class LoginAuthenticationHandler implements HttpHandler { userProfile = new BaseUserProfile(); try { authenticate(userProfile, - (String) context.httpRequest().getParameter().get("DEFAULT", userParameterName), - (String) context.httpRequest().getParameter().get("DEFAULT", passwordParameterName), + (String) context.httpRequest().getParameter().get(userParameterName, Parameter.Domain.DEFAULT, Parameter.Domain.FORM), + (String) context.httpRequest().getParameter().get(passwordParameterName, Parameter.Domain.DEFAULT, Parameter.Domain.FORM), context.httpRequest()); context.attributes().put("userprofile", userProfile); } catch (Exception e) { diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRouteResolver.java b/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRouteResolver.java index fb660a8..f186cb0 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRouteResolver.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRouteResolver.java @@ -15,6 +15,8 @@ import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; +import static org.xbib.net.Parameter.Domain.PATH; + public class BaseHttpRouteResolver implements HttpRouteResolver { private static final Logger logger = Logger.getLogger(BaseHttpRouteResolver.class.getName()); @@ -33,7 +35,7 @@ public class BaseHttpRouteResolver implements HttpRouteResolver { @Override public void resolve(HttpRoute httpRoute, ResultListener listener) { for (Map.Entry entry : builder.routes) { - ParameterBuilder parameterBuilder = Parameter.builder().domain("PATH"); + ParameterBuilder parameterBuilder = Parameter.builder().domain(PATH); boolean match = entry.getKey().matches(parameterBuilder, httpRoute); if (match && listener != null) { String path = httpRoute.getEffectivePath(); diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRouterBuilder.java b/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRouterBuilder.java index 2ab0567..1e10084 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRouterBuilder.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRouterBuilder.java @@ -12,6 +12,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import org.xbib.net.http.server.handler.UnauthorizedHandler; import org.xbib.net.http.server.handler.VersionNotSupportedHandler; @@ -39,7 +40,11 @@ public class BaseHttpRouterBuilder implements HttpRouterBuilder { @Override public BaseHttpRouterBuilder setPrefix(String prefix) { - this.prefix = prefix; + Objects.requireNonNull(prefix); + // Add ending slash if missing. + // We require a prefix with ending slash. Otherwise, obscure things can happen in path parameter handling, + // if a path parameter has a common prefix with this prefix. + this.prefix = prefix.isEmpty() || prefix.endsWith("/") ? prefix : prefix + "/"; return this; } diff --git a/settings.gradle b/settings.gradle index 27ab120..9ea6dba 100644 --- a/settings.gradle +++ b/settings.gradle @@ -8,7 +8,7 @@ dependencyResolutionManagement { version('netty-tcnative', '2.0.59.Final') version('datastructures', '2.0.0') version('config', '5.0.2') - version('net', '3.0.4') + version('net', '3.0.5') library('junit-jupiter-api', 'org.junit.jupiter', 'junit-jupiter-api').versionRef('junit') library('junit-jupiter-params', 'org.junit.jupiter', 'junit-jupiter-params').versionRef('junit') library('junit-jupiter-engine', 'org.junit.jupiter', 'junit-jupiter-engine').versionRef('junit')