rename getBaseURL() to getURL(), rename getServerURL() to getBaseURL() for clarity

This commit is contained in:
Jörg Prante 2024-02-21 21:11:25 +01:00
parent 095bf2f610
commit 8ea5472223
22 changed files with 72 additions and 79 deletions

View file

@ -1,3 +1,3 @@
group = org.xbib group = org.xbib
name = net-http name = net-http
version = 4.1.2 version = 4.2.0

View file

@ -83,6 +83,11 @@ public abstract class BaseHttpRequest implements HttpRequest {
return builder.remoteAddress; return builder.remoteAddress;
} }
@Override
public URL getURL() {
return builder.url;
}
@Override @Override
public URL getBaseURL() { public URL getBaseURL() {
return URL.builder() return URL.builder()
@ -92,11 +97,6 @@ public abstract class BaseHttpRequest implements HttpRequest {
.build(); .build();
} }
@Override
public URL getURL() {
return builder.url;
}
@Override @Override
public HttpVersion getVersion() { public HttpVersion getVersion() {
return builder.httpVersion; return builder.httpVersion;

View file

@ -15,7 +15,7 @@ import org.xbib.net.http.cookie.Cookie;
public interface HttpRequest extends Request { public interface HttpRequest extends Request {
URL getURL(); URL getBaseURL();
HttpVersion getVersion(); HttpVersion getVersion();

View file

@ -41,8 +41,8 @@ public class HttpsRequestBuilder extends HttpRequestBuilder {
} }
@Override @Override
public HttpsRequestBuilder setBaseURL(URL baseURL) { public HttpsRequestBuilder setUrl(URL url) {
super.setBaseURL(baseURL); super.setUrl(url);
return this; return this;
} }

View file

@ -67,7 +67,7 @@ public class NettyHttps2ServerMultiRequestLoadTest {
.body("secure domain " + .body("secure domain " +
" SNI host " + ctx.getRequest().as(HttpsRequest.class).getSNIHost() + " " + " SNI host " + ctx.getRequest().as(HttpsRequest.class).getSNIHost() + " " +
" SSL peer host " + ctx.getRequest().as(HttpsRequest.class).getSSLSession() + " " + " SSL peer host " + ctx.getRequest().as(HttpsRequest.class).getSSLSession() + " " +
" base URL = " + ctx.getRequestBuilder().getBaseURL() + " " + " url = " + ctx.getRequestBuilder().getUrl() + " " +
ctx.getRequest().getParameter() + " " + ctx.getRequest().getParameter() + " " +
ctx.getRequest().getLocalAddress() + " " + ctx.getRequest().getLocalAddress() + " " +
ctx.getRequest().getRemoteAddress()); ctx.getRequest().getRemoteAddress());

View file

@ -67,7 +67,7 @@ public class NettyHttps2ServerTest {
.body("secure domain " + .body("secure domain " +
" SNI host " + ctx.getRequest().as(HttpsRequest.class).getSNIHost() + " " + " SNI host " + ctx.getRequest().as(HttpsRequest.class).getSNIHost() + " " +
" SSL peer host " + ctx.getRequest().as(HttpsRequest.class).getSSLSession() + " " + " SSL peer host " + ctx.getRequest().as(HttpsRequest.class).getSSLSession() + " " +
" base URL = " + ctx.getRequestBuilder().getBaseURL() + " " + " url = " + ctx.getRequestBuilder().getUrl() + " " +
ctx.getRequest().getParameter() + " " + ctx.getRequest().getParameter() + " " +
ctx.getRequest().getLocalAddress() + " " + ctx.getRequest().getLocalAddress() + " " +
ctx.getRequest().getRemoteAddress()); ctx.getRequest().getRemoteAddress());

View file

@ -17,8 +17,6 @@ import org.xbib.net.http.server.BaseHttpRequestBuilder;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import org.xbib.net.http.server.Message; import org.xbib.net.http.server.Message;
public class HttpRequestBuilder extends BaseHttpRequestBuilder { public class HttpRequestBuilder extends BaseHttpRequestBuilder {
@ -80,8 +78,8 @@ public class HttpRequestBuilder extends BaseHttpRequestBuilder {
} }
@Override @Override
public HttpRequestBuilder setBaseURL(URL baseURL) { public HttpRequestBuilder setUrl(URL url) {
super.setBaseURL(baseURL); super.setUrl(url);
return this; return this;
} }

View file

@ -65,8 +65,8 @@ public class HttpRequestBuilder extends BaseHttpRequestBuilder {
} }
@Override @Override
public HttpRequestBuilder setBaseURL(URL baseURL) { public HttpRequestBuilder setUrl(URL url) {
super.setBaseURL(baseURL); super.setUrl(url);
return this; return this;
} }

View file

@ -34,8 +34,8 @@ public class HttpsRequestBuilder extends HttpRequestBuilder {
} }
@Override @Override
public HttpsRequestBuilder setBaseURL(URL baseURL) { public HttpsRequestBuilder setUrl(URL url) {
super.setBaseURL(baseURL); super.setUrl(url);
return this; return this;
} }

View file

@ -70,8 +70,8 @@ public class HttpRequestBuilder extends BaseHttpRequestBuilder {
} }
@Override @Override
public HttpRequestBuilder setBaseURL(URL baseURL) { public HttpRequestBuilder setUrl(URL url) {
super.setBaseURL(baseURL); super.setUrl(url);
return this; return this;
} }

View file

@ -29,8 +29,8 @@ public class HttpRouterTest {
@Test @Test
public void routerTest() throws Exception { public void routerTest() throws Exception {
URL baseURL = URL.http().host("localhost").port(8008).build(); URL url = URL.http().host("localhost").port(8008).build();
HttpAddress httpAddress = HttpAddress.of(baseURL); HttpAddress httpAddress = HttpAddress.of(url);
BaseHttpRouter router = BaseHttpRouter.builder() BaseHttpRouter router = BaseHttpRouter.builder()
.addDomain(BaseHttpDomain.builder() .addDomain(BaseHttpDomain.builder()
@ -55,7 +55,7 @@ public class HttpRouterTest {
.setOutputStream(outputStream); .setOutputStream(outputStream);
HttpRequestBuilder httpRequest = HttpRequest.builder() HttpRequestBuilder httpRequest = HttpRequest.builder()
.setBaseURL(baseURL) .setUrl(url)
.setVersion(HttpVersion.HTTP_1_1) .setVersion(HttpVersion.HTTP_1_1)
.setMethod(HttpMethod.DELETE) .setMethod(HttpMethod.DELETE)
.setRequestURI("/demo") .setRequestURI("/demo")

View file

@ -1,8 +1,6 @@
dependencies { dependencies {
api project(':net-http') api project(':net-http')
api libs.config api libs.config
implementation libs.settings.datastructures.json
implementation libs.settings.datastructures.yaml
implementation libs.datastructures.tiny implementation libs.datastructures.tiny
implementation libs.datastructures.json.tiny implementation libs.datastructures.json.tiny
} }

View file

@ -58,13 +58,13 @@ public abstract class BaseHttpRequest implements HttpRequest {
} }
@Override @Override
public URL getBaseURL() { public URL getURL() {
return builder.baseURL; return builder.url;
} }
@Override @Override
public URL getServerURL() { public URL getBaseURL() {
return builder.serverURL; return builder.baseUrl;
} }
@Override @Override
@ -136,7 +136,7 @@ public abstract class BaseHttpRequest implements HttpRequest {
jsonBuilder.buildKey("local").buildMap(local); jsonBuilder.buildKey("local").buildMap(local);
Map<String, Object> remote = Map.of("host", builder.remoteAddress.getHostString(), "port", builder.remoteAddress.getPort()); Map<String, Object> remote = Map.of("host", builder.remoteAddress.getHostString(), "port", builder.remoteAddress.getPort());
jsonBuilder.buildKey("remote").buildMap(remote); jsonBuilder.buildKey("remote").buildMap(remote);
jsonBuilder.buildKey("baseurl").buildValue(builder.baseURL.toString()); jsonBuilder.buildKey("url").buildValue(builder.url.toString());
jsonBuilder.buildKey("version").buildValue(builder.getVersion().toString()); jsonBuilder.buildKey("version").buildValue(builder.getVersion().toString());
jsonBuilder.buildKey("method").buildValue(builder.getMethod().toString()); jsonBuilder.buildKey("method").buildValue(builder.getMethod().toString());
Map<String, Object> headerMap = builder.getHeaders().entries().stream() Map<String, Object> headerMap = builder.getHeaders().entries().stream()

View file

@ -32,9 +32,9 @@ public abstract class BaseHttpRequestBuilder implements HttpRequestBuilder {
protected InetSocketAddress remoteAddress; protected InetSocketAddress remoteAddress;
protected URL serverURL; protected URL url;
protected URL baseURL; protected URL baseUrl;
protected String requestPath; protected String requestPath;
@ -77,7 +77,7 @@ public abstract class BaseHttpRequestBuilder implements HttpRequestBuilder {
String remoteHost = Maps.getString(remoteMap, "host"); String remoteHost = Maps.getString(remoteMap, "host");
int remotePort = Maps.getInteger(remoteMap, "port", -1); int remotePort = Maps.getInteger(remoteMap, "port", -1);
setRemoteAddress(new InetSocketAddress(remoteHost, remotePort)); setRemoteAddress(new InetSocketAddress(remoteHost, remotePort));
setBaseURL(URL.from(Maps.getString(map, "baseurl"))); setUrl(URL.from(Maps.getString(map, "url")));
setVersion(HttpVersion.valueOf(Maps.getString(map, "version"))); setVersion(HttpVersion.valueOf(Maps.getString(map, "version")));
setMethod(HttpMethod.valueOf(Maps.getString(map, "method"))); setMethod(HttpMethod.valueOf(Maps.getString(map, "method")));
HttpHeaders httpHeaders = new HttpHeaders(); HttpHeaders httpHeaders = new HttpHeaders();
@ -204,54 +204,53 @@ public abstract class BaseHttpRequestBuilder implements HttpRequestBuilder {
} }
@Override @Override
public BaseHttpRequestBuilder setBaseURL(URL baseURL) { public BaseHttpRequestBuilder setUrl(URL url) {
if (done) { if (done) {
return this; return this;
} }
this.baseURL = baseURL; this.url = url;
return this; return this;
} }
public BaseHttpRequestBuilder setBaseURL(HttpAddress httpAddress, String uri, String hostAndPort) { public BaseHttpRequestBuilder setBaseURL(HttpAddress httpAddress,
String requestUri,
String hostAndPort) {
if (done) { if (done) {
return this; return this;
} }
Objects.requireNonNull(httpAddress); Objects.requireNonNull(httpAddress);
Objects.requireNonNull(uri); Objects.requireNonNull(requestUri);
String scheme = httpAddress.isSecure() ? "https" : "http"; String scheme = httpAddress.isSecure() ? "https" : "http";
setAddress(httpAddress); setAddress(httpAddress);
setRequestURI(uri); setRequestURI(requestUri);
String host = stripPort(hostAndPort); String host = stripPort(hostAndPort);
String extractedPort = extractPort(hostAndPort); String extractedPort = extractPort(hostAndPort);
Integer port = extractedPort != null ? Integer.parseInt(extractedPort) : httpAddress.getPort(); Integer port = extractedPort != null ? Integer.parseInt(extractedPort) : httpAddress.getPort();
this.serverURL = URL.builder() this.baseUrl = URL.builder()
.scheme(scheme) .scheme(scheme)
.host(host) .host(host)
.port(port) .port(port)
.build(); .build();
URLBuilder builder = URL.builder(URL.from(uri)); URLBuilder builder = URL.builder(URL.from(requestUri));
URL url = builder.build(); URL url = builder.build();
if (!url.isAbsolute()) { this.url = url.isAbsolute() ? url : builder
this.baseURL = builder
.scheme(scheme) .scheme(scheme)
.host(host) .host(host)
.port(port) .port(port)
.build(); .build();
} else {
this.baseURL = url;
}
if (!httpAddress.base().getScheme().equals(url.getScheme())) { if (!httpAddress.base().getScheme().equals(url.getScheme())) {
throw new IllegalArgumentException("scheme mismatch in request: " + httpAddress.base().getScheme() + " != " + url.getScheme()); throw new IllegalArgumentException("scheme mismatch in request: " + httpAddress.base().getScheme() + " != " + url.getScheme());
} }
if (url.getPort() != null && !httpAddress.getPort().equals(url.getPort())) { if (url.getPort() != null && !httpAddress.getPort().equals(url.getPort())) {
throw new IllegalArgumentException("port mismatch in request: " + httpAddress.getPort() + " != " + url.getPort()); throw new IllegalArgumentException("port mismatch in request: " + httpAddress.getPort() +
" != " + url.getPort());
} }
return this; return this;
} }
@Override @Override
public URL getBaseURL() { public URL getUrl() {
return baseURL; return url;
} }
@Override @Override

View file

@ -15,7 +15,7 @@ import org.xbib.net.http.server.route.HttpRouterContext;
public interface HttpRequest extends Request { public interface HttpRequest extends Request {
URL getServerURL(); URL getBaseURL();
HttpRouterContext getContext(); HttpRouterContext getContext();

View file

@ -14,7 +14,7 @@ public interface HttpRequestBuilder {
HttpRequestBuilder setAddress(HttpAddress httpAddress); HttpRequestBuilder setAddress(HttpAddress httpAddress);
HttpRequestBuilder setBaseURL(URL baseURL); HttpRequestBuilder setUrl(URL url);
HttpRequestBuilder setRequestURI(String requestURI); HttpRequestBuilder setRequestURI(String requestURI);
@ -34,7 +34,7 @@ public interface HttpRequestBuilder {
HttpRequestBuilder addPart(Message message); HttpRequestBuilder addPart(Message message);
URL getBaseURL(); URL getUrl();
HttpMethod getMethod(); HttpMethod getMethod();

View file

@ -68,7 +68,7 @@ public abstract class AbstractResourceHandler implements HttpHandler {
} else if (resource.isDirectory()) { } else if (resource.isDirectory()) {
logger.log(Level.FINEST, "we have a directory request"); logger.log(Level.FINEST, "we have a directory request");
if (!resource.getResourcePath().isEmpty() && !resource.getResourcePath().endsWith("/")) { if (!resource.getResourcePath().isEmpty() && !resource.getResourcePath().endsWith("/")) {
URL url = context.getRequestBuilder().getBaseURL().resolve(resource.getName() + '/'); URL url = context.getRequestBuilder().getUrl().resolve(resource.getName() + '/');
String loc = URL.builder(url) String loc = URL.builder(url)
.query(url.getQuery()) .query(url.getQuery())
.fragment(url.getFragment()) .fragment(url.getFragment())

View file

@ -43,7 +43,7 @@ public class HtmlTemplateResource implements HttpServerResource {
private final String suffix; private final String suffix;
protected final boolean negotiateLocale; protected final boolean shouldNegotiateLocale;
protected final HttpResponseBuilder responseBuilder; protected final HttpResponseBuilder responseBuilder;
@ -54,7 +54,7 @@ public class HtmlTemplateResource implements HttpServerResource {
this.templateResourceHandler = templateResourceHandler; this.templateResourceHandler = templateResourceHandler;
this.application = httpRouterContext.getAttributes().get(Application.class, "application"); this.application = httpRouterContext.getAttributes().get(Application.class, "application");
Objects.requireNonNull(application); Objects.requireNonNull(application);
this.negotiateLocale = application.getSettings().getAsBoolean("negotiateLocale", false); this.shouldNegotiateLocale = application.getSettings().getAsBoolean("negotiateLocale", false);
this.responseBuilder = httpRouterContext.getAttributes().get(HttpResponseBuilder.class, "responsebuilder"); this.responseBuilder = httpRouterContext.getAttributes().get(HttpResponseBuilder.class, "responsebuilder");
Objects.requireNonNull(responseBuilder); Objects.requireNonNull(responseBuilder);
Path root = templateResourceHandler.getRoot(); Path root = templateResourceHandler.getRoot();
@ -63,7 +63,7 @@ public class HtmlTemplateResource implements HttpServerResource {
throw new IllegalArgumentException("no home path set for template resource resolving"); throw new IllegalArgumentException("no home path set for template resource resolving");
} }
this.resourcePath = httpRouterContext.getRequestBuilder().getRequestPath().substring(1); this.resourcePath = httpRouterContext.getRequestBuilder().getRequestPath().substring(1);
this.path = resourcePath.length() > 0 ? root.resolve(resourcePath) : root; this.path = !resourcePath.isEmpty() ? root.resolve(resourcePath) : root;
logger.log(Level.FINEST, "class = " + getClass().getName() + logger.log(Level.FINEST, "class = " + getClass().getName() +
" root = " + root + " root = " + root +
" resource path = " + resourcePath + " resource path = " + resourcePath +
@ -191,11 +191,11 @@ public class HtmlTemplateResource implements HttpServerResource {
} }
public String url(HttpRequest request, String rel, boolean absolute) { public String url(HttpRequest request, String rel, boolean absolute) {
String prefix = application.getSettings().get("web.prefix", "/"); String webPrefix = application.getSettings().get("web.prefix", "/");
if (!prefix.endsWith("/")) { if (!webPrefix.endsWith("/")) {
prefix = prefix + "/"; webPrefix = webPrefix + "/";
} }
URL url = request.getServerURL().resolve(prefix).resolve(rel); URL url = request.getBaseURL().resolve(webPrefix).resolve(rel);
return absolute ? url.toExternalForm() : toOrigin(url); return absolute ? url.toExternalForm() : toOrigin(url);
} }

View file

@ -82,8 +82,8 @@ public class BaseHttpRouter implements HttpRouter {
HttpResponseBuilder responseBuilder) { HttpResponseBuilder responseBuilder) {
Objects.requireNonNull(requestBuilder); Objects.requireNonNull(requestBuilder);
Objects.requireNonNull(requestBuilder.getRequestURI()); Objects.requireNonNull(requestBuilder.getRequestURI());
Objects.requireNonNull(requestBuilder.getBaseURL()); Objects.requireNonNull(requestBuilder.getUrl());
HttpDomain httpDomain = findDomain(requestBuilder.getBaseURL()); HttpDomain httpDomain = findDomain(requestBuilder.getUrl());
if (httpDomain == null) { if (httpDomain == null) {
httpDomain = builder.domains.iterator().next(); httpDomain = builder.domains.iterator().next();
} }
@ -204,14 +204,14 @@ public class BaseHttpRouter implements HttpRouter {
String contextPath = pathResolverResult.getContext() != null ? String contextPath = pathResolverResult.getContext() != null ?
PATH_SEPARATOR + String.join(PATH_SEPARATOR, pathResolverResult.getContext()) : null; PATH_SEPARATOR + String.join(PATH_SEPARATOR, pathResolverResult.getContext()) : null;
httpRouterContext.setContextPath(contextPath); httpRouterContext.setContextPath(contextPath);
httpRouterContext.setContextURL(httpRouterContext.getRequestBuilder().getBaseURL().resolve(contextPath != null ? contextPath + "/" : "")); httpRouterContext.setContextURL(httpRouterContext.getRequestBuilder().getUrl().resolve(contextPath != null ? contextPath + "/" : ""));
} else { } else {
// path resolver result null means "404 not found". Set default values. // path resolver result null means "404 not found". Set default values.
httpRouterContext.getAttributes().put("context", null); httpRouterContext.getAttributes().put("context", null);
httpRouterContext.getAttributes().put("handler", null); httpRouterContext.getAttributes().put("handler", null);
httpRouterContext.getAttributes().put("pathparams", null); httpRouterContext.getAttributes().put("pathparams", null);
httpRouterContext.setContextPath(PATH_SEPARATOR); httpRouterContext.setContextPath(PATH_SEPARATOR);
httpRouterContext.setContextURL(httpRouterContext.getRequestBuilder().getBaseURL()); httpRouterContext.setContextURL(httpRouterContext.getRequestBuilder().getUrl());
} }
HttpRequest httpRequest = createRequest(httpRouterContext, pathResolverResult); HttpRequest httpRequest = createRequest(httpRouterContext, pathResolverResult);
httpRouterContext.setRequest(httpRequest); httpRouterContext.setRequest(httpRequest);

View file

@ -115,18 +115,16 @@ public abstract class DefaultMarkupTemplate extends BaseTemplate {
} }
public String url(String rel, boolean absolute) { public String url(String rel, boolean absolute) {
String prefix = application.getSettings().get("web.prefix", "/"); String webPrefix = application.getSettings().get("web.prefix", "/");
if (!prefix.endsWith("/")) { if (!webPrefix.endsWith("/")) {
prefix = prefix + "/"; webPrefix = webPrefix + "/";
} }
if (request != null) { if (request != null) {
URL url = request.getServerURL().resolve(prefix).resolve(rel); URL url = request.getBaseURL().resolve(webPrefix).resolve(rel);
logger.log(Level.FINEST, "server base URL = " + request.getServerURL() +
" prefix = " + prefix + " rel = " + rel + " --> " + url);
return absolute ? url.toExternalForm() : toOrigin(url); return absolute ? url.toExternalForm() : toOrigin(url);
} else { } else {
logger.log(Level.WARNING, "request is null, returning " + prefix + rel); logger.log(Level.WARNING, "request is null, returning " + webPrefix + rel);
return prefix + rel; return webPrefix + rel;
} }
} }
@ -135,7 +133,7 @@ public abstract class DefaultMarkupTemplate extends BaseTemplate {
} }
public String encodeUrl(String rel, boolean absolute) { public String encodeUrl(String rel, boolean absolute) {
URLBuilder builder = URL.builder(request.getServerURL().resolve(rel)); URLBuilder builder = URL.builder(request.getBaseURL().resolve(rel));
if (session != null) { if (session != null) {
if (getModel().containsKey("session.url.enabled") && getModel().containsKey("session.url.parametername")) { if (getModel().containsKey("session.url.enabled") && getModel().containsKey("session.url.parametername")) {
String sessionIdParameterName = (String) getModel().get("session.url.parametername"); String sessionIdParameterName = (String) getModel().get("session.url.parametername");

View file

@ -124,7 +124,7 @@ public class GroovyTemplateResource extends HtmlTemplateResource {
httpRouterContext.getAttributes().put("writable", writable); httpRouterContext.getAttributes().put("writable", writable);
return; return;
} }
if (!negotiateLocale) { if (!shouldNegotiateLocale) {
// if no locale negotiation configured, set always the applicaiton locale. This constant value never changes. // if no locale negotiation configured, set always the applicaiton locale. This constant value never changes.
templateResolver.setLocale(application.getLocale()); templateResolver.setLocale(application.getLocale());
Writable writable = template.make(binding.getVariables()); Writable writable = template.make(binding.getVariables());

View file

@ -6,7 +6,7 @@ dependencyResolutionManagement {
version('netty', '4.1.107.Final') version('netty', '4.1.107.Final')
version('netty-tcnative', '2.0.62.Final') version('netty-tcnative', '2.0.62.Final')
version('datastructures', '5.0.6') version('datastructures', '5.0.6')
version('net', '4.1.0') version('net', '4.2.0')
library('netty-codec-http2', 'io.netty', 'netty-codec-http2').versionRef('netty') library('netty-codec-http2', 'io.netty', 'netty-codec-http2').versionRef('netty')
library('netty-handler', 'io.netty', 'netty-handler').versionRef('netty') library('netty-handler', 'io.netty', 'netty-handler').versionRef('netty')
library('netty-handler-proxy', 'io.netty', 'netty-handler-proxy').versionRef('netty') library('netty-handler-proxy', 'io.netty', 'netty-handler-proxy').versionRef('netty')