From 4f979768575b261c4790c04ddfae0d3ff7ac6ee3 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Tue, 1 Oct 2024 13:34:31 +0300 Subject: [PATCH] Provide access to the default redirect handler This is useful for building redirect handlers for clients that need to delegate to the default behavior. In Quarkus we will leverage this to get rid of the deprecated (and since removed) calls to redirectHandler --- .../io/vertx/core/http/HttpClientAgent.java | 15 ++++ .../http/impl/DefaultRedirectHandler.java | 72 +++++++++++++++++++ .../vertx/core/http/impl/HttpClientImpl.java | 61 +--------------- 3 files changed, 90 insertions(+), 58 deletions(-) create mode 100644 vertx-core/src/main/java/io/vertx/core/http/impl/DefaultRedirectHandler.java diff --git a/vertx-core/src/main/java/io/vertx/core/http/HttpClientAgent.java b/vertx-core/src/main/java/io/vertx/core/http/HttpClientAgent.java index 1ac860af6da..f1356386c6a 100644 --- a/vertx-core/src/main/java/io/vertx/core/http/HttpClientAgent.java +++ b/vertx-core/src/main/java/io/vertx/core/http/HttpClientAgent.java @@ -1,9 +1,12 @@ package io.vertx.core.http; +import io.vertx.codegen.annotations.GenIgnore; import io.vertx.codegen.annotations.VertxGen; import io.vertx.core.Future; +import io.vertx.core.http.impl.DefaultRedirectHandler; import io.vertx.core.metrics.Measured; import io.vertx.core.net.ClientSSLOptions; +import java.util.function.Function; /** * An asynchronous HTTP client. @@ -34,6 +37,9 @@ @VertxGen public interface HttpClientAgent extends HttpClient, Measured { + @GenIgnore + Function> DEFAULT_REDIRECT_HANDLER = new DefaultRedirectHandler(); + /** *

Update the client with new SSL {@code options}, the update happens if the options object is valid and different * from the existing options object. @@ -72,4 +78,13 @@ default Future updateSSLOptions(ClientSSLOptions options) { */ Future connect(HttpConnectOptions options); + /** + * Provides a way for users of the client to get access to the default redirect handler. + * This is useful for building redirect handlers that need to delegate to the default behavior. + */ + @GenIgnore + default Function> defaultRedirectHandler() { + return DEFAULT_REDIRECT_HANDLER; + } + } diff --git a/vertx-core/src/main/java/io/vertx/core/http/impl/DefaultRedirectHandler.java b/vertx-core/src/main/java/io/vertx/core/http/impl/DefaultRedirectHandler.java new file mode 100644 index 00000000000..8feb888b58c --- /dev/null +++ b/vertx-core/src/main/java/io/vertx/core/http/impl/DefaultRedirectHandler.java @@ -0,0 +1,72 @@ +package io.vertx.core.http.impl; + +import static io.vertx.core.http.HttpHeaders.CONTENT_LENGTH; +import io.vertx.core.Future; +import io.vertx.core.http.HttpClientResponse; +import io.vertx.core.http.HttpHeaders; +import io.vertx.core.http.HttpMethod; +import io.vertx.core.http.RequestOptions; +import java.net.URI; +import java.util.function.Function; + + +public class DefaultRedirectHandler implements Function> { + + public DefaultRedirectHandler() { + } + + @Override + public Future apply(HttpClientResponse resp) { + try { + int statusCode = resp.statusCode(); + String location = resp.getHeader(HttpHeaders.LOCATION); + if (location != null && (statusCode == 301 || statusCode == 302 || statusCode == 303 || statusCode == 307 + || statusCode == 308)) { + HttpMethod m = resp.request().getMethod(); + if (statusCode == 303) { + m = HttpMethod.GET; + } else if (m != HttpMethod.GET && m != HttpMethod.HEAD) { + return null; + } + URI uri = HttpUtils.resolveURIReference(resp.request().absoluteURI(), location); + boolean ssl; + int port = uri.getPort(); + String protocol = uri.getScheme(); + char chend = protocol.charAt(protocol.length() - 1); + if (chend == 'p') { + ssl = false; + if (port == -1) { + port = 80; + } + } else if (chend == 's') { + ssl = true; + if (port == -1) { + port = 443; + } + } else { + return null; + } + String requestURI = uri.getPath(); + if (requestURI == null || requestURI.isEmpty()) { + requestURI = "/"; + } + String query = uri.getQuery(); + if (query != null) { + requestURI += "?" + query; + } + RequestOptions options = new RequestOptions(); + options.setMethod(m); + options.setHost(uri.getHost()); + options.setPort(port); + options.setSsl(ssl); + options.setURI(requestURI); + options.setHeaders(resp.request().headers()); + options.removeHeader(CONTENT_LENGTH); + return Future.succeededFuture(options); + } + return null; + } catch (Exception e) { + return Future.failedFuture(e); + } + } +} diff --git a/vertx-core/src/main/java/io/vertx/core/http/impl/HttpClientImpl.java b/vertx-core/src/main/java/io/vertx/core/http/impl/HttpClientImpl.java index 66291c811f8..7e6fc0ccd13 100644 --- a/vertx-core/src/main/java/io/vertx/core/http/impl/HttpClientImpl.java +++ b/vertx-core/src/main/java/io/vertx/core/http/impl/HttpClientImpl.java @@ -34,14 +34,11 @@ import io.vertx.core.spi.metrics.PoolMetrics; import java.lang.ref.WeakReference; -import java.net.URI; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Function; import java.util.regex.Pattern; -import static io.vertx.core.http.HttpHeaders.*; - /** * This class is thread-safe. * @@ -52,63 +49,10 @@ public class HttpClientImpl extends HttpClientBase implements HttpClientInternal // Pattern to check we are not dealing with an absoluate URI static final Pattern ABS_URI_START_PATTERN = Pattern.compile("^\\p{Alpha}[\\p{Alpha}\\p{Digit}+.\\-]*:"); - private static final Function> DEFAULT_HANDLER = resp -> { - try { - int statusCode = resp.statusCode(); - String location = resp.getHeader(HttpHeaders.LOCATION); - if (location != null && (statusCode == 301 || statusCode == 302 || statusCode == 303 || statusCode == 307 || statusCode == 308)) { - HttpMethod m = resp.request().getMethod(); - if (statusCode == 303) { - m = HttpMethod.GET; - } else if (m != HttpMethod.GET && m != HttpMethod.HEAD) { - return null; - } - URI uri = HttpUtils.resolveURIReference(resp.request().absoluteURI(), location); - boolean ssl; - int port = uri.getPort(); - String protocol = uri.getScheme(); - char chend = protocol.charAt(protocol.length() - 1); - if (chend == 'p') { - ssl = false; - if (port == -1) { - port = 80; - } - } else if (chend == 's') { - ssl = true; - if (port == -1) { - port = 443; - } - } else { - return null; - } - String requestURI = uri.getPath(); - if (requestURI == null || requestURI.isEmpty()) { - requestURI = "/"; - } - String query = uri.getQuery(); - if (query != null) { - requestURI += "?" + query; - } - RequestOptions options = new RequestOptions(); - options.setMethod(m); - options.setHost(uri.getHost()); - options.setPort(port); - options.setSsl(ssl); - options.setURI(requestURI); - options.setHeaders(resp.request().headers()); - options.removeHeader(CONTENT_LENGTH); - return Future.succeededFuture(options); - } - return null; - } catch (Exception e) { - return Future.failedFuture(e); - } - }; - private final PoolOptions poolOptions; private final ResourceManager httpCM; private final EndpointResolverInternal endpointResolver; - private volatile Function> redirectHandler = DEFAULT_HANDLER; + private volatile Function> redirectHandler = defaultRedirectHandler(); private long timerID; private volatile Handler connectionHandler; private final Function contextProvider; @@ -223,7 +167,7 @@ protected void doClose(Promise p) { public void redirectHandler(Function> handler) { if (handler == null) { - handler = DEFAULT_HANDLER; + handler = defaultRedirectHandler(); } redirectHandler = handler; } @@ -487,4 +431,5 @@ HttpClientRequest createRequest(HttpConnection connection, HttpClientStream stre } return request; } + }