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 1, 2024
1 parent c70a816 commit a4cbd17
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.vertx.core.http;

import static io.vertx.core.http.HttpHeaders.CONTENT_LENGTH;
import io.vertx.core.Future;
import io.vertx.core.http.impl.HttpUtils;
import java.net.URI;
import java.util.function.Function;


class DefaultRedirectHandler implements Function<HttpClientResponse, Future<RequestOptions>> {

public static final DefaultRedirectHandler INSTANCE = new DefaultRedirectHandler();

private 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);
}
}
}
13 changes: 13 additions & 0 deletions vertx-core/src/main/java/io/vertx/core/http/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@

package io.vertx.core.http;

import static io.vertx.core.http.HttpHeaders.CONTENT_LENGTH;
import io.vertx.codegen.annotations.GenIgnore;
import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.Future;

import io.vertx.core.http.impl.HttpUtils;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

/**
* The API to interacts with an HTTP server.
Expand Down Expand Up @@ -113,4 +118,12 @@ default Future<Void> close() {
*/
Future<Void> shutdown(long timeout, TimeUnit unit);

/**
* 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<HttpClientResponse, Future<RequestOptions>> defaultRedirectHandler() {
return DefaultRedirectHandler.INSTANCE;
}
}
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 = defaultRedirectHandler();
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 = defaultRedirectHandler();
}
redirectHandler = handler;
}
Expand Down Expand Up @@ -487,4 +431,5 @@ HttpClientRequest createRequest(HttpConnection connection, HttpClientStream stre
}
return request;
}

}

0 comments on commit a4cbd17

Please sign in to comment.