Session.swift 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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. ///
  34. /// - Note: This instance should **NOT** be used to interact with the underlying `URLSessionTask`s. Doing so will
  35. /// break internal Alamofire logic that tracks those tasks.
  36. ///
  37. public let session: URLSession
  38. /// Instance's `SessionDelegate`, which handles the `URLSessionDelegate` methods and `Request` interaction.
  39. public let delegate: SessionDelegate
  40. /// Root `DispatchQueue` for all internal callbacks and state update. **MUST** be a serial queue.
  41. public let rootQueue: DispatchQueue
  42. /// Value determining whether this instance automatically calls `resume()` on all created `Request`s.
  43. public let startRequestsImmediately: Bool
  44. /// `DispatchQueue` on which `URLRequest`s are created asynchronously. By default this queue uses `rootQueue` as its
  45. /// `target`, but a separate queue can be used if request creation is determined to be a bottleneck. Always profile
  46. /// and test before introducing an additional queue.
  47. public let requestQueue: DispatchQueue
  48. /// `DispatchQueue` passed to all `Request`s on which they perform their response serialization. By default this
  49. /// queue uses `rootQueue` as its `target` but a separate queue can be used if response serialization is determined
  50. /// to be a bottleneck. Always profile and test before introducing an additional queue.
  51. public let serializationQueue: DispatchQueue
  52. /// `RequestInterceptor` used for all `Request` created by the instance. `RequestInterceptor`s can also be set on a
  53. /// per-`Request` basis, in which case the `Request`'s interceptor takes precedence over this value.
  54. public let interceptor: RequestInterceptor?
  55. /// `ServerTrustManager` instance used to evaluate all trust challenges and provide certificate and key pinning.
  56. public let serverTrustManager: ServerTrustManager?
  57. /// `RedirectHandler` instance used to provide customization for request redirection.
  58. public let redirectHandler: RedirectHandler?
  59. /// `CachedResponseHandler` instance used to provide customization of cached response handling.
  60. public let cachedResponseHandler: CachedResponseHandler?
  61. /// `CompositeEventMonitor` used to compose Alamofire's `defaultEventMonitors` and any passed `EventMonitor`s.
  62. public let eventMonitor: CompositeEventMonitor
  63. /// `EventMonitor`s included in all instances. `[AlamofireNotifications()]` by default.
  64. public let defaultEventMonitors: [EventMonitor] = [AlamofireNotifications()]
  65. /// Internal map between `Request`s and any `URLSessionTasks` that may be in flight for them.
  66. var requestTaskMap = RequestTaskMap()
  67. /// `Set` of currently active `Request`s.
  68. var activeRequests: Set<Request> = []
  69. /// Completion events awaiting `URLSessionTaskMetrics`.
  70. var waitingCompletions: [URLSessionTask: () -> Void] = [:]
  71. /// Creates a `Session` from a `URLSession` and other parameters.
  72. ///
  73. /// - Note: When passing a `URLSession`, you must create the `URLSession` with a specific `delegateQueue` value and
  74. /// pass the `delegateQueue`'s `underlyingQueue` as the `rootQueue` parameter of this initializer.
  75. ///
  76. /// - Parameters:
  77. /// - session: Underlying `URLSession` for this instance.
  78. /// - delegate: `SessionDelegate` that handles `session`'s delegate callbacks as well as `Request`
  79. /// interaction.
  80. /// - rootQueue: Root `DispatchQueue` for all internal callbacks and state updates. **MUST** be a
  81. /// serial queue.
  82. /// - startRequestsImmediately: Determines whether this instance will automatically start all `Request`s. `true`
  83. /// by default. If set to `false`, all `Request`s created must have `.resume()` called.
  84. /// on them for them to start.
  85. /// - requestQueue: `DispatchQueue` on which to perform `URLRequest` creation. By default this queue
  86. /// will use the `rootQueue` as its `target`. A separate queue can be used if it's
  87. /// determined request creation is a bottleneck, but that should only be done after
  88. /// careful testing and profiling. `nil` by default.
  89. /// - serializationQueue: `DispatchQueue` on which to perform all response serialization. By default this
  90. /// queue will use the `rootQueue` as its `target`. A separate queue can be used if
  91. /// it's determined response serialization is a bottleneck, but that should only be
  92. /// done after careful testing and profiling. `nil` by default.
  93. /// - interceptor: `RequestInterceptor` to be used for all `Request`s created by this instance. `nil`
  94. /// by default.
  95. /// - serverTrustManager: `ServerTrustManager` to be used for all trust evaluations by this instance. `nil`
  96. /// by default.
  97. /// - redirectHandler: `RedirectHandler` to be used by all `Request`s created by this instance. `nil` by
  98. /// default.
  99. /// - cachedResponseHandler: `CachedResponseHandler` to be used by all `Request`s created by this instance.
  100. /// `nil` by default.
  101. /// - eventMonitors: Additional `EventMonitor`s used by the instance. Alamofire always adds a
  102. /// `AlamofireNotifications` `EventMonitor` to the array passed here. `[]` by default.
  103. public init(session: URLSession,
  104. delegate: SessionDelegate,
  105. rootQueue: DispatchQueue,
  106. startRequestsImmediately: Bool = true,
  107. requestQueue: DispatchQueue? = nil,
  108. serializationQueue: DispatchQueue? = nil,
  109. interceptor: RequestInterceptor? = nil,
  110. serverTrustManager: ServerTrustManager? = nil,
  111. redirectHandler: RedirectHandler? = nil,
  112. cachedResponseHandler: CachedResponseHandler? = nil,
  113. eventMonitors: [EventMonitor] = []) {
  114. precondition(session.configuration.identifier == nil,
  115. "Alamofire does not support background URLSessionConfigurations.")
  116. precondition(session.delegateQueue.underlyingQueue === rootQueue,
  117. "Session(session:) initializer must be passed the DispatchQueue used as the delegateQueue's underlyingQueue as rootQueue.")
  118. self.session = session
  119. self.delegate = delegate
  120. self.rootQueue = rootQueue
  121. self.startRequestsImmediately = startRequestsImmediately
  122. self.requestQueue = requestQueue ?? DispatchQueue(label: "\(rootQueue.label).requestQueue", target: rootQueue)
  123. self.serializationQueue = serializationQueue ?? DispatchQueue(label: "\(rootQueue.label).serializationQueue", target: rootQueue)
  124. self.interceptor = interceptor
  125. self.serverTrustManager = serverTrustManager
  126. self.redirectHandler = redirectHandler
  127. self.cachedResponseHandler = cachedResponseHandler
  128. eventMonitor = CompositeEventMonitor(monitors: defaultEventMonitors + eventMonitors)
  129. delegate.eventMonitor = eventMonitor
  130. delegate.stateProvider = self
  131. }
  132. /// Creates a `Session` from a `URLSessionConfiguration`.
  133. ///
  134. /// - Note: This initializer lets Alamofire handle the creation of the underlying `URLSession` and its
  135. /// `delegateQueue`, and is the recommended initializer for most uses.
  136. ///
  137. /// - Parameters:
  138. /// - configuration: `URLSessionConfiguration` to be used to create the underlying `URLSession`. Changes
  139. /// to this value after being passed to this initializer will have no effect.
  140. /// `URLSessionConfiguration.af.default` by default.
  141. /// - delegate: `SessionDelegate` that handles `session`'s delegate callbacks as well as `Request`
  142. /// interaction. `SessionDelegate()` by default.
  143. /// - rootQueue: Root `DispatchQueue` for all internal callbacks and state updates. **MUST** be a
  144. /// serial queue. `DispatchQueue(label: "org.alamofire.session.rootQueue")` by default.
  145. /// - startRequestsImmediately: Determines whether this instance will automatically start all `Request`s. `true`
  146. /// by default. If set to `false`, all `Request`s created must have `.resume()` called.
  147. /// on them for them to start.
  148. /// - requestQueue: `DispatchQueue` on which to perform `URLRequest` creation. By default this queue
  149. /// will use the `rootQueue` as its `target`. A separate queue can be used if it's
  150. /// determined request creation is a bottleneck, but that should only be done after
  151. /// careful testing and profiling. `nil` by default.
  152. /// - serializationQueue: `DispatchQueue` on which to perform all response serialization. By default this
  153. /// queue will use the `rootQueue` as its `target`. A separate queue can be used if
  154. /// it's determined response serialization is a bottleneck, but that should only be
  155. /// done after careful testing and profiling. `nil` by default.
  156. /// - interceptor: `RequestInterceptor` to be used for all `Request`s created by this instance. `nil`
  157. /// by default.
  158. /// - serverTrustManager: `ServerTrustManager` to be used for all trust evaluations by this instance. `nil`
  159. /// by default.
  160. /// - redirectHandler: `RedirectHandler` to be used by all `Request`s created by this instance. `nil` by
  161. /// default.
  162. /// - cachedResponseHandler: `CachedResponseHandler` to be used by all `Request`s created by this instance.
  163. /// `nil` by default.
  164. /// - eventMonitors: Additional `EventMonitor`s used by the instance. Alamofire always adds a
  165. /// `AlamofireNotifications` `EventMonitor` to the array passed here. `[]` by default.
  166. public convenience init(configuration: URLSessionConfiguration = URLSessionConfiguration.af.default,
  167. delegate: SessionDelegate = SessionDelegate(),
  168. rootQueue: DispatchQueue = DispatchQueue(label: "org.alamofire.session.rootQueue"),
  169. startRequestsImmediately: Bool = true,
  170. requestQueue: DispatchQueue? = nil,
  171. serializationQueue: DispatchQueue? = nil,
  172. interceptor: RequestInterceptor? = nil,
  173. serverTrustManager: ServerTrustManager? = nil,
  174. redirectHandler: RedirectHandler? = nil,
  175. cachedResponseHandler: CachedResponseHandler? = nil,
  176. eventMonitors: [EventMonitor] = []) {
  177. precondition(configuration.identifier == nil, "Alamofire does not support background URLSessionConfigurations.")
  178. // Retarget the incoming rootQueue for safety, unless it's the main queue, which we know is safe.
  179. let serialRootQueue = (rootQueue === DispatchQueue.main) ? rootQueue : DispatchQueue(label: rootQueue.label,
  180. target: rootQueue)
  181. let delegateQueue = OperationQueue(maxConcurrentOperationCount: 1, underlyingQueue: serialRootQueue, name: "\(serialRootQueue.label).sessionDelegate")
  182. let session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: delegateQueue)
  183. self.init(session: session,
  184. delegate: delegate,
  185. rootQueue: serialRootQueue,
  186. startRequestsImmediately: startRequestsImmediately,
  187. requestQueue: requestQueue,
  188. serializationQueue: serializationQueue,
  189. interceptor: interceptor,
  190. serverTrustManager: serverTrustManager,
  191. redirectHandler: redirectHandler,
  192. cachedResponseHandler: cachedResponseHandler,
  193. eventMonitors: eventMonitors)
  194. }
  195. deinit {
  196. finishRequestsForDeinit()
  197. session.invalidateAndCancel()
  198. }
  199. // MARK: - All Requests API
  200. /// Perform an action on all active `Request`s.
  201. ///
  202. /// - Note: The provided `action` closure is performed asynchronously, meaning that some `Request`s may complete and
  203. /// be unavailable by time it runs. Additionally, this action is performed on the instances's `rootQueue`,
  204. /// so care should be taken that actions are fast. Once the work on the `Request`s is complete, any
  205. /// additional work should be performed on another queue.
  206. ///
  207. /// - Parameters:
  208. /// - action: Closure to perform with all `Request`s.
  209. public func withAllRequests(perform action: @escaping (Set<Request>) -> Void) {
  210. rootQueue.async {
  211. action(self.activeRequests)
  212. }
  213. }
  214. /// Cancel all active `Request`s, optionally calling a completion handler when complete.
  215. ///
  216. /// - Note: This is an asynchronous operation and does not block the creation of future `Request`s. Cancelled
  217. /// `Request`s may not cancel immediately due internal work, and may not cancel at all if they are close to
  218. /// completion when cancelled.
  219. ///
  220. /// - Parameters:
  221. /// - queue: `DispatchQueue` on which the completion handler is run. `.main` by default.
  222. /// - completion: Closure to be called when all `Request`s have been cancelled.
  223. public func cancelAllRequests(completingOnQueue queue: DispatchQueue = .main, completion: (() -> Void)? = nil) {
  224. withAllRequests { requests in
  225. requests.forEach { $0.cancel() }
  226. queue.async {
  227. completion?()
  228. }
  229. }
  230. }
  231. // MARK: - DataRequest
  232. /// Closure which provides a `URLRequest` for mutation.
  233. public typealias RequestModifier = (inout URLRequest) throws -> Void
  234. struct RequestConvertible: URLRequestConvertible {
  235. let url: URLConvertible
  236. let method: HTTPMethod
  237. let parameters: Parameters?
  238. let encoding: ParameterEncoding
  239. let headers: HTTPHeaders?
  240. let requestModifier: RequestModifier?
  241. func asURLRequest() throws -> URLRequest {
  242. var request = try URLRequest(url: url, method: method, headers: headers)
  243. try requestModifier?(&request)
  244. return try encoding.encode(request, with: parameters)
  245. }
  246. }
  247. /// Creates a `DataRequest` from a `URLRequest` created using the passed components and a `RequestInterceptor`.
  248. ///
  249. /// - Parameters:
  250. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  251. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  252. /// - parameters: `Parameters` (a.k.a. `[String: Any]`) value to be encoded into the `URLRequest`. `nil` by
  253. /// default.
  254. /// - encoding: `ParameterEncoding` to be used to encode the `parameters` value into the `URLRequest`.
  255. /// `URLEncoding.default` by default.
  256. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  257. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  258. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the provided
  259. /// parameters. `nil` by default.
  260. ///
  261. /// - Returns: The created `DataRequest`.
  262. open func request(_ convertible: URLConvertible,
  263. method: HTTPMethod = .get,
  264. parameters: Parameters? = nil,
  265. encoding: ParameterEncoding = URLEncoding.default,
  266. headers: HTTPHeaders? = nil,
  267. interceptor: RequestInterceptor? = nil,
  268. requestModifier: RequestModifier? = nil) -> DataRequest {
  269. let convertible = RequestConvertible(url: convertible,
  270. method: method,
  271. parameters: parameters,
  272. encoding: encoding,
  273. headers: headers,
  274. requestModifier: requestModifier)
  275. return request(convertible, interceptor: interceptor)
  276. }
  277. struct RequestEncodableConvertible<Parameters: Encodable>: URLRequestConvertible {
  278. let url: URLConvertible
  279. let method: HTTPMethod
  280. let parameters: Parameters?
  281. let encoder: ParameterEncoder
  282. let headers: HTTPHeaders?
  283. let requestModifier: RequestModifier?
  284. func asURLRequest() throws -> URLRequest {
  285. var request = try URLRequest(url: url, method: method, headers: headers)
  286. try requestModifier?(&request)
  287. return try parameters.map { try encoder.encode($0, into: request) } ?? request
  288. }
  289. }
  290. /// Creates a `DataRequest` from a `URLRequest` created using the passed components, `Encodable` parameters, and a
  291. /// `RequestInterceptor`.
  292. ///
  293. /// - Parameters:
  294. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  295. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  296. /// - parameters: `Encodable` value to be encoded into the `URLRequest`. `nil` by default.
  297. /// - encoder: `ParameterEncoder` to be used to encode the `parameters` value into the `URLRequest`.
  298. /// `URLEncodedFormParameterEncoder.default` by default.
  299. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  300. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  301. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from
  302. /// the provided parameters. `nil` by default.
  303. ///
  304. /// - Returns: The created `DataRequest`.
  305. open func request<Parameters: Encodable>(_ convertible: URLConvertible,
  306. method: HTTPMethod = .get,
  307. parameters: Parameters? = nil,
  308. encoder: ParameterEncoder = URLEncodedFormParameterEncoder.default,
  309. headers: HTTPHeaders? = nil,
  310. interceptor: RequestInterceptor? = nil,
  311. requestModifier: RequestModifier? = nil) -> DataRequest {
  312. let convertible = RequestEncodableConvertible(url: convertible,
  313. method: method,
  314. parameters: parameters,
  315. encoder: encoder,
  316. headers: headers,
  317. requestModifier: requestModifier)
  318. return request(convertible, interceptor: interceptor)
  319. }
  320. /// Creates a `DataRequest` from a `URLRequestConvertible` value and a `RequestInterceptor`.
  321. ///
  322. /// - Parameters:
  323. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  324. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  325. ///
  326. /// - Returns: The created `DataRequest`.
  327. open func request(_ convertible: URLRequestConvertible, interceptor: RequestInterceptor? = nil) -> DataRequest {
  328. let request = DataRequest(convertible: convertible,
  329. underlyingQueue: rootQueue,
  330. serializationQueue: serializationQueue,
  331. eventMonitor: eventMonitor,
  332. interceptor: interceptor,
  333. delegate: self)
  334. perform(request)
  335. return request
  336. }
  337. // MARK: - DataStreamRequest
  338. /// Creates a `DataStreamRequest` from the passed components, `Encodable` parameters, and `RequestInterceptor`.
  339. ///
  340. /// - Parameters:
  341. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  342. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  343. /// - parameters: `Encodable` value to be encoded into the `URLRequest`. `nil` by default.
  344. /// - encoder: `ParameterEncoder` to be used to encode the `parameters` value into the
  345. /// `URLRequest`.
  346. /// `URLEncodedFormParameterEncoder.default` by default.
  347. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  348. /// - automaticallyCancelOnStreamError: `Bool` indicating whether the instance should be canceled when an `Error`
  349. /// is thrown while serializing stream `Data`. `false` by default.
  350. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil`
  351. /// by default.
  352. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from
  353. /// the provided parameters. `nil` by default.
  354. ///
  355. /// - Returns: The created `DataStream` request.
  356. open func streamRequest<Parameters: Encodable>(_ convertible: URLConvertible,
  357. method: HTTPMethod = .get,
  358. parameters: Parameters? = nil,
  359. encoder: ParameterEncoder = URLEncodedFormParameterEncoder.default,
  360. headers: HTTPHeaders? = nil,
  361. automaticallyCancelOnStreamError: Bool = false,
  362. interceptor: RequestInterceptor? = nil,
  363. requestModifier: RequestModifier? = nil) -> DataStreamRequest {
  364. let convertible = RequestEncodableConvertible(url: convertible,
  365. method: method,
  366. parameters: parameters,
  367. encoder: encoder,
  368. headers: headers,
  369. requestModifier: requestModifier)
  370. return streamRequest(convertible,
  371. automaticallyCancelOnStreamError: automaticallyCancelOnStreamError,
  372. interceptor: interceptor)
  373. }
  374. /// Creates a `DataStreamRequest` from the passed components and `RequestInterceptor`.
  375. ///
  376. /// - Parameters:
  377. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  378. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  379. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  380. /// - automaticallyCancelOnStreamError: `Bool` indicating whether the instance should be canceled when an `Error`
  381. /// is thrown while serializing stream `Data`. `false` by default.
  382. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil`
  383. /// by default.
  384. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from
  385. /// the provided parameters. `nil` by default.
  386. ///
  387. /// - Returns: The created `DataStream` request.
  388. open func streamRequest(_ convertible: URLConvertible,
  389. method: HTTPMethod = .get,
  390. headers: HTTPHeaders? = nil,
  391. automaticallyCancelOnStreamError: Bool = false,
  392. interceptor: RequestInterceptor? = nil,
  393. requestModifier: RequestModifier? = nil) -> DataStreamRequest {
  394. let convertible = RequestEncodableConvertible(url: convertible,
  395. method: method,
  396. parameters: Empty?.none,
  397. encoder: URLEncodedFormParameterEncoder.default,
  398. headers: headers,
  399. requestModifier: requestModifier)
  400. return streamRequest(convertible,
  401. automaticallyCancelOnStreamError: automaticallyCancelOnStreamError,
  402. interceptor: interceptor)
  403. }
  404. /// Creates a `DataStreamRequest` from the passed `URLRequestConvertible` value and `RequestInterceptor`.
  405. ///
  406. /// - Parameters:
  407. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  408. /// - automaticallyCancelOnStreamError: `Bool` indicating whether the instance should be canceled when an `Error`
  409. /// is thrown while serializing stream `Data`. `false` by default.
  410. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil`
  411. /// by default.
  412. ///
  413. /// - Returns: The created `DataStreamRequest`.
  414. open func streamRequest(_ convertible: URLRequestConvertible,
  415. automaticallyCancelOnStreamError: Bool = false,
  416. interceptor: RequestInterceptor? = nil) -> DataStreamRequest {
  417. let request = DataStreamRequest(convertible: convertible,
  418. automaticallyCancelOnStreamError: automaticallyCancelOnStreamError,
  419. underlyingQueue: rootQueue,
  420. serializationQueue: serializationQueue,
  421. eventMonitor: eventMonitor,
  422. interceptor: interceptor,
  423. delegate: self)
  424. perform(request)
  425. return request
  426. }
  427. #if canImport(Darwin) && !canImport(FoundationNetworking)
  428. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  429. open func websocketRequest(to convertible: URLRequestConvertible,
  430. protocol: String? = nil,
  431. maximumMessageSize: Int = 1_048_576,
  432. pingInterval: TimeInterval? = nil,
  433. interceptor: RequestInterceptor? = nil) -> WebSocketRequest {
  434. let request = WebSocketRequest(convertible: convertible,
  435. protocol: `protocol`,
  436. maximumMessageSize: maximumMessageSize,
  437. pingInterval: pingInterval,
  438. underlyingQueue: rootQueue,
  439. serializationQueue: serializationQueue,
  440. eventMonitor: eventMonitor,
  441. interceptor: interceptor,
  442. delegate: self)
  443. perform(request)
  444. return request
  445. }
  446. #endif
  447. // MARK: - DownloadRequest
  448. /// Creates a `DownloadRequest` using a `URLRequest` created using the passed components, `RequestInterceptor`, and
  449. /// `Destination`.
  450. ///
  451. /// - Parameters:
  452. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  453. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  454. /// - parameters: `Parameters` (a.k.a. `[String: Any]`) value to be encoded into the `URLRequest`. `nil` by
  455. /// default.
  456. /// - encoding: `ParameterEncoding` to be used to encode the `parameters` value into the `URLRequest`.
  457. /// Defaults to `URLEncoding.default`.
  458. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  459. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  460. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the provided
  461. /// parameters. `nil` by default.
  462. /// - destination: `DownloadRequest.Destination` closure used to determine how and where the downloaded file
  463. /// should be moved. `nil` by default.
  464. ///
  465. /// - Returns: The created `DownloadRequest`.
  466. open func download(_ convertible: URLConvertible,
  467. method: HTTPMethod = .get,
  468. parameters: Parameters? = nil,
  469. encoding: ParameterEncoding = URLEncoding.default,
  470. headers: HTTPHeaders? = nil,
  471. interceptor: RequestInterceptor? = nil,
  472. requestModifier: RequestModifier? = nil,
  473. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  474. let convertible = RequestConvertible(url: convertible,
  475. method: method,
  476. parameters: parameters,
  477. encoding: encoding,
  478. headers: headers,
  479. requestModifier: requestModifier)
  480. return download(convertible, interceptor: interceptor, to: destination)
  481. }
  482. /// Creates a `DownloadRequest` from a `URLRequest` created using the passed components, `Encodable` parameters, and
  483. /// a `RequestInterceptor`.
  484. ///
  485. /// - Parameters:
  486. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  487. /// - method: `HTTPMethod` for the `URLRequest`. `.get` by default.
  488. /// - parameters: Value conforming to `Encodable` to be encoded into the `URLRequest`. `nil` by default.
  489. /// - encoder: `ParameterEncoder` to be used to encode the `parameters` value into the `URLRequest`.
  490. /// Defaults to `URLEncodedFormParameterEncoder.default`.
  491. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  492. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  493. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the provided
  494. /// parameters. `nil` by default.
  495. /// - destination: `DownloadRequest.Destination` closure used to determine how and where the downloaded file
  496. /// should be moved. `nil` by default.
  497. ///
  498. /// - Returns: The created `DownloadRequest`.
  499. open func download<Parameters: Encodable>(_ convertible: URLConvertible,
  500. method: HTTPMethod = .get,
  501. parameters: Parameters? = nil,
  502. encoder: ParameterEncoder = URLEncodedFormParameterEncoder.default,
  503. headers: HTTPHeaders? = nil,
  504. interceptor: RequestInterceptor? = nil,
  505. requestModifier: RequestModifier? = nil,
  506. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  507. let convertible = RequestEncodableConvertible(url: convertible,
  508. method: method,
  509. parameters: parameters,
  510. encoder: encoder,
  511. headers: headers,
  512. requestModifier: requestModifier)
  513. return download(convertible, interceptor: interceptor, to: destination)
  514. }
  515. /// Creates a `DownloadRequest` from a `URLRequestConvertible` value, a `RequestInterceptor`, and a `Destination`.
  516. ///
  517. /// - Parameters:
  518. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  519. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  520. /// - destination: `DownloadRequest.Destination` closure used to determine how and where the downloaded file
  521. /// should be moved. `nil` by default.
  522. ///
  523. /// - Returns: The created `DownloadRequest`.
  524. open func download(_ convertible: URLRequestConvertible,
  525. interceptor: RequestInterceptor? = nil,
  526. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  527. let request = DownloadRequest(downloadable: .request(convertible),
  528. underlyingQueue: rootQueue,
  529. serializationQueue: serializationQueue,
  530. eventMonitor: eventMonitor,
  531. interceptor: interceptor,
  532. delegate: self,
  533. destination: destination ?? DownloadRequest.defaultDestination)
  534. perform(request)
  535. return request
  536. }
  537. /// Creates a `DownloadRequest` from the `resumeData` produced from a previously cancelled `DownloadRequest`, as
  538. /// well as a `RequestInterceptor`, and a `Destination`.
  539. ///
  540. /// - Note: If `destination` is not specified, the download will be moved to a temporary location determined by
  541. /// Alamofire. The file will not be deleted until the system purges the temporary files.
  542. ///
  543. /// - 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),
  544. /// `resumeData` is broken on background URL session configurations. There's an underlying bug in the `resumeData`
  545. /// generation logic where the data is written incorrectly and will always fail to resume the download. For more
  546. /// information about the bug and possible workarounds, please refer to the [this Stack Overflow post](http://stackoverflow.com/a/39347461/1342462).
  547. ///
  548. /// - Parameters:
  549. /// - data: The resume data from a previously cancelled `DownloadRequest` or `URLSessionDownloadTask`.
  550. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  551. /// - destination: `DownloadRequest.Destination` closure used to determine how and where the downloaded file
  552. /// should be moved. `nil` by default.
  553. ///
  554. /// - Returns: The created `DownloadRequest`.
  555. open func download(resumingWith data: Data,
  556. interceptor: RequestInterceptor? = nil,
  557. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  558. let request = DownloadRequest(downloadable: .resumeData(data),
  559. underlyingQueue: rootQueue,
  560. serializationQueue: serializationQueue,
  561. eventMonitor: eventMonitor,
  562. interceptor: interceptor,
  563. delegate: self,
  564. destination: destination ?? DownloadRequest.defaultDestination)
  565. perform(request)
  566. return request
  567. }
  568. // MARK: - UploadRequest
  569. struct ParameterlessRequestConvertible: URLRequestConvertible {
  570. let url: URLConvertible
  571. let method: HTTPMethod
  572. let headers: HTTPHeaders?
  573. let requestModifier: RequestModifier?
  574. func asURLRequest() throws -> URLRequest {
  575. var request = try URLRequest(url: url, method: method, headers: headers)
  576. try requestModifier?(&request)
  577. return request
  578. }
  579. }
  580. struct Upload: UploadConvertible {
  581. let request: URLRequestConvertible
  582. let uploadable: UploadableConvertible
  583. func createUploadable() throws -> UploadRequest.Uploadable {
  584. try uploadable.createUploadable()
  585. }
  586. func asURLRequest() throws -> URLRequest {
  587. try request.asURLRequest()
  588. }
  589. }
  590. // MARK: Data
  591. /// Creates an `UploadRequest` for the given `Data`, `URLRequest` components, and `RequestInterceptor`.
  592. ///
  593. /// - Parameters:
  594. /// - data: The `Data` to upload.
  595. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  596. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  597. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  598. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  599. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  600. /// default.
  601. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the provided
  602. /// parameters. `nil` by default.
  603. ///
  604. /// - Returns: The created `UploadRequest`.
  605. open func upload(_ data: Data,
  606. to convertible: URLConvertible,
  607. method: HTTPMethod = .post,
  608. headers: HTTPHeaders? = nil,
  609. interceptor: RequestInterceptor? = nil,
  610. fileManager: FileManager = .default,
  611. requestModifier: RequestModifier? = nil) -> UploadRequest {
  612. let convertible = ParameterlessRequestConvertible(url: convertible,
  613. method: method,
  614. headers: headers,
  615. requestModifier: requestModifier)
  616. return upload(data, with: convertible, interceptor: interceptor, fileManager: fileManager)
  617. }
  618. /// Creates an `UploadRequest` for the given `Data` using the `URLRequestConvertible` value and `RequestInterceptor`.
  619. ///
  620. /// - Parameters:
  621. /// - data: The `Data` to upload.
  622. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  623. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  624. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  625. /// default.
  626. ///
  627. /// - Returns: The created `UploadRequest`.
  628. open func upload(_ data: Data,
  629. with convertible: URLRequestConvertible,
  630. interceptor: RequestInterceptor? = nil,
  631. fileManager: FileManager = .default) -> UploadRequest {
  632. upload(.data(data), with: convertible, interceptor: interceptor, fileManager: fileManager)
  633. }
  634. // MARK: File
  635. /// Creates an `UploadRequest` for the file at the given file `URL`, using a `URLRequest` from the provided
  636. /// components and `RequestInterceptor`.
  637. ///
  638. /// - Parameters:
  639. /// - fileURL: The `URL` of the file to upload.
  640. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  641. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  642. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  643. /// - interceptor: `RequestInterceptor` value to be used by the returned `UploadRequest`. `nil` by default.
  644. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  645. /// default.
  646. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the provided
  647. /// parameters. `nil` by default.
  648. ///
  649. /// - Returns: The created `UploadRequest`.
  650. open func upload(_ fileURL: URL,
  651. to convertible: URLConvertible,
  652. method: HTTPMethod = .post,
  653. headers: HTTPHeaders? = nil,
  654. interceptor: RequestInterceptor? = nil,
  655. fileManager: FileManager = .default,
  656. requestModifier: RequestModifier? = nil) -> UploadRequest {
  657. let convertible = ParameterlessRequestConvertible(url: convertible,
  658. method: method,
  659. headers: headers,
  660. requestModifier: requestModifier)
  661. return upload(fileURL, with: convertible, interceptor: interceptor, fileManager: fileManager)
  662. }
  663. /// Creates an `UploadRequest` for the file at the given file `URL` using the `URLRequestConvertible` value and
  664. /// `RequestInterceptor`.
  665. ///
  666. /// - Parameters:
  667. /// - fileURL: The `URL` of the file to upload.
  668. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  669. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  670. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  671. /// default.
  672. ///
  673. /// - Returns: The created `UploadRequest`.
  674. open func upload(_ fileURL: URL,
  675. with convertible: URLRequestConvertible,
  676. interceptor: RequestInterceptor? = nil,
  677. fileManager: FileManager = .default) -> UploadRequest {
  678. upload(.file(fileURL, shouldRemove: false), with: convertible, interceptor: interceptor, fileManager: fileManager)
  679. }
  680. // MARK: InputStream
  681. /// Creates an `UploadRequest` from the `InputStream` provided using a `URLRequest` from the provided components and
  682. /// `RequestInterceptor`.
  683. ///
  684. /// - Parameters:
  685. /// - stream: The `InputStream` that provides the data to upload.
  686. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  687. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  688. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  689. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  690. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  691. /// default.
  692. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the provided
  693. /// parameters. `nil` by default.
  694. ///
  695. /// - Returns: The created `UploadRequest`.
  696. open func upload(_ stream: InputStream,
  697. to convertible: URLConvertible,
  698. method: HTTPMethod = .post,
  699. headers: HTTPHeaders? = nil,
  700. interceptor: RequestInterceptor? = nil,
  701. fileManager: FileManager = .default,
  702. requestModifier: RequestModifier? = nil) -> UploadRequest {
  703. let convertible = ParameterlessRequestConvertible(url: convertible,
  704. method: method,
  705. headers: headers,
  706. requestModifier: requestModifier)
  707. return upload(stream, with: convertible, interceptor: interceptor, fileManager: fileManager)
  708. }
  709. /// Creates an `UploadRequest` from the provided `InputStream` using the `URLRequestConvertible` value and
  710. /// `RequestInterceptor`.
  711. ///
  712. /// - Parameters:
  713. /// - stream: The `InputStream` that provides the data to upload.
  714. /// - convertible: `URLRequestConvertible` value to be used to create the `URLRequest`.
  715. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  716. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  717. /// default.
  718. ///
  719. /// - Returns: The created `UploadRequest`.
  720. open func upload(_ stream: InputStream,
  721. with convertible: URLRequestConvertible,
  722. interceptor: RequestInterceptor? = nil,
  723. fileManager: FileManager = .default) -> UploadRequest {
  724. upload(.stream(stream), with: convertible, interceptor: interceptor, fileManager: fileManager)
  725. }
  726. // MARK: MultipartFormData
  727. /// Creates an `UploadRequest` for the multipart form data built using a closure and sent using the provided
  728. /// `URLRequest` components and `RequestInterceptor`.
  729. ///
  730. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cumulative
  731. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  732. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  733. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  734. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  735. /// used for larger payloads such as video content.
  736. ///
  737. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  738. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  739. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  740. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  741. /// technique was used.
  742. ///
  743. /// - Parameters:
  744. /// - multipartFormData: `MultipartFormData` building closure.
  745. /// - url: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  746. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  747. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  748. /// default.
  749. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  750. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  751. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  752. /// - fileManager: `FileManager` to be used if the form data exceeds the memory threshold and is
  753. /// written to disk before being uploaded. `.default` instance by default.
  754. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the
  755. /// provided parameters. `nil` by default.
  756. ///
  757. /// - Returns: The created `UploadRequest`.
  758. open func upload(multipartFormData: @escaping (MultipartFormData) -> Void,
  759. to url: URLConvertible,
  760. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  761. method: HTTPMethod = .post,
  762. headers: HTTPHeaders? = nil,
  763. interceptor: RequestInterceptor? = nil,
  764. fileManager: FileManager = .default,
  765. requestModifier: RequestModifier? = nil) -> UploadRequest {
  766. let convertible = ParameterlessRequestConvertible(url: url,
  767. method: method,
  768. headers: headers,
  769. requestModifier: requestModifier)
  770. let formData = MultipartFormData(fileManager: fileManager)
  771. multipartFormData(formData)
  772. return upload(multipartFormData: formData,
  773. with: convertible,
  774. usingThreshold: encodingMemoryThreshold,
  775. interceptor: interceptor,
  776. fileManager: fileManager)
  777. }
  778. /// Creates an `UploadRequest` using a `MultipartFormData` building closure, the provided `URLRequestConvertible`
  779. /// value, and a `RequestInterceptor`.
  780. ///
  781. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cumulative
  782. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  783. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  784. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  785. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  786. /// used for larger payloads such as video content.
  787. ///
  788. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  789. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  790. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  791. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  792. /// technique was used.
  793. ///
  794. /// - Parameters:
  795. /// - multipartFormData: `MultipartFormData` building closure.
  796. /// - request: `URLRequestConvertible` value to be used to create the `URLRequest`.
  797. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  798. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  799. /// default.
  800. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  801. /// - fileManager: `FileManager` to be used if the form data exceeds the memory threshold and is
  802. /// written to disk before being uploaded. `.default` instance by default.
  803. ///
  804. /// - Returns: The created `UploadRequest`.
  805. open func upload(multipartFormData: @escaping (MultipartFormData) -> Void,
  806. with request: URLRequestConvertible,
  807. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  808. interceptor: RequestInterceptor? = nil,
  809. fileManager: FileManager = .default) -> UploadRequest {
  810. let formData = MultipartFormData(fileManager: fileManager)
  811. multipartFormData(formData)
  812. return upload(multipartFormData: formData,
  813. with: request,
  814. usingThreshold: encodingMemoryThreshold,
  815. interceptor: interceptor,
  816. fileManager: fileManager)
  817. }
  818. /// Creates an `UploadRequest` for the prebuilt `MultipartFormData` value using the provided `URLRequest` components
  819. /// and `RequestInterceptor`.
  820. ///
  821. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cumulative
  822. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  823. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  824. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  825. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  826. /// used for larger payloads such as video content.
  827. ///
  828. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  829. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  830. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  831. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  832. /// technique was used.
  833. ///
  834. /// - Parameters:
  835. /// - multipartFormData: `MultipartFormData` instance to upload.
  836. /// - url: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  837. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  838. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  839. /// default.
  840. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  841. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  842. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  843. /// - fileManager: `FileManager` to be used if the form data exceeds the memory threshold and is
  844. /// written to disk before being uploaded. `.default` instance by default.
  845. /// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the
  846. /// provided parameters. `nil` by default.
  847. ///
  848. /// - Returns: The created `UploadRequest`.
  849. open func upload(multipartFormData: MultipartFormData,
  850. to url: URLConvertible,
  851. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  852. method: HTTPMethod = .post,
  853. headers: HTTPHeaders? = nil,
  854. interceptor: RequestInterceptor? = nil,
  855. fileManager: FileManager = .default,
  856. requestModifier: RequestModifier? = nil) -> UploadRequest {
  857. let convertible = ParameterlessRequestConvertible(url: url,
  858. method: method,
  859. headers: headers,
  860. requestModifier: requestModifier)
  861. let multipartUpload = MultipartUpload(encodingMemoryThreshold: encodingMemoryThreshold,
  862. request: convertible,
  863. multipartFormData: multipartFormData)
  864. return upload(multipartUpload, interceptor: interceptor, fileManager: fileManager)
  865. }
  866. /// Creates an `UploadRequest` for the prebuilt `MultipartFormData` value using the providing `URLRequestConvertible`
  867. /// value and `RequestInterceptor`.
  868. ///
  869. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cumulative
  870. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  871. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  872. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  873. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  874. /// used for larger payloads such as video content.
  875. ///
  876. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  877. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  878. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  879. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  880. /// technique was used.
  881. ///
  882. /// - Parameters:
  883. /// - multipartFormData: `MultipartFormData` instance to upload.
  884. /// - request: `URLRequestConvertible` value to be used to create the `URLRequest`.
  885. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  886. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  887. /// default.
  888. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  889. /// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
  890. /// default.
  891. ///
  892. /// - Returns: The created `UploadRequest`.
  893. open func upload(multipartFormData: MultipartFormData,
  894. with request: URLRequestConvertible,
  895. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  896. interceptor: RequestInterceptor? = nil,
  897. fileManager: FileManager = .default) -> UploadRequest {
  898. let multipartUpload = MultipartUpload(encodingMemoryThreshold: encodingMemoryThreshold,
  899. request: request,
  900. multipartFormData: multipartFormData)
  901. return upload(multipartUpload, interceptor: interceptor, fileManager: fileManager)
  902. }
  903. // MARK: - Internal API
  904. // MARK: Uploadable
  905. func upload(_ uploadable: UploadRequest.Uploadable,
  906. with convertible: URLRequestConvertible,
  907. interceptor: RequestInterceptor?,
  908. fileManager: FileManager) -> UploadRequest {
  909. let uploadable = Upload(request: convertible, uploadable: uploadable)
  910. return upload(uploadable, interceptor: interceptor, fileManager: fileManager)
  911. }
  912. func upload(_ upload: UploadConvertible, interceptor: RequestInterceptor?, fileManager: FileManager) -> UploadRequest {
  913. let request = UploadRequest(convertible: upload,
  914. underlyingQueue: rootQueue,
  915. serializationQueue: serializationQueue,
  916. eventMonitor: eventMonitor,
  917. interceptor: interceptor,
  918. fileManager: fileManager,
  919. delegate: self)
  920. perform(request)
  921. return request
  922. }
  923. // MARK: Perform
  924. /// Starts performing the provided `Request`.
  925. ///
  926. /// - Parameter request: The `Request` to perform.
  927. func perform(_ request: Request) {
  928. rootQueue.async {
  929. guard !request.isCancelled else { return }
  930. self.activeRequests.insert(request)
  931. self.requestQueue.async {
  932. // Leaf types must come first, otherwise they will cast as their superclass.
  933. switch request {
  934. case let r as UploadRequest: self.performUploadRequest(r) // UploadRequest must come before DataRequest due to subtype relationship.
  935. case let r as DataRequest: self.performDataRequest(r)
  936. case let r as DownloadRequest: self.performDownloadRequest(r)
  937. case let r as DataStreamRequest: self.performDataStreamRequest(r)
  938. default:
  939. #if canImport(Darwin) && !canImport(FoundationNetworking)
  940. if #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *),
  941. let request = request as? WebSocketRequest {
  942. self.performWebSocketRequest(request)
  943. } else {
  944. fatalError("Attempted to perform unsupported Request subclass: \(type(of: request))")
  945. }
  946. #else
  947. fatalError("Attempted to perform unsupported Request subclass: \(type(of: request))")
  948. #endif
  949. }
  950. }
  951. }
  952. }
  953. func performDataRequest(_ request: DataRequest) {
  954. dispatchPrecondition(condition: .onQueue(requestQueue))
  955. performSetupOperations(for: request, convertible: request.convertible)
  956. }
  957. func performDataStreamRequest(_ request: DataStreamRequest) {
  958. dispatchPrecondition(condition: .onQueue(requestQueue))
  959. performSetupOperations(for: request, convertible: request.convertible)
  960. }
  961. #if canImport(Darwin) && !canImport(FoundationNetworking)
  962. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  963. func performWebSocketRequest(_ request: WebSocketRequest) {
  964. dispatchPrecondition(condition: .onQueue(requestQueue))
  965. performSetupOperations(for: request, convertible: request.convertible)
  966. }
  967. #endif
  968. func performUploadRequest(_ request: UploadRequest) {
  969. dispatchPrecondition(condition: .onQueue(requestQueue))
  970. performSetupOperations(for: request, convertible: request.convertible) {
  971. do {
  972. let uploadable = try request.upload.createUploadable()
  973. self.rootQueue.async { request.didCreateUploadable(uploadable) }
  974. return true
  975. } catch {
  976. self.rootQueue.async { request.didFailToCreateUploadable(with: error.asAFError(or: .createUploadableFailed(error: error))) }
  977. return false
  978. }
  979. }
  980. }
  981. func performDownloadRequest(_ request: DownloadRequest) {
  982. dispatchPrecondition(condition: .onQueue(requestQueue))
  983. switch request.downloadable {
  984. case let .request(convertible):
  985. performSetupOperations(for: request, convertible: convertible)
  986. case let .resumeData(resumeData):
  987. rootQueue.async { self.didReceiveResumeData(resumeData, for: request) }
  988. }
  989. }
  990. func performSetupOperations(for request: Request,
  991. convertible: URLRequestConvertible,
  992. shouldCreateTask: @escaping () -> Bool = { true }) {
  993. dispatchPrecondition(condition: .onQueue(requestQueue))
  994. let initialRequest: URLRequest
  995. do {
  996. initialRequest = try convertible.asURLRequest()
  997. try initialRequest.validate()
  998. } catch {
  999. rootQueue.async { request.didFailToCreateURLRequest(with: error.asAFError(or: .createURLRequestFailed(error: error))) }
  1000. return
  1001. }
  1002. rootQueue.async { request.didCreateInitialURLRequest(initialRequest) }
  1003. guard !request.isCancelled else { return }
  1004. guard let adapter = adapter(for: request) else {
  1005. guard shouldCreateTask() else { return }
  1006. rootQueue.async { self.didCreateURLRequest(initialRequest, for: request) }
  1007. return
  1008. }
  1009. let adapterState = RequestAdapterState(requestID: request.id, session: self)
  1010. adapter.adapt(initialRequest, using: adapterState) { result in
  1011. do {
  1012. let adaptedRequest = try result.get()
  1013. try adaptedRequest.validate()
  1014. self.rootQueue.async { request.didAdaptInitialRequest(initialRequest, to: adaptedRequest) }
  1015. guard shouldCreateTask() else { return }
  1016. self.rootQueue.async { self.didCreateURLRequest(adaptedRequest, for: request) }
  1017. } catch {
  1018. self.rootQueue.async { request.didFailToAdaptURLRequest(initialRequest, withError: .requestAdaptationFailed(error: error)) }
  1019. }
  1020. }
  1021. }
  1022. // MARK: - Task Handling
  1023. func didCreateURLRequest(_ urlRequest: URLRequest, for request: Request) {
  1024. dispatchPrecondition(condition: .onQueue(rootQueue))
  1025. request.didCreateURLRequest(urlRequest)
  1026. guard !request.isCancelled else { return }
  1027. let task = request.task(for: urlRequest, using: session)
  1028. requestTaskMap[request] = task
  1029. request.didCreateTask(task)
  1030. updateStatesForTask(task, request: request)
  1031. }
  1032. func didReceiveResumeData(_ data: Data, for request: DownloadRequest) {
  1033. dispatchPrecondition(condition: .onQueue(rootQueue))
  1034. guard !request.isCancelled else { return }
  1035. let task = request.task(forResumeData: data, using: session)
  1036. requestTaskMap[request] = task
  1037. request.didCreateTask(task)
  1038. updateStatesForTask(task, request: request)
  1039. }
  1040. func updateStatesForTask(_ task: URLSessionTask, request: Request) {
  1041. dispatchPrecondition(condition: .onQueue(rootQueue))
  1042. request.withState { state in
  1043. switch state {
  1044. case .initialized, .finished:
  1045. // Do nothing.
  1046. break
  1047. case .resumed:
  1048. task.resume()
  1049. rootQueue.async { request.didResumeTask(task) }
  1050. case .suspended:
  1051. task.suspend()
  1052. rootQueue.async { request.didSuspendTask(task) }
  1053. case .cancelled:
  1054. // Resume to ensure metrics are gathered.
  1055. task.resume()
  1056. task.cancel()
  1057. rootQueue.async { request.didCancelTask(task) }
  1058. }
  1059. }
  1060. }
  1061. // MARK: - Adapters and Retriers
  1062. func adapter(for request: Request) -> RequestAdapter? {
  1063. if let requestInterceptor = request.interceptor, let sessionInterceptor = interceptor {
  1064. return Interceptor(adapters: [requestInterceptor, sessionInterceptor])
  1065. } else {
  1066. return request.interceptor ?? interceptor
  1067. }
  1068. }
  1069. func retrier(for request: Request) -> RequestRetrier? {
  1070. if let requestInterceptor = request.interceptor, let sessionInterceptor = interceptor {
  1071. return Interceptor(retriers: [requestInterceptor, sessionInterceptor])
  1072. } else {
  1073. return request.interceptor ?? interceptor
  1074. }
  1075. }
  1076. // MARK: - Invalidation
  1077. func finishRequestsForDeinit() {
  1078. requestTaskMap.requests.forEach { request in
  1079. rootQueue.async {
  1080. request.finish(error: AFError.sessionDeinitialized)
  1081. }
  1082. }
  1083. }
  1084. }
  1085. // MARK: - RequestDelegate
  1086. extension Session: RequestDelegate {
  1087. public var sessionConfiguration: URLSessionConfiguration {
  1088. session.configuration
  1089. }
  1090. public var startImmediately: Bool { startRequestsImmediately }
  1091. public func cleanup(after request: Request) {
  1092. activeRequests.remove(request)
  1093. }
  1094. public func retryResult(for request: Request, dueTo error: AFError, completion: @escaping (RetryResult) -> Void) {
  1095. guard let retrier = retrier(for: request) else {
  1096. rootQueue.async { completion(.doNotRetry) }
  1097. return
  1098. }
  1099. retrier.retry(request, for: self, dueTo: error) { retryResult in
  1100. self.rootQueue.async {
  1101. guard let retryResultError = retryResult.error else { completion(retryResult); return }
  1102. let retryError = AFError.requestRetryFailed(retryError: retryResultError, originalError: error)
  1103. completion(.doNotRetryWithError(retryError))
  1104. }
  1105. }
  1106. }
  1107. public func retryRequest(_ request: Request, withDelay timeDelay: TimeInterval?) {
  1108. rootQueue.async {
  1109. let retry: () -> Void = {
  1110. guard !request.isCancelled else { return }
  1111. request.prepareForRetry()
  1112. self.perform(request)
  1113. }
  1114. if let retryDelay = timeDelay {
  1115. self.rootQueue.after(retryDelay) { retry() }
  1116. } else {
  1117. retry()
  1118. }
  1119. }
  1120. }
  1121. }
  1122. // MARK: - SessionStateProvider
  1123. extension Session: SessionStateProvider {
  1124. func request(for task: URLSessionTask) -> Request? {
  1125. dispatchPrecondition(condition: .onQueue(rootQueue))
  1126. return requestTaskMap[task]
  1127. }
  1128. func didGatherMetricsForTask(_ task: URLSessionTask) {
  1129. dispatchPrecondition(condition: .onQueue(rootQueue))
  1130. let didDisassociate = requestTaskMap.disassociateIfNecessaryAfterGatheringMetricsForTask(task)
  1131. if didDisassociate {
  1132. waitingCompletions[task]?()
  1133. waitingCompletions[task] = nil
  1134. }
  1135. }
  1136. func didCompleteTask(_ task: URLSessionTask, completion: @escaping () -> Void) {
  1137. dispatchPrecondition(condition: .onQueue(rootQueue))
  1138. let didDisassociate = requestTaskMap.disassociateIfNecessaryAfterCompletingTask(task)
  1139. if didDisassociate {
  1140. completion()
  1141. } else {
  1142. waitingCompletions[task] = completion
  1143. }
  1144. }
  1145. func credential(for task: URLSessionTask, in protectionSpace: URLProtectionSpace) -> URLCredential? {
  1146. dispatchPrecondition(condition: .onQueue(rootQueue))
  1147. return requestTaskMap[task]?.credential ??
  1148. session.configuration.urlCredentialStorage?.defaultCredential(for: protectionSpace)
  1149. }
  1150. func cancelRequestsForSessionInvalidation(with error: Error?) {
  1151. dispatchPrecondition(condition: .onQueue(rootQueue))
  1152. requestTaskMap.requests.forEach { $0.finish(error: AFError.sessionInvalidated(error: error)) }
  1153. }
  1154. }