Session.swift 71 KB

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