Skip to content

Commit

Permalink
tmp redirect handler
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Nov 6, 2023
1 parent 6f89046 commit 01b88b4
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion src/test/java/itest/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@
*/
package itest.util;

import java.net.URI;

import io.cryostat.core.sys.Environment;
import io.cryostat.util.HttpStatusCodeIdentifier;

import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpHeaders;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.file.FileSystem;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.RequestOptions;
import io.vertx.core.http.impl.HttpUtils;
import io.vertx.ext.web.client.WebClient;
import org.jboss.logging.Logger;

public class Utils {

Expand Down Expand Up @@ -50,7 +59,63 @@ public class Utils {

private static final Vertx VERTX =
Vertx.vertx(new VertxOptions().setPreferNativeTransport(true));
public static final HttpClient HTTP_CLIENT = VERTX.createHttpClient(HTTP_CLIENT_OPTIONS);
public static final HttpClient HTTP_CLIENT =
VERTX.createHttpClient(HTTP_CLIENT_OPTIONS)
.redirectHandler(
resp -> {
Logger logger = Logger.getLogger(WebClient.class);
// see io.vertx.core.http.impl.HttpClientImpl.DEFAULT_HANDLER
// lightly modified version of the default handler. Ignores handling
// of specific HTTP GET and HEAD methods and allows any verb to be
// redirected, and does not rewrite port numbers
try {
int statusCode = resp.statusCode();
String location = resp.getHeader(HttpHeaders.LOCATION);
if (location == null
|| !HttpStatusCodeIdentifier.isRedirectCode(
statusCode)) {
logger.infov(
"Ignoring non-redirect request: {0} {1}",
location, statusCode);
return null;
}
HttpMethod m = resp.request().getMethod();
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;
} else if (chend == 's') {
ssl = true;
} 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(HttpHeaders.CONTENT_LENGTH);
logger.infov("Redirecting request to: {0}", options.toJson());
return Future.succeededFuture(options);
} catch (Exception e) {
return Future.failedFuture(e);
}
});
private static final WebClient WEB_CLIENT_INSTANCE = WebClient.wrap(HTTP_CLIENT);

public static WebClient getWebClient() {
Expand Down

0 comments on commit 01b88b4

Please sign in to comment.