Use more HTTPTypes

This commit is contained in:
Kabir Oberai
2024-12-24 02:54:09 +05:30
Unverified
parent 71a82d1e05
commit 94dbbf1726
14 changed files with 112 additions and 232 deletions
+3
View File
@@ -33,6 +33,7 @@ let package = Package(
.package(url: "https://github.com/kabiroberai/zsign", .upToNextMinor(from: "1.3.0")),
.package(url: "https://github.com/apple/swift-system", from: "1.4.0"),
.package(url: "https://github.com/apple/swift-http-types", from: "1.3.1"),
.package(url: "https://github.com/apple/swift-certificates", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-crypto", from: "3.9.1"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
@@ -84,6 +85,8 @@ let package = Package(
.product(name: "_CryptoExtras", package: "swift-crypto"),
.product(name: "X509", package: "swift-certificates"),
.product(name: "BigInt", package: "BigInt"),
.product(name: "HTTPTypes", package: "swift-http-types"),
.product(name: "HTTPTypesFoundation", package: "swift-http-types"),
.product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"),
.product(
name: "OpenAPIAsyncHTTPClient",
@@ -78,34 +78,30 @@ public struct DeveloperServicesClient: Sendable {
let deviceInfo = try deviceInfoProvider.fetch()
var httpRequest = HTTPRequest(url: url, method: "POST")
var httpRequest = HTTPRequest(method: .post, url: url)
let acceptedLanguages = Locale.preferredLanguages.joined(separator: ", ")
httpRequest.headers["Accept-Language"] = acceptedLanguages
httpRequest.headers["Accept"] = request.apiVersion.accept
httpRequest.headers["Content-Type"] = request.apiVersion.contentType
httpRequest.headers["User-Agent"] = "Xcode"
request.methodOverride.map { httpRequest.headers["X-HTTP-Method-Override"] = $0 }
httpRequest.headerFields[.acceptLanguage] = acceptedLanguages
httpRequest.headerFields[.accept] = request.apiVersion.accept
httpRequest.headerFields[.contentType] = request.apiVersion.contentType
httpRequest.headerFields[.userAgent] = "Xcode"
request.methodOverride.map { httpRequest.headerFields[.init("X-HTTP-Method-Override")!] = $0 }
httpRequest.headers[DeviceInfo.xcodeVersionKey] = DeviceInfo.xcodeVersion
httpRequest.headers[DeviceInfo.clientInfoKey] = deviceInfo.clientInfo.clientString
httpRequest.headers[DeviceInfo.deviceIDKey] = deviceInfo.deviceID
httpRequest.headerFields[.init(DeviceInfo.xcodeVersionKey)!] = DeviceInfo.xcodeVersion
httpRequest.headerFields[.init(DeviceInfo.clientInfoKey)!] = deviceInfo.clientInfo.clientString
httpRequest.headerFields[.init(DeviceInfo.deviceIDKey)!] = deviceInfo.deviceID
httpRequest.headers["X-Apple-I-Identity-Id"] = loginToken.adsid
httpRequest.headers["X-Apple-App-Info"] = AppTokenKey.xcode.rawValue
httpRequest.headers["X-Apple-GS-Token"] = loginToken.token
httpRequest.headerFields[.init("X-Apple-I-Identity-Id")!] = loginToken.adsid
httpRequest.headerFields[.init("X-Apple-App-Info")!] = AppTokenKey.xcode.rawValue
httpRequest.headerFields[.init("X-Apple-GS-Token")!] = loginToken.token
anisetteData.dictionary.forEach { httpRequest.headers[$0] = $1 }
httpRequest.body = try .buffer(request.apiVersion.body(withParameters: request.parameters))
anisetteData.dictionary.forEach { httpRequest.headerFields[.init($0)!] = $1 }
request.configure(urlRequest: &httpRequest)
let resp = try await httpClient.makeRequest(httpRequest)
let body = try request.apiVersion.body(withParameters: request.parameters)
// we don't throw if data is nil because sometimes no data is
// okay (eg in the case of EmptyResponse)
let data = resp.body ?? Data()
let (_, data) = try await httpClient.makeRequest(httpRequest, body: body)
// String(data: data, encoding: .utf8).map { print("\(url): \($0)") }
@@ -167,29 +167,25 @@ public struct ADIDataProvider: AnisetteDataProvider {
let body = try Self.encoder.encode(GSARequest(request: request))
var request = HTTPRequest(
url: endpointURL,
method: "POST",
headers: [
"Content-Type": "text/x-xml-plist",
AnisetteData.localUserIDKey: self.localUserID,
"Accept-Language": "en_US"
],
body: .buffer(body)
)
request.headers["X-MMe-Country"] = Locale.current.regionCode
var request = HTTPRequest(url: endpointURL)
request.method = .post
request.headerFields = [
.contentType: "text/x-xml-plist",
.acceptLanguage: "en_US",
.init(AnisetteData.localUserIDKey)!: self.localUserID,
]
request.headerFields[.init("X-MMe-Country")!] = Locale.current.regionCode
request.headers[DeviceInfo.clientInfoKey] = try await clientInfo()
request.headers[DeviceInfo.deviceIDKey] = self.localUserUID.uuidString
request.headerFields[.init(DeviceInfo.clientInfoKey)!] = try await clientInfo()
request.headerFields[.init(DeviceInfo.deviceIDKey)!] = self.localUserUID.uuidString
// request.headers[DeviceInfo.clientInfoKey] = self.deviceInfo.clientInfo.clientString
// self.deviceInfo.dictionary.forEach { request.headers[$0] = $1 }
let resp = try await self.httpClient.makeRequest(request)
let (_, response) = try await self.httpClient.makeRequest(request, body: body)
guard let body = resp.body else { throw ADIError.noResponse }
// print(String(data: body, encoding: .utf8) ?? "<no utf8 data>")
// print(String(data: response, encoding: .utf8) ?? "<no utf8 data>")
return try GrandSlamOperationDecoder<Response>.decode(data: body)
return try GrandSlamOperationDecoder<Response>.decode(data: response)
}
private func endProvisioning(
@@ -34,7 +34,7 @@ struct OmnisetteADIProvider: RawADIProvider {
}
let body = try await client.makeRequest(
HTTPRequest(url: url.appendingPathComponent("v3/client_info"))
).body ?? Data()
).body
let clientInfo = try Self.decoder.decode(ClientInfo.self, from: body)
return clientInfo.clientInfo
}
@@ -74,13 +74,13 @@ struct OmnisetteADIProvider: RawADIProvider {
}
var request = HTTPRequest(url: url.appendingPathComponent("v3/get_headers"))
request.method = "POST"
request.body = .buffer(try Self.encoder.encode(Request(
request.method = .post
request.headerFields[.contentType] = "application/json"
let body = try Self.encoder.encode(Request(
identifier: userID.rawBytes,
adiPb: provisioningInfo
)))
request.headers["Content-Type"] = "application/json"
let response = try await client.makeRequest(request).body ?? Data()
))
let response = try await client.makeRequest(request, body: body).body
let decoded = try Self.decoder.decode(Response.self, from: response)
if let rinfo = UInt64(decoded.rinfo) {
routingInfo = rinfo
@@ -31,7 +31,7 @@ public actor SupersetteADIProvider: RawADIProvider {
let progString = "\(Int(progress * 100))%".padding(toLength: 4, leading: true)
print("\r[Downloading libraries] \(progString)", terminator: "")
fflush(stdoutSafe)
}.body!
}.body
try data.write(to: applemusic)
}
@@ -28,23 +28,21 @@ struct GrandSlamClient: Sendable {
let url = try await lookupManager.fetchURL(forEndpoint: R.endpoint)
let method = request.method(deviceInfo: deviceInfo, anisetteData: anisetteData)
var httpRequest = HTTPRequest(url: url, method: method.name)
httpRequest.headers = [
"Content-Type": "text/x-xml-plist",
DeviceInfo.clientInfoKey: deviceInfo.clientInfo.clientString
]
switch method {
var httpRequest = HTTPRequest(method: method.httpMethod, url: url)
httpRequest.headerFields[.contentType] = "text/x-xml-plist"
httpRequest.headerFields[.init(DeviceInfo.clientInfoKey)!] = deviceInfo.clientInfo.clientString
let body: Data? = switch method {
case .get:
break
nil
case .post(let body):
httpRequest.body = try .buffer(PropertyListSerialization.data(
try PropertyListSerialization.data(
fromPropertyList: body, format: .xml, options: 0
))
)
}
request.configure(request: &httpRequest, deviceInfo: deviceInfo, anisetteData: anisetteData)
let resp = try await httpClient.makeRequest(httpRequest)
return try R.Decoder.decode(data: resp.body ?? .init())
let resp = try await httpClient.makeRequest(httpRequest, body: body)
return try R.Decoder.decode(data: resp.body)
}
}
@@ -8,6 +8,7 @@
import Foundation
import Dependencies
import HTTPTypesFoundation
actor GrandSlamLookupManager {
@@ -37,18 +38,18 @@ actor GrandSlamLookupManager {
"X-Mme-Device-Id" = "[REDACTED]";
} */
var request = HTTPRequest(url: Self.lookupURL)
request.headers = [
DeviceInfo.clientInfoKey: deviceInfo.clientInfo.clientString,
DeviceInfo.deviceIDKey: deviceInfo.deviceID,
AnisetteData.iLocaleKey: Locale.current.identifier,
AnisetteData.timeZoneKey: TimeZone.current.identifier,
"X-Apple-I-TimeZone-Offset": "\(TimeZone.current.secondsFromGMT())"
request.headerFields = [
.init(DeviceInfo.clientInfoKey)!: deviceInfo.clientInfo.clientString,
.init(DeviceInfo.deviceIDKey)!: deviceInfo.deviceID,
.init(AnisetteData.iLocaleKey)!: Locale.current.identifier,
.init(AnisetteData.timeZoneKey)!: TimeZone.current.identifier,
.init("X-Apple-I-TimeZone-Offset")!: "\(TimeZone.current.secondsFromGMT())"
]
request.headers["X-MMe-Country"] = Locale.current.regionCode
request.headerFields[.init("X-MMe-Country")!] = Locale.current.regionCode
let resp = try await httpClient.makeRequest(request)
let (_, body) = try await httpClient.makeRequest(request)
return try self.decoder.decode(Response.self, from: resp.body ?? .init()).urls
return try self.decoder.decode(Response.self, from: body).urls
}
private func fetchEndpoints() async throws -> GrandSlamEndpoints {
@@ -70,8 +70,8 @@ extension GrandSlamOperationRequest {
static var endpoint: GrandSlamEndpoint { .lookup(\.gsService) }
func configure(request: inout HTTPRequest, deviceInfo: DeviceInfo, anisetteData: AnisetteData) {
request.headers["Accept"] = "*/*"
request.headers["User-Agent"] = deviceInfo.clientInfo.userAgent
request.headerFields[.accept] = "*/*"
request.headerFields[.userAgent] = deviceInfo.clientInfo.userAgent
}
func method(deviceInfo: DeviceInfo, anisetteData: AnisetteData) -> GrandSlamMethod {
@@ -12,10 +12,10 @@ enum GrandSlamMethod {
case get
case post([String: Any])
var name: String {
var httpMethod: HTTPRequest.Method {
switch self {
case .get: return "GET"
case .post: return "POST"
case .get: return .get
case .post: return .post
}
}
}
@@ -18,13 +18,13 @@ extension GrandSlamTwoFactorRequest {
var extraHeaders: [String: String] { [:] }
func configure(request: inout HTTPRequest, deviceInfo: DeviceInfo, anisetteData: AnisetteData) {
request.headers["Accept"] = "application/x-buddyml"
request.headers["Content-Type"] = "application/x-plist"
request.headers["X-Apple-App-Info"] = "com.apple.gs.xcode.auth"
request.headers[DeviceInfo.xcodeVersionKey] = DeviceInfo.xcodeVersion
request.headers["X-Apple-Identity-Token"] = loginData.identityToken
anisetteData.dictionary.forEach { request.headers[$0] = $1 }
extraHeaders.forEach { request.headers[$0] = $1 }
request.headerFields[.accept] = "application/x-buddyml"
request.headerFields[.contentType] = "application/x-plist"
request.headerFields[.init("X-Apple-App-Info")!] = "com.apple.gs.xcode.auth"
request.headerFields[.init(DeviceInfo.xcodeVersionKey)!] = DeviceInfo.xcodeVersion
request.headerFields[.init("X-Apple-Identity-Token")!] = loginData.identityToken
anisetteData.dictionary.forEach { request.headerFields[.init($0)!] = $1 }
extraHeaders.forEach { request.headerFields[.init($0)!] = $1 }
}
func method(deviceInfo: DeviceInfo, anisetteData: AnisetteData) -> GrandSlamMethod {
@@ -33,38 +33,6 @@ extension HTTPClientDependencyKey: DependencyKey {
}
extension HTTPClient: HTTPClientProtocol {
@discardableResult
public func makeRequest(_ request: HTTPRequest, onProgress: sending @isolated(any) (Double?) -> Void) async throws -> HTTPResponse {
// print("Requesting \(request.url)")
var httpRequest = HTTPClientRequest(url: request.url.absoluteString)
httpRequest.method = request.method.map(HTTPMethod.init(rawValue:)) ?? .GET
httpRequest.headers = HTTPHeaders(request.headers.map { $0 })
httpRequest.body = switch request.body {
case .buffer(let data):
.bytes(data)
case nil:
nil
}
let response = try await execute(httpRequest, deadline: .distantFuture)
let expectedBytes = response.headers.first(name: "content-length").flatMap(Int.init).map(Double.init)
if expectedBytes == nil {
await onProgress(nil)
}
var body = Data()
for try await chunk in response.body {
body += chunk.readableBytesView
if let expectedBytes {
await onProgress(min(Double(body.count) / expectedBytes, 1))
}
}
return HTTPResponse(
url: request.url,
status: Int(response.status.code),
headers: [:],
body: body
)
}
public func makeWebSocket(url: URL) async throws -> any WebSocketSession {
let (stream, continuation) = AsyncStream.makeStream(of: WebSocketSessionWrapper.self)
async let value = stream.first(where: { _ in true })
@@ -10,62 +10,56 @@ import OpenAPIRuntime
import Dependencies
import HTTPTypes
public struct HTTPRequest: Sendable {
public enum Body: Sendable {
case buffer(Data)
}
public var url: URL
public var method: String?
public var headers: [String: String]
public var body: Body?
public init(
url: URL,
method: String? = nil,
headers: [String: String] = [:],
body: Body? = nil
) {
self.url = url
self.method = method
self.headers = headers
self.body = body
}
}
public struct HTTPResponse: Sendable {
public var url: URL
public var status: Int
public var headers: [String: String]
public var body: Data?
public init(
url: URL,
status: Int,
headers: [String: String] = [:],
body: Data? = nil
) {
self.url = url
self.status = status
self.headers = headers
self.body = body
}
}
public typealias HTTPRequest = HTTPTypes.HTTPRequest
public typealias HTTPResponse = HTTPTypes.HTTPResponse
public typealias HTTPBody = OpenAPIRuntime.HTTPBody
public protocol HTTPClientProtocol: Sendable {
var asOpenAPITransport: ClientTransport { get }
func makeRequest(
_ request: HTTPRequest,
onProgress: sending @isolated(any) (Double?) -> Void
) async throws -> HTTPResponse
func makeWebSocket(url: URL) async throws -> WebSocketSession
}
extension HTTPClientProtocol {
public func makeRequest(_ request: HTTPRequest) async throws -> HTTPResponse {
try await makeRequest(request) { _ in }
public func send(
_ request: HTTPRequest,
body: HTTPBody? = nil
) async throws -> (response: HTTPResponse, body: HTTPBody?) {
let transport = asOpenAPITransport
let request = request
var baseComponents = URLComponents()
baseComponents.scheme = request.scheme
baseComponents.host = request.authority
return try await transport.send(
request,
body: body,
baseURL: baseComponents.url!,
operationID: "dummy"
)
}
public func makeRequest(
_ request: HTTPRequest,
body: Data? = nil,
onProgress: sending @isolated(any) (Double?) -> Void = { _ in }
) async throws -> (response: HTTPResponse, body: Data) {
await onProgress(0)
let (response, body) = try await send(request, body: body.map { HTTPBody($0) })
guard let body else {
return (response, Data())
}
switch body.length {
case .unknown:
return (response, try await body.reduce(into: Data()) { $0 += $1 })
case .known(let length):
var data = Data(capacity: Int(length))
let total = Double(length)
for try await chunk in body {
data += chunk
await onProgress(min(Double(data.count) / total, 1))
}
return (response, data)
}
}
}
@@ -56,82 +56,6 @@ private final class Client: HTTPClientProtocol {
URLSessionTransport(configuration: .init(session: session))
}
public func makeRequest(
_ request: HTTPRequest,
onProgress: sending @isolated(any) (Double?) -> Void
) async throws -> HTTPResponse {
final class ProgressDelegate: NSObject, URLSessionDataDelegate {
private let progressContinuation: AsyncStream<Double>.Continuation
let progressStream: AsyncStream<Double>
@MainActor private var count = 0
override init() {
(progressStream, progressContinuation) = AsyncStream<Double>.makeStream(
// it's okay to skip missed progress updates
bufferingPolicy: .bufferingNewest(1)
)
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
let count = MainActor.assumeIsolated {
self.count += data.count
return self.count
}
if let contentLength = dataTask.response?.expectedContentLength, contentLength != -1 {
let progress = min(Double(count) / Double(contentLength), 1)
progressContinuation.yield(progress)
}
}
func finish() {
progressContinuation.finish()
}
}
var req = URLRequest(url: request.url)
req.httpMethod = request.method
request.headers.forEach { k, v in
req.setValue(v, forHTTPHeaderField: k)
}
switch request.body {
case .buffer(let data):
req.httpBody = data
case nil:
break
}
let delegate = ProgressDelegate()
async let progressWatcher: Void = {
for await progress in delegate.progressStream {
await onProgress(progress)
}
}()
let (data, response) = if #available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) {
try await session.data(for: req, delegate: delegate)
} else {
try await session.data(for: req)
}
delegate.finish()
_ = await progressWatcher
guard let httpResp = response as? HTTPURLResponse else { throw UnknownHTTPError() }
let headers = [String: String](
uniqueKeysWithValues: httpResp.allHeaderFields.compactMap { k, v in
guard let kStr = k as? String,
let vStr = v as? String
else { return nil }
return (kStr, vStr)
}
)
return HTTPResponse(
url: request.url,
status: httpResp.statusCode,
headers: headers,
body: data
)
}
public func makeWebSocket(url: URL) async throws -> any WebSocketSession {
let task = session.webSocketTask(with: url)
let (event, eventContinuation) = AsyncStream<URLSessionWebSocketTask.CloseCode?>.makeStream()
@@ -77,7 +77,7 @@ struct DarwinSDKVersions: Decodable {
static func all() async throws -> DarwinSDKVersions {
@Dependency(\.httpClient) var httpClient
let data = try await httpClient.makeRequest(HTTPRequest(url: url)).body ?? Data()
let data = try await httpClient.makeRequest(HTTPRequest(url: url)).body
return try decoder.decode(self, from: data)
}
}