Session.swift 58 KB

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