Skip to content
Permalink
Browse files
Handle redirects manually
Reviewed-by: ehelin
  • Loading branch information
rwestberg committed Jan 17, 2020
1 parent a667c28 commit fd9d0e685aa7cc9197086d7b5fe3aff4ced8d5b6
Showing 2 changed files with 11 additions and 4 deletions.
@@ -150,21 +150,28 @@ private void awaitPublication(URI uri, Duration timeout) throws IOException {
var uriBuilder = URIBuilder.base(uri);
var client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(30))
.followRedirects(HttpClient.Redirect.NORMAL)
.build();
while (Instant.now().isBefore(end)) {
var uncachedUri = uriBuilder.setQuery(Map.of("nocache", UUID.randomUUID().toString())).build();
log.fine("Validating webrev URL: " + uncachedUri);
var request = HttpRequest.newBuilder(uncachedUri)
.timeout(Duration.ofSeconds(30))
.GET()
.build();
try {
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() < 400) {
if (response.statusCode() < 300) {
log.info(response.statusCode() + " when checking " + uncachedUri + " - success!");
// Success!
return;
}
if (response.statusCode() < 400) {
var newLocation = response.headers().firstValue("location");
if (newLocation.isPresent()) {
log.info("Webrev url redirection: " + newLocation.get());
uriBuilder = URIBuilder.base(newLocation.get());
continue;
}
}
log.info(response.statusCode() + " when checking " + uncachedUri + " - waiting...");
Thread.sleep(Duration.ofSeconds(10).toMillis());
} catch (InterruptedException ignored) {
@@ -41,7 +41,7 @@ public void handle(HttpExchange exchange) throws IOException {
var response = "ok!";
var responseBytes = response.getBytes(StandardCharsets.UTF_8);
if (!exchange.getRequestURI().toString().contains("final=true")) {
exchange.getResponseHeaders().add("Location", exchange.getRequestURI().toString() + "&final=true");
exchange.getResponseHeaders().add("Location", uri() + "&final=true");
exchange.sendResponseHeaders(302, responseBytes.length);
} else {
redirectFollowed = true;

0 comments on commit fd9d0e6

Please sign in to comment.