Skip to content

Commit f2ebb11

Browse files
authored
fix: skills download redirect error (#7)
1 parent e81cc76 commit f2ebb11

3 files changed

Lines changed: 34 additions & 13 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
group=io.flamingock
2-
version=1.2.0-beta
2+
version=1.2.0-beta.2

src/main/java/io/flamingock/cli/executor/util/http/HttpFileDownloader.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ public class HttpFileDownloader {
3737
private final Duration requestTimeout;
3838

3939
public HttpFileDownloader() {
40-
this(DEFAULT_CONNECT_TIMEOUT, DEFAULT_REQUEST_TIMEOUT, (request, target, resolvedConnectTimeout) -> HttpClient.newBuilder()
41-
.connectTimeout(resolvedConnectTimeout)
42-
.build()
43-
.send(request, HttpResponse.BodyHandlers.ofFile(target)));
40+
this(DEFAULT_CONNECT_TIMEOUT, DEFAULT_REQUEST_TIMEOUT, (request, target, resolvedConnectTimeout) ->
41+
createHttpClient(resolvedConnectTimeout).send(request, HttpResponse.BodyHandlers.ofFile(target)));
4442
}
4543

4644
HttpFileDownloader(Duration connectTimeout, Duration requestTimeout, DownloadExecutor downloadExecutor) {
@@ -69,15 +67,9 @@ public Path downloadTo(Path workspace, URI sourceUri, String targetFileName, Str
6967
.header("User-Agent", userAgent)
7068
.GET()
7169
.build();
70+
HttpResponse<Path> response;
7271
try {
73-
HttpResponse<Path> response = downloadExecutor.download(request, targetFile, connectTimeout);
74-
if (response.statusCode() < 200 || response.statusCode() >= 300) {
75-
deletePartialFile(targetFile);
76-
throw new IOException("Download failed while fetching " + downloadLabel
77-
+ " with HTTP " + response.statusCode() + " from " + sourceUri
78-
+ ". Check your network connection and retry.");
79-
}
80-
return targetFile;
72+
response = downloadExecutor.download(request, targetFile, connectTimeout);
8173
} catch (IOException e) {
8274
deletePartialFile(targetFile);
8375
throw new IOException("Download failed while fetching " + downloadLabel
@@ -88,6 +80,21 @@ public Path downloadTo(Path workspace, URI sourceUri, String targetFileName, Str
8880
throw new IOException("Download interrupted while fetching " + downloadLabel
8981
+ ". Retry the command once the interruption is cleared.", e);
9082
}
83+
84+
if (response.statusCode() < 200 || response.statusCode() >= 300) {
85+
deletePartialFile(targetFile);
86+
throw new IOException("Download failed while fetching " + downloadLabel
87+
+ " with HTTP " + response.statusCode() + " from " + sourceUri
88+
+ ". Check your network connection and retry.");
89+
}
90+
return targetFile;
91+
}
92+
93+
static HttpClient createHttpClient(Duration connectTimeout) {
94+
return HttpClient.newBuilder()
95+
.connectTimeout(connectTimeout)
96+
.followRedirects(HttpClient.Redirect.NORMAL)
97+
.build();
9198
}
9299

93100
private void deletePartialFile(Path targetFile) throws IOException {

src/test/java/io/flamingock/cli/executor/util/http/HttpFileDownloaderTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import javax.net.ssl.SSLSession;
2222
import java.io.IOException;
2323
import java.net.URI;
24+
import java.net.http.HttpClient;
2425
import java.net.http.HttpHeaders;
2526
import java.net.http.HttpRequest;
2627
import java.net.http.HttpResponse;
@@ -68,9 +69,18 @@ void downloadTo_deletesPartialArchiveWhenServerReturnsFailure() {
6869

6970
assertTrue(exception.getMessage().contains(DOWNLOAD_LABEL));
7071
assertTrue(exception.getMessage().contains("HTTP 503"));
72+
assertEquals(1, countOccurrences(exception.getMessage(), "Download failed while fetching"));
7173
assertFalse(Files.exists(workspace.resolve(TARGET_FILE_NAME)));
7274
}
7375

76+
@Test
77+
void createHttpClient_followsStandardRedirects() {
78+
HttpClient client = HttpFileDownloader.createHttpClient(Duration.ofSeconds(10));
79+
80+
assertEquals(HttpClient.Redirect.NORMAL, client.followRedirects());
81+
assertEquals(Duration.ofSeconds(10), client.connectTimeout().orElseThrow());
82+
}
83+
7484
@Test
7585
void downloadTo_setsReasonableTimeoutsAndUserAgent() throws Exception {
7686
RecordingDownloadExecutor executor = new RecordingDownloadExecutor(200);
@@ -137,6 +147,10 @@ public HttpResponse<Path> download(HttpRequest request, Path target, Duration co
137147
}
138148
}
139149

150+
private static int countOccurrences(String text, String token) {
151+
return text.split(java.util.regex.Pattern.quote(token), -1).length - 1;
152+
}
153+
140154
private record HttpResponseStub(int statusCode, Path body, HttpRequest request) implements HttpResponse<Path> {
141155

142156
@Override

0 commit comments

Comments
 (0)