Skip to content

Commit

Permalink
Provide access to the default redirect handler
Browse files Browse the repository at this point in the history
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
  • Loading branch information
geoand committed Oct 2, 2024
1 parent c70a816 commit 62138e8
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 58 deletions.
10 changes: 10 additions & 0 deletions vertx-core/src/main/java/io/vertx/core/http/HttpClientAgent.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -34,6 +37,13 @@
@VertxGen
public interface HttpClientAgent extends HttpClient, Measured {

/**
* Constant containing the default redirect handler of used by the client.
* This is useful for building redirect handlers that need to delegate to the default behavior.
*/
@GenIgnore
Function<HttpClientResponse, Future<RequestOptions>> DEFAULT_REDIRECT_HANDLER = new DefaultRedirectHandler();

/**
* <p>Update the client with new SSL {@code options}, the update happens if the options object is valid and different
* from the existing options object.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2011-2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
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<HttpClientResponse, Future<RequestOptions>> {

public DefaultRedirectHandler() {
}

@Override
public Future<RequestOptions> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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<HttpClientResponse, Future<RequestOptions>> 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<EndpointKey, SharedHttpClientConnectionGroup> httpCM;
private final EndpointResolverInternal endpointResolver;
private volatile Function<HttpClientResponse, Future<RequestOptions>> redirectHandler = DEFAULT_HANDLER;
private volatile Function<HttpClientResponse, Future<RequestOptions>> redirectHandler = DEFAULT_REDIRECT_HANDLER;
private long timerID;
private volatile Handler<HttpConnection> connectionHandler;
private final Function<ContextInternal, ContextInternal> contextProvider;
Expand Down Expand Up @@ -223,7 +167,7 @@ protected void doClose(Promise<Void> p) {

public void redirectHandler(Function<HttpClientResponse, Future<RequestOptions>> handler) {
if (handler == null) {
handler = DEFAULT_HANDLER;
handler = DEFAULT_REDIRECT_HANDLER;
}
redirectHandler = handler;
}
Expand Down Expand Up @@ -487,4 +431,5 @@ HttpClientRequest createRequest(HttpConnection connection, HttpClientStream stre
}
return request;
}

}

0 comments on commit 62138e8

Please sign in to comment.