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
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;
}
@Override
public URL getURL() {
return builder.url;
}
@Override
public URL getBaseURL() {
return URL.builder()
@ -92,11 +97,6 @@ public abstract class BaseHttpRequest implements HttpRequest {
.build();
}
@Override
public URL getURL() {
return builder.url;
}
@Override
public HttpVersion getVersion() {
return builder.httpVersion;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -68,7 +68,7 @@ public abstract class AbstractResourceHandler implements HttpHandler {
} else if (resource.isDirectory()) {
logger.log(Level.FINEST, "we have a directory request");
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)
.query(url.getQuery())
.fragment(url.getFragment())

View file

@ -43,7 +43,7 @@ public class HtmlTemplateResource implements HttpServerResource {
private final String suffix;
protected final boolean negotiateLocale;
protected final boolean shouldNegotiateLocale;
protected final HttpResponseBuilder responseBuilder;
@ -54,7 +54,7 @@ public class HtmlTemplateResource implements HttpServerResource {
this.templateResourceHandler = templateResourceHandler;
this.application = httpRouterContext.getAttributes().get(Application.class, "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");
Objects.requireNonNull(responseBuilder);
Path root = templateResourceHandler.getRoot();
@ -63,7 +63,7 @@ public class HtmlTemplateResource implements HttpServerResource {
throw new IllegalArgumentException("no home path set for template resource resolving");
}
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() +
" root = " + root +
" resource path = " + resourcePath +
@ -191,11 +191,11 @@ public class HtmlTemplateResource implements HttpServerResource {
}
public String url(HttpRequest request, String rel, boolean absolute) {
String prefix = application.getSettings().get("web.prefix", "/");
if (!prefix.endsWith("/")) {
prefix = prefix + "/";
String webPrefix = application.getSettings().get("web.prefix", "/");
if (!webPrefix.endsWith("/")) {
webPrefix = webPrefix + "/";
}
URL url = request.getServerURL().resolve(prefix).resolve(rel);
URL url = request.getBaseURL().resolve(webPrefix).resolve(rel);
return absolute ? url.toExternalForm() : toOrigin(url);
}

View file

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

View file

@ -115,18 +115,16 @@ public abstract class DefaultMarkupTemplate extends BaseTemplate {
}
public String url(String rel, boolean absolute) {
String prefix = application.getSettings().get("web.prefix", "/");
if (!prefix.endsWith("/")) {
prefix = prefix + "/";
String webPrefix = application.getSettings().get("web.prefix", "/");
if (!webPrefix.endsWith("/")) {
webPrefix = webPrefix + "/";
}
if (request != null) {
URL url = request.getServerURL().resolve(prefix).resolve(rel);
logger.log(Level.FINEST, "server base URL = " + request.getServerURL() +
" prefix = " + prefix + " rel = " + rel + " --> " + url);
URL url = request.getBaseURL().resolve(webPrefix).resolve(rel);
return absolute ? url.toExternalForm() : toOrigin(url);
} else {
logger.log(Level.WARNING, "request is null, returning " + prefix + rel);
return prefix + rel;
logger.log(Level.WARNING, "request is null, returning " + webPrefix + rel);
return webPrefix + rel;
}
}
@ -135,7 +133,7 @@ public abstract class DefaultMarkupTemplate extends BaseTemplate {
}
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 (getModel().containsKey("session.url.enabled") && getModel().containsKey("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);
return;
}
if (!negotiateLocale) {
if (!shouldNegotiateLocale) {
// if no locale negotiation configured, set always the applicaiton locale. This constant value never changes.
templateResolver.setLocale(application.getLocale());
Writable writable = template.make(binding.getVariables());

View file

@ -6,7 +6,7 @@ dependencyResolutionManagement {
version('netty', '4.1.107.Final')
version('netty-tcnative', '2.0.62.Final')
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-handler', 'io.netty', 'netty-handler').versionRef('netty')
library('netty-handler-proxy', 'io.netty', 'netty-handler-proxy').versionRef('netty')