Session.swift 56 KB

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