Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide access to the default redirect handler #5338

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
geoand marked this conversation as resolved.
Show resolved Hide resolved

/**
* <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;
geoand marked this conversation as resolved.
Show resolved Hide resolved

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;
}

}
Loading