Session.swift 69 KB

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