1111//===----------------------------------------------------------------------===//
1212
1313import AsyncAlgorithms
14- import AsyncHTTPClient
1514import GeneratorEngine
1615import RegexBuilder
1716
@@ -28,16 +27,19 @@ let byteCountFormatter = ByteCountFormatter()
2827
2928extension SwiftSDKGenerator {
3029 func downloadArtifacts(
31- _ client: HTTPClient , _ engine: Engine ,
30+ _ client: some HTTPClientProtocol , _ engine: Engine ,
3231 downloadableArtifacts: inout DownloadableArtifacts ,
3332 itemsToDownload: @Sendable ( DownloadableArtifacts ) -> [ DownloadableArtifacts . Item ]
3433 ) async throws {
3534 logGenerationStep ( " Downloading required toolchain packages... " )
36- var headRequest = HTTPClientRequest ( url: downloadableArtifacts. hostLLVM. remoteURL. absoluteString)
37- headRequest. method = . HEAD
38- headRequest. headers = [ " Accept " : " */* " , " User-Agent " : " Swift SDK Generator " ]
39- let isLLVMBinaryArtifactAvailable = try await client. execute ( headRequest, deadline: . distantFuture)
40- . status == . ok
35+ let hostLLVMURL = downloadableArtifacts. hostLLVM. remoteURL
36+ // Workaround an issue with github.com returning 400 instead of 404 status to HEAD requests from AHC.
37+ let isLLVMBinaryArtifactAvailable = try await type ( of: client) . with ( http1Only: true ) {
38+ try await $0. head (
39+ url: hostLLVMURL. absoluteString,
40+ headers: [ " Accept " : " */* " , " User-Agent " : " Swift SDK Generator " ]
41+ )
42+ }
4143
4244 if !isLLVMBinaryArtifactAvailable {
4345 downloadableArtifacts. useLLVMSources ( )
@@ -46,7 +48,7 @@ extension SwiftSDKGenerator {
4648 let results = try await withThrowingTaskGroup ( of: FileCacheRecord . self) { group in
4749 for item in itemsToDownload ( downloadableArtifacts) {
4850 group. addTask {
49- try await engine [ DownloadArtifactQuery ( artifact: item) ]
51+ try await engine [ DownloadArtifactQuery ( artifact: item, httpClient : client ) ]
5052 }
5153 }
5254
@@ -64,7 +66,7 @@ extension SwiftSDKGenerator {
6466 }
6567
6668 func downloadUbuntuPackages(
67- _ client: HTTPClient ,
69+ _ client: some HTTPClientProtocol ,
6870 _ engine: Engine ,
6971 requiredPackages: [ String ] ,
7072 versionsConfiguration: VersionsConfiguration ,
@@ -112,7 +114,7 @@ extension SwiftSDKGenerator {
112114 let pathsConfiguration = self . pathsConfiguration
113115
114116 try await inTemporaryDirectory { fs, tmpDir in
115- let downloadedFiles = try await self . downloadFiles ( from: urls, to: tmpDir, engine)
117+ let downloadedFiles = try await self . downloadFiles ( from: urls, to: tmpDir, client , engine)
116118 report ( downloadedFiles: downloadedFiles)
117119
118120 for fileName in urls. map ( \. lastPathComponent) {
@@ -123,11 +125,13 @@ extension SwiftSDKGenerator {
123125 try createDirectoryIfNeeded ( at: pathsConfiguration. toolchainBinDirPath)
124126 }
125127
126- func downloadFiles( from urls: [ URL ] , to directory: FilePath , _ engine: Engine ) async throws -> [ ( URL , UInt64 ) ] {
128+ func downloadFiles( from urls: [ URL ] , to directory: FilePath , _ client : some HTTPClientProtocol , _ engine: Engine ) async throws -> [ ( URL , UInt64 ) ] {
127129 try await withThrowingTaskGroup ( of: ( URL, UInt64) . self) {
128130 for url in urls {
129131 $0. addTask {
130- let downloadedFilePath = try await engine [ DownloadFileQuery ( remoteURL: url, localDirectory: directory) ]
132+ let downloadedFilePath = try await engine [ DownloadFileQuery (
133+ remoteURL: url, localDirectory: directory, httpClient: client
134+ ) ]
131135 let filePath = downloadedFilePath. path
132136 guard let fileSize = try FileManager . default. attributesOfItem (
133137 atPath: filePath. string
@@ -153,12 +157,12 @@ private func report(downloadedFiles: [(URL, UInt64)]) {
153157 }
154158}
155159
156- extension HTTPClient {
160+ extension HTTPClientProtocol {
157161 private func downloadUbuntuPackagesList(
158162 from url: String ,
159163 isVerbose: Bool
160164 ) async throws -> String ? {
161- guard let packages = try await get ( url: url) . get ( ) . body? . unzip ( isVerbose: isVerbose) else {
165+ guard let packages = try await get ( url: url) . body? . unzip ( isVerbose: isVerbose) else {
162166 throw FileOperationError . downloadFailed ( url)
163167 }
164168
0 commit comments