Session.swift 52 KB

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