|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2021 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See http://swift.org/LICENSE.txt for license information |
| 8 | + See http://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | + */ |
| 10 | + |
| 11 | +import Basics |
| 12 | +import PackageLoading |
| 13 | +import PackageModel |
| 14 | + |
| 15 | +import TSCBasic |
| 16 | +import TSCUtility |
| 17 | + |
| 18 | +import struct Foundation.URL |
| 19 | +import struct Foundation.URLComponents |
| 20 | +import struct Foundation.URLQueryItem |
| 21 | + |
| 22 | +import Dispatch |
| 23 | + |
| 24 | +public enum RegistryError: Error { |
| 25 | + case invalidPackage(PackageReference) |
| 26 | + case invalidOperation |
| 27 | + case invalidResponse |
| 28 | + case invalidURL |
| 29 | + case invalidChecksum(expected: String, actual: String) |
| 30 | +} |
| 31 | + |
| 32 | +public final class RegistryManager { |
| 33 | + internal static var archiverFactory: (FileSystem) -> Archiver = { fileSystem in |
| 34 | + return ZipArchiver(fileSystem: fileSystem) |
| 35 | + } |
| 36 | + |
| 37 | + internal static var clientFactory: (DiagnosticsEngine?) -> HTTPClientProtocol = { diagnosticEngine in |
| 38 | + var configuration = HTTPClientConfiguration() |
| 39 | + return HTTPClient(configuration: configuration, handler: nil, diagnosticsEngine: diagnosticEngine) |
| 40 | + } |
| 41 | + |
| 42 | + private static var cache = ThreadSafeKeyValueStore<URL, RegistryManager>() |
| 43 | + |
| 44 | + private let registryBaseURL: Foundation.URL |
| 45 | + private let client: HTTPClientProtocol |
| 46 | + private let identityResolver: IdentityResolver |
| 47 | + private let diagnosticEngine: DiagnosticsEngine? |
| 48 | + |
| 49 | + public init(registryBaseURL: Foundation.URL, |
| 50 | + identityResolver: IdentityResolver, |
| 51 | + diagnosticEngine: DiagnosticsEngine? = nil) |
| 52 | + { |
| 53 | + self.registryBaseURL = registryBaseURL |
| 54 | + self.client = Self.clientFactory(diagnosticEngine) |
| 55 | + self.identityResolver = identityResolver |
| 56 | + self.diagnosticEngine = diagnosticEngine |
| 57 | + } |
| 58 | + |
| 59 | + public func fetchVersions( |
| 60 | + of package: PackageReference, |
| 61 | + on queue: DispatchQueue, |
| 62 | + completion: @escaping (Result<[Version], Error>) -> Void |
| 63 | + ) { |
| 64 | + guard case let (scope, name)? = package.scopeAndName else { |
| 65 | + return completion(.failure(RegistryError.invalidPackage(package))) |
| 66 | + } |
| 67 | + |
| 68 | + var components = URLComponents(url: registryBaseURL, resolvingAgainstBaseURL: true)! |
| 69 | + components.path += "/\(scope)/\(name)" |
| 70 | + |
| 71 | + guard let url = components.url else { |
| 72 | + return completion(.failure(RegistryError.invalidURL)) |
| 73 | + } |
| 74 | + |
| 75 | + let request = HTTPClient.Request( |
| 76 | + method: .get, |
| 77 | + url: url, |
| 78 | + headers: [ |
| 79 | + "Accept": "application/vnd.swift.registry.v1+json" |
| 80 | + ] |
| 81 | + ) |
| 82 | + |
| 83 | + client.execute(request, progress: nil) { result in |
| 84 | + completion(result.tryMap { response in |
| 85 | + if response.statusCode == 200, |
| 86 | + response.headers.get("Content-Version").first == "1", |
| 87 | + response.headers.get("Content-Type").first?.hasPrefix("application/json") == true, |
| 88 | + let data = response.body, |
| 89 | + case .dictionary(let payload) = try? JSON(data: data), |
| 90 | + case .dictionary(let releases) = payload["releases"] |
| 91 | + { |
| 92 | + let versions = releases.filter { (try? $0.value.getJSON("problem")) == nil } |
| 93 | + .compactMap { Version(string: $0.key) } |
| 94 | + .sorted(by: >) |
| 95 | + return versions |
| 96 | + } else { |
| 97 | + throw RegistryError.invalidResponse |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public func fetchManifest( |
| 104 | + for version: Version, |
| 105 | + of package: PackageReference, |
| 106 | + using manifestLoader: ManifestLoaderProtocol, |
| 107 | + toolsVersion: ToolsVersion = .currentToolsVersion, |
| 108 | + swiftLanguageVersion: SwiftLanguageVersion? = nil, |
| 109 | + on queue: DispatchQueue, |
| 110 | + completion: @escaping (Result<Manifest, Error>) -> Void |
| 111 | + ) { |
| 112 | + guard case let (scope, name)? = package.scopeAndName else { |
| 113 | + return completion(.failure(RegistryError.invalidPackage(package))) |
| 114 | + } |
| 115 | + |
| 116 | + var components = URLComponents(url: registryBaseURL, resolvingAgainstBaseURL: true)! |
| 117 | + components.path += "/\(scope)/\(name)/\(version)/Package.swift" |
| 118 | + if let swiftLanguageVersion = swiftLanguageVersion { |
| 119 | + components.queryItems = [ |
| 120 | + URLQueryItem(name: "swift-version", value: swiftLanguageVersion.rawValue) |
| 121 | + ] |
| 122 | + } |
| 123 | + |
| 124 | + guard let url = components.url else { |
| 125 | + return completion(.failure(RegistryError.invalidURL)) |
| 126 | + } |
| 127 | + |
| 128 | + let request = HTTPClient.Request( |
| 129 | + method: .get, |
| 130 | + url: url, |
| 131 | + headers: [ |
| 132 | + "Accept": "application/vnd.swift.registry.v1+swift" |
| 133 | + ] |
| 134 | + ) |
| 135 | + |
| 136 | + client.execute(request, progress: nil) { result in |
| 137 | + do { |
| 138 | + if case .failure(let error) = result { |
| 139 | + throw error |
| 140 | + } else if case .success(let response) = result, |
| 141 | + response.statusCode == 200, |
| 142 | + response.headers.get("Content-Version").first == "1", |
| 143 | + response.headers.get("Content-Type").first?.hasPrefix("text/x-swift") == true, |
| 144 | + let data = response.body |
| 145 | + { |
| 146 | + let fileSystem = InMemoryFileSystem() |
| 147 | + |
| 148 | + let filename: String |
| 149 | + if let swiftLanguageVersion = swiftLanguageVersion { |
| 150 | + filename = Manifest.basename + "@swift-\(swiftLanguageVersion).swift" |
| 151 | + } else { |
| 152 | + filename = Manifest.basename + ".swift" |
| 153 | + } |
| 154 | + |
| 155 | + try fileSystem.writeFileContents(.root.appending(component: filename), bytes: ByteString(data)) |
| 156 | + manifestLoader.load( |
| 157 | + at: .root, |
| 158 | + packageIdentity: package.identity, |
| 159 | + packageKind: .remote, |
| 160 | + packageLocation: package.location, |
| 161 | + version: version, |
| 162 | + revision: nil, |
| 163 | + toolsVersion: .currentToolsVersion, |
| 164 | + identityResolver: self.identityResolver, |
| 165 | + fileSystem: fileSystem, |
| 166 | + diagnostics: self.diagnosticEngine, |
| 167 | + on: .sharedConcurrent, |
| 168 | + completion: completion |
| 169 | + ) |
| 170 | + } else { |
| 171 | + throw RegistryError.invalidResponse |
| 172 | + } |
| 173 | + } catch { |
| 174 | + queue.async { |
| 175 | + completion(.failure(error)) |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + public func downloadSourceArchive( |
| 182 | + for version: Version, |
| 183 | + of package: PackageReference, |
| 184 | + into fileSystem: FileSystem, |
| 185 | + at destinationPath: AbsolutePath, |
| 186 | + expectedChecksum: ByteString? = nil, |
| 187 | + on queue: DispatchQueue, |
| 188 | + completion: @escaping (Result<Void, Error>) -> Void |
| 189 | + ) { |
| 190 | + guard case let (scope, name)? = package.scopeAndName else { |
| 191 | + return completion(.failure(RegistryError.invalidPackage(package))) |
| 192 | + } |
| 193 | + |
| 194 | + var components = URLComponents(url: registryBaseURL, resolvingAgainstBaseURL: true)! |
| 195 | + components.path += "/\(scope)/\(name)/\(version).zip" |
| 196 | + |
| 197 | + guard let url = components.url else { |
| 198 | + return completion(.failure(RegistryError.invalidURL)) |
| 199 | + } |
| 200 | + |
| 201 | + let request = HTTPClient.Request( |
| 202 | + method: .get, |
| 203 | + url: url, |
| 204 | + headers: [ |
| 205 | + "Accept": "application/vnd.swift.registry.v1+zip" |
| 206 | + ] |
| 207 | + ) |
| 208 | + |
| 209 | + client.execute(request, progress: nil) { result in |
| 210 | + switch result { |
| 211 | + case .success(let response): |
| 212 | + if response.statusCode == 200, |
| 213 | + response.headers.get("Content-Version").first == "1", |
| 214 | + response.headers.get("Content-Type").first?.hasPrefix("application/zip") == true, |
| 215 | + let digest = response.headers.get("Digest").first, |
| 216 | + let data = response.body |
| 217 | + { |
| 218 | + do { |
| 219 | + let contents = ByteString(data) |
| 220 | + let advertisedChecksum = digest.spm_dropPrefix("sha-256=") |
| 221 | + let actualChecksum = SHA256().hash(contents).hexadecimalRepresentation |
| 222 | + |
| 223 | + guard (expectedChecksum?.hexadecimalRepresentation ?? actualChecksum) == actualChecksum, |
| 224 | + advertisedChecksum == actualChecksum |
| 225 | + else { |
| 226 | + throw RegistryError.invalidChecksum( |
| 227 | + expected: expectedChecksum?.hexadecimalRepresentation ?? advertisedChecksum, |
| 228 | + actual: actualChecksum |
| 229 | + ) |
| 230 | + } |
| 231 | + |
| 232 | + let archivePath = destinationPath.withExtension("zip") |
| 233 | + try fileSystem.writeFileContents(archivePath, bytes: contents) |
| 234 | + |
| 235 | + try fileSystem.createDirectory(destinationPath, recursive: true) |
| 236 | + |
| 237 | + let archiver = Self.archiverFactory(fileSystem) |
| 238 | + // TODO: Bail if archive contains relative paths or overlapping files |
| 239 | + archiver.extract(from: archivePath, to: destinationPath) { result in |
| 240 | + completion(result) |
| 241 | + try? fileSystem.removeFileTree(archivePath) |
| 242 | + } |
| 243 | + } catch { |
| 244 | + try? fileSystem.removeFileTree(destinationPath) |
| 245 | + completion(.failure(error)) |
| 246 | + } |
| 247 | + } else { |
| 248 | + completion(.failure(RegistryError.invalidResponse)) |
| 249 | + } |
| 250 | + case .failure(let error): |
| 251 | + completion(.failure(error)) |
| 252 | + } |
| 253 | + } |
| 254 | + } |
| 255 | +} |
| 256 | + |
| 257 | +private extension String { |
| 258 | + /// Drops the given suffix from the string, if present. |
| 259 | + func spm_dropPrefix(_ prefix: String) -> String { |
| 260 | + if hasPrefix(prefix) { |
| 261 | + return String(dropFirst(prefix.count)) |
| 262 | + } |
| 263 | + return self |
| 264 | + } |
| 265 | +} |
| 266 | + |
| 267 | +private extension AbsolutePath { |
| 268 | + func withExtension(_ extension: String) -> AbsolutePath { |
| 269 | + guard !self.isRoot else { return self } |
| 270 | + let `extension` = `extension`.spm_dropPrefix(".") |
| 271 | + return AbsolutePath(self, RelativePath("..")).appending(component: "\(basename).\(`extension`)") |
| 272 | + } |
| 273 | +} |
| 274 | + |
| 275 | +// FIXME: Implement in PackageIdentity |
| 276 | +private extension PackageReference { |
| 277 | + var scopeAndName: (String, String)? { |
| 278 | + let components = identity.description.split(separator: ".", maxSplits: 1, omittingEmptySubsequences: true) |
| 279 | + guard let scope = components.first, |
| 280 | + let name = components.last, |
| 281 | + components.count == 2 |
| 282 | + else { return nil } |
| 283 | + |
| 284 | + return (String(scope), String(name)) |
| 285 | + } |
| 286 | +} |
0 commit comments