2021-07-30 23:03:25 +02:00
|
|
|
//
|
|
|
|
|
// Copyright RevenueCat Inc. All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the MIT License (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
|
//
|
|
|
|
|
// HTTPClient.swift
|
|
|
|
|
//
|
|
|
|
|
// Created by César de la Vega on 7/22/21.
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
2022-08-31 13:30:38 -07:00
|
|
|
// swiftlint:disable file_length
|
|
|
|
|
|
2021-08-23 11:20:55 -07:00
|
|
|
class HTTPClient {
|
2021-07-30 23:03:25 +02:00
|
|
|
|
2023-02-22 11:30:20 -08:00
|
|
|
typealias RequestHeaders = HTTPRequest.Headers
|
|
|
|
|
typealias ResponseHeaders = HTTPResponse<HTTPEmptyResponseBody>.Headers
|
2023-06-30 12:24:53 -07:00
|
|
|
typealias Completion<Value: HTTPResponseBody> = (VerifiedHTTPResponse<Value>.Result) -> Void
|
2022-03-04 13:46:24 -08:00
|
|
|
|
2022-06-14 15:12:46 -07:00
|
|
|
let systemInfo: SystemInfo
|
|
|
|
|
let timeout: TimeInterval
|
|
|
|
|
let apiKey: String
|
|
|
|
|
let authHeaders: RequestHeaders
|
2022-05-12 15:43:10 -07:00
|
|
|
|
|
|
|
|
private let session: URLSession
|
2022-02-23 18:05:59 -05:00
|
|
|
private let state: Atomic<State> = .init(.initial)
|
2022-02-19 10:11:52 -07:00
|
|
|
private let eTagManager: ETagManager
|
2022-01-13 16:24:37 +01:00
|
|
|
private let dnsChecker: DNSCheckerType.Type
|
2023-07-05 16:39:04 -07:00
|
|
|
private let signing: SigningType
|
2021-07-30 23:03:25 +02:00
|
|
|
|
2022-06-14 15:12:46 -07:00
|
|
|
init(apiKey: String,
|
|
|
|
|
systemInfo: SystemInfo,
|
|
|
|
|
eTagManager: ETagManager,
|
2023-07-05 16:39:04 -07:00
|
|
|
signing: SigningType,
|
2022-06-14 15:12:46 -07:00
|
|
|
dnsChecker: DNSCheckerType.Type = DNSChecker.self,
|
|
|
|
|
requestTimeout: TimeInterval = Configuration.networkTimeoutDefault) {
|
2021-07-30 23:03:25 +02:00
|
|
|
let config = URLSessionConfiguration.ephemeral
|
|
|
|
|
config.httpMaximumConnectionsPerHost = 1
|
2022-05-12 15:43:10 -07:00
|
|
|
config.timeoutIntervalForRequest = requestTimeout
|
|
|
|
|
config.timeoutIntervalForResource = requestTimeout
|
2023-06-16 10:26:39 -07:00
|
|
|
config.urlCache = nil // We implement our own caching with `ETagManager`.
|
2023-03-23 14:34:14 -07:00
|
|
|
self.session = URLSession(configuration: config,
|
|
|
|
|
delegate: RedirectLoggerSessionDelegate(),
|
|
|
|
|
delegateQueue: nil)
|
2021-07-30 23:03:25 +02:00
|
|
|
self.systemInfo = systemInfo
|
|
|
|
|
self.eTagManager = eTagManager
|
2023-02-16 03:06:59 -08:00
|
|
|
self.signing = signing
|
2023-07-05 16:39:04 -07:00
|
|
|
self.dnsChecker = dnsChecker
|
2022-05-12 15:43:10 -07:00
|
|
|
self.timeout = requestTimeout
|
2022-06-14 15:12:46 -07:00
|
|
|
self.apiKey = apiKey
|
2022-10-20 13:03:15 -05:00
|
|
|
self.authHeaders = HTTPClient.authorizationHeader(withAPIKey: apiKey)
|
2021-07-30 23:03:25 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-22 09:20:25 -08:00
|
|
|
/// - Parameter verificationMode: if `nil`, this will default to `SystemInfo.responseVerificationMode`
|
|
|
|
|
func perform<Value: HTTPResponseBody>(
|
|
|
|
|
_ request: HTTPRequest,
|
|
|
|
|
with verificationMode: Signing.ResponseVerificationMode? = nil,
|
|
|
|
|
completionHandler: Completion<Value>?
|
|
|
|
|
) {
|
2023-03-23 14:21:47 -07:00
|
|
|
#if DEBUG
|
2023-05-19 10:54:42 -07:00
|
|
|
guard !self.systemInfo.dangerousSettings.internalSettings.forceServerErrors else {
|
|
|
|
|
Logger.warn(Strings.network.api_request_forcing_server_error(request))
|
2023-09-07 12:25:19 -07:00
|
|
|
|
|
|
|
|
// `FB13133387`: when computing offline CustomerInfo, `StoreKit.Transaction.unfinished`
|
|
|
|
|
// might be empty if called immediately after `Product.purchase()`.
|
|
|
|
|
// This introduces a delay to simulate a real API request, and avoid that race condition.
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(300)) {
|
|
|
|
|
completionHandler?(
|
|
|
|
|
.failure(.errorResponse(Self.serverErrorResponse, .internalServerError))
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 14:21:47 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-10-20 13:03:15 -05:00
|
|
|
self.perform(request: .init(httpRequest: request,
|
2023-02-08 09:05:20 -08:00
|
|
|
authHeaders: self.authHeaders,
|
2023-02-22 09:20:25 -08:00
|
|
|
verificationMode: verificationMode ?? self.systemInfo.responseVerificationMode,
|
2022-10-20 13:03:15 -05:00
|
|
|
completionHandler: completionHandler))
|
2021-07-30 23:03:25 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-23 11:20:55 -07:00
|
|
|
func clearCaches() {
|
2022-10-20 13:03:15 -05:00
|
|
|
self.eTagManager.clearCaches()
|
2021-07-30 23:03:25 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-13 11:57:18 -07:00
|
|
|
var signatureVerificationEnabled: Bool {
|
|
|
|
|
return self.systemInfo.responseVerificationMode.isEnabled
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 23:03:25 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-04 13:46:24 -08:00
|
|
|
extension HTTPClient {
|
|
|
|
|
|
|
|
|
|
static func authorizationHeader(withAPIKey apiKey: String) -> RequestHeaders {
|
2023-02-27 09:07:23 -08:00
|
|
|
return [RequestHeader.authorization.rawValue: "Bearer \(apiKey)"]
|
2022-03-04 13:46:24 -08:00
|
|
|
}
|
|
|
|
|
|
2023-02-08 09:05:20 -08:00
|
|
|
static func nonceHeader(with data: Data) -> RequestHeaders {
|
2023-02-27 09:07:23 -08:00
|
|
|
return [RequestHeader.nonce.rawValue: data.base64EncodedString()]
|
2023-02-08 09:05:20 -08:00
|
|
|
}
|
|
|
|
|
|
2023-07-13 07:45:16 -07:00
|
|
|
static func postParametersHeaderForSigning(with body: HTTPRequestBody) -> RequestHeaders {
|
|
|
|
|
if let header = body.postParameterHeader {
|
|
|
|
|
return [RequestHeader.postParameters.rawValue: header]
|
|
|
|
|
} else {
|
|
|
|
|
return [:]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-27 09:07:23 -08:00
|
|
|
enum RequestHeader: String {
|
|
|
|
|
|
|
|
|
|
case authorization = "Authorization"
|
|
|
|
|
case nonce = "X-Nonce"
|
|
|
|
|
case eTag = "X-RevenueCat-ETag"
|
2023-04-14 14:32:24 -07:00
|
|
|
case eTagValidationTime = "X-RC-Last-Refresh-Time"
|
2023-07-13 07:45:16 -07:00
|
|
|
case postParameters = "X-Post-Params-Hash"
|
2023-02-27 09:07:23 -08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum ResponseHeader: String {
|
|
|
|
|
|
|
|
|
|
case eTag = "X-RevenueCat-ETag"
|
2023-06-21 11:02:45 -07:00
|
|
|
case location = "Location"
|
2023-02-27 09:07:23 -08:00
|
|
|
case signature = "X-Signature"
|
2023-03-01 09:24:32 -08:00
|
|
|
case requestDate = "X-RevenueCat-Request-Time"
|
2023-05-24 09:51:50 -07:00
|
|
|
case contentType = "Content-Type"
|
2023-06-19 08:16:57 -07:00
|
|
|
case isLoadShedder = "X-RevenueCat-Fortress"
|
2023-02-27 09:07:23 -08:00
|
|
|
|
|
|
|
|
}
|
2023-02-08 09:05:20 -08:00
|
|
|
|
2022-03-04 13:46:24 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-31 13:30:38 -07:00
|
|
|
// @unchecked because:
|
|
|
|
|
// - Class is not `final` (it's mocked). This implicitly makes subclasses `Sendable` even if they're not thread-safe.
|
|
|
|
|
extension HTTPClient: @unchecked Sendable {}
|
|
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
2022-02-23 18:05:59 -05:00
|
|
|
private extension HTTPClient {
|
2022-04-01 14:22:29 -07:00
|
|
|
|
2022-02-23 18:05:59 -05:00
|
|
|
struct State {
|
|
|
|
|
var queuedRequests: [Request]
|
|
|
|
|
var currentSerialRequest: Request?
|
|
|
|
|
|
2022-06-14 15:12:46 -07:00
|
|
|
static let initial: Self = .init(queuedRequests: [], currentSerialRequest: nil)
|
2022-02-23 18:05:59 -05:00
|
|
|
}
|
2022-04-01 14:22:29 -07:00
|
|
|
|
2022-02-16 13:41:28 -08:00
|
|
|
struct Request: CustomStringConvertible {
|
|
|
|
|
|
2022-03-04 13:42:23 -08:00
|
|
|
var httpRequest: HTTPRequest
|
2022-03-04 13:46:24 -08:00
|
|
|
var headers: HTTPClient.RequestHeaders
|
2023-02-22 09:20:25 -08:00
|
|
|
var verificationMode: Signing.ResponseVerificationMode
|
2022-04-12 15:11:22 -07:00
|
|
|
var completionHandler: HTTPClient.Completion<Data>?
|
2022-02-16 13:41:28 -08:00
|
|
|
var retried: Bool = false
|
|
|
|
|
|
2022-06-14 15:12:46 -07:00
|
|
|
init<Value: HTTPResponseBody>(httpRequest: HTTPRequest,
|
2023-02-08 09:05:20 -08:00
|
|
|
authHeaders: HTTPClient.RequestHeaders,
|
2023-02-22 09:20:25 -08:00
|
|
|
verificationMode: Signing.ResponseVerificationMode,
|
2022-06-14 15:12:46 -07:00
|
|
|
completionHandler: HTTPClient.Completion<Value>?) {
|
2023-02-22 09:20:25 -08:00
|
|
|
self.httpRequest = httpRequest.requestAddingNonceIfRequired(with: verificationMode)
|
2023-07-13 07:45:16 -07:00
|
|
|
self.headers = self.httpRequest.headers(with: authHeaders,
|
|
|
|
|
verificationMode: verificationMode)
|
2023-02-22 09:20:25 -08:00
|
|
|
self.verificationMode = verificationMode
|
2022-04-12 15:11:22 -07:00
|
|
|
|
|
|
|
|
if let completionHandler = completionHandler {
|
|
|
|
|
self.completionHandler = { result in
|
|
|
|
|
completionHandler(result.parseResponse())
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
self.completionHandler = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-04 13:42:23 -08:00
|
|
|
var method: HTTPRequest.Method { self.httpRequest.method }
|
2023-08-10 15:23:13 -07:00
|
|
|
var path: String { self.httpRequest.path.relativePath }
|
2022-02-16 13:41:28 -08:00
|
|
|
|
2022-03-04 13:46:24 -08:00
|
|
|
func adding(defaultHeaders: HTTPClient.RequestHeaders) -> Self {
|
2022-02-16 13:41:28 -08:00
|
|
|
var copy = self
|
|
|
|
|
copy.headers = defaultHeaders.merging(self.headers)
|
|
|
|
|
|
|
|
|
|
return copy
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func retriedRequest() -> Self {
|
|
|
|
|
var copy = self
|
|
|
|
|
copy.retried = true
|
|
|
|
|
|
|
|
|
|
return copy
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var description: String {
|
|
|
|
|
"""
|
|
|
|
|
<\(type(of: self)): httpMethod=\(self.method.httpMethod)
|
|
|
|
|
path=\(self.path)
|
|
|
|
|
headers=\(self.headers.description )
|
|
|
|
|
retried=\(self.retried)
|
|
|
|
|
>
|
|
|
|
|
"""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 23:03:25 +02:00
|
|
|
private extension HTTPClient {
|
|
|
|
|
|
|
|
|
|
var defaultHeaders: [String: String] {
|
|
|
|
|
var headers: [String: String] = [
|
|
|
|
|
"content-type": "application/json",
|
|
|
|
|
"X-Version": SystemInfo.frameworkVersion,
|
|
|
|
|
"X-Platform": SystemInfo.platformHeader,
|
|
|
|
|
"X-Platform-Version": SystemInfo.systemVersion,
|
|
|
|
|
"X-Platform-Flavor": systemInfo.platformFlavor,
|
|
|
|
|
"X-Client-Version": SystemInfo.appVersion,
|
|
|
|
|
"X-Client-Build-Version": SystemInfo.buildVersion,
|
2022-09-05 15:10:44 -07:00
|
|
|
"X-Client-Bundle-ID": SystemInfo.bundleIdentifier,
|
2022-12-09 10:39:09 -08:00
|
|
|
"X-StoreKit2-Enabled": "\(self.systemInfo.storeKit2Setting.isEnabledAndAvailable)",
|
2022-11-23 11:22:29 -06:00
|
|
|
"X-Observer-Mode-Enabled": "\(self.systemInfo.observerMode)",
|
2022-05-20 14:51:48 -07:00
|
|
|
"X-Is-Sandbox": "\(self.systemInfo.isSandbox)"
|
2021-07-30 23:03:25 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
if let platformFlavorVersion = self.systemInfo.platformFlavorVersion {
|
|
|
|
|
headers["X-Platform-Flavor-Version"] = platformFlavorVersion
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 20:25:04 +01:00
|
|
|
if let idfv = systemInfo.identifierForVendor {
|
2021-07-30 23:03:25 +02:00
|
|
|
headers["X-Apple-Device-Identifier"] = idfv
|
|
|
|
|
}
|
2023-04-25 17:21:46 -03:00
|
|
|
|
|
|
|
|
if systemInfo.dangerousSettings.customEntitlementComputation {
|
|
|
|
|
headers["X-Custom-Entitlements-Computation"] = "\(true)"
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 23:03:25 +02:00
|
|
|
return headers
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 14:21:47 -07:00
|
|
|
static let serverErrorResponse: ErrorResponse = .init(code: .internalServerError,
|
|
|
|
|
originalCode: BackendErrorCode.unknownBackendError.rawValue)
|
|
|
|
|
|
2022-02-23 18:09:50 -05:00
|
|
|
func perform(request: Request) {
|
|
|
|
|
if !request.retried {
|
2022-02-23 18:05:59 -05:00
|
|
|
let requestEnqueued: Bool = self.state.modify {
|
|
|
|
|
if $0.currentSerialRequest != nil {
|
|
|
|
|
Logger.debug(Strings.network.serial_request_queued(httpMethod: request.method.httpMethod,
|
|
|
|
|
path: request.path,
|
|
|
|
|
queuedRequestsCount: $0.queuedRequests.count))
|
|
|
|
|
|
|
|
|
|
$0.queuedRequests.append(request)
|
|
|
|
|
return true
|
|
|
|
|
} else {
|
|
|
|
|
Logger.debug(Strings.network.starting_request(httpMethod: request.method.httpMethod,
|
|
|
|
|
path: request.path))
|
|
|
|
|
$0.currentSerialRequest = request
|
|
|
|
|
return false
|
|
|
|
|
}
|
2021-07-30 23:03:25 +02:00
|
|
|
}
|
2022-02-23 18:05:59 -05:00
|
|
|
|
|
|
|
|
guard !requestEnqueued else { return }
|
2021-07-30 23:03:25 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:09:50 -05:00
|
|
|
self.start(request: request)
|
2021-07-30 23:03:25 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-12 15:11:22 -07:00
|
|
|
/// - Returns: `nil` if the request must be retried
|
2022-04-01 14:22:29 -07:00
|
|
|
func parse(urlResponse: URLResponse?,
|
|
|
|
|
request: Request,
|
|
|
|
|
urlRequest: URLRequest,
|
|
|
|
|
data: Data?,
|
2023-06-30 12:24:53 -07:00
|
|
|
error networkError: Error?) -> VerifiedHTTPResponse<Data>.Result? {
|
2022-04-01 14:22:29 -07:00
|
|
|
if let networkError = networkError {
|
2022-04-21 09:31:20 -07:00
|
|
|
return .failure(NetworkError(networkError, dnsChecker: self.dnsChecker))
|
2022-04-01 14:22:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard let httpURLResponse = urlResponse as? HTTPURLResponse else {
|
2022-04-12 15:11:22 -07:00
|
|
|
return .failure(.unexpectedResponse(urlResponse))
|
2022-04-01 14:22:29 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-30 12:24:53 -07:00
|
|
|
let statusCode: HTTPStatusCode = .init(rawValue: httpURLResponse.statusCode)
|
2022-04-01 14:22:29 -07:00
|
|
|
|
2023-06-30 12:24:53 -07:00
|
|
|
// `nil` if status code is 304, since the response will be empty and fetched from the eTag.
|
|
|
|
|
let dataIfAvailable = statusCode == .notModified
|
|
|
|
|
? nil
|
|
|
|
|
: data
|
2022-04-01 14:22:29 -07:00
|
|
|
|
2023-06-30 12:24:53 -07:00
|
|
|
return self.createVerifiedResponse(request: request,
|
|
|
|
|
urlRequest: urlRequest,
|
|
|
|
|
data: dataIfAvailable,
|
|
|
|
|
response: httpURLResponse)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// - Returns `Result<VerifiedHTTPResponse<Data>, NetworkError>?`
|
|
|
|
|
private func createVerifiedResponse(
|
|
|
|
|
request: Request,
|
|
|
|
|
urlRequest: URLRequest,
|
|
|
|
|
data: Data?,
|
|
|
|
|
response httpURLResponse: HTTPURLResponse
|
|
|
|
|
) -> VerifiedHTTPResponse<Data>.Result? {
|
2022-04-12 15:11:22 -07:00
|
|
|
return Result
|
2023-06-30 12:24:53 -07:00
|
|
|
.success(data)
|
|
|
|
|
.mapToResponse(response: httpURLResponse, request: request.httpRequest)
|
|
|
|
|
// Verify response
|
|
|
|
|
.map { cachedResponse -> VerifiedHTTPResponse<Data?> in
|
|
|
|
|
return cachedResponse.verify(
|
2023-07-05 16:39:04 -07:00
|
|
|
signing: self.signing(for: request.httpRequest),
|
2023-06-30 12:24:53 -07:00
|
|
|
request: request.httpRequest,
|
2023-07-05 16:39:04 -07:00
|
|
|
publicKey: request.verificationMode.publicKey
|
2023-06-30 12:24:53 -07:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
// Fetch from ETagManager if available
|
|
|
|
|
.map { (response) -> VerifiedHTTPResponse<Data>? in
|
|
|
|
|
return self.eTagManager.httpResultFromCacheOrBackend(
|
2023-03-01 09:24:32 -08:00
|
|
|
with: response,
|
|
|
|
|
request: urlRequest,
|
|
|
|
|
retried: request.retried
|
2023-06-30 12:24:53 -07:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
// Upgrade to error in enforced mode
|
|
|
|
|
.flatMap { response -> Result<VerifiedHTTPResponse<Data>?, NetworkError> in
|
2023-07-07 14:05:55 -07:00
|
|
|
if let response = response, response.verificationResult == .failed {
|
|
|
|
|
if case .enforced = request.verificationMode {
|
|
|
|
|
return .failure(.signatureVerificationFailed(path: request.httpRequest.path,
|
|
|
|
|
code: response.statusCode))
|
|
|
|
|
} else {
|
|
|
|
|
// Any other mode gets forwarded as a success, but we log the error
|
|
|
|
|
Logger.error(Strings.signing.request_failed_verification(request.httpRequest))
|
|
|
|
|
return .success(response)
|
|
|
|
|
}
|
2023-06-30 12:24:53 -07:00
|
|
|
} else {
|
|
|
|
|
return .success(response)
|
2023-03-01 09:24:32 -08:00
|
|
|
}
|
2022-04-01 14:22:29 -07:00
|
|
|
}
|
2022-04-05 11:39:49 -07:00
|
|
|
.asOptionalResult?
|
2022-04-12 15:11:22 -07:00
|
|
|
.convertUnsuccessfulResponseToError()
|
2022-04-01 14:22:29 -07:00
|
|
|
}
|
|
|
|
|
|
2022-02-16 13:41:28 -08:00
|
|
|
func handle(urlResponse: URLResponse?,
|
|
|
|
|
request: Request,
|
|
|
|
|
urlRequest: URLRequest,
|
|
|
|
|
data: Data?,
|
2022-02-23 18:09:50 -05:00
|
|
|
error networkError: Error?) {
|
2023-05-19 11:58:12 -07:00
|
|
|
RCTestAssertNotMainThread()
|
|
|
|
|
|
2022-04-05 11:39:49 -07:00
|
|
|
let response = self.parse(
|
2022-04-01 14:22:29 -07:00
|
|
|
urlResponse: urlResponse,
|
|
|
|
|
request: request,
|
|
|
|
|
urlRequest: urlRequest,
|
|
|
|
|
data: data,
|
|
|
|
|
error: networkError
|
|
|
|
|
)
|
2021-07-30 23:03:25 +02:00
|
|
|
|
2022-04-05 11:39:49 -07:00
|
|
|
if let response = response {
|
2022-11-14 09:15:25 -08:00
|
|
|
switch response {
|
|
|
|
|
case let .success(response):
|
2023-05-15 19:28:25 -04:00
|
|
|
Logger.debug(Strings.network.api_request_completed(
|
|
|
|
|
request.httpRequest,
|
|
|
|
|
// Getting status code from the original response to detect 304s
|
|
|
|
|
// If that can't be extracted, get status code from the parsed response.
|
|
|
|
|
httpCode: urlResponse?.httpStatusCode ?? response.statusCode
|
|
|
|
|
))
|
2023-06-19 08:16:57 -07:00
|
|
|
|
|
|
|
|
if response.isLoadShedder {
|
|
|
|
|
Logger.debug(Strings.network.request_handled_by_load_shedder(request.httpRequest.path))
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 09:15:25 -08:00
|
|
|
case let .failure(error):
|
2023-06-16 07:49:52 -07:00
|
|
|
Logger.debug(Strings.network.api_request_failed(request.httpRequest,
|
|
|
|
|
httpCode: urlResponse?.httpStatusCode,
|
|
|
|
|
error: error))
|
2022-11-14 09:15:25 -08:00
|
|
|
}
|
|
|
|
|
|
2022-04-05 11:39:49 -07:00
|
|
|
request.completionHandler?(response)
|
|
|
|
|
} else {
|
2022-04-01 14:22:29 -07:00
|
|
|
Logger.debug(Strings.network.retrying_request(httpMethod: request.method.httpMethod,
|
|
|
|
|
path: request.path))
|
2021-07-30 23:03:25 +02:00
|
|
|
|
2022-04-01 14:22:29 -07:00
|
|
|
self.state.modify {
|
|
|
|
|
$0.queuedRequests.insert(request.retriedRequest(), at: 0)
|
2021-07-30 23:03:25 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:09:50 -05:00
|
|
|
self.beginNextRequest()
|
2021-07-30 23:03:25 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:05:59 -05:00
|
|
|
func beginNextRequest() {
|
|
|
|
|
let nextRequest: Request? = self.state.modify {
|
|
|
|
|
Logger.debug(Strings.network.serial_request_done(httpMethod: $0.currentSerialRequest?.method.httpMethod,
|
|
|
|
|
path: $0.currentSerialRequest?.path,
|
|
|
|
|
queuedRequestsCount: $0.queuedRequests.count))
|
|
|
|
|
$0.currentSerialRequest = $0.queuedRequests.popFirst()
|
|
|
|
|
|
|
|
|
|
return $0.currentSerialRequest
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let nextRequest = nextRequest {
|
|
|
|
|
Logger.debug(Strings.network.starting_next_request(request: nextRequest.description))
|
2022-02-23 18:09:50 -05:00
|
|
|
self.start(request: nextRequest)
|
2022-02-23 18:05:59 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:09:50 -05:00
|
|
|
func start(request: Request) {
|
2022-02-23 18:05:59 -05:00
|
|
|
let urlRequest = self.convert(request: request.adding(defaultHeaders: self.defaultHeaders))
|
|
|
|
|
|
|
|
|
|
guard let urlRequest = urlRequest else {
|
2023-06-16 09:19:16 -07:00
|
|
|
let error: NetworkError = .unableToCreateRequest(request.httpRequest.path)
|
2022-02-23 18:05:59 -05:00
|
|
|
|
2023-06-16 09:19:16 -07:00
|
|
|
Logger.error(error.description)
|
|
|
|
|
request.completionHandler?(.failure(error))
|
2022-02-23 18:05:59 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 14:32:54 -05:00
|
|
|
Logger.debug(Strings.network.api_request_started(request.httpRequest))
|
2022-02-23 18:05:59 -05:00
|
|
|
|
2023-03-23 14:34:14 -07:00
|
|
|
let task = self.session.dataTask(with: urlRequest) { (data, urlResponse, error) -> Void in
|
2022-02-23 18:05:59 -05:00
|
|
|
self.handle(urlResponse: urlResponse,
|
|
|
|
|
request: request,
|
|
|
|
|
urlRequest: urlRequest,
|
|
|
|
|
data: data,
|
2022-02-23 18:09:50 -05:00
|
|
|
error: error)
|
2022-02-23 18:05:59 -05:00
|
|
|
}
|
|
|
|
|
task.resume()
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 13:41:28 -08:00
|
|
|
func convert(request: Request) -> URLRequest? {
|
2023-08-10 15:23:13 -07:00
|
|
|
guard let requestURL = request.httpRequest.path.url(proxyURL: SystemInfo.proxyURL) else {
|
2021-07-30 23:03:25 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
var urlRequest = URLRequest(url: requestURL)
|
2022-02-16 13:41:28 -08:00
|
|
|
urlRequest.httpMethod = request.method.httpMethod
|
2023-02-13 07:02:18 -08:00
|
|
|
urlRequest.allHTTPHeaderFields = self.headers(for: request, urlRequest: urlRequest)
|
2021-07-30 23:03:25 +02:00
|
|
|
|
2022-03-08 12:56:13 -08:00
|
|
|
do {
|
2023-05-31 07:11:23 -07:00
|
|
|
urlRequest.httpBody = try request.httpRequest.requestBody?.jsonEncodedData
|
2022-03-08 12:56:13 -08:00
|
|
|
} catch {
|
|
|
|
|
Logger.error(Strings.network.creating_json_error(error: error.localizedDescription))
|
|
|
|
|
return nil
|
2021-07-30 23:03:25 +02:00
|
|
|
}
|
2022-02-16 13:41:28 -08:00
|
|
|
|
2021-07-30 23:03:25 +02:00
|
|
|
return urlRequest
|
|
|
|
|
}
|
2023-02-13 07:02:18 -08:00
|
|
|
|
|
|
|
|
private func headers(for request: Request, urlRequest: URLRequest) -> HTTPClient.RequestHeaders {
|
|
|
|
|
if request.httpRequest.path.shouldSendEtag {
|
2023-02-16 03:06:59 -08:00
|
|
|
let eTagHeader = self.eTagManager.eTagHeader(
|
|
|
|
|
for: urlRequest,
|
2023-06-30 12:24:53 -07:00
|
|
|
withSignatureVerification: request.verificationMode.isEnabled,
|
2023-03-13 12:24:02 -07:00
|
|
|
refreshETag: request.retried
|
2023-02-16 03:06:59 -08:00
|
|
|
)
|
2023-02-13 07:02:18 -08:00
|
|
|
return request.headers.merging(eTagHeader)
|
|
|
|
|
} else {
|
|
|
|
|
return request.headers
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-24 12:56:37 -07:00
|
|
|
|
2023-07-05 16:39:04 -07:00
|
|
|
private func signing(for request: HTTPRequest) -> SigningType {
|
2023-06-13 13:43:16 -07:00
|
|
|
#if DEBUG
|
|
|
|
|
if self.systemInfo.dangerousSettings.internalSettings.forceSignatureFailures {
|
|
|
|
|
Logger.warn(Strings.network.api_request_forcing_signature_failure(request))
|
2023-07-05 16:39:04 -07:00
|
|
|
return FakeSigning.default
|
2023-06-13 13:43:16 -07:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return self.signing
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 23:03:25 +02:00
|
|
|
}
|
2022-03-04 13:42:23 -08:00
|
|
|
|
2022-08-31 13:30:38 -07:00
|
|
|
// MARK: - Extensions
|
|
|
|
|
|
2023-02-16 10:27:04 -07:00
|
|
|
extension HTTPRequest {
|
2023-02-08 09:05:20 -08:00
|
|
|
|
2023-02-22 09:20:25 -08:00
|
|
|
func requestAddingNonceIfRequired(
|
|
|
|
|
with verificationMode: Signing.ResponseVerificationMode
|
|
|
|
|
) -> HTTPRequest {
|
|
|
|
|
var result = self
|
|
|
|
|
|
|
|
|
|
if result.nonce == nil,
|
2023-07-06 10:08:45 -07:00
|
|
|
result.path.needsNonceForSigning,
|
2023-02-22 09:20:25 -08:00
|
|
|
verificationMode.isEnabled,
|
|
|
|
|
#available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.2, *) {
|
|
|
|
|
result.addRandomNonce()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 07:45:16 -07:00
|
|
|
func headers(
|
|
|
|
|
with authHeaders: HTTPClient.RequestHeaders,
|
|
|
|
|
verificationMode: Signing.ResponseVerificationMode
|
|
|
|
|
) -> HTTPClient.RequestHeaders {
|
2023-02-08 09:05:20 -08:00
|
|
|
var result: HTTPClient.RequestHeaders = [:]
|
|
|
|
|
|
|
|
|
|
if self.path.authenticated {
|
|
|
|
|
result += authHeaders
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let nonce = self.nonce {
|
|
|
|
|
result += HTTPClient.nonceHeader(with: nonce)
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 07:45:16 -07:00
|
|
|
if #available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.2, *),
|
|
|
|
|
verificationMode.isEnabled,
|
|
|
|
|
self.path.supportsSignatureVerification,
|
|
|
|
|
let body = self.requestBody {
|
|
|
|
|
result += HTTPClient.postParametersHeaderForSigning(with: body)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 09:05:20 -08:00
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-10 15:23:13 -07:00
|
|
|
/// Add a nonce to the request
|
|
|
|
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.2, *)
|
|
|
|
|
private mutating func addRandomNonce() {
|
|
|
|
|
self.nonce = Data.randomNonce()
|
2022-03-04 13:42:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2022-03-08 12:56:13 -08:00
|
|
|
|
2022-04-21 09:31:20 -07:00
|
|
|
private extension NetworkError {
|
|
|
|
|
|
|
|
|
|
/// Creates a `NetworkError` from any request `Error`.
|
|
|
|
|
init(_ error: Error, dnsChecker: DNSCheckerType.Type) {
|
2022-12-21 08:25:23 -08:00
|
|
|
if let blockedError = dnsChecker.errorWithBlockedHostFromError(error) {
|
2022-04-21 09:31:20 -07:00
|
|
|
Logger.error(blockedError.description)
|
|
|
|
|
self = blockedError
|
|
|
|
|
} else {
|
|
|
|
|
let nsError = error as NSError
|
|
|
|
|
|
|
|
|
|
switch (nsError.domain, nsError.code) {
|
|
|
|
|
case (NSURLErrorDomain, NSURLErrorNotConnectedToInternet):
|
|
|
|
|
self = .offlineConnection()
|
|
|
|
|
default:
|
|
|
|
|
self = .networkError(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-30 12:24:53 -07:00
|
|
|
extension Result where Success == Data?, Failure == NetworkError {
|
2022-04-12 15:11:22 -07:00
|
|
|
|
2023-06-30 12:24:53 -07:00
|
|
|
/// Converts a `Result<Data?, NetworkError>` into `Result<HTTPResponse<Data?>, NetworkError>`
|
|
|
|
|
func mapToResponse(
|
|
|
|
|
response: HTTPURLResponse,
|
|
|
|
|
request: HTTPRequest
|
|
|
|
|
) -> Result<HTTPResponse<Data?>, Failure> {
|
|
|
|
|
return self.flatMap { body in
|
|
|
|
|
return .success(
|
|
|
|
|
.init(
|
|
|
|
|
statusCode: .init(rawValue: response.statusCode),
|
|
|
|
|
responseHeaders: response.allHeaderFields,
|
|
|
|
|
body: body
|
|
|
|
|
)
|
|
|
|
|
)
|
2022-04-12 15:11:22 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2023-03-01 09:24:32 -08:00
|
|
|
|
2023-06-30 12:24:53 -07:00
|
|
|
extension Result where Success == VerifiedHTTPResponse<Data>, Failure == NetworkError {
|
|
|
|
|
|
|
|
|
|
// Parses a `Result<VerifiedHTTPResponse<Data>>` to `Result<VerifiedHTTPResponse<Value>>`
|
|
|
|
|
func parseResponse<Value: HTTPResponseBody>() -> VerifiedHTTPResponse<Value>.Result {
|
|
|
|
|
return self.flatMap { response in // Convert the `Result` type
|
|
|
|
|
Result<VerifiedHTTPResponse<Value>, Error> { // Create a new `Result<Value>`
|
|
|
|
|
try response.mapBody { data in // Convert the from `Data` -> `Value`
|
|
|
|
|
try Value.create(with: data) // Decode `Data` into `Value`
|
|
|
|
|
}
|
|
|
|
|
.copyWithNewRequestDate() // Update request date for 304 responses
|
|
|
|
|
}
|
|
|
|
|
// Convert decoding errors into `NetworkError.decoding`
|
|
|
|
|
.mapError { NetworkError.decoding($0, response.response.body) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension Result where Success == VerifiedHTTPResponse<Data>, Failure == NetworkError {
|
|
|
|
|
|
|
|
|
|
// Converts an unsuccessful response into a `Result.failure`
|
|
|
|
|
fileprivate func convertUnsuccessfulResponseToError() -> Self {
|
|
|
|
|
return self.flatMap {
|
|
|
|
|
$0.response.statusCode.isSuccessfulResponse
|
|
|
|
|
? .success($0)
|
|
|
|
|
: .failure($0.response.parseUnsuccessfulResponse())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private extension VerifiedHTTPResponse {
|
2023-03-01 09:24:32 -08:00
|
|
|
|
2023-05-24 09:51:50 -07:00
|
|
|
func copyWithNewRequestDate() -> Self {
|
2023-03-01 09:24:32 -08:00
|
|
|
// Update request time from server unless it failed verification.
|
|
|
|
|
guard self.verificationResult != .failed, let requestDate = self.requestDate else { return self }
|
|
|
|
|
|
|
|
|
|
return self.mapBody {
|
|
|
|
|
return $0.copy(with: requestDate)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 08:16:57 -07:00
|
|
|
var isLoadShedder: Bool {
|
2023-06-30 12:24:53 -07:00
|
|
|
return self.response.value(forHeaderField: HTTPClient.ResponseHeader.isLoadShedder) == "true"
|
2023-06-19 08:16:57 -07:00
|
|
|
}
|
|
|
|
|
|
2023-03-01 09:24:32 -08:00
|
|
|
}
|
2023-05-24 09:51:50 -07:00
|
|
|
|
|
|
|
|
private extension HTTPResponse where Body == Data {
|
|
|
|
|
|
|
|
|
|
func parseUnsuccessfulResponse() -> NetworkError {
|
2023-08-17 09:45:22 -07:00
|
|
|
let contentType = self.value(forHeaderField: HTTPClient.ResponseHeader.contentType) ?? ""
|
|
|
|
|
let isJSON = contentType.starts(with: "application/json")
|
2023-05-24 09:51:50 -07:00
|
|
|
|
|
|
|
|
return .errorResponse(
|
|
|
|
|
isJSON
|
|
|
|
|
? .from(self.body)
|
|
|
|
|
: .defaultResponse,
|
|
|
|
|
self.statusCode
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|