Session.swift 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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: - DataStreamRequest
  300. /// Creates a `DataStreamRequest` from the passed components, `Encodable` parameters, and `RequestInterceptor`.
  301. ///
  302. /// - Parameters:
  303. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  304. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  305. /// - parameters: `Encodable` value to be encoded into the `URLRequest`. `nil` by default.
  306. /// - encoder: `ParameterEncoder` to be used to encode the `parameters` value into the
  307. /// `URLRequest`.
  308. /// `URLEncodedFormParameterEncoder.default` by default.
  309. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  310. /// - automaticallyCancelOnStreamError: `Bool` indicating whether the instance should be canceled when an `Error`
  311. /// is thrown while serializing stream `Data`. `false` by default.
  312. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil`
  313. /// by default.
  314. ///
  315. /// - Returns: The created `DataStream` request.
  316. open func streamRequest<Parameters: Encodable>(_ convertible: URLConvertible,
  317. method: HTTPMethod = .get,
  318. parameters: Parameters? = nil,
  319. encoder: ParameterEncoder = URLEncodedFormParameterEncoder.default,
  320. headers: HTTPHeaders? = nil,
  321. automaticallyCancelOnStreamError: Bool = false,
  322. interceptor: RequestInterceptor? = nil) -> DataStreamRequest {
  323. let convertible = RequestEncodableConvertible(url: convertible,
  324. method: method,
  325. parameters: parameters,
  326. encoder: encoder,
  327. headers: headers)
  328. return streamRequest(convertible,
  329. automaticallyCancelOnStreamError: automaticallyCancelOnStreamError,
  330. interceptor: interceptor)
  331. }
  332. /// Creates a `DataStreamRequest` from the passed components and `RequestInterceptor`.
  333. ///
  334. /// - Parameters:
  335. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  336. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  337. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  338. /// - automaticallyCancelOnStreamError: `Bool` indicating whether the instance should be canceled when an `Error`
  339. /// is thrown while serializing stream `Data`. `false` by default.
  340. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil`
  341. /// by default.
  342. ///
  343. /// - Returns: The created `DataStream` request.
  344. open func streamRequest(_ convertible: URLConvertible,
  345. method: HTTPMethod = .get,
  346. headers: HTTPHeaders? = nil,
  347. automaticallyCancelOnStreamError: Bool = false,
  348. interceptor: RequestInterceptor? = nil) -> DataStreamRequest {
  349. let convertible = RequestEncodableConvertible(url: convertible,
  350. method: method,
  351. parameters: Optional<Empty>.none,
  352. encoder: URLEncodedFormParameterEncoder.default,
  353. headers: headers)
  354. return streamRequest(convertible,
  355. automaticallyCancelOnStreamError: automaticallyCancelOnStreamError,
  356. interceptor: interceptor)
  357. }
  358. /// Creates a `DataStreamRequest` from the passed `URLRequestConvertible` value and `RequestInterceptor`.
  359. ///
  360. /// - Parameters:
  361. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  362. /// - automaticallyCancelOnStreamError: `Bool` indicating whether the instance should be canceled when an `Error`
  363. /// is thrown while serializing stream `Data`. `false` by default.
  364. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil`
  365. /// by default.
  366. ///
  367. /// - Returns: The created `DataStreamRequest`.
  368. open func streamRequest(_ convertible: URLRequestConvertible,
  369. automaticallyCancelOnStreamError: Bool = false,
  370. interceptor: RequestInterceptor? = nil) -> DataStreamRequest {
  371. let request = DataStreamRequest(convertible: convertible,
  372. automaticallyCancelOnStreamError: automaticallyCancelOnStreamError,
  373. underlyingQueue: rootQueue,
  374. serializationQueue: serializationQueue,
  375. eventMonitor: eventMonitor,
  376. interceptor: interceptor,
  377. delegate: self)
  378. perform(request)
  379. return request
  380. }
  381. // MARK: - DownloadRequest
  382. /// Creates a `DownloadRequest` using a `URLRequest` created using the passed components, `RequestInterceptor`, and
  383. /// `Destination`.
  384. ///
  385. /// - Parameters:
  386. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  387. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  388. /// - parameters: `Parameters` (a.k.a. `[String: Any]`) value to be encoded into the `URLRequest`. `nil` by default.
  389. /// - encoding: `ParameterEncoding` to be used to encode the `parameters` value into the `URLRequest`. Defaults
  390. /// to `URLEncoding.default`.
  391. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  392. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  393. /// - destination: `DownloadRequest.Destination` closure used to determine how and where the downloaded file
  394. /// should be moved. `nil` by default.
  395. ///
  396. /// - Returns: The created `DownloadRequest`.
  397. open func download(_ convertible: URLConvertible,
  398. method: HTTPMethod = .get,
  399. parameters: Parameters? = nil,
  400. encoding: ParameterEncoding = URLEncoding.default,
  401. headers: HTTPHeaders? = nil,
  402. interceptor: RequestInterceptor? = nil,
  403. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  404. let convertible = RequestConvertible(url: convertible,
  405. method: method,
  406. parameters: parameters,
  407. encoding: encoding,
  408. headers: headers)
  409. return download(convertible, interceptor: interceptor, to: destination)
  410. }
  411. /// Creates a `DownloadRequest` from a `URLRequest` created using the passed components, `Encodable` parameters, and
  412. /// a `RequestInterceptor`.
  413. ///
  414. /// - Parameters:
  415. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  416. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  417. /// - parameters: Value conforming to `Encodable` to be encoded into the `URLRequest`. `nil` by default.
  418. /// - encoder: `ParameterEncoder` to be used to encode the `parameters` value into the `URLRequest`. Defaults
  419. /// to `URLEncodedFormParameterEncoder.default`.
  420. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  421. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  422. /// - destination: `DownloadRequest.Destination` closure used to determine how and where the downloaded file
  423. /// should be moved. `nil` by default.
  424. ///
  425. /// - Returns: The created `DownloadRequest`.
  426. open func download<Parameters: Encodable>(_ convertible: URLConvertible,
  427. method: HTTPMethod = .get,
  428. parameters: Parameters? = nil,
  429. encoder: ParameterEncoder = URLEncodedFormParameterEncoder.default,
  430. headers: HTTPHeaders? = nil,
  431. interceptor: RequestInterceptor? = nil,
  432. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  433. let convertible = RequestEncodableConvertible(url: convertible,
  434. method: method,
  435. parameters: parameters,
  436. encoder: encoder,
  437. headers: headers)
  438. return download(convertible, interceptor: interceptor, to: destination)
  439. }
  440. /// Creates a `DownloadRequest` from a `URLRequestConvertible` value, a `RequestInterceptor`, and a `Destination`.
  441. ///
  442. /// - Parameters:
  443. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  444. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  445. /// - destination: `DownloadRequest.Destination` closure used to determine how and where the downloaded file
  446. /// should be moved. `nil` by default.
  447. ///
  448. /// - Returns: The created `DownloadRequest`.
  449. open func download(_ convertible: URLRequestConvertible,
  450. interceptor: RequestInterceptor? = nil,
  451. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  452. let request = DownloadRequest(downloadable: .request(convertible),
  453. underlyingQueue: rootQueue,
  454. serializationQueue: serializationQueue,
  455. eventMonitor: eventMonitor,
  456. interceptor: interceptor,
  457. delegate: self,
  458. destination: destination ?? DownloadRequest.defaultDestination)
  459. perform(request)
  460. return request
  461. }
  462. /// Creates a `DownloadRequest` from the `resumeData` produced from a previously cancelled `DownloadRequest`, as
  463. /// well as a `RequestInterceptor`, and a `Destination`.
  464. ///
  465. /// - Note: If `destination` is not specified, the download will be moved to a temporary location determined by
  466. /// Alamofire. The file will not be deleted until the system purges the temporary files.
  467. ///
  468. /// - 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),
  469. /// `resumeData` is broken on background URL session configurations. There's an underlying bug in the `resumeData`
  470. /// generation logic where the data is written incorrectly and will always fail to resume the download. For more
  471. /// information about the bug and possible workarounds, please refer to the [this Stack Overflow post](http://stackoverflow.com/a/39347461/1342462).
  472. ///
  473. /// - Parameters:
  474. /// - data: The resume data from a previously cancelled `DownloadRequest` or `URLSessionDownloadTask`.
  475. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  476. /// - destination: `DownloadRequest.Destination` closure used to determine how and where the downloaded file
  477. /// should be moved. `nil` by default.
  478. ///
  479. /// - Returns: The created `DownloadRequest`.
  480. open func download(resumingWith data: Data,
  481. interceptor: RequestInterceptor? = nil,
  482. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  483. let request = DownloadRequest(downloadable: .resumeData(data),
  484. underlyingQueue: rootQueue,
  485. serializationQueue: serializationQueue,
  486. eventMonitor: eventMonitor,
  487. interceptor: interceptor,
  488. delegate: self,
  489. destination: destination ?? DownloadRequest.defaultDestination)
  490. perform(request)
  491. return request
  492. }
  493. // MARK: - UploadRequest
  494. struct ParameterlessRequestConvertible: URLRequestConvertible {
  495. let url: URLConvertible
  496. let method: HTTPMethod
  497. let headers: HTTPHeaders?
  498. func asURLRequest() throws -> URLRequest {
  499. try URLRequest(url: url, method: method, headers: headers)
  500. }
  501. }
  502. struct Upload: UploadConvertible {
  503. let request: URLRequestConvertible
  504. let uploadable: UploadableConvertible
  505. func createUploadable() throws -> UploadRequest.Uploadable {
  506. try uploadable.createUploadable()
  507. }
  508. func asURLRequest() throws -> URLRequest {
  509. try request.asURLRequest()
  510. }
  511. }
  512. // MARK: Data
  513. /// Creates an `UploadRequest` for the given `Data`, `URLRequest` components, and `RequestInterceptor`.
  514. ///
  515. /// - Parameters:
  516. /// - data: The `Data` to upload.
  517. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  518. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  519. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  520. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  521. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  522. /// default.
  523. ///
  524. /// - Returns: The created `UploadRequest`.
  525. open func upload(_ data: Data,
  526. to convertible: URLConvertible,
  527. method: HTTPMethod = .post,
  528. headers: HTTPHeaders? = nil,
  529. interceptor: RequestInterceptor? = nil,
  530. fileManager: FileManager = .default) -> UploadRequest {
  531. let convertible = ParameterlessRequestConvertible(url: convertible, method: method, headers: headers)
  532. return upload(data, with: convertible, interceptor: interceptor, fileManager: fileManager)
  533. }
  534. /// Creates an `UploadRequest` for the given `Data` using the `URLRequestConvertible` value and `RequestInterceptor`.
  535. ///
  536. /// - Parameters:
  537. /// - data: The `Data` to upload.
  538. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  539. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  540. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  541. /// default.
  542. ///
  543. /// - Returns: The created `UploadRequest`.
  544. open func upload(_ data: Data,
  545. with convertible: URLRequestConvertible,
  546. interceptor: RequestInterceptor? = nil,
  547. fileManager: FileManager = .default) -> UploadRequest {
  548. upload(.data(data), with: convertible, interceptor: interceptor, fileManager: fileManager)
  549. }
  550. // MARK: File
  551. /// Creates an `UploadRequest` for the file at the given file `URL`, using a `URLRequest` from the provided
  552. /// components and `RequestInterceptor`.
  553. ///
  554. /// - Parameters:
  555. /// - fileURL: The `URL` of the file to upload.
  556. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  557. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  558. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  559. /// - interceptor: `RequestInterceptor` value to be used by the returned `UploadRequest`. `nil` by default.
  560. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  561. /// default.
  562. ///
  563. /// - Returns: The created `UploadRequest`.
  564. open func upload(_ fileURL: URL,
  565. to convertible: URLConvertible,
  566. method: HTTPMethod = .post,
  567. headers: HTTPHeaders? = nil,
  568. interceptor: RequestInterceptor? = nil,
  569. fileManager: FileManager = .default) -> UploadRequest {
  570. let convertible = ParameterlessRequestConvertible(url: convertible, method: method, headers: headers)
  571. return upload(fileURL, with: convertible, interceptor: interceptor, fileManager: fileManager)
  572. }
  573. /// Creates an `UploadRequest` for the file at the given file `URL` using the `URLRequestConvertible` value and
  574. /// `RequestInterceptor`.
  575. ///
  576. /// - Parameters:
  577. /// - fileURL: The `URL` of the file to upload.
  578. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  579. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  580. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  581. /// default.
  582. ///
  583. /// - Returns: The created `UploadRequest`.
  584. open func upload(_ fileURL: URL,
  585. with convertible: URLRequestConvertible,
  586. interceptor: RequestInterceptor? = nil,
  587. fileManager: FileManager = .default) -> UploadRequest {
  588. upload(.file(fileURL, shouldRemove: false), with: convertible, interceptor: interceptor, fileManager: fileManager)
  589. }
  590. // MARK: InputStream
  591. /// Creates an `UploadRequest` from the `InputStream` provided using a `URLRequest` from the provided components and
  592. /// `RequestInterceptor`.
  593. ///
  594. /// - Parameters:
  595. /// - stream: The `InputStream` that provides the data to upload.
  596. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  597. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  598. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  599. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  600. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  601. /// default.
  602. ///
  603. /// - Returns: The created `UploadRequest`.
  604. open func upload(_ stream: InputStream,
  605. to convertible: URLConvertible,
  606. method: HTTPMethod = .post,
  607. headers: HTTPHeaders? = nil,
  608. interceptor: RequestInterceptor? = nil,
  609. fileManager: FileManager = .default) -> UploadRequest {
  610. let convertible = ParameterlessRequestConvertible(url: convertible, method: method, headers: headers)
  611. return upload(stream, with: convertible, interceptor: interceptor, fileManager: fileManager)
  612. }
  613. /// Creates an `UploadRequest` from the provided `InputStream` using the `URLRequestConvertible` value and
  614. /// `RequestInterceptor`.
  615. ///
  616. /// - Parameters:
  617. /// - stream: The `InputStream` that provides the data to upload.
  618. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  619. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  620. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  621. /// default.
  622. ///
  623. /// - Returns: The created `UploadRequest`.
  624. open func upload(_ stream: InputStream,
  625. with convertible: URLRequestConvertible,
  626. interceptor: RequestInterceptor? = nil,
  627. fileManager: FileManager = .default) -> UploadRequest {
  628. upload(.stream(stream), with: convertible, interceptor: interceptor, fileManager: fileManager)
  629. }
  630. // MARK: MultipartFormData
  631. /// Creates an `UploadRequest` for the multipart form data built using a closure and sent using the provided
  632. /// `URLRequest` components and `RequestInterceptor`.
  633. ///
  634. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cumulative
  635. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  636. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  637. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  638. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  639. /// used for larger payloads such as video content.
  640. ///
  641. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  642. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  643. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  644. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  645. /// technique was used.
  646. ///
  647. /// - Parameters:
  648. /// - multipartFormData: `MultipartFormData` building closure.
  649. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  650. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  651. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  652. /// default.
  653. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  654. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  655. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  656. /// - fileManager: `FileManager` to be used if the form data exceeds the memory threshold and is
  657. /// written to disk before being uploaded. `.default` instance by default.
  658. ///
  659. /// - Returns: The created `UploadRequest`.
  660. open func upload(multipartFormData: @escaping (MultipartFormData) -> Void,
  661. to url: URLConvertible,
  662. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  663. method: HTTPMethod = .post,
  664. headers: HTTPHeaders? = nil,
  665. interceptor: RequestInterceptor? = nil,
  666. fileManager: FileManager = .default) -> UploadRequest {
  667. let convertible = ParameterlessRequestConvertible(url: url, method: method, headers: headers)
  668. let formData = MultipartFormData(fileManager: fileManager)
  669. multipartFormData(formData)
  670. return upload(multipartFormData: formData,
  671. with: convertible,
  672. usingThreshold: encodingMemoryThreshold,
  673. interceptor: interceptor,
  674. fileManager: fileManager)
  675. }
  676. /// Creates an `UploadRequest` using a `MultipartFormData` building closure, the provided `URLRequestConvertible`
  677. /// value, and a `RequestInterceptor`.
  678. ///
  679. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cumulative
  680. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  681. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  682. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  683. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  684. /// used for larger payloads such as video content.
  685. ///
  686. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  687. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  688. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  689. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  690. /// technique was used.
  691. ///
  692. /// - Parameters:
  693. /// - multipartFormData: `MultipartFormData` building closure.
  694. /// - request: `URLRequestConvertible` value to be used to create the `URLRequest`.
  695. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  696. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  697. /// default.
  698. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  699. /// - fileManager: `FileManager` to be used if the form data exceeds the memory threshold and is
  700. /// written to disk before being uploaded. `.default` instance by default.
  701. ///
  702. /// - Returns: The created `UploadRequest`.
  703. open func upload(multipartFormData: @escaping (MultipartFormData) -> Void,
  704. with request: URLRequestConvertible,
  705. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  706. interceptor: RequestInterceptor? = nil,
  707. fileManager: FileManager = .default) -> UploadRequest {
  708. let formData = MultipartFormData(fileManager: fileManager)
  709. multipartFormData(formData)
  710. return upload(multipartFormData: formData,
  711. with: request,
  712. usingThreshold: encodingMemoryThreshold,
  713. interceptor: interceptor,
  714. fileManager: fileManager)
  715. }
  716. /// Creates an `UploadRequest` for the prebuilt `MultipartFormData` value using the provided `URLRequest` components
  717. /// and `RequestInterceptor`.
  718. ///
  719. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cumulative
  720. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  721. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  722. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  723. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  724. /// used for larger payloads such as video content.
  725. ///
  726. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  727. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  728. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  729. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  730. /// technique was used.
  731. ///
  732. /// - Parameters:
  733. /// - multipartFormData: `MultipartFormData` instance to upload.
  734. /// - url: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  735. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  736. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  737. /// default.
  738. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  739. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  740. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  741. /// - fileManager: `FileManager` to be used if the form data exceeds the memory threshold and is
  742. /// written to disk before being uploaded. `.default` instance by default.
  743. ///
  744. /// - Returns: The created `UploadRequest`.
  745. open func upload(multipartFormData: MultipartFormData,
  746. to url: URLConvertible,
  747. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  748. method: HTTPMethod = .post,
  749. headers: HTTPHeaders? = nil,
  750. interceptor: RequestInterceptor? = nil,
  751. fileManager: FileManager = .default) -> UploadRequest {
  752. let convertible = ParameterlessRequestConvertible(url: url, method: method, headers: headers)
  753. let multipartUpload = MultipartUpload(isInBackgroundSession: session.configuration.identifier != nil,
  754. encodingMemoryThreshold: encodingMemoryThreshold,
  755. request: convertible,
  756. multipartFormData: multipartFormData)
  757. return upload(multipartUpload, interceptor: interceptor, fileManager: fileManager)
  758. }
  759. /// Creates an `UploadRequest` for the prebuilt `MultipartFormData` value using the providing `URLRequestConvertible`
  760. /// value and `RequestInterceptor`.
  761. ///
  762. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cumulative
  763. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  764. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  765. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  766. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  767. /// used for larger payloads such as video content.
  768. ///
  769. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  770. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  771. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  772. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  773. /// technique was used.
  774. ///
  775. /// - Parameters:
  776. /// - multipartFormData: `MultipartFormData` instance to upload.
  777. /// - request: `URLRequestConvertible` value to be used to create the `URLRequest`.
  778. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  779. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  780. /// default.
  781. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  782. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  783. /// default.
  784. ///
  785. /// - Returns: The created `UploadRequest`.
  786. open func upload(multipartFormData: MultipartFormData,
  787. with request: URLRequestConvertible,
  788. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  789. interceptor: RequestInterceptor? = nil,
  790. fileManager: FileManager = .default) -> UploadRequest {
  791. let multipartUpload = MultipartUpload(isInBackgroundSession: session.configuration.identifier != nil,
  792. encodingMemoryThreshold: encodingMemoryThreshold,
  793. request: request,
  794. multipartFormData: multipartFormData)
  795. return upload(multipartUpload, interceptor: interceptor, fileManager: fileManager)
  796. }
  797. // MARK: - Internal API
  798. // MARK: Uploadable
  799. func upload(_ uploadable: UploadRequest.Uploadable,
  800. with convertible: URLRequestConvertible,
  801. interceptor: RequestInterceptor?,
  802. fileManager: FileManager) -> UploadRequest {
  803. let uploadable = Upload(request: convertible, uploadable: uploadable)
  804. return upload(uploadable, interceptor: interceptor, fileManager: fileManager)
  805. }
  806. func upload(_ upload: UploadConvertible, interceptor: RequestInterceptor?, fileManager: FileManager) -> UploadRequest {
  807. let request = UploadRequest(convertible: upload,
  808. underlyingQueue: rootQueue,
  809. serializationQueue: serializationQueue,
  810. eventMonitor: eventMonitor,
  811. interceptor: interceptor,
  812. fileManager: fileManager,
  813. delegate: self)
  814. perform(request)
  815. return request
  816. }
  817. // MARK: Perform
  818. /// Perform `Request`.
  819. ///
  820. /// - Note: Called during retry.
  821. ///
  822. /// - Parameter request: The `Request` to perform.
  823. func perform(_ request: Request) {
  824. // Leaf types must come first, otherwise they will cast as their superclass.
  825. switch request {
  826. case let r as UploadRequest: perform(r) // UploadRequest must come before DataRequest due to subtype relationship.
  827. case let r as DataRequest: perform(r)
  828. case let r as DownloadRequest: perform(r)
  829. case let r as DataStreamRequest: perform(r)
  830. default: fatalError("Attempted to perform unsupported Request subclass: \(type(of: request))")
  831. }
  832. }
  833. func perform(_ request: DataRequest) {
  834. requestQueue.async {
  835. guard !request.isCancelled else { return }
  836. self.activeRequests.insert(request)
  837. self.performSetupOperations(for: request, convertible: request.convertible)
  838. }
  839. }
  840. func perform(_ request: DataStreamRequest) {
  841. requestQueue.async {
  842. guard !request.isCancelled else { return }
  843. self.activeRequests.insert(request)
  844. self.performSetupOperations(for: request, convertible: request.convertible)
  845. }
  846. }
  847. func perform(_ request: UploadRequest) {
  848. requestQueue.async {
  849. guard !request.isCancelled else { return }
  850. self.activeRequests.insert(request)
  851. do {
  852. let uploadable = try request.upload.createUploadable()
  853. self.rootQueue.async { request.didCreateUploadable(uploadable) }
  854. self.performSetupOperations(for: request, convertible: request.convertible)
  855. } catch {
  856. self.rootQueue.async { request.didFailToCreateUploadable(with: error.asAFError(or: .createUploadableFailed(error: error))) }
  857. }
  858. }
  859. }
  860. func perform(_ request: DownloadRequest) {
  861. requestQueue.async {
  862. guard !request.isCancelled else { return }
  863. self.activeRequests.insert(request)
  864. switch request.downloadable {
  865. case let .request(convertible):
  866. self.performSetupOperations(for: request, convertible: convertible)
  867. case let .resumeData(resumeData):
  868. self.rootQueue.async { self.didReceiveResumeData(resumeData, for: request) }
  869. }
  870. }
  871. }
  872. func performSetupOperations(for request: Request, convertible: URLRequestConvertible) {
  873. let initialRequest: URLRequest
  874. do {
  875. initialRequest = try convertible.asURLRequest()
  876. try initialRequest.validate()
  877. } catch {
  878. rootQueue.async { request.didFailToCreateURLRequest(with: error.asAFError(or: .createURLRequestFailed(error: error))) }
  879. return
  880. }
  881. rootQueue.async { request.didCreateInitialURLRequest(initialRequest) }
  882. guard !request.isCancelled else { return }
  883. guard let adapter = adapter(for: request) else {
  884. rootQueue.async { self.didCreateURLRequest(initialRequest, for: request) }
  885. return
  886. }
  887. adapter.adapt(initialRequest, for: self) { result in
  888. do {
  889. let adaptedRequest = try result.get()
  890. try adaptedRequest.validate()
  891. self.rootQueue.async {
  892. request.didAdaptInitialRequest(initialRequest, to: adaptedRequest)
  893. self.didCreateURLRequest(adaptedRequest, for: request)
  894. }
  895. } catch {
  896. self.rootQueue.async { request.didFailToAdaptURLRequest(initialRequest, withError: .requestAdaptationFailed(error: error)) }
  897. }
  898. }
  899. }
  900. // MARK: - Task Handling
  901. func didCreateURLRequest(_ urlRequest: URLRequest, for request: Request) {
  902. request.didCreateURLRequest(urlRequest)
  903. guard !request.isCancelled else { return }
  904. let task = request.task(for: urlRequest, using: session)
  905. requestTaskMap[request] = task
  906. request.didCreateTask(task)
  907. updateStatesForTask(task, request: request)
  908. }
  909. func didReceiveResumeData(_ data: Data, for request: DownloadRequest) {
  910. guard !request.isCancelled else { return }
  911. let task = request.task(forResumeData: data, using: session)
  912. requestTaskMap[request] = task
  913. request.didCreateTask(task)
  914. updateStatesForTask(task, request: request)
  915. }
  916. func updateStatesForTask(_ task: URLSessionTask, request: Request) {
  917. request.withState { state in
  918. switch state {
  919. case .initialized, .finished:
  920. // Do nothing.
  921. break
  922. case .resumed:
  923. task.resume()
  924. rootQueue.async { request.didResumeTask(task) }
  925. case .suspended:
  926. task.suspend()
  927. rootQueue.async { request.didSuspendTask(task) }
  928. case .cancelled:
  929. // Resume to ensure metrics are gathered.
  930. task.resume()
  931. task.cancel()
  932. rootQueue.async { request.didCancelTask(task) }
  933. }
  934. }
  935. }
  936. // MARK: - Adapters and Retriers
  937. func adapter(for request: Request) -> RequestAdapter? {
  938. if let requestInterceptor = request.interceptor, let sessionInterceptor = interceptor {
  939. return Interceptor(adapters: [requestInterceptor, sessionInterceptor])
  940. } else {
  941. return request.interceptor ?? interceptor
  942. }
  943. }
  944. func retrier(for request: Request) -> RequestRetrier? {
  945. if let requestInterceptor = request.interceptor, let sessionInterceptor = interceptor {
  946. return Interceptor(retriers: [requestInterceptor, sessionInterceptor])
  947. } else {
  948. return request.interceptor ?? interceptor
  949. }
  950. }
  951. // MARK: - Invalidation
  952. func finishRequestsForDeinit() {
  953. requestTaskMap.requests.forEach { request in
  954. rootQueue.async {
  955. request.finish(error: AFError.sessionDeinitialized)
  956. }
  957. }
  958. }
  959. }
  960. // MARK: - RequestDelegate
  961. extension Session: RequestDelegate {
  962. public var sessionConfiguration: URLSessionConfiguration {
  963. session.configuration
  964. }
  965. public var startImmediately: Bool { startRequestsImmediately }
  966. public func cleanup(after request: Request) {
  967. activeRequests.remove(request)
  968. }
  969. public func retryResult(for request: Request, dueTo error: AFError, completion: @escaping (RetryResult) -> Void) {
  970. guard let retrier = retrier(for: request) else {
  971. rootQueue.async { completion(.doNotRetry) }
  972. return
  973. }
  974. retrier.retry(request, for: self, dueTo: error) { retryResult in
  975. self.rootQueue.async {
  976. guard let retryResultError = retryResult.error else { completion(retryResult); return }
  977. let retryError = AFError.requestRetryFailed(retryError: retryResultError, originalError: error)
  978. completion(.doNotRetryWithError(retryError))
  979. }
  980. }
  981. }
  982. public func retryRequest(_ request: Request, withDelay timeDelay: TimeInterval?) {
  983. rootQueue.async {
  984. let retry: () -> Void = {
  985. guard !request.isCancelled else { return }
  986. request.prepareForRetry()
  987. self.perform(request)
  988. }
  989. if let retryDelay = timeDelay {
  990. self.rootQueue.after(retryDelay) { retry() }
  991. } else {
  992. retry()
  993. }
  994. }
  995. }
  996. }
  997. // MARK: - SessionStateProvider
  998. extension Session: SessionStateProvider {
  999. func request(for task: URLSessionTask) -> Request? {
  1000. dispatchPrecondition(condition: .onQueue(rootQueue))
  1001. return requestTaskMap[task]
  1002. }
  1003. func didGatherMetricsForTask(_ task: URLSessionTask) {
  1004. dispatchPrecondition(condition: .onQueue(rootQueue))
  1005. let didDisassociate = requestTaskMap.disassociateIfNecessaryAfterGatheringMetricsForTask(task)
  1006. if didDisassociate {
  1007. waitingCompletions[task]?()
  1008. waitingCompletions[task] = nil
  1009. }
  1010. }
  1011. func didCompleteTask(_ task: URLSessionTask, completion: @escaping () -> Void) {
  1012. dispatchPrecondition(condition: .onQueue(rootQueue))
  1013. let didDisassociate = requestTaskMap.disassociateIfNecessaryAfterCompletingTask(task)
  1014. if didDisassociate {
  1015. completion()
  1016. } else {
  1017. waitingCompletions[task] = completion
  1018. }
  1019. }
  1020. func credential(for task: URLSessionTask, in protectionSpace: URLProtectionSpace) -> URLCredential? {
  1021. dispatchPrecondition(condition: .onQueue(rootQueue))
  1022. return requestTaskMap[task]?.credential ??
  1023. session.configuration.urlCredentialStorage?.defaultCredential(for: protectionSpace)
  1024. }
  1025. func cancelRequestsForSessionInvalidation(with error: Error?) {
  1026. dispatchPrecondition(condition: .onQueue(rootQueue))
  1027. requestTaskMap.requests.forEach { $0.finish(error: AFError.sessionInvalidated(error: error)) }
  1028. }
  1029. }