Session.swift 57 KB

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