Session.swift 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  1. //
  2. // Session.swift
  3. //
  4. // Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Foundation
  25. /// `Session` creates and manages Alamofire's `Request` types during their lifetimes. It also provides common
  26. /// functionality for all `Request`s, including queuing, interception, trust management, redirect handling, and response
  27. /// cache handling.
  28. open class Session: @unchecked Sendable {
  29. /// Shared singleton instance used by all `AF.request` APIs. Cannot be modified.
  30. public static let `default` = Session()
  31. /// Type describing the timing of `Request` setup after creation.
  32. public enum RequestSetup {
  33. /// Calls the needed lifetime methods on created `Request`s only after they have `resume` called. `Request`s are
  34. /// inert upon creation.
  35. case lazy
  36. /// Calls the needed lifetime methods on created `Request`s immediately upon creation. Default in Alamofire
  37. /// versions before 5.11.
  38. ///
  39. /// - Warning: May lead to races between `Request.resume()` and various lifetime events. Not recommended except
  40. /// for backward compatibility.
  41. case eager
  42. }
  43. /// Underlying `URLSession` used to create `URLSessionTasks` for this instance, and for which this instance's
  44. /// `delegate` handles `URLSessionDelegate` callbacks.
  45. ///
  46. /// - Note: This instance should **NOT** be used to interact with the underlying `URLSessionTask`s. Doing so will
  47. /// break internal Alamofire logic that tracks those tasks.
  48. ///
  49. public let session: URLSession
  50. /// Instance's `SessionDelegate`, which handles the `URLSessionDelegate` methods and `Request` interaction.
  51. public let delegate: SessionDelegate
  52. /// Root `DispatchQueue` for all internal callbacks and state update. **MUST** be a serial queue.
  53. public let rootQueue: DispatchQueue
  54. /// Value determining whether this instance automatically calls `resume()` on all created `Request`s.
  55. public let startRequestsImmediately: Bool
  56. /// Value determining the timing of `Request` setup after creation. `.lazy` by default.
  57. ///
  58. /// - Note: Prior to Alamofire 5.11, `Session` acted as if this was set to `.eager`, so if any behavior changes are
  59. /// noted, `.eager` can be used for backward compatibility.
  60. public let requestSetup: RequestSetup
  61. /// `DispatchQueue` on which `URLRequest`s are created asynchronously. By default this queue uses `rootQueue` as its
  62. /// `target`, but a separate queue can be used if request creation is determined to be a bottleneck. Always profile
  63. /// and test before introducing an additional queue.
  64. public let requestQueue: DispatchQueue
  65. /// `DispatchQueue` passed to all `Request`s on which they perform their response serialization. By default this
  66. /// queue uses `rootQueue` as its `target` but a separate queue can be used if response serialization is determined
  67. /// to be a bottleneck. Always profile and test before introducing an additional queue.
  68. public let serializationQueue: DispatchQueue
  69. /// `RequestInterceptor` used for all `Request` created by the instance. `RequestInterceptor`s can also be added on a
  70. /// per-`Request` basis, in which case interceptors from the `Session` are executed first. `nil` by default.
  71. public let interceptor: (any RequestInterceptor)?
  72. /// `ServerTrustManager` instance used to evaluate all trust challenges and provide certificate and key pinning.
  73. public let serverTrustManager: ServerTrustManager?
  74. /// `RedirectHandler` instance used to provide customization for request redirection.
  75. public let redirectHandler: (any RedirectHandler)?
  76. /// `CachedResponseHandler` instance used to provide customization of cached response handling.
  77. public let cachedResponseHandler: (any CachedResponseHandler)?
  78. /// `CompositeEventMonitor` used to compose any passed `EventMonitor`s. `EventMonitor`s can also be added on a
  79. /// per-`Request` basis, in which case monitors from the `Session` are executed first.
  80. public let eventMonitor: CompositeEventMonitor
  81. /// `EventMonitor`s included in all instances unless overwritten. `[AlamofireNotifications()]` by default.
  82. @available(*, deprecated, message: "Use [AlamofireNotifications()] directly.")
  83. public let defaultEventMonitors: [any EventMonitor] = [AlamofireNotifications()]
  84. /// Internal map between `Request`s and any `URLSessionTasks` that may be in flight for them.
  85. var requestTaskMap = RequestTaskMap()
  86. /// `Set` of currently active `Request`s.
  87. var activeRequests: Set<Request> = []
  88. /// Completion events awaiting `URLSessionTaskMetrics`.
  89. var waitingCompletions: [URLSessionTask: () -> Void] = [:]
  90. /// Creates a `Session` from a `URLSession` and other parameters.
  91. ///
  92. /// - Note: When passing a `URLSession`, you must create the `URLSession` with a specific `delegateQueue` value and
  93. /// pass the `delegateQueue`'s `underlyingQueue` as the `rootQueue` parameter of this initializer.
  94. ///
  95. /// - Parameters:
  96. /// - session: Underlying `URLSession` for this instance.
  97. /// - delegate: `SessionDelegate` that handles `session`'s delegate callbacks as well as `Request`
  98. /// interaction.
  99. /// - rootQueue: Root `DispatchQueue` for all internal callbacks and state updates. **MUST** be a
  100. /// serial queue.
  101. /// - startRequestsImmediately: Determines whether this instance will automatically start all `Request`s once a
  102. /// response handler is added. `true` by default. If set to `false`, all `Request`s
  103. /// created must have `.resume()` called to start.
  104. /// - requestSetup: Determines when the `Session` will perform the internal setup for created
  105. /// `Request`s. `.lazy` by default.
  106. /// - requestQueue: `DispatchQueue` on which to perform `URLRequest` creation. By default this queue
  107. /// will use the `rootQueue` as its `target`. A separate queue can be used if it's
  108. /// determined request creation is a bottleneck, but that should only be done after
  109. /// careful testing and profiling. `nil` by default.
  110. /// - serializationQueue: `DispatchQueue` on which to perform all response serialization. By default this
  111. /// queue will use the `rootQueue` as its `target`. A separate queue can be used if
  112. /// it's determined response serialization is a bottleneck, but that should only be
  113. /// done after careful testing and profiling. `nil` by default.
  114. /// - interceptor: `RequestInterceptor` to be used for all `Request`s created by this instance. `nil`
  115. /// by default.
  116. /// - serverTrustManager: `ServerTrustManager` to be used for all trust evaluations by this instance. `nil`
  117. /// by default.
  118. /// - redirectHandler: `RedirectHandler` to be used by all `Request`s created by this instance. `nil` by
  119. /// default.
  120. /// - cachedResponseHandler: `CachedResponseHandler` to be used by all `Request`s created by this instance.
  121. /// `nil` by default.
  122. /// - eventMonitors: `EventMonitor`s used by the instance. `[AlamofireNotifications()]` by default.
  123. public init(session: URLSession,
  124. delegate: SessionDelegate,
  125. rootQueue: DispatchQueue,
  126. startRequestsImmediately: Bool = true,
  127. requestSetup: RequestSetup = .lazy,
  128. requestQueue: DispatchQueue? = nil,
  129. serializationQueue: DispatchQueue? = nil,
  130. interceptor: (any RequestInterceptor)? = nil,
  131. serverTrustManager: ServerTrustManager? = nil,
  132. redirectHandler: (any RedirectHandler)? = nil,
  133. cachedResponseHandler: (any CachedResponseHandler)? = nil,
  134. eventMonitors: [any EventMonitor] = [AlamofireNotifications()]) {
  135. precondition(session.configuration.identifier == nil,
  136. "Alamofire does not support background URLSessionConfigurations.")
  137. precondition(session.delegateQueue.underlyingQueue === rootQueue,
  138. "Session(session:) initializer must be passed the DispatchQueue used as the delegateQueue's underlyingQueue as rootQueue.")
  139. self.session = session
  140. self.delegate = delegate
  141. self.rootQueue = rootQueue
  142. self.startRequestsImmediately = startRequestsImmediately
  143. self.requestSetup = requestSetup
  144. self.requestQueue = requestQueue ?? DispatchQueue(label: "\(rootQueue.label).requestQueue", target: rootQueue)
  145. self.serializationQueue = serializationQueue ?? DispatchQueue(label: "\(rootQueue.label).serializationQueue", target: rootQueue)
  146. self.interceptor = interceptor
  147. self.serverTrustManager = serverTrustManager
  148. self.redirectHandler = redirectHandler
  149. self.cachedResponseHandler = cachedResponseHandler
  150. eventMonitor = CompositeEventMonitor(queue: rootQueue, monitors: eventMonitors)
  151. delegate.eventMonitor = eventMonitor
  152. delegate.stateProvider = self
  153. }
  154. /// Creates a `Session` from a `URLSessionConfiguration`.
  155. ///
  156. /// - Note: This initializer lets Alamofire handle the creation of the underlying `URLSession` and its
  157. /// `delegateQueue`, and is the recommended initializer for most uses.
  158. ///
  159. /// - Parameters:
  160. /// - configuration: `URLSessionConfiguration` to be used to create the underlying `URLSession`. Changes
  161. /// to this value after being passed to this initializer will have no effect.
  162. /// `URLSessionConfiguration.af.default` by default.
  163. /// - delegate: `SessionDelegate` that handles `session`'s delegate callbacks as well as `Request`
  164. /// interaction. `SessionDelegate()` by default.
  165. /// - rootQueue: Root `DispatchQueue` for all internal callbacks and state updates. **MUST** be a
  166. /// serial queue. `DispatchQueue(label: "org.alamofire.session.rootQueue")` by default.
  167. /// - startRequestsImmediately: Determines whether this instance will automatically start all `Request`s once a
  168. /// response handler is added. `true` by default. If set to `false`, all `Request`s
  169. /// created must have `.resume()` called to start.
  170. /// - requestSetup: Determines when the `Session` will perform the internal setup for created
  171. /// `Request`s. `.lazy` by default.
  172. /// - requestQueue: `DispatchQueue` on which to perform `URLRequest` creation. By default this queue
  173. /// will use the `rootQueue` as its `target`. A separate queue can be used if it's
  174. /// determined request creation is a bottleneck, but that should only be done after
  175. /// careful testing and profiling. `nil` by default.
  176. /// - serializationQueue: `DispatchQueue` on which to perform all response serialization. By default this
  177. /// queue will use the `rootQueue` as its `target`. A separate queue can be used if
  178. /// it's determined response serialization is a bottleneck, but that should only be
  179. /// done after careful testing and profiling. `nil` by default.
  180. /// - interceptor: `RequestInterceptor` to be used for all `Request`s created by this instance. `nil`
  181. /// by default.
  182. /// - serverTrustManager: `ServerTrustManager` to be used for all trust evaluations by this instance. `nil`
  183. /// by default.
  184. /// - redirectHandler: `RedirectHandler` to be used by all `Request`s created by this instance. `nil` by
  185. /// default.
  186. /// - cachedResponseHandler: `CachedResponseHandler` to be used by all `Request`s created by this instance.
  187. /// `nil` by default.
  188. /// - eventMonitors: `EventMonitor`s used by the instance. `[AlamofireNotifications()]` by default.
  189. public convenience init(configuration: URLSessionConfiguration = URLSessionConfiguration.af.default,
  190. delegate: SessionDelegate = SessionDelegate(),
  191. rootQueue: DispatchQueue = DispatchQueue(label: "org.alamofire.session.rootQueue"),
  192. startRequestsImmediately: Bool = true,
  193. requestSetup: RequestSetup = .lazy,
  194. requestQueue: DispatchQueue? = nil,
  195. serializationQueue: DispatchQueue? = nil,
  196. interceptor: (any RequestInterceptor)? = nil,
  197. serverTrustManager: ServerTrustManager? = nil,
  198. redirectHandler: (any RedirectHandler)? = nil,
  199. cachedResponseHandler: (any CachedResponseHandler)? = nil,
  200. eventMonitors: [any EventMonitor] = [AlamofireNotifications()]) {
  201. precondition(configuration.identifier == nil, "Alamofire does not support background URLSessionConfigurations.")
  202. // Retarget the incoming rootQueue for safety, unless it's the main queue, which we know is safe.
  203. let serialRootQueue = (rootQueue === DispatchQueue.main) ? rootQueue : DispatchQueue(label: rootQueue.label,
  204. target: rootQueue)
  205. let delegateQueue = OperationQueue(maxConcurrentOperationCount: 1, underlyingQueue: serialRootQueue, name: "\(serialRootQueue.label).sessionDelegate")
  206. let session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: delegateQueue)
  207. self.init(session: session,
  208. delegate: delegate,
  209. rootQueue: serialRootQueue,
  210. startRequestsImmediately: startRequestsImmediately,
  211. requestSetup: requestSetup,
  212. requestQueue: requestQueue,
  213. serializationQueue: serializationQueue,
  214. interceptor: interceptor,
  215. serverTrustManager: serverTrustManager,
  216. redirectHandler: redirectHandler,
  217. cachedResponseHandler: cachedResponseHandler,
  218. eventMonitors: eventMonitors)
  219. }
  220. deinit {
  221. finishRequestsForDeinit()
  222. session.invalidateAndCancel()
  223. }
  224. // MARK: - All Requests API
  225. /// Perform an action on all active `Request`s.
  226. ///
  227. /// - Note: The provided `action` closure is performed asynchronously, meaning that some `Request`s may complete and
  228. /// be unavailable by time it runs. Additionally, this action is performed on the instances's `rootQueue`,
  229. /// so care should be taken that actions are fast. Once the work on the `Request`s is complete, any
  230. /// additional work should be performed on another queue.
  231. ///
  232. /// - Parameters:
  233. /// - action: Closure to perform with all `Request`s.
  234. public func withAllRequests(perform action: @escaping @Sendable (Set<Request>) -> Void) {
  235. rootQueue.async {
  236. action(self.activeRequests)
  237. }
  238. }
  239. /// Cancel all active `Request`s, optionally calling a completion handler when complete.
  240. ///
  241. /// - Note: This is an asynchronous operation and does not block the creation of future `Request`s. Cancelled
  242. /// `Request`s may not cancel immediately due internal work, and may not cancel at all if they are close to
  243. /// completion when cancelled.
  244. ///
  245. /// - Parameters:
  246. /// - queue: `DispatchQueue` on which the completion handler is run. `.main` by default.
  247. /// - completion: Closure to be called when all `Request`s have been cancelled.
  248. public func cancelAllRequests(completingOnQueue queue: DispatchQueue = .main, completion: (@Sendable () -> Void)? = nil) {
  249. withAllRequests { requests in
  250. requests.forEach { $0.cancel() }
  251. queue.async {
  252. completion?()
  253. }
  254. }
  255. }
  256. // MARK: - DataRequest
  257. /// Closure which provides a `URLRequest` for mutation.
  258. public typealias RequestModifier = @Sendable (inout URLRequest) throws -> Void
  259. struct RequestConvertible: URLRequestConvertible {
  260. let url: any URLConvertible
  261. let method: HTTPMethod
  262. let parameters: Parameters?
  263. let encoding: any ParameterEncoding
  264. let headers: HTTPHeaders?
  265. let requestModifier: RequestModifier?
  266. func asURLRequest() throws -> URLRequest {
  267. var request = try URLRequest(url: url, method: method, headers: headers)
  268. try requestModifier?(&request)
  269. return try encoding.encode(request, with: parameters)
  270. }
  271. }
  272. /// Creates a `DataRequest` from a `URLRequest` created using the passed components and a `RequestInterceptor`.
  273. ///
  274. /// - Parameters:
  275. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  276. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  277. /// - parameters: `Parameters` (a.k.a. `[String: Any]`) value to be encoded into the `URLRequest`.
  278. /// `nil` by default.
  279. /// - encoding: `ParameterEncoding` to be used to encode the `parameters` value into the
  280. /// `URLRequest`. `URLEncoding.default` by default.
  281. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  282. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  283. /// - shouldAutomaticallyResume: Whether the `DataRequest` should resume after the first response handler is added.
  284. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the
  285. /// provided parameters. `nil` by default.
  286. ///
  287. /// - Returns: The created `DataRequest`.
  288. open func request(_ convertible: any URLConvertible,
  289. method: HTTPMethod = .get,
  290. parameters: Parameters? = nil,
  291. encoding: any ParameterEncoding = URLEncoding.default,
  292. headers: HTTPHeaders? = nil,
  293. interceptor: (any RequestInterceptor)? = nil,
  294. shouldAutomaticallyResume: Bool? = nil,
  295. requestModifier: RequestModifier? = nil) -> DataRequest {
  296. let convertible = RequestConvertible(url: convertible,
  297. method: method,
  298. parameters: parameters,
  299. encoding: encoding,
  300. headers: headers,
  301. requestModifier: requestModifier)
  302. return request(convertible, interceptor: interceptor, shouldAutomaticallyResume: shouldAutomaticallyResume)
  303. }
  304. struct RequestEncodableConvertible<Parameters: Encodable & Sendable>: URLRequestConvertible {
  305. let url: any URLConvertible
  306. let method: HTTPMethod
  307. let parameters: Parameters?
  308. let encoder: any ParameterEncoder
  309. let headers: HTTPHeaders?
  310. let requestModifier: RequestModifier?
  311. func asURLRequest() throws -> URLRequest {
  312. var request = try URLRequest(url: url, method: method, headers: headers)
  313. try requestModifier?(&request)
  314. return try parameters.map { try encoder.encode($0, into: request) } ?? request
  315. }
  316. }
  317. /// Creates a `DataRequest` from a `URLRequest` created using the passed components, `Encodable` parameters, and a
  318. /// `RequestInterceptor`.
  319. ///
  320. /// - Parameters:
  321. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  322. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  323. /// - parameters: `Encodable` value to be encoded into the `URLRequest`. `nil` by default.
  324. /// - encoder: `ParameterEncoder` to be used to encode the `parameters` value into the `URLRequest`.
  325. /// `URLEncodedFormParameterEncoder.default` by default.
  326. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  327. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  328. /// - shouldAutomaticallyResume: Whether the `DataRequest` should resume after the first response handler is added.
  329. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the provided parameters. `nil` by default.
  330. ///
  331. /// - Returns: The created `DataRequest`.
  332. open func request<Parameters: Encodable & Sendable>(_ convertible: any URLConvertible,
  333. method: HTTPMethod = .get,
  334. parameters: Parameters? = nil,
  335. encoder: any ParameterEncoder = URLEncodedFormParameterEncoder.default,
  336. headers: HTTPHeaders? = nil,
  337. interceptor: (any RequestInterceptor)? = nil,
  338. shouldAutomaticallyResume: Bool? = nil,
  339. requestModifier: RequestModifier? = nil) -> DataRequest {
  340. let convertible = RequestEncodableConvertible(url: convertible,
  341. method: method,
  342. parameters: parameters,
  343. encoder: encoder,
  344. headers: headers,
  345. requestModifier: requestModifier)
  346. return request(convertible, interceptor: interceptor, shouldAutomaticallyResume: shouldAutomaticallyResume)
  347. }
  348. /// Creates a `DataRequest` from a `URLRequestConvertible` value and a `RequestInterceptor`.
  349. ///
  350. /// - Parameters:
  351. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  352. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  353. /// - shouldAutomaticallyResume: Whether the `DataRequest` should resume after the first response handler is added.
  354. ///
  355. /// - Returns: The created `DataRequest`.
  356. open func request(_ convertible: any URLRequestConvertible,
  357. interceptor: (any RequestInterceptor)? = nil,
  358. shouldAutomaticallyResume: Bool? = nil) -> DataRequest {
  359. let request = DataRequest(convertible: convertible,
  360. underlyingQueue: rootQueue,
  361. serializationQueue: serializationQueue,
  362. eventMonitor: eventMonitor,
  363. interceptor: interceptor,
  364. shouldAutomaticallyResume: shouldAutomaticallyResume,
  365. delegate: self)
  366. performEagerlyIfNecessary(request)
  367. return request
  368. }
  369. // MARK: - DataStreamRequest
  370. /// Creates a `DataStreamRequest` from the passed components, `Encodable` parameters, and `RequestInterceptor`.
  371. ///
  372. /// - Parameters:
  373. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  374. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  375. /// - parameters: `Encodable` value to be encoded into the `URLRequest`. `nil` by default.
  376. /// - encoder: `ParameterEncoder` to be used to encode the `parameters` value into the
  377. /// `URLRequest`.
  378. /// `URLEncodedFormParameterEncoder.default` by default.
  379. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  380. /// - automaticallyCancelOnStreamError: `Bool` indicating whether the instance should be canceled when an `Error`
  381. /// is thrown while serializing stream `Data`. `false` by default.
  382. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil`
  383. /// by default.
  384. /// - shouldAutomaticallyResume: Whether the `DataStreamRequest` should resume after the first response
  385. /// handler is added.
  386. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from
  387. /// the provided parameters. `nil` by default.
  388. ///
  389. /// - Returns: The created `DataStream` request.
  390. open func streamRequest<Parameters: Encodable & Sendable>(_ convertible: any URLConvertible,
  391. method: HTTPMethod = .get,
  392. parameters: Parameters? = nil,
  393. encoder: any ParameterEncoder = URLEncodedFormParameterEncoder.default,
  394. headers: HTTPHeaders? = nil,
  395. automaticallyCancelOnStreamError: Bool = false,
  396. interceptor: (any RequestInterceptor)? = nil,
  397. shouldAutomaticallyResume: Bool? = nil,
  398. requestModifier: RequestModifier? = nil) -> DataStreamRequest {
  399. let convertible = RequestEncodableConvertible(url: convertible,
  400. method: method,
  401. parameters: parameters,
  402. encoder: encoder,
  403. headers: headers,
  404. requestModifier: requestModifier)
  405. return streamRequest(convertible,
  406. automaticallyCancelOnStreamError: automaticallyCancelOnStreamError,
  407. interceptor: interceptor,
  408. shouldAutomaticallyResume: shouldAutomaticallyResume)
  409. }
  410. /// Creates a `DataStreamRequest` from the passed components and `RequestInterceptor`.
  411. ///
  412. /// - Parameters:
  413. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  414. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  415. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  416. /// - automaticallyCancelOnStreamError: `Bool` indicating whether the instance should be canceled when an `Error`
  417. /// is thrown while serializing stream `Data`. `false` by default.
  418. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil`
  419. /// by default.
  420. /// - shouldAutomaticallyResume: Whether the `DataStreamRequest` should resume after the first response
  421. /// handler is added.
  422. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from
  423. /// the provided parameters. `nil` by default.
  424. ///
  425. /// - Returns: The created `DataStream` request.
  426. open func streamRequest(_ convertible: any URLConvertible,
  427. method: HTTPMethod = .get,
  428. headers: HTTPHeaders? = nil,
  429. automaticallyCancelOnStreamError: Bool = false,
  430. interceptor: (any RequestInterceptor)? = nil,
  431. shouldAutomaticallyResume: Bool? = nil,
  432. requestModifier: RequestModifier? = nil) -> DataStreamRequest {
  433. let convertible = RequestEncodableConvertible(url: convertible,
  434. method: method,
  435. parameters: Empty?.none,
  436. encoder: URLEncodedFormParameterEncoder.default,
  437. headers: headers,
  438. requestModifier: requestModifier)
  439. return streamRequest(convertible,
  440. automaticallyCancelOnStreamError: automaticallyCancelOnStreamError,
  441. interceptor: interceptor,
  442. shouldAutomaticallyResume: shouldAutomaticallyResume)
  443. }
  444. /// Creates a `DataStreamRequest` from the passed `URLRequestConvertible` value and `RequestInterceptor`.
  445. ///
  446. /// - Parameters:
  447. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  448. /// - automaticallyCancelOnStreamError: `Bool` indicating whether the instance should be canceled when an `Error`
  449. /// is thrown while serializing stream `Data`. `false` by default.
  450. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil`
  451. /// by default.
  452. /// - shouldAutomaticallyResume: Whether the `DataStreamRequest` should resume after the first response
  453. /// handler is added.
  454. ///
  455. /// - Returns: The created `DataStreamRequest`.
  456. open func streamRequest(_ convertible: any URLRequestConvertible,
  457. automaticallyCancelOnStreamError: Bool = false,
  458. interceptor: (any RequestInterceptor)? = nil,
  459. shouldAutomaticallyResume: Bool? = nil) -> DataStreamRequest {
  460. let request = DataStreamRequest(convertible: convertible,
  461. automaticallyCancelOnStreamError: automaticallyCancelOnStreamError,
  462. underlyingQueue: rootQueue,
  463. serializationQueue: serializationQueue,
  464. eventMonitor: eventMonitor,
  465. interceptor: interceptor,
  466. shouldAutomaticallyResume: shouldAutomaticallyResume,
  467. delegate: self)
  468. performEagerlyIfNecessary(request)
  469. return request
  470. }
  471. #if canImport(Darwin) && !canImport(FoundationNetworking) // Only Apple platforms support URLSessionWebSocketTask.
  472. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  473. @_spi(WebSocket) open func webSocketRequest(
  474. to url: any URLConvertible,
  475. configuration: WebSocketRequest.Configuration = .default,
  476. headers: HTTPHeaders? = nil,
  477. interceptor: (any RequestInterceptor)? = nil,
  478. shouldAutomaticallyResume: Bool? = nil,
  479. requestModifier: RequestModifier? = nil
  480. ) -> WebSocketRequest {
  481. webSocketRequest(
  482. to: url,
  483. configuration: configuration,
  484. parameters: Empty?.none,
  485. encoder: URLEncodedFormParameterEncoder.default,
  486. headers: headers,
  487. interceptor: interceptor,
  488. requestModifier: requestModifier
  489. )
  490. }
  491. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  492. @_spi(WebSocket) open func webSocketRequest<Parameters>(
  493. to url: any URLConvertible,
  494. configuration: WebSocketRequest.Configuration = .default,
  495. parameters: Parameters? = nil,
  496. encoder: any ParameterEncoder = URLEncodedFormParameterEncoder.default,
  497. headers: HTTPHeaders? = nil,
  498. interceptor: (any RequestInterceptor)? = nil,
  499. shouldAutomaticallyResume: Bool? = nil,
  500. requestModifier: RequestModifier? = nil
  501. ) -> WebSocketRequest where Parameters: Encodable & Sendable {
  502. let convertible = RequestEncodableConvertible(url: url,
  503. method: .get,
  504. parameters: parameters,
  505. encoder: encoder,
  506. headers: headers,
  507. requestModifier: requestModifier)
  508. let request = WebSocketRequest(convertible: convertible,
  509. configuration: configuration,
  510. underlyingQueue: rootQueue,
  511. serializationQueue: serializationQueue,
  512. eventMonitor: eventMonitor,
  513. interceptor: interceptor,
  514. shouldAutomaticallyResume: shouldAutomaticallyResume,
  515. delegate: self)
  516. performEagerlyIfNecessary(request)
  517. return request
  518. }
  519. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  520. @_spi(WebSocket) open func webSocketRequest(performing convertible: any URLRequestConvertible,
  521. configuration: WebSocketRequest.Configuration = .default,
  522. interceptor: (any RequestInterceptor)? = nil,
  523. shouldAutomaticallyResume: Bool? = nil) -> WebSocketRequest {
  524. let request = WebSocketRequest(convertible: convertible,
  525. configuration: configuration,
  526. underlyingQueue: rootQueue,
  527. serializationQueue: serializationQueue,
  528. eventMonitor: eventMonitor,
  529. interceptor: interceptor,
  530. shouldAutomaticallyResume: shouldAutomaticallyResume,
  531. delegate: self)
  532. performEagerlyIfNecessary(request)
  533. return request
  534. }
  535. #endif
  536. // MARK: - DownloadRequest
  537. /// Creates a `DownloadRequest` using a `URLRequest` created using the passed components, `RequestInterceptor`, and
  538. /// `Destination`.
  539. ///
  540. /// - Parameters:
  541. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  542. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  543. /// - parameters: `Parameters` (a.k.a. `[String: Any]`) value to be encoded into the `URLRequest`.
  544. /// `nil` by default.
  545. /// - encoding: `ParameterEncoding` to be used to encode the `parameters` value into the `URLRequest`.
  546. /// Defaults to `URLEncoding.default`.
  547. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  548. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  549. /// - shouldAutomaticallyResume: Whether the `DownloadRequest` should resume after the first response handler is added.
  550. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the provided
  551. /// parameters. `nil` by default.
  552. /// - destination: `DownloadRequest.Destination` closure used to determine how and where the downloaded file
  553. /// should be moved. `nil` by default.
  554. ///
  555. /// - Returns: The created `DownloadRequest`.
  556. open func download(_ convertible: any URLConvertible,
  557. method: HTTPMethod = .get,
  558. parameters: Parameters? = nil,
  559. encoding: any ParameterEncoding = URLEncoding.default,
  560. headers: HTTPHeaders? = nil,
  561. interceptor: (any RequestInterceptor)? = nil,
  562. shouldAutomaticallyResume: Bool? = nil,
  563. requestModifier: RequestModifier? = nil,
  564. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  565. let convertible = RequestConvertible(url: convertible,
  566. method: method,
  567. parameters: parameters,
  568. encoding: encoding,
  569. headers: headers,
  570. requestModifier: requestModifier)
  571. return download(convertible, interceptor: interceptor, shouldAutomaticallyResume: shouldAutomaticallyResume, to: destination)
  572. }
  573. /// Creates a `DownloadRequest` from a `URLRequest` created using the passed components, `Encodable` parameters, and
  574. /// a `RequestInterceptor`.
  575. ///
  576. /// - Parameters:
  577. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  578. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  579. /// - parameters: Value conforming to `Encodable` to be encoded into the `URLRequest`. `nil` by default.
  580. /// - encoder: `ParameterEncoder` to be used to encode the `parameters` value into the `URLRequest`.
  581. /// Defaults to `URLEncodedFormParameterEncoder.default`.
  582. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  583. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  584. /// - shouldAutomaticallyResume: Whether the `DownloadRequest` should resume after the first response handler is added.
  585. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the
  586. /// provided parameters. `nil` by default.
  587. /// - destination: `DownloadRequest.Destination` closure used to determine how and where the
  588. /// downloaded file should be moved. `nil` by default.
  589. ///
  590. /// - Returns: The created `DownloadRequest`.
  591. open func download<Parameters: Encodable & Sendable>(_ convertible: any URLConvertible,
  592. method: HTTPMethod = .get,
  593. parameters: Parameters? = nil,
  594. encoder: any ParameterEncoder = URLEncodedFormParameterEncoder.default,
  595. headers: HTTPHeaders? = nil,
  596. interceptor: (any RequestInterceptor)? = nil,
  597. shouldAutomaticallyResume: Bool? = nil,
  598. requestModifier: RequestModifier? = nil,
  599. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  600. let convertible = RequestEncodableConvertible(url: convertible,
  601. method: method,
  602. parameters: parameters,
  603. encoder: encoder,
  604. headers: headers,
  605. requestModifier: requestModifier)
  606. return download(convertible, interceptor: interceptor, shouldAutomaticallyResume: shouldAutomaticallyResume, to: destination)
  607. }
  608. /// Creates a `DownloadRequest` from a `URLRequestConvertible` value, a `RequestInterceptor`, and a `Destination`.
  609. ///
  610. /// - Parameters:
  611. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  612. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  613. /// - shouldAutomaticallyResume: Whether the `DownloadRequest` should resume after the first response handler is added.
  614. /// - destination: `DownloadRequest.Destination` closure used to determine how and where the
  615. /// downloaded file should be moved. `nil` by default.
  616. ///
  617. /// - Returns: The created `DownloadRequest`.
  618. open func download(_ convertible: any URLRequestConvertible,
  619. interceptor: (any RequestInterceptor)? = nil,
  620. shouldAutomaticallyResume: Bool? = nil,
  621. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  622. let request = DownloadRequest(downloadable: .request(convertible),
  623. underlyingQueue: rootQueue,
  624. serializationQueue: serializationQueue,
  625. eventMonitor: eventMonitor,
  626. interceptor: interceptor,
  627. shouldAutomaticallyResume: shouldAutomaticallyResume,
  628. delegate: self,
  629. destination: destination ?? DownloadRequest.defaultDestination)
  630. performEagerlyIfNecessary(request)
  631. return request
  632. }
  633. /// Creates a `DownloadRequest` from the `resumeData` produced from a previously cancelled `DownloadRequest`, as
  634. /// well as a `RequestInterceptor`, and a `Destination`.
  635. ///
  636. /// - Note: If `destination` is not specified, the download will be moved to a temporary location determined by
  637. /// Alamofire. The file will not be deleted until the system purges the temporary files.
  638. ///
  639. /// - Note: On some versions of all Apple platforms (iOS 10 - 10.2, macOS 10.12 - 10.12.2, tvOS 10 - 10.1, watchOS 3 - 3.1.1),
  640. /// `resumeData` is broken on background URL session configurations. There's an underlying bug in the `resumeData`
  641. /// generation logic where the data is written incorrectly and will always fail to resume the download. For more
  642. /// information about the bug and possible workarounds, please refer to the [this Stack Overflow post](http://stackoverflow.com/a/39347461/1342462).
  643. ///
  644. /// - Parameters:
  645. /// - data: The resume data from a previously cancelled `DownloadRequest` or `URLSessionDownloadTask`.
  646. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  647. /// - shouldAutomaticallyResume: Whether the `DownloadRequest` should resume after the first response handler is added.
  648. /// - destination: `DownloadRequest.Destination` closure used to determine how and where the
  649. /// downloaded file should be moved. `nil` by default.
  650. ///
  651. /// - Returns: The created `DownloadRequest`.
  652. open func download(resumingWith data: Data,
  653. interceptor: (any RequestInterceptor)? = nil,
  654. shouldAutomaticallyResume: Bool? = nil,
  655. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  656. let request = DownloadRequest(downloadable: .resumeData(data),
  657. underlyingQueue: rootQueue,
  658. serializationQueue: serializationQueue,
  659. eventMonitor: eventMonitor,
  660. interceptor: interceptor,
  661. shouldAutomaticallyResume: shouldAutomaticallyResume,
  662. delegate: self,
  663. destination: destination ?? DownloadRequest.defaultDestination)
  664. performEagerlyIfNecessary(request)
  665. return request
  666. }
  667. // MARK: - UploadRequest
  668. struct ParameterlessRequestConvertible: URLRequestConvertible {
  669. let url: any URLConvertible
  670. let method: HTTPMethod
  671. let headers: HTTPHeaders?
  672. let requestModifier: RequestModifier?
  673. func asURLRequest() throws -> URLRequest {
  674. var request = try URLRequest(url: url, method: method, headers: headers)
  675. try requestModifier?(&request)
  676. return request
  677. }
  678. }
  679. struct Upload: UploadConvertible {
  680. let request: any URLRequestConvertible
  681. let uploadable: any UploadableConvertible
  682. func createUploadable() throws -> UploadRequest.Uploadable {
  683. try uploadable.createUploadable()
  684. }
  685. func asURLRequest() throws -> URLRequest {
  686. try request.asURLRequest()
  687. }
  688. }
  689. // MARK: Data
  690. /// Creates an `UploadRequest` for the given `Data`, `URLRequest` components, and `RequestInterceptor`.
  691. ///
  692. /// - Parameters:
  693. /// - data: The `Data` to upload.
  694. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  695. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  696. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  697. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  698. /// - shouldAutomaticallyResume: Whether the `UploadRequest` should resume after the first response handler is added.
  699. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default`
  700. /// instance by default.
  701. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the
  702. /// provided parameters. `nil` by default.
  703. ///
  704. /// - Returns: The created `UploadRequest`.
  705. open func upload(_ data: Data,
  706. to convertible: any URLConvertible,
  707. method: HTTPMethod = .post,
  708. headers: HTTPHeaders? = nil,
  709. interceptor: (any RequestInterceptor)? = nil,
  710. shouldAutomaticallyResume: Bool? = nil,
  711. fileManager: FileManager = .default,
  712. requestModifier: RequestModifier? = nil) -> UploadRequest {
  713. let convertible = ParameterlessRequestConvertible(url: convertible,
  714. method: method,
  715. headers: headers,
  716. requestModifier: requestModifier)
  717. return upload(data,
  718. with: convertible,
  719. interceptor: interceptor,
  720. shouldAutomaticallyResume: shouldAutomaticallyResume,
  721. fileManager: fileManager)
  722. }
  723. /// Creates an `UploadRequest` for the given `Data` using the `URLRequestConvertible` value and `RequestInterceptor`.
  724. ///
  725. /// - Parameters:
  726. /// - data: The `Data` to upload.
  727. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  728. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  729. /// - shouldAutomaticallyResume: Whether the `UploadRequest` should resume after the first response handler is added.
  730. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by default.
  731. ///
  732. /// - Returns: The created `UploadRequest`.
  733. open func upload(_ data: Data,
  734. with convertible: any URLRequestConvertible,
  735. interceptor: (any RequestInterceptor)? = nil,
  736. shouldAutomaticallyResume: Bool? = nil,
  737. fileManager: FileManager = .default) -> UploadRequest {
  738. upload(.data(data),
  739. with: convertible,
  740. interceptor: interceptor,
  741. shouldAutomaticallyResume: shouldAutomaticallyResume,
  742. fileManager: fileManager)
  743. }
  744. // MARK: File
  745. /// Creates an `UploadRequest` for the file at the given file `URL`, using a `URLRequest` from the provided
  746. /// components and `RequestInterceptor`.
  747. ///
  748. /// - Parameters:
  749. /// - fileURL: The `URL` of the file to upload.
  750. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  751. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  752. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  753. /// - interceptor: `RequestInterceptor` value to be used by the returned `UploadRequest`. `nil` by default.
  754. /// - shouldAutomaticallyResume: Whether the `UploadRequest` should resume after the first response handler is added.
  755. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by default.
  756. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the provided parameters. `nil` by default.
  757. ///
  758. /// - Returns: The created `UploadRequest`.
  759. open func upload(_ fileURL: URL,
  760. to convertible: any URLConvertible,
  761. method: HTTPMethod = .post,
  762. headers: HTTPHeaders? = nil,
  763. interceptor: (any RequestInterceptor)? = nil,
  764. shouldAutomaticallyResume: Bool? = nil,
  765. fileManager: FileManager = .default,
  766. requestModifier: RequestModifier? = nil) -> UploadRequest {
  767. let convertible = ParameterlessRequestConvertible(url: convertible,
  768. method: method,
  769. headers: headers,
  770. requestModifier: requestModifier)
  771. return upload(fileURL,
  772. with: convertible,
  773. interceptor: interceptor,
  774. shouldAutomaticallyResume: shouldAutomaticallyResume,
  775. fileManager: fileManager)
  776. }
  777. /// Creates an `UploadRequest` for the file at the given file `URL` using the `URLRequestConvertible` value and
  778. /// `RequestInterceptor`.
  779. ///
  780. /// - Parameters:
  781. /// - fileURL: The `URL` of the file to upload.
  782. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  783. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  784. /// - shouldAutomaticallyResume: Whether the `UploadRequest` should resume after the first response handler is added.
  785. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default`
  786. /// instance by default.
  787. ///
  788. /// - Returns: The created `UploadRequest`.
  789. open func upload(_ fileURL: URL,
  790. with convertible: any URLRequestConvertible,
  791. interceptor: (any RequestInterceptor)? = nil,
  792. shouldAutomaticallyResume: Bool? = nil,
  793. fileManager: FileManager = .default) -> UploadRequest {
  794. upload(.file(fileURL, shouldRemove: false),
  795. with: convertible,
  796. interceptor: interceptor,
  797. shouldAutomaticallyResume: shouldAutomaticallyResume,
  798. fileManager: fileManager)
  799. }
  800. // MARK: InputStream
  801. /// Creates an `UploadRequest` from the `InputStream` provided using a `URLRequest` from the provided components and
  802. /// `RequestInterceptor`.
  803. ///
  804. /// - Parameters:
  805. /// - stream: The `InputStream` that provides the data to upload.
  806. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  807. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  808. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  809. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  810. /// - shouldAutomaticallyResume: Whether the `UploadRequest` should resume after the first response handler is added.
  811. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default`
  812. /// instance by default.
  813. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the
  814. /// provided parameters. `nil` by default.
  815. ///
  816. /// - Returns: The created `UploadRequest`.
  817. open func upload(_ stream: InputStream,
  818. to convertible: any URLConvertible,
  819. method: HTTPMethod = .post,
  820. headers: HTTPHeaders? = nil,
  821. interceptor: (any RequestInterceptor)? = nil,
  822. fileManager: FileManager = .default,
  823. requestModifier: RequestModifier? = nil) -> UploadRequest {
  824. let convertible = ParameterlessRequestConvertible(url: convertible,
  825. method: method,
  826. headers: headers,
  827. requestModifier: requestModifier)
  828. return upload(stream, with: convertible, interceptor: interceptor, fileManager: fileManager)
  829. }
  830. /// Creates an `UploadRequest` from the provided `InputStream` using the `URLRequestConvertible` value and
  831. /// `RequestInterceptor`.
  832. ///
  833. /// - Parameters:
  834. /// - stream: The `InputStream` that provides the data to upload.
  835. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  836. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  837. /// - shouldAutomaticallyResume: Whether the `UploadRequest` should resume after the first response handler is added.
  838. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by default.
  839. ///
  840. /// - Returns: The created `UploadRequest`.
  841. open func upload(_ stream: InputStream,
  842. with convertible: any URLRequestConvertible,
  843. interceptor: (any RequestInterceptor)? = nil,
  844. shouldAutomaticallyResume: Bool? = nil,
  845. fileManager: FileManager = .default) -> UploadRequest {
  846. upload(.stream(stream), with: convertible, interceptor: interceptor, shouldAutomaticallyResume: shouldAutomaticallyResume, fileManager: fileManager)
  847. }
  848. // MARK: MultipartFormData
  849. /// Creates an `UploadRequest` for the multipart form data built using a closure and sent using the provided
  850. /// `URLRequest` components and `RequestInterceptor`.
  851. ///
  852. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cumulative
  853. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  854. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  855. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  856. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  857. /// used for larger payloads such as video content.
  858. ///
  859. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  860. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  861. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  862. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  863. /// technique was used.
  864. ///
  865. /// - Parameters:
  866. /// - multipartFormData: `MultipartFormData` building closure.
  867. /// - url: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  868. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  869. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  870. /// default.
  871. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  872. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  873. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  874. /// - fileManager: `FileManager` to be used if the form data exceeds the memory threshold and is
  875. /// written to disk before being uploaded. `.default` instance by default.
  876. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the
  877. /// provided parameters. `nil` by default.
  878. ///
  879. /// - Returns: The created `UploadRequest`.
  880. open func upload(multipartFormData: @escaping (MultipartFormData) -> Void,
  881. to url: any URLConvertible,
  882. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  883. method: HTTPMethod = .post,
  884. headers: HTTPHeaders? = nil,
  885. interceptor: (any RequestInterceptor)? = nil,
  886. fileManager: FileManager = .default,
  887. requestModifier: RequestModifier? = nil) -> UploadRequest {
  888. let convertible = ParameterlessRequestConvertible(url: url,
  889. method: method,
  890. headers: headers,
  891. requestModifier: requestModifier)
  892. let formData = MultipartFormData(fileManager: fileManager)
  893. multipartFormData(formData)
  894. return upload(multipartFormData: formData,
  895. with: convertible,
  896. usingThreshold: encodingMemoryThreshold,
  897. interceptor: interceptor,
  898. fileManager: fileManager)
  899. }
  900. /// Creates an `UploadRequest` using a `MultipartFormData` building closure, the provided `URLRequestConvertible`
  901. /// value, and a `RequestInterceptor`.
  902. ///
  903. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cumulative
  904. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  905. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  906. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  907. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  908. /// used for larger payloads such as video content.
  909. ///
  910. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  911. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  912. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  913. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  914. /// technique was used.
  915. ///
  916. /// - Parameters:
  917. /// - multipartFormData: `MultipartFormData` building closure.
  918. /// - request: `URLRequestConvertible` value to be used to create the `URLRequest`.
  919. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  920. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  921. /// default.
  922. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  923. /// - fileManager: `FileManager` to be used if the form data exceeds the memory threshold and is
  924. /// written to disk before being uploaded. `.default` instance by default.
  925. ///
  926. /// - Returns: The created `UploadRequest`.
  927. open func upload(multipartFormData: @escaping (MultipartFormData) -> Void,
  928. with request: any URLRequestConvertible,
  929. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  930. interceptor: (any RequestInterceptor)? = nil,
  931. fileManager: FileManager = .default) -> UploadRequest {
  932. let formData = MultipartFormData(fileManager: fileManager)
  933. multipartFormData(formData)
  934. return upload(multipartFormData: formData,
  935. with: request,
  936. usingThreshold: encodingMemoryThreshold,
  937. interceptor: interceptor,
  938. fileManager: fileManager)
  939. }
  940. /// Creates an `UploadRequest` for the prebuilt `MultipartFormData` value using the provided `URLRequest` components
  941. /// and `RequestInterceptor`.
  942. ///
  943. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cumulative
  944. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  945. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  946. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  947. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  948. /// used for larger payloads such as video content.
  949. ///
  950. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  951. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  952. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  953. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  954. /// technique was used.
  955. ///
  956. /// - Parameters:
  957. /// - multipartFormData: `MultipartFormData` instance to upload.
  958. /// - url: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  959. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  960. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  961. /// default.
  962. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  963. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  964. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  965. /// - fileManager: `FileManager` to be used if the form data exceeds the memory threshold and is
  966. /// written to disk before being uploaded. `.default` instance by default.
  967. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the
  968. /// provided parameters. `nil` by default.
  969. ///
  970. /// - Returns: The created `UploadRequest`.
  971. open func upload(multipartFormData: MultipartFormData,
  972. to url: any URLConvertible,
  973. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  974. method: HTTPMethod = .post,
  975. headers: HTTPHeaders? = nil,
  976. interceptor: (any RequestInterceptor)? = nil,
  977. shouldAutomaticallyResume: Bool? = nil,
  978. fileManager: FileManager = .default,
  979. requestModifier: RequestModifier? = nil) -> UploadRequest {
  980. let convertible = ParameterlessRequestConvertible(url: url,
  981. method: method,
  982. headers: headers,
  983. requestModifier: requestModifier)
  984. let multipartUpload = MultipartUpload(encodingMemoryThreshold: encodingMemoryThreshold,
  985. request: convertible,
  986. multipartFormData: multipartFormData)
  987. return upload(multipartUpload, interceptor: interceptor, shouldAutomaticallyResume: shouldAutomaticallyResume, fileManager: fileManager)
  988. }
  989. /// Creates an `UploadRequest` for the prebuilt `MultipartFormData` value using the providing `URLRequestConvertible`
  990. /// value and `RequestInterceptor`.
  991. ///
  992. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cumulative
  993. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  994. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  995. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  996. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  997. /// used for larger payloads such as video content.
  998. ///
  999. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  1000. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  1001. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  1002. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  1003. /// technique was used.
  1004. ///
  1005. /// - Parameters:
  1006. /// - multipartFormData: `MultipartFormData` instance to upload.
  1007. /// - request: `URLRequestConvertible` value to be used to create the `URLRequest`.
  1008. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  1009. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  1010. /// default.
  1011. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  1012. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  1013. /// default.
  1014. ///
  1015. /// - Returns: The created `UploadRequest`.
  1016. open func upload(multipartFormData: MultipartFormData,
  1017. with request: any URLRequestConvertible,
  1018. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  1019. interceptor: (any RequestInterceptor)? = nil,
  1020. shouldAutomaticallyResume: Bool? = nil,
  1021. fileManager: FileManager = .default) -> UploadRequest {
  1022. let multipartUpload = MultipartUpload(encodingMemoryThreshold: encodingMemoryThreshold,
  1023. request: request,
  1024. multipartFormData: multipartFormData)
  1025. return upload(multipartUpload, interceptor: interceptor, shouldAutomaticallyResume: shouldAutomaticallyResume, fileManager: fileManager)
  1026. }
  1027. // MARK: - Internal API
  1028. // MARK: Uploadable
  1029. func upload(_ uploadable: UploadRequest.Uploadable,
  1030. with convertible: any URLRequestConvertible,
  1031. interceptor: (any RequestInterceptor)?,
  1032. shouldAutomaticallyResume: Bool?,
  1033. fileManager: FileManager) -> UploadRequest {
  1034. let uploadable = Upload(request: convertible, uploadable: uploadable)
  1035. return upload(uploadable, interceptor: interceptor, shouldAutomaticallyResume: shouldAutomaticallyResume, fileManager: fileManager)
  1036. }
  1037. func upload(_ upload: any UploadConvertible,
  1038. interceptor: (any RequestInterceptor)?,
  1039. shouldAutomaticallyResume: Bool?,
  1040. fileManager: FileManager) -> UploadRequest {
  1041. let request = UploadRequest(convertible: upload,
  1042. underlyingQueue: rootQueue,
  1043. serializationQueue: serializationQueue,
  1044. eventMonitor: eventMonitor,
  1045. interceptor: interceptor,
  1046. shouldAutomaticallyResume: shouldAutomaticallyResume,
  1047. fileManager: fileManager,
  1048. delegate: self)
  1049. performEagerlyIfNecessary(request)
  1050. return request
  1051. }
  1052. // MARK: Perform
  1053. func performEagerlyIfNecessary(_ request: Request) {
  1054. guard requestSetup == .eager else { return }
  1055. perform(request)
  1056. }
  1057. /// Starts performing the provided `Request`.
  1058. ///
  1059. /// - Parameter request: The `Request` to perform.
  1060. func perform(_ request: Request) {
  1061. rootQueue.async {
  1062. guard !request.isCancelled else { return }
  1063. self.activeRequests.insert(request)
  1064. self.requestQueue.async {
  1065. // Leaf types must come first, otherwise they will cast as their superclass.
  1066. switch request {
  1067. // UploadRequest must come before DataRequest due to subtype relationship.
  1068. case let r as UploadRequest: self.performUploadRequest(r)
  1069. case let r as DataRequest: self.performDataRequest(r)
  1070. case let r as DownloadRequest: self.performDownloadRequest(r)
  1071. case let r as DataStreamRequest: self.performDataStreamRequest(r)
  1072. default:
  1073. #if canImport(Darwin) && !canImport(FoundationNetworking)
  1074. if #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *),
  1075. let request = request as? WebSocketRequest {
  1076. self.performWebSocketRequest(request)
  1077. } else {
  1078. fatalError("Attempted to perform unsupported Request subclass: \(type(of: request))")
  1079. }
  1080. #else
  1081. fatalError("Attempted to perform unsupported Request subclass: \(type(of: request))")
  1082. #endif
  1083. }
  1084. }
  1085. }
  1086. }
  1087. func performDataRequest(_ request: DataRequest) {
  1088. dispatchPrecondition(condition: .onQueue(requestQueue))
  1089. performSetupOperations(for: request, convertible: request.convertible)
  1090. }
  1091. func performDataStreamRequest(_ request: DataStreamRequest) {
  1092. dispatchPrecondition(condition: .onQueue(requestQueue))
  1093. performSetupOperations(for: request, convertible: request.convertible)
  1094. }
  1095. #if canImport(Darwin) && !canImport(FoundationNetworking)
  1096. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  1097. func performWebSocketRequest(_ request: WebSocketRequest) {
  1098. dispatchPrecondition(condition: .onQueue(requestQueue))
  1099. performSetupOperations(for: request, convertible: request.convertible)
  1100. }
  1101. #endif
  1102. func performUploadRequest(_ request: UploadRequest) {
  1103. dispatchPrecondition(condition: .onQueue(requestQueue))
  1104. performSetupOperations(for: request, convertible: request.convertible) {
  1105. do {
  1106. let uploadable = try request.upload.createUploadable()
  1107. self.rootQueue.async { request.didCreateUploadable(uploadable) }
  1108. return true
  1109. } catch {
  1110. self.rootQueue.async { request.didFailToCreateUploadable(with: error.asAFError(or: .createUploadableFailed(error: error))) }
  1111. return false
  1112. }
  1113. }
  1114. }
  1115. func performDownloadRequest(_ request: DownloadRequest) {
  1116. dispatchPrecondition(condition: .onQueue(requestQueue))
  1117. switch request.downloadable {
  1118. case let .request(convertible):
  1119. performSetupOperations(for: request, convertible: convertible)
  1120. case let .resumeData(resumeData):
  1121. rootQueue.async { self.didReceiveResumeData(resumeData, for: request) }
  1122. }
  1123. }
  1124. func performSetupOperations(for request: Request,
  1125. convertible: any URLRequestConvertible,
  1126. shouldCreateTask: @escaping @Sendable () -> Bool = { true }) {
  1127. dispatchPrecondition(condition: .onQueue(requestQueue))
  1128. let initialRequest: URLRequest
  1129. do {
  1130. initialRequest = try convertible.asURLRequest()
  1131. try initialRequest.validate()
  1132. } catch {
  1133. rootQueue.async { request.didFailToCreateURLRequest(with: error.asAFError(or: .createURLRequestFailed(error: error))) }
  1134. return
  1135. }
  1136. rootQueue.async { request.didCreateInitialURLRequest(initialRequest) }
  1137. guard !request.isCancelled else { return }
  1138. guard let adapter = adapter(for: request) else {
  1139. guard shouldCreateTask() else { return }
  1140. rootQueue.async { self.didCreateURLRequest(initialRequest, for: request) }
  1141. return
  1142. }
  1143. let adapterState = RequestAdapterState(requestID: request.id, session: self)
  1144. adapter.adapt(initialRequest, using: adapterState) { result in
  1145. do {
  1146. let adaptedRequest = try result.get()
  1147. try adaptedRequest.validate()
  1148. self.rootQueue.async { request.didAdaptInitialRequest(initialRequest, to: adaptedRequest) }
  1149. guard shouldCreateTask() else { return }
  1150. self.rootQueue.async { self.didCreateURLRequest(adaptedRequest, for: request) }
  1151. } catch {
  1152. self.rootQueue.async { request.didFailToAdaptURLRequest(initialRequest, withError: .requestAdaptationFailed(error: error)) }
  1153. }
  1154. }
  1155. }
  1156. // MARK: - Task Handling
  1157. func didCreateURLRequest(_ urlRequest: URLRequest, for request: Request) {
  1158. dispatchPrecondition(condition: .onQueue(rootQueue))
  1159. request.didCreateURLRequest(urlRequest)
  1160. guard !request.isCancelled else { return }
  1161. let task = request.task(for: urlRequest, using: session)
  1162. requestTaskMap[request] = task
  1163. request.didCreateTask(task)
  1164. updateStatesForTask(task, request: request)
  1165. }
  1166. func didReceiveResumeData(_ data: Data, for request: DownloadRequest) {
  1167. dispatchPrecondition(condition: .onQueue(rootQueue))
  1168. guard !request.isCancelled else { return }
  1169. let task = request.task(forResumeData: data, using: session)
  1170. requestTaskMap[request] = task
  1171. request.didCreateTask(task)
  1172. updateStatesForTask(task, request: request)
  1173. }
  1174. func updateStatesForTask(_ task: URLSessionTask, request: Request) {
  1175. dispatchPrecondition(condition: .onQueue(rootQueue))
  1176. request.withState { state in
  1177. switch state {
  1178. case .initialized, .finished:
  1179. // Do nothing.
  1180. break
  1181. case .resumed:
  1182. task.resume()
  1183. rootQueue.async { request.didResumeTask(task) }
  1184. case .suspended:
  1185. task.suspend()
  1186. rootQueue.async { request.didSuspendTask(task) }
  1187. case .cancelled:
  1188. // Resume to ensure metrics are gathered.
  1189. task.resume()
  1190. task.cancel()
  1191. rootQueue.async { request.didCancelTask(task) }
  1192. }
  1193. }
  1194. }
  1195. // MARK: - Adapters and Retriers
  1196. func adapter(for request: Request) -> (any RequestAdapter)? {
  1197. if let requestInterceptor = request.interceptor, let sessionInterceptor = interceptor {
  1198. Interceptor(adapters: [sessionInterceptor, requestInterceptor])
  1199. } else {
  1200. request.interceptor ?? interceptor
  1201. }
  1202. }
  1203. func retrier(for request: Request) -> (any RequestRetrier)? {
  1204. if let requestInterceptor = request.interceptor, let sessionInterceptor = interceptor {
  1205. Interceptor(retriers: [sessionInterceptor, requestInterceptor])
  1206. } else {
  1207. request.interceptor ?? interceptor
  1208. }
  1209. }
  1210. // MARK: - Invalidation
  1211. func finishRequestsForDeinit() {
  1212. for request in requestTaskMap.requests {
  1213. rootQueue.async {
  1214. request.finish(error: AFError.sessionDeinitialized)
  1215. }
  1216. }
  1217. }
  1218. }
  1219. // MARK: - RequestDelegate
  1220. extension Session: RequestDelegate {
  1221. public var sessionConfiguration: URLSessionConfiguration {
  1222. session.configuration
  1223. }
  1224. public var startImmediately: Bool { startRequestsImmediately }
  1225. public func readyToPerform(request: Request) {
  1226. rootQueue.async { [self] in
  1227. // TODO: Find a better condition to check.
  1228. // Called either when the Request is manually or automatically resumed.
  1229. if requestTaskMap[request] == nil {
  1230. perform(request)
  1231. }
  1232. }
  1233. }
  1234. public func cleanup(after request: Request) {
  1235. activeRequests.remove(request)
  1236. }
  1237. public func retryResult(for request: Request, dueTo error: AFError, completion: @escaping @Sendable (RetryResult) -> Void) {
  1238. guard let retrier = retrier(for: request) else {
  1239. rootQueue.async { completion(.doNotRetry) }
  1240. return
  1241. }
  1242. retrier.retry(request, for: self, dueTo: error) { retryResult in
  1243. self.rootQueue.async {
  1244. guard let retryResultError = retryResult.error else { completion(retryResult); return }
  1245. let retryError = AFError.requestRetryFailed(retryError: retryResultError, originalError: error)
  1246. completion(.doNotRetryWithError(retryError))
  1247. }
  1248. }
  1249. }
  1250. public func retryRequest(_ request: Request, withDelay timeDelay: TimeInterval?) {
  1251. rootQueue.async {
  1252. let retry: @Sendable () -> Void = {
  1253. guard !request.isCancelled else { return }
  1254. request.prepareForRetry()
  1255. self.perform(request)
  1256. }
  1257. if let retryDelay = timeDelay {
  1258. self.rootQueue.after(retryDelay) { retry() }
  1259. } else {
  1260. retry()
  1261. }
  1262. }
  1263. }
  1264. }
  1265. // MARK: - SessionStateProvider
  1266. extension Session: SessionStateProvider {
  1267. func request(for task: URLSessionTask) -> Request? {
  1268. dispatchPrecondition(condition: .onQueue(rootQueue))
  1269. return requestTaskMap[task]
  1270. }
  1271. func didGatherMetricsForTask(_ task: URLSessionTask) {
  1272. dispatchPrecondition(condition: .onQueue(rootQueue))
  1273. let didDisassociate = requestTaskMap.disassociateIfNecessaryAfterGatheringMetricsForTask(task)
  1274. if didDisassociate {
  1275. waitingCompletions[task]?()
  1276. waitingCompletions[task] = nil
  1277. }
  1278. }
  1279. func didCompleteTask(_ task: URLSessionTask, completion: @escaping () -> Void) {
  1280. dispatchPrecondition(condition: .onQueue(rootQueue))
  1281. let didDisassociate = requestTaskMap.disassociateIfNecessaryAfterCompletingTask(task)
  1282. if didDisassociate {
  1283. completion()
  1284. } else {
  1285. waitingCompletions[task] = completion
  1286. }
  1287. }
  1288. func credential(for task: URLSessionTask, in protectionSpace: URLProtectionSpace) -> URLCredential? {
  1289. dispatchPrecondition(condition: .onQueue(rootQueue))
  1290. return requestTaskMap[task]?.credential ??
  1291. session.configuration.urlCredentialStorage?.defaultCredential(for: protectionSpace)
  1292. }
  1293. func cancelRequestsForSessionInvalidation(with error: (any Error)?) {
  1294. dispatchPrecondition(condition: .onQueue(rootQueue))
  1295. requestTaskMap.requests.forEach { $0.finish(error: AFError.sessionInvalidated(error: error)) }
  1296. }
  1297. }