Session.swift 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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 {
  29. /// Shared singleton instance used by all `AF.request` APIs. Cannot be modified.
  30. public static let `default` = Session()
  31. /// Underlying `URLSession` used to create `URLSessionTasks` for this instance, and for which this instance's
  32. /// `delegate` handles `URLSessionDelegate` callbacks.
  33. public let session: URLSession
  34. /// Instance's `SessionDelegate`, which handles the `URLSessionDelegate` methods and `Request` interaction.
  35. public let delegate: SessionDelegate
  36. /// Root `DispatchQueue` for all internal callbacks and state update. **MUST** be a serial queue.
  37. public let rootQueue: DispatchQueue
  38. /// Value determining whether this instance automatically calls `resume()` on all created `Request`s.
  39. public let startRequestsImmediately: Bool
  40. /// `DispatchQueue` on which `URLRequest`s are created asynchronously. By default this queue uses `rootQueue` as its
  41. /// `target`, but a separate queue can be used if request creation is determined to be a bottleneck. Always profile
  42. /// and test before introducing an additional queue.
  43. public let requestQueue: DispatchQueue
  44. /// `DispatchQueue` passed to all `Request`s on which they perform their response serialization. By default this
  45. /// queue uses `rootQueue` as its `target` but a separate queue can be used if response serialization is determined
  46. /// to be a bottleneck. Always profile and test before introducing an additional queue.
  47. public let serializationQueue: DispatchQueue
  48. /// `RequestInterceptor` used for all `Request` created by the instance. `RequestInterceptor`s can also be set on a
  49. /// per-`Request` basis, in which case the `Request`'s interceptor takes precedence over this value.
  50. public let interceptor: RequestInterceptor?
  51. /// `ServerTrustManager` instance used to evaluate all trust challenges and provide certificate and key pinning.
  52. public let serverTrustManager: ServerTrustManager?
  53. /// `RedirectHandler` instance used to provide customization for request redirection.
  54. public let redirectHandler: RedirectHandler?
  55. /// `CachedResponseHandler` instance used to provide customization of cached response handling.
  56. public let cachedResponseHandler: CachedResponseHandler?
  57. /// `CompositeEventMonitor` used to compose Alamofire's `defaultEventMonitors` and any passed `EventMonitor`s.
  58. public let eventMonitor: CompositeEventMonitor
  59. /// `EventMonitor`s included in all instances. `[AlamofireNotifications()]` by default.
  60. public let defaultEventMonitors: [EventMonitor] = [AlamofireNotifications()]
  61. /// Internal map between `Request`s and any `URLSessionTasks` that may be in flight for them.
  62. var requestTaskMap = RequestTaskMap()
  63. /// Set of currently active `Request`s.
  64. var activeRequests: Set<Request> = []
  65. /// Creates a `Session` from a `URLSession` and other parameters.
  66. ///
  67. /// - Note: When passing a `URLSession`, you must create the `URLSession` with a specific `delegateQueue` value and
  68. /// pass the `delegateQueue`'s `underlyingQueue` as the `rootQueue` parameter of this initializer.
  69. ///
  70. /// - Parameters:
  71. /// - session: Underlying `URLSession` for this instance.
  72. /// - delegate: `SessionDelegate` that handles `session`'s delegate callbacks as well as `Request`
  73. /// interaction.
  74. /// - rootQueue: Root `DispatchQueue` for all internal callbacks and state updates. **MUST** be a
  75. /// serial queue.
  76. /// - startRequestsImmediately: Determines whether this instance will automatically start all `Request`s. `true`
  77. /// by default. If set to `false`, all `Request`s created must have `.resume()` called.
  78. /// on them for them to start.
  79. /// - requestQueue: `DispatchQueue` on which to perform `URLRequest` creation. By default this queue
  80. /// will use the `rootQueue` as its `target`. A separate queue can be used if it's
  81. /// determined request creation is a bottleneck, but that should only be done after
  82. /// careful testing and profiling. `nil` by default.
  83. /// - serializationQueue: `DispatchQueue` on which to perform all response serialization. By default this
  84. /// queue will use the `rootQueue` as its `target`. A separate queue can be used if
  85. /// it's determined response serialization is a bottleneck, but that should only be
  86. /// done after careful testing and profiling. `nil` by default.
  87. /// - interceptor: `RequestInterceptor` to be used for all `Request`s created by this instance. `nil`
  88. /// by default.
  89. /// - serverTrustManager: `ServerTrustManager` to be used for all trust evaluations by this instance. `nil`
  90. /// by default.
  91. /// - redirectHandler: `RedirectHandler` to be used by all `Request`s created by this instance. `nil` by
  92. /// default.
  93. /// - cachedResponseHandler: `CachedResponseHandler` to be used by all `Request`s created by this instance.
  94. /// `nil` by default.
  95. /// - eventMonitors: Additional `EventMonitor`s used by the instance. Alamofire always adds a
  96. /// `AlamofireNotifications` `EventMonitor` to the array passed here. `[]` by default.
  97. public init(session: URLSession,
  98. delegate: SessionDelegate,
  99. rootQueue: DispatchQueue,
  100. startRequestsImmediately: Bool = true,
  101. requestQueue: DispatchQueue? = nil,
  102. serializationQueue: DispatchQueue? = nil,
  103. interceptor: RequestInterceptor? = nil,
  104. serverTrustManager: ServerTrustManager? = nil,
  105. redirectHandler: RedirectHandler? = nil,
  106. cachedResponseHandler: CachedResponseHandler? = nil,
  107. eventMonitors: [EventMonitor] = []) {
  108. precondition(session.delegateQueue.underlyingQueue === rootQueue,
  109. "Session(session:) intializer must be passed the DispatchQueue used as the delegateQueue's underlyingQueue as rootQueue.")
  110. self.session = session
  111. self.delegate = delegate
  112. self.rootQueue = rootQueue
  113. self.startRequestsImmediately = startRequestsImmediately
  114. self.requestQueue = requestQueue ?? DispatchQueue(label: "\(rootQueue.label).requestQueue", target: rootQueue)
  115. self.serializationQueue = serializationQueue ?? DispatchQueue(label: "\(rootQueue.label).serializationQueue", target: rootQueue)
  116. self.interceptor = interceptor
  117. self.serverTrustManager = serverTrustManager
  118. self.redirectHandler = redirectHandler
  119. self.cachedResponseHandler = cachedResponseHandler
  120. eventMonitor = CompositeEventMonitor(monitors: defaultEventMonitors + eventMonitors)
  121. delegate.eventMonitor = eventMonitor
  122. delegate.stateProvider = self
  123. }
  124. /// Creates a `Session` from a `URLSessionConfiguration`.
  125. ///
  126. /// - Note: This initializer lets Alamofire handle the creation of the underlying `URLSession` and its
  127. /// `delegateQueue`, and is the recommended initializer for most uses.
  128. ///
  129. /// - Parameters:
  130. /// - configuration: `URLSessionConfiguration` to be used to create the underlying `URLSession`. Changes
  131. /// to this value after being passed to this initializer will have no effect.
  132. /// `URLSessionConfiguration.af.default` by default.
  133. /// - delegate: `SessionDelegate` that handles `session`'s delegate callbacks as well as `Request`
  134. /// interaction. `SessionDelegate()` by default.
  135. /// - rootQueue: Root `DispatchQueue` for all internal callbacks and state updates. **MUST** be a
  136. /// serial queue. `DispatchQueue(label: "org.alamofire.session.rootQueue")` by default.
  137. /// - startRequestsImmediately: Determines whether this instance will automatically start all `Request`s. `true`
  138. /// by default. If set to `false`, all `Request`s created must have `.resume()` called.
  139. /// on them for them to start.
  140. /// - requestQueue: `DispatchQueue` on which to perform `URLRequest` creation. By default this queue
  141. /// will use the `rootQueue` as its `target`. A separate queue can be used if it's
  142. /// determined request creation is a bottleneck, but that should only be done after
  143. /// careful testing and profiling. `nil` by default.
  144. /// - serializationQueue: `DispatchQueue` on which to perform all response serialization. By default this
  145. /// queue will use the `rootQueue` as its `target`. A separate queue can be used if
  146. /// it's determined response serialization is a bottleneck, but that should only be
  147. /// done after careful testing and profiling. `nil` by default.
  148. /// - interceptor: `RequestInterceptor` to be used for all `Request`s created by this instance. `nil`
  149. /// by default.
  150. /// - serverTrustManager: `ServerTrustManager` to be used for all trust evaluations by this instance. `nil`
  151. /// by default.
  152. /// - redirectHandler: `RedirectHandler` to be used by all `Request`s created by this instance. `nil` by
  153. /// default.
  154. /// - cachedResponseHandler: `CachedResponseHandler` to be used by all `Request`s created by this instance.
  155. /// `nil` by default.
  156. /// - eventMonitors: Additional `EventMonitor`s used by the instance. Alamofire always adds a
  157. /// `AlamofireNotifications` `EventMonitor` to the array passed here. `[]` by default.
  158. public convenience init(configuration: URLSessionConfiguration = URLSessionConfiguration.af.default,
  159. delegate: SessionDelegate = SessionDelegate(),
  160. rootQueue: DispatchQueue = DispatchQueue(label: "org.alamofire.session.rootQueue"),
  161. startRequestsImmediately: Bool = true,
  162. requestQueue: DispatchQueue? = nil,
  163. serializationQueue: DispatchQueue? = nil,
  164. interceptor: RequestInterceptor? = nil,
  165. serverTrustManager: ServerTrustManager? = nil,
  166. redirectHandler: RedirectHandler? = nil,
  167. cachedResponseHandler: CachedResponseHandler? = nil,
  168. eventMonitors: [EventMonitor] = []) {
  169. let delegateQueue = OperationQueue(maxConcurrentOperationCount: 1, underlyingQueue: rootQueue, name: "org.alamofire.session.sessionDelegateQueue")
  170. let session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: delegateQueue)
  171. self.init(session: session,
  172. delegate: delegate,
  173. rootQueue: rootQueue,
  174. startRequestsImmediately: startRequestsImmediately,
  175. requestQueue: requestQueue,
  176. serializationQueue: serializationQueue,
  177. interceptor: interceptor,
  178. serverTrustManager: serverTrustManager,
  179. redirectHandler: redirectHandler,
  180. cachedResponseHandler: cachedResponseHandler,
  181. eventMonitors: eventMonitors)
  182. }
  183. deinit {
  184. finishRequestsForDeinit()
  185. session.invalidateAndCancel()
  186. }
  187. // MARK: - Cancellation
  188. /// Cancel all active `Request`s, optionally calling a completion handler when complete.
  189. ///
  190. /// - Note: This is an asynchronous operation and does not block the creation of future `Request`s. Cancelled
  191. /// `Request`s may not cancel immediately due internal work, and may not cancel at all if they are close to
  192. /// completion when cancelled.
  193. ///
  194. /// - Parameters:
  195. /// - queue: `DispatchQueue` on which the completion handler is run. `.main` by default.
  196. /// - completion: Closure to be called when all `Request`s have been cancelled.
  197. public func cancelAllRequests(completingOnQueue queue: DispatchQueue = .main, completion: (() -> Void)? = nil) {
  198. rootQueue.async {
  199. self.activeRequests.forEach { $0.cancel() }
  200. queue.async { completion?() }
  201. }
  202. }
  203. // MARK: - DataRequest
  204. struct RequestConvertible: URLRequestConvertible {
  205. let url: URLConvertible
  206. let method: HTTPMethod
  207. let parameters: Parameters?
  208. let encoding: ParameterEncoding
  209. let headers: HTTPHeaders?
  210. func asURLRequest() throws -> URLRequest {
  211. let request = try URLRequest(url: url, method: method, headers: headers)
  212. return try encoding.encode(request, with: parameters)
  213. }
  214. }
  215. /// Creates a `DataRequest` from a `URLRequest` created using the passed components and a `RequestInterceptor`.
  216. ///
  217. /// - Parameters:
  218. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  219. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  220. /// - parameters: `Parameters` (a.k.a. `[String: Any]`) value to be encoded into the `URLRequest`. `nil` by default.
  221. /// - encoding: `ParameterEncoding` to be used to encode the `parameters` value into the `URLRequest`.
  222. /// `URLEncoding.default` by default.
  223. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  224. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  225. ///
  226. /// - Returns: The created `DataRequest`.
  227. open func request(_ convertible: URLConvertible,
  228. method: HTTPMethod = .get,
  229. parameters: Parameters? = nil,
  230. encoding: ParameterEncoding = URLEncoding.default,
  231. headers: HTTPHeaders? = nil,
  232. interceptor: RequestInterceptor? = nil) -> DataRequest {
  233. let convertible = RequestConvertible(url: convertible,
  234. method: method,
  235. parameters: parameters,
  236. encoding: encoding,
  237. headers: headers)
  238. return request(convertible, interceptor: interceptor)
  239. }
  240. struct RequestEncodableConvertible<Parameters: Encodable>: URLRequestConvertible {
  241. let url: URLConvertible
  242. let method: HTTPMethod
  243. let parameters: Parameters?
  244. let encoder: ParameterEncoder
  245. let headers: HTTPHeaders?
  246. func asURLRequest() throws -> URLRequest {
  247. let request = try URLRequest(url: url, method: method, headers: headers)
  248. return try parameters.map { try encoder.encode($0, into: request) } ?? request
  249. }
  250. }
  251. /// Creates a `DataRequest` from a `URLRequest` created using the passed components, `Encodable` parameters, and a
  252. /// `RequestInterceptor`.
  253. ///
  254. /// - Parameters:
  255. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  256. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  257. /// - parameters: `Encodable` value to be encoded into the `URLRequest`. `nil` by default.
  258. /// - encoder: `ParameterEncoder` to be used to encode the `parameters` value into the `URLRequest`.
  259. /// `URLEncodedFormParameterEncoder.default` by default.
  260. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  261. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  262. ///
  263. /// - Returns: The created `DataRequest`.
  264. open func request<Parameters: Encodable>(_ convertible: URLConvertible,
  265. method: HTTPMethod = .get,
  266. parameters: Parameters? = nil,
  267. encoder: ParameterEncoder = URLEncodedFormParameterEncoder.default,
  268. headers: HTTPHeaders? = nil,
  269. interceptor: RequestInterceptor? = nil) -> DataRequest {
  270. let convertible = RequestEncodableConvertible(url: convertible,
  271. method: method,
  272. parameters: parameters,
  273. encoder: encoder,
  274. headers: headers)
  275. return request(convertible, interceptor: interceptor)
  276. }
  277. /// Creates a `DataRequest` from a `URLRequestConvertible` value and a `RequestInterceptor`.
  278. ///
  279. /// - Parameters:
  280. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  281. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  282. ///
  283. /// - Returns: The created `DataRequest`.
  284. open func request(_ convertible: URLRequestConvertible, interceptor: RequestInterceptor? = nil) -> DataRequest {
  285. let request = DataRequest(convertible: convertible,
  286. underlyingQueue: rootQueue,
  287. serializationQueue: serializationQueue,
  288. eventMonitor: eventMonitor,
  289. interceptor: interceptor,
  290. delegate: self)
  291. perform(request)
  292. return request
  293. }
  294. // MARK: - DownloadRequest
  295. /// Creates a `DownloadRequest` using a `URLRequest` created using the passed components, `RequestInterceptor`, and
  296. /// `Destination`.
  297. ///
  298. /// - Parameters:
  299. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  300. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  301. /// - parameters: `Parameters` (a.k.a. `[String: Any]`) value to be encoded into the `URLRequest`. `nil` by default.
  302. /// - encoding: `ParameterEncoding` to be used to encode the `parameters` value into the `URLRequest`. Defaults
  303. /// to `URLEncoding.default`.
  304. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  305. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  306. /// - destination: `DownloadRequest.Destination` closure used to determine how and where the downloaded file
  307. /// should be moved. `nil` by default.
  308. ///
  309. /// - Returns: The created `DownloadRequest`.
  310. open func download(_ convertible: URLConvertible,
  311. method: HTTPMethod = .get,
  312. parameters: Parameters? = nil,
  313. encoding: ParameterEncoding = URLEncoding.default,
  314. headers: HTTPHeaders? = nil,
  315. interceptor: RequestInterceptor? = nil,
  316. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  317. let convertible = RequestConvertible(url: convertible,
  318. method: method,
  319. parameters: parameters,
  320. encoding: encoding,
  321. headers: headers)
  322. return download(convertible, interceptor: interceptor, to: destination)
  323. }
  324. /// Creates a `DownloadRequest` from a `URLRequest` created using the passed components, `Encodable` parameters, and
  325. /// a `RequestInterceptor`.
  326. ///
  327. /// - Parameters:
  328. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  329. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  330. /// - parameters: Value conforming to `Encodable` to be encoded into the `URLRequest`. `nil` by default.
  331. /// - encoder: `ParameterEncoder` to be used to encode the `parameters` value into the `URLRequest`. Defaults
  332. /// to `URLEncodedFormParameterEncoder.default`.
  333. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  334. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  335. /// - destination: `DownloadRequest.Destination` closure used to determine how and where the downloaded file
  336. /// should be moved. `nil` by default.
  337. ///
  338. /// - Returns: The created `DownloadRequest`.
  339. open func download<Parameters: Encodable>(_ convertible: URLConvertible,
  340. method: HTTPMethod = .get,
  341. parameters: Parameters? = nil,
  342. encoder: ParameterEncoder = URLEncodedFormParameterEncoder.default,
  343. headers: HTTPHeaders? = nil,
  344. interceptor: RequestInterceptor? = nil,
  345. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  346. let convertible = RequestEncodableConvertible(url: convertible,
  347. method: method,
  348. parameters: parameters,
  349. encoder: encoder,
  350. headers: headers)
  351. return download(convertible, interceptor: interceptor, to: destination)
  352. }
  353. /// Creates a `DownloadRequest` from a `URLRequestConvertible` value, a `RequestInterceptor`, and a `Destination`.
  354. ///
  355. /// - Parameters:
  356. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  357. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  358. /// - destination: `DownloadRequest.Destination` closure used to determine how and where the downloaded file
  359. /// should be moved. `nil` by default.
  360. ///
  361. /// - Returns: The created `DownloadRequest`.
  362. open func download(_ convertible: URLRequestConvertible,
  363. interceptor: RequestInterceptor? = nil,
  364. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  365. let request = DownloadRequest(downloadable: .request(convertible),
  366. underlyingQueue: rootQueue,
  367. serializationQueue: serializationQueue,
  368. eventMonitor: eventMonitor,
  369. interceptor: interceptor,
  370. delegate: self,
  371. destination: destination ?? DownloadRequest.defaultDestination)
  372. perform(request)
  373. return request
  374. }
  375. /// Creates a `DownloadRequest` from the `resumeData` produced from a previously cancelled `DownloadRequest`, as
  376. /// well as a `RequestInterceptor`, and a `Destination`.
  377. ///
  378. /// - Note: If `destination` is not specified, the download will be moved to a temporary location determined by
  379. /// Alamofire. The file will not be deleted until the system purges the temporary files.
  380. ///
  381. /// - 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),
  382. /// `resumeData` is broken on background URL session configurations. There's an underlying bug in the `resumeData`
  383. /// generation logic where the data is written incorrectly and will always fail to resume the download. For more
  384. /// information about the bug and possible workarounds, please refer to the [this Stack Overflow post](http://stackoverflow.com/a/39347461/1342462).
  385. ///
  386. /// - Parameters:
  387. /// - data: The resume data from a previously cancelled `DownloadRequest` or `URLSessionDownloadTask`.
  388. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  389. /// - destination: `DownloadRequest.Destination` closure used to determine how and where the downloaded file
  390. /// should be moved. `nil` by default.
  391. ///
  392. /// - Returns: The created `DownloadRequest`.
  393. open func download(resumingWith data: Data,
  394. interceptor: RequestInterceptor? = nil,
  395. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  396. let request = DownloadRequest(downloadable: .resumeData(data),
  397. underlyingQueue: rootQueue,
  398. serializationQueue: serializationQueue,
  399. eventMonitor: eventMonitor,
  400. interceptor: interceptor,
  401. delegate: self,
  402. destination: destination ?? DownloadRequest.defaultDestination)
  403. perform(request)
  404. return request
  405. }
  406. // MARK: - UploadRequest
  407. struct ParameterlessRequestConvertible: URLRequestConvertible {
  408. let url: URLConvertible
  409. let method: HTTPMethod
  410. let headers: HTTPHeaders?
  411. func asURLRequest() throws -> URLRequest {
  412. return try URLRequest(url: url, method: method, headers: headers)
  413. }
  414. }
  415. struct Upload: UploadConvertible {
  416. let request: URLRequestConvertible
  417. let uploadable: UploadableConvertible
  418. func createUploadable() throws -> UploadRequest.Uploadable {
  419. return try uploadable.createUploadable()
  420. }
  421. func asURLRequest() throws -> URLRequest {
  422. return try request.asURLRequest()
  423. }
  424. }
  425. // MARK: Data
  426. /// Creates an `UploadRequest` for the given `Data`, `URLRequest` components, and `RequestInterceptor`.
  427. ///
  428. /// - Parameters:
  429. /// - data: The `Data` to upload.
  430. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  431. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  432. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  433. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  434. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  435. /// default.
  436. ///
  437. /// - Returns: The created `UploadRequest`.
  438. open func upload(_ data: Data,
  439. to convertible: URLConvertible,
  440. method: HTTPMethod = .post,
  441. headers: HTTPHeaders? = nil,
  442. interceptor: RequestInterceptor? = nil,
  443. fileManager: FileManager = .default) -> UploadRequest {
  444. let convertible = ParameterlessRequestConvertible(url: convertible, method: method, headers: headers)
  445. return upload(data, with: convertible, interceptor: interceptor, fileManager: fileManager)
  446. }
  447. /// Creates an `UploadRequest` for the given `Data` using the `URLRequestConvertible` value and `RequestInterceptor`.
  448. ///
  449. /// - Parameters:
  450. /// - data: The `Data` to upload.
  451. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  452. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  453. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  454. /// default.
  455. ///
  456. /// - Returns: The created `UploadRequest`.
  457. open func upload(_ data: Data,
  458. with convertible: URLRequestConvertible,
  459. interceptor: RequestInterceptor? = nil,
  460. fileManager: FileManager = .default) -> UploadRequest {
  461. return upload(.data(data), with: convertible, interceptor: interceptor, fileManager: fileManager)
  462. }
  463. // MARK: File
  464. /// Creates an `UploadRequest` for the file at the given file `URL`, using a `URLRequest` from the provided
  465. /// components and `RequestInterceptor`.
  466. ///
  467. /// - Parameters:
  468. /// - fileURL: The `URL` of the file to upload.
  469. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  470. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  471. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  472. /// - interceptor: `RequestInterceptor` value to be used by the returned `UploadRequest`. `nil` by default.
  473. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  474. /// default.
  475. ///
  476. /// - Returns: The created `UploadRequest`.
  477. open func upload(_ fileURL: URL,
  478. to convertible: URLConvertible,
  479. method: HTTPMethod = .post,
  480. headers: HTTPHeaders? = nil,
  481. interceptor: RequestInterceptor? = nil,
  482. fileManager: FileManager = .default) -> UploadRequest {
  483. let convertible = ParameterlessRequestConvertible(url: convertible, method: method, headers: headers)
  484. return upload(fileURL, with: convertible, interceptor: interceptor, fileManager: fileManager)
  485. }
  486. /// Creates an `UploadRequest` for the file at the given file `URL` using the `URLRequestConvertible` value and
  487. /// `RequestInterceptor`.
  488. ///
  489. /// - Parameters:
  490. /// - fileURL: The `URL` of the file to upload.
  491. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  492. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  493. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  494. /// default.
  495. ///
  496. /// - Returns: The created `UploadRequest`.
  497. open func upload(_ fileURL: URL,
  498. with convertible: URLRequestConvertible,
  499. interceptor: RequestInterceptor? = nil,
  500. fileManager: FileManager = .default) -> UploadRequest {
  501. return upload(.file(fileURL, shouldRemove: false), with: convertible, interceptor: interceptor, fileManager: fileManager)
  502. }
  503. // MARK: InputStream
  504. /// Creates an `UploadRequest` from the `InputStream` provided using a `URLRequest` from the provided components and
  505. /// `RequestInterceptor`.
  506. ///
  507. /// - Parameters:
  508. /// - stream: The `InputStream` that provides the data to upload.
  509. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  510. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  511. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  512. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  513. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  514. /// default.
  515. ///
  516. /// - Returns: The created `UploadRequest`.
  517. open func upload(_ stream: InputStream,
  518. to convertible: URLConvertible,
  519. method: HTTPMethod = .post,
  520. headers: HTTPHeaders? = nil,
  521. interceptor: RequestInterceptor? = nil,
  522. fileManager: FileManager = .default) -> UploadRequest {
  523. let convertible = ParameterlessRequestConvertible(url: convertible, method: method, headers: headers)
  524. return upload(stream, with: convertible, interceptor: interceptor, fileManager: fileManager)
  525. }
  526. /// Creates an `UploadRequest` from the provided `InputStream` using the `URLRequestConvertible` value and
  527. /// `RequestInterceptor`.
  528. ///
  529. /// - Parameters:
  530. /// - stream: The `InputStream` that provides the data to upload.
  531. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  532. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  533. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  534. /// default.
  535. ///
  536. /// - Returns: The created `UploadRequest`.
  537. open func upload(_ stream: InputStream,
  538. with convertible: URLRequestConvertible,
  539. interceptor: RequestInterceptor? = nil,
  540. fileManager: FileManager = .default) -> UploadRequest {
  541. return upload(.stream(stream), with: convertible, interceptor: interceptor, fileManager: fileManager)
  542. }
  543. // MARK: MultipartFormData
  544. /// Creates an `UploadRequest` for the multipart form data built using a closure and sent using the provided
  545. /// `URLRequest` components and `RequestInterceptor`.
  546. ///
  547. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cumulative
  548. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  549. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  550. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  551. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  552. /// used for larger payloads such as video content.
  553. ///
  554. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  555. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  556. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  557. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  558. /// technique was used.
  559. ///
  560. /// - Parameters:
  561. /// - multipartFormData: `MultipartFormData` building closure.
  562. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  563. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  564. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  565. /// default.
  566. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  567. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  568. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  569. /// - fileManager: `FileManager` to be used if the form data exceeds the memory threshold and is
  570. /// written to disk before being uploaded. `.default` instance by default.
  571. ///
  572. /// - Returns: The created `UploadRequest`.
  573. open func upload(multipartFormData: @escaping (MultipartFormData) -> Void,
  574. to url: URLConvertible,
  575. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  576. method: HTTPMethod = .post,
  577. headers: HTTPHeaders? = nil,
  578. interceptor: RequestInterceptor? = nil,
  579. fileManager: FileManager = .default) -> UploadRequest {
  580. let convertible = ParameterlessRequestConvertible(url: url, method: method, headers: headers)
  581. let formData = MultipartFormData(fileManager: fileManager)
  582. multipartFormData(formData)
  583. return upload(multipartFormData: formData,
  584. with: convertible,
  585. usingThreshold: encodingMemoryThreshold,
  586. interceptor: interceptor,
  587. fileManager: fileManager)
  588. }
  589. /// Creates an `UploadRequest` using a `MultipartFormData` building closure, the provided `URLRequestConvertible`
  590. /// value, and a `RequestInterceptor`.
  591. ///
  592. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cumulative
  593. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  594. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  595. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  596. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  597. /// used for larger payloads such as video content.
  598. ///
  599. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  600. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  601. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  602. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  603. /// technique was used.
  604. ///
  605. /// - Parameters:
  606. /// - multipartFormData: `MultipartFormData` building closure.
  607. /// - request: `URLRequestConvertible` value to be used to create the `URLRequest`.
  608. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  609. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  610. /// default.
  611. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  612. /// - fileManager: `FileManager` to be used if the form data exceeds the memory threshold and is
  613. /// written to disk before being uploaded. `.default` instance by default.
  614. ///
  615. /// - Returns: The created `UploadRequest`.
  616. open func upload(multipartFormData: @escaping (MultipartFormData) -> Void,
  617. with request: URLRequestConvertible,
  618. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  619. interceptor: RequestInterceptor? = nil,
  620. fileManager: FileManager = .default) -> UploadRequest {
  621. let formData = MultipartFormData(fileManager: fileManager)
  622. multipartFormData(formData)
  623. return upload(multipartFormData: formData,
  624. with: request,
  625. usingThreshold: encodingMemoryThreshold,
  626. interceptor: interceptor,
  627. fileManager: fileManager)
  628. }
  629. /// Creates an `UploadRequest` for the prebuilt `MultipartFormData` value using the provided `URLRequest` components
  630. /// and `RequestInterceptor`.
  631. ///
  632. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cumulative
  633. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  634. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  635. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  636. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  637. /// used for larger payloads such as video content.
  638. ///
  639. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  640. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  641. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  642. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  643. /// technique was used.
  644. ///
  645. /// - Parameters:
  646. /// - multipartFormData: `MultipartFormData` instance to upload.
  647. /// - url: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  648. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  649. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  650. /// default.
  651. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  652. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  653. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  654. /// - fileManager: `FileManager` to be used if the form data exceeds the memory threshold and is
  655. /// written to disk before being uploaded. `.default` instance by default.
  656. ///
  657. /// - Returns: The created `UploadRequest`.
  658. open func upload(multipartFormData: MultipartFormData,
  659. to url: URLConvertible,
  660. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  661. method: HTTPMethod = .post,
  662. headers: HTTPHeaders? = nil,
  663. interceptor: RequestInterceptor? = nil,
  664. fileManager: FileManager = .default) -> UploadRequest {
  665. let convertible = ParameterlessRequestConvertible(url: url, method: method, headers: headers)
  666. let multipartUpload = MultipartUpload(isInBackgroundSession: (session.configuration.identifier != nil),
  667. encodingMemoryThreshold: encodingMemoryThreshold,
  668. request: convertible,
  669. multipartFormData: multipartFormData)
  670. return upload(multipartUpload, interceptor: interceptor, fileManager: fileManager)
  671. }
  672. /// Creates an `UploadRequest` for the prebuilt `MultipartFormData` value using the providing `URLRequestConvertible`
  673. /// value and `RequestInterceptor`.
  674. ///
  675. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cumulative
  676. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  677. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  678. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  679. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  680. /// used for larger payloads such as video content.
  681. ///
  682. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  683. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  684. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  685. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  686. /// technique was used.
  687. ///
  688. /// - Parameters:
  689. /// - multipartFormData: `MultipartFormData` instance to upload.
  690. /// - request: `URLRequestConvertible` value to be used to create the `URLRequest`.
  691. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  692. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  693. /// default.
  694. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  695. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  696. /// default.
  697. ///
  698. /// - Returns: The created `UploadRequest`.
  699. open func upload(multipartFormData: MultipartFormData,
  700. with request: URLRequestConvertible,
  701. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  702. interceptor: RequestInterceptor? = nil,
  703. fileManager: FileManager = .default) -> UploadRequest {
  704. let multipartUpload = MultipartUpload(isInBackgroundSession: (session.configuration.identifier != nil),
  705. encodingMemoryThreshold: encodingMemoryThreshold,
  706. request: request,
  707. multipartFormData: multipartFormData)
  708. return upload(multipartUpload, interceptor: interceptor, fileManager: fileManager)
  709. }
  710. // MARK: - Internal API
  711. // MARK: Uploadable
  712. func upload(_ uploadable: UploadRequest.Uploadable,
  713. with convertible: URLRequestConvertible,
  714. interceptor: RequestInterceptor?,
  715. fileManager: FileManager) -> UploadRequest {
  716. let uploadable = Upload(request: convertible, uploadable: uploadable)
  717. return upload(uploadable, interceptor: interceptor, fileManager: fileManager)
  718. }
  719. func upload(_ upload: UploadConvertible, interceptor: RequestInterceptor?, fileManager: FileManager) -> UploadRequest {
  720. let request = UploadRequest(convertible: upload,
  721. underlyingQueue: rootQueue,
  722. serializationQueue: serializationQueue,
  723. eventMonitor: eventMonitor,
  724. interceptor: interceptor,
  725. fileManager: fileManager,
  726. delegate: self)
  727. perform(request)
  728. return request
  729. }
  730. // MARK: Perform
  731. /// Perform `Request`.
  732. ///
  733. /// - Note: Called during retry.
  734. ///
  735. /// - Parameter request: The `Request` to perform.
  736. func perform(_ request: Request) {
  737. switch request {
  738. case let r as DataRequest: perform(r)
  739. case let r as UploadRequest: perform(r)
  740. case let r as DownloadRequest: perform(r)
  741. default: fatalError("Attempted to perform unsupported Request subclass: \(type(of: request))")
  742. }
  743. }
  744. func perform(_ request: DataRequest) {
  745. requestQueue.async {
  746. guard !request.isCancelled else { return }
  747. self.activeRequests.insert(request)
  748. self.performSetupOperations(for: request, convertible: request.convertible)
  749. }
  750. }
  751. func perform(_ request: UploadRequest) {
  752. requestQueue.async {
  753. guard !request.isCancelled else { return }
  754. self.activeRequests.insert(request)
  755. do {
  756. let uploadable = try request.upload.createUploadable()
  757. self.rootQueue.async { request.didCreateUploadable(uploadable) }
  758. self.performSetupOperations(for: request, convertible: request.convertible)
  759. } catch {
  760. self.rootQueue.async { request.didFailToCreateUploadable(with: error) }
  761. }
  762. }
  763. }
  764. func perform(_ request: DownloadRequest) {
  765. requestQueue.async {
  766. guard !request.isCancelled else { return }
  767. self.activeRequests.insert(request)
  768. switch request.downloadable {
  769. case let .request(convertible):
  770. self.performSetupOperations(for: request, convertible: convertible)
  771. case let .resumeData(resumeData):
  772. self.rootQueue.async { self.didReceiveResumeData(resumeData, for: request) }
  773. }
  774. }
  775. }
  776. func performSetupOperations(for request: Request, convertible: URLRequestConvertible) {
  777. do {
  778. let initialRequest = try convertible.asURLRequest()
  779. rootQueue.async { request.didCreateInitialURLRequest(initialRequest) }
  780. guard !request.isCancelled else { return }
  781. if let adapter = adapter(for: request) {
  782. adapter.adapt(initialRequest, for: self) { result in
  783. do {
  784. let adaptedRequest = try result.get()
  785. self.rootQueue.async {
  786. request.didAdaptInitialRequest(initialRequest, to: adaptedRequest)
  787. self.didCreateURLRequest(adaptedRequest, for: request)
  788. }
  789. } catch {
  790. let adaptError = AFError.requestAdaptationFailed(error: error)
  791. self.rootQueue.async { request.didFailToAdaptURLRequest(initialRequest, withError: adaptError) }
  792. }
  793. }
  794. } else {
  795. rootQueue.async { self.didCreateURLRequest(initialRequest, for: request) }
  796. }
  797. } catch {
  798. rootQueue.async { request.didFailToCreateURLRequest(with: error) }
  799. }
  800. }
  801. // MARK: - Task Handling
  802. func didCreateURLRequest(_ urlRequest: URLRequest, for request: Request) {
  803. request.didCreateURLRequest(urlRequest)
  804. guard !request.isCancelled else { return }
  805. let task = request.task(for: urlRequest, using: session)
  806. requestTaskMap[request] = task
  807. request.didCreateTask(task)
  808. updateStatesForTask(task, request: request)
  809. }
  810. func didReceiveResumeData(_ data: Data, for request: DownloadRequest) {
  811. guard !request.isCancelled else { return }
  812. let task = request.task(forResumeData: data, using: session)
  813. requestTaskMap[request] = task
  814. request.didCreateTask(task)
  815. updateStatesForTask(task, request: request)
  816. }
  817. func updateStatesForTask(_ task: URLSessionTask, request: Request) {
  818. request.withState { (state) in
  819. switch (startRequestsImmediately, state) {
  820. case (true, .initialized):
  821. rootQueue.async { request.resume() }
  822. case (false, .initialized):
  823. // Do nothing.
  824. break
  825. case (_, .resumed):
  826. task.resume()
  827. rootQueue.async { request.didResumeTask(task) }
  828. case (_, .suspended):
  829. task.suspend()
  830. rootQueue.async { request.didSuspendTask(task) }
  831. case (_, .cancelled):
  832. // Resume to ensure metrics are gathered.
  833. task.resume()
  834. task.cancel()
  835. rootQueue.async { request.didCancelTask(task) }
  836. case (_, .finished):
  837. // Do nothing
  838. break
  839. }
  840. }
  841. }
  842. // MARK: - Adapters and Retriers
  843. func adapter(for request: Request) -> RequestAdapter? {
  844. if let requestInterceptor = request.interceptor, let sessionInterceptor = interceptor {
  845. return Interceptor(adapters: [requestInterceptor, sessionInterceptor])
  846. } else {
  847. return request.interceptor ?? interceptor
  848. }
  849. }
  850. func retrier(for request: Request) -> RequestRetrier? {
  851. if let requestInterceptor = request.interceptor, let sessionInterceptor = interceptor {
  852. return Interceptor(retriers: [requestInterceptor, sessionInterceptor])
  853. } else {
  854. return request.interceptor ?? interceptor
  855. }
  856. }
  857. // MARK: - Invalidation
  858. func finishRequestsForDeinit() {
  859. requestTaskMap.requests.forEach { $0.finish(error: AFError.sessionDeinitialized) }
  860. }
  861. }
  862. // MARK: - RequestDelegate
  863. extension Session: RequestDelegate {
  864. public var sessionConfiguration: URLSessionConfiguration {
  865. return session.configuration
  866. }
  867. public func cleanup(after request: Request) {
  868. activeRequests.remove(request)
  869. }
  870. public func retryResult(for request: Request, dueTo error: Error, completion: @escaping (RetryResult) -> Void) {
  871. guard let retrier = retrier(for: request) else {
  872. rootQueue.async { completion(.doNotRetry) }
  873. return
  874. }
  875. retrier.retry(request, for: self, dueTo: error) { retryResult in
  876. self.rootQueue.async {
  877. guard let retryResultError = retryResult.error else { completion(retryResult); return }
  878. let retryError = AFError.requestRetryFailed(retryError: retryResultError, originalError: error)
  879. completion(.doNotRetryWithError(retryError))
  880. }
  881. }
  882. }
  883. public func retryRequest(_ request: Request, withDelay timeDelay: TimeInterval?) {
  884. self.rootQueue.async {
  885. let retry: () -> Void = {
  886. guard !request.isCancelled else { return }
  887. request.prepareForRetry()
  888. self.perform(request)
  889. }
  890. if let retryDelay = timeDelay {
  891. self.rootQueue.after(retryDelay) { retry() }
  892. } else {
  893. retry()
  894. }
  895. }
  896. }
  897. }
  898. // MARK: - SessionStateProvider
  899. extension Session: SessionStateProvider {
  900. func request(for task: URLSessionTask) -> Request? {
  901. return requestTaskMap[task]
  902. }
  903. func didGatherMetricsForTask(_ task: URLSessionTask) {
  904. requestTaskMap.disassociateIfNecessaryAfterGatheringMetricsForTask(task)
  905. }
  906. func didCompleteTask(_ task: URLSessionTask) {
  907. requestTaskMap.disassociateIfNecessaryAfterCompletingTask(task)
  908. }
  909. func credential(for task: URLSessionTask, in protectionSpace: URLProtectionSpace) -> URLCredential? {
  910. return requestTaskMap[task]?.credential ??
  911. session.configuration.urlCredentialStorage?.defaultCredential(for: protectionSpace)
  912. }
  913. func cancelRequestsForSessionInvalidation(with error: Error?) {
  914. requestTaskMap.requests.forEach { $0.finish(error: AFError.sessionInvalidated(error: error)) }
  915. }
  916. }