SessionManager.swift 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. //
  2. // SessionManager.swift
  3. //
  4. // Copyright (c) 2014-2017 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. /// Responsible for creating and managing `Request` objects, as well as their underlying `NSURLSession`.
  26. open class SessionManager {
  27. // MARK: - Helper Types
  28. /// Defines whether the `MultipartFormData` encoding was successful and contains result of the encoding as
  29. /// associated values.
  30. ///
  31. /// - Success: Represents a successful `MultipartFormData` encoding and contains the new `UploadRequest` along with
  32. /// streaming information.
  33. /// - Failure: Used to represent a failure in the `MultipartFormData` encoding and also contains the encoding
  34. /// error.
  35. public enum MultipartFormDataEncodingResult {
  36. case success(request: UploadRequest, streamingFromDisk: Bool, streamFileURL: URL?)
  37. case failure(Error)
  38. }
  39. // MARK: - Properties
  40. /// A default instance of `SessionManager`, used by top-level Alamofire request methods, and suitable for use
  41. /// directly for any ad hoc requests.
  42. open static let `default`: SessionManager = {
  43. let configuration = URLSessionConfiguration.default
  44. configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
  45. return SessionManager(configuration: configuration)
  46. }()
  47. /// Creates default values for the "Accept-Encoding", "Accept-Language" and "User-Agent" headers.
  48. open static let defaultHTTPHeaders: HTTPHeaders = {
  49. // Accept-Encoding HTTP Header; see https://tools.ietf.org/html/rfc7230#section-4.2.3
  50. let acceptEncoding: String = {
  51. let encodings: [String]
  52. if #available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *) {
  53. encodings = ["br", "gzip", "deflate"]
  54. } else {
  55. encodings = ["gzip", "deflate"]
  56. }
  57. return encodings.enumerated().map { (index, encoding) in
  58. let quality = 1.0 - (Double(index) * 0.1)
  59. return "\(encoding);q=\(quality)"
  60. }.joined(separator: ", ")
  61. }()
  62. // Accept-Language HTTP Header; see https://tools.ietf.org/html/rfc7231#section-5.3.5
  63. let acceptLanguage = Locale.preferredLanguages.prefix(6).enumerated().map { index, languageCode in
  64. let quality = 1.0 - (Double(index) * 0.1)
  65. return "\(languageCode);q=\(quality)"
  66. }.joined(separator: ", ")
  67. // User-Agent Header; see https://tools.ietf.org/html/rfc7231#section-5.5.3
  68. // Example: `iOS Example/1.0 (org.alamofire.iOS-Example; build:1; iOS 10.0.0) Alamofire/4.0.0`
  69. let userAgent: String = {
  70. if let info = Bundle.main.infoDictionary {
  71. let executable = info[kCFBundleExecutableKey as String] as? String ?? "Unknown"
  72. let bundle = info[kCFBundleIdentifierKey as String] as? String ?? "Unknown"
  73. let appVersion = info["CFBundleShortVersionString"] as? String ?? "Unknown"
  74. let appBuild = info[kCFBundleVersionKey as String] as? String ?? "Unknown"
  75. let osNameVersion: String = {
  76. let version = ProcessInfo.processInfo.operatingSystemVersion
  77. let versionString = "\(version.majorVersion).\(version.minorVersion).\(version.patchVersion)"
  78. let osName: String = {
  79. #if os(iOS)
  80. return "iOS"
  81. #elseif os(watchOS)
  82. return "watchOS"
  83. #elseif os(tvOS)
  84. return "tvOS"
  85. #elseif os(macOS)
  86. return "OS X"
  87. #elseif os(Linux)
  88. return "Linux"
  89. #else
  90. return "Unknown"
  91. #endif
  92. }()
  93. return "\(osName) \(versionString)"
  94. }()
  95. let alamofireVersion: String = {
  96. guard
  97. let afInfo = Bundle(for: SessionManager.self).infoDictionary,
  98. let build = afInfo["CFBundleShortVersionString"]
  99. else { return "Unknown" }
  100. return "Alamofire/\(build)"
  101. }()
  102. return "\(executable)/\(appVersion) (\(bundle); build:\(appBuild); \(osNameVersion)) \(alamofireVersion)"
  103. }
  104. return "Alamofire"
  105. }()
  106. return [
  107. "Accept-Encoding": acceptEncoding,
  108. "Accept-Language": acceptLanguage,
  109. "User-Agent": userAgent
  110. ]
  111. }()
  112. /// Default memory threshold used when encoding `MultipartFormData` in bytes.
  113. open static let multipartFormDataEncodingMemoryThreshold: UInt64 = 10_000_000
  114. /// The underlying session.
  115. open let session: URLSession
  116. /// The session delegate handling all the task and session delegate callbacks.
  117. open let delegate: SessionDelegate
  118. /// Whether to start requests immediately after being constructed. `true` by default.
  119. open var startRequestsImmediately: Bool = true
  120. /// The request adapter called each time a new request is created.
  121. open var adapter: RequestAdapter?
  122. /// The request retrier called each time a request encounters an error to determine whether to retry the request.
  123. open var retrier: RequestRetrier? {
  124. get { return delegate.retrier }
  125. set { delegate.retrier = newValue }
  126. }
  127. /// The background completion handler closure provided by the UIApplicationDelegate
  128. /// `application:handleEventsForBackgroundURLSession:completionHandler:` method. By setting the background
  129. /// completion handler, the SessionDelegate `sessionDidFinishEventsForBackgroundURLSession` closure implementation
  130. /// will automatically call the handler.
  131. ///
  132. /// If you need to handle your own events before the handler is called, then you need to override the
  133. /// SessionDelegate `sessionDidFinishEventsForBackgroundURLSession` and manually call the handler when finished.
  134. ///
  135. /// `nil` by default.
  136. open var backgroundCompletionHandler: (() -> Void)?
  137. let queue = DispatchQueue(label: "org.alamofire.session-manager." + UUID().uuidString)
  138. // MARK: - Lifecycle
  139. /// Creates an instance with the specified `configuration`, `delegate` and `serverTrustManager`.
  140. ///
  141. /// - parameter configuration: The configuration used to construct the managed session.
  142. /// `URLSessionConfiguration.default` by default.
  143. /// - parameter delegate: The delegate used when initializing the session. `SessionDelegate()` by
  144. /// default.
  145. /// - parameter serverTrustManager: The server trust policy manager to use for evaluating all server trust
  146. /// challenges. `nil` by default.
  147. ///
  148. /// - returns: The new `SessionManager` instance.
  149. public init(
  150. configuration: URLSessionConfiguration = URLSessionConfiguration.default,
  151. delegate: SessionDelegate = SessionDelegate(),
  152. serverTrustManager: ServerTrustManager? = nil)
  153. {
  154. self.delegate = delegate
  155. self.session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
  156. commonInit(serverTrustManager: serverTrustManager)
  157. }
  158. /// Creates an instance with the specified `session`, `delegate` and `serverTrustManager`.
  159. ///
  160. /// - parameter session: The URL session.
  161. /// - parameter delegate: The delegate of the URL session. Must equal the URL session's delegate.
  162. /// - parameter serverTrustManager: The server trust policy manager to use for evaluating all server trust
  163. /// challenges. `nil` by default.
  164. ///
  165. /// - returns: The new `SessionManager` instance if the URL session's delegate matches; `nil` otherwise.
  166. public init?(
  167. session: URLSession,
  168. delegate: SessionDelegate,
  169. serverTrustManager: ServerTrustManager? = nil)
  170. {
  171. guard delegate === session.delegate else { return nil }
  172. self.delegate = delegate
  173. self.session = session
  174. commonInit(serverTrustManager: serverTrustManager)
  175. }
  176. private func commonInit(serverTrustManager: ServerTrustManager?) {
  177. session.serverTrustManager = serverTrustManager
  178. delegate.sessionManager = self
  179. delegate.sessionDidFinishEventsForBackgroundURLSession = { [weak self] session in
  180. guard let strongSelf = self else { return }
  181. DispatchQueue.main.async { strongSelf.backgroundCompletionHandler?() }
  182. }
  183. }
  184. deinit {
  185. session.invalidateAndCancel()
  186. }
  187. // MARK: - Data Request
  188. /// Creates a `DataRequest` to retrieve the contents of the specified `url`, `method`, `parameters`, `encoding`
  189. /// and `headers`.
  190. ///
  191. /// - parameter url: The URL.
  192. /// - parameter method: The HTTP method. `.get` by default.
  193. /// - parameter parameters: The parameters. `nil` by default.
  194. /// - parameter encoding: The parameter encoding. `URLEncoding.default` by default.
  195. /// - parameter headers: The HTTP headers. `nil` by default.
  196. ///
  197. /// - returns: The created `DataRequest`.
  198. @discardableResult
  199. open func request(
  200. _ url: URLConvertible,
  201. method: HTTPMethod = .get,
  202. parameters: Parameters? = nil,
  203. encoding: ParameterEncoding = URLEncoding.default,
  204. headers: HTTPHeaders? = nil)
  205. -> DataRequest
  206. {
  207. var originalRequest: URLRequest?
  208. do {
  209. originalRequest = try URLRequest(url: url, method: method, headers: headers)
  210. let encodedURLRequest = try encoding.encode(originalRequest!, with: parameters)
  211. return request(encodedURLRequest)
  212. } catch {
  213. return request(originalRequest, failedWith: error)
  214. }
  215. }
  216. /// Creates a `DataRequest` to retrieve the contents of a URL based on the specified `urlRequest`.
  217. ///
  218. /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  219. ///
  220. /// - parameter urlRequest: The URL request.
  221. ///
  222. /// - returns: The created `DataRequest`.
  223. @discardableResult
  224. open func request(_ urlRequest: URLRequestConvertible) -> DataRequest {
  225. var originalRequest: URLRequest?
  226. do {
  227. originalRequest = try urlRequest.asURLRequest()
  228. let originalTask = DataRequest.Requestable(urlRequest: originalRequest!)
  229. let task = try originalTask.task(session: session, adapter: adapter, queue: queue)
  230. let request = DataRequest(session: session, requestTask: .data(originalTask, task))
  231. delegate[task] = request
  232. if startRequestsImmediately { request.resume() }
  233. return request
  234. } catch {
  235. return request(originalRequest, failedWith: error)
  236. }
  237. }
  238. // MARK: Private - Request Implementation
  239. private func request(_ urlRequest: URLRequest?, failedWith error: Error) -> DataRequest {
  240. var requestTask: Request.RequestTask = .data(nil, nil)
  241. if let urlRequest = urlRequest {
  242. let originalTask = DataRequest.Requestable(urlRequest: urlRequest)
  243. requestTask = .data(originalTask, nil)
  244. }
  245. let underlyingError = error.underlyingAdaptError ?? error
  246. let request = DataRequest(session: session, requestTask: requestTask, error: underlyingError)
  247. if let retrier = retrier, error is AdaptError {
  248. allowRetrier(retrier, toRetry: request, with: underlyingError)
  249. } else {
  250. if startRequestsImmediately { request.resume() }
  251. }
  252. return request
  253. }
  254. // MARK: - Download Request
  255. // MARK: URL Request
  256. /// Creates a `DownloadRequest` to retrieve the contents the specified `url`, `method`, `parameters`, `encoding`,
  257. /// `headers` and save them to the `destination`.
  258. ///
  259. /// If `destination` is not specified, the contents will remain in the temporary location determined by the
  260. /// underlying URL session.
  261. ///
  262. /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  263. ///
  264. /// - parameter url: The URL.
  265. /// - parameter method: The HTTP method. `.get` by default.
  266. /// - parameter parameters: The parameters. `nil` by default.
  267. /// - parameter encoding: The parameter encoding. `URLEncoding.default` by default.
  268. /// - parameter headers: The HTTP headers. `nil` by default.
  269. /// - parameter destination: The closure used to determine the destination of the downloaded file. `nil` by default.
  270. ///
  271. /// - returns: The created `DownloadRequest`.
  272. @discardableResult
  273. open func download(
  274. _ url: URLConvertible,
  275. method: HTTPMethod = .get,
  276. parameters: Parameters? = nil,
  277. encoding: ParameterEncoding = URLEncoding.default,
  278. headers: HTTPHeaders? = nil,
  279. to destination: DownloadRequest.DownloadFileDestination? = nil)
  280. -> DownloadRequest
  281. {
  282. do {
  283. let urlRequest = try URLRequest(url: url, method: method, headers: headers)
  284. let encodedURLRequest = try encoding.encode(urlRequest, with: parameters)
  285. return download(encodedURLRequest, to: destination)
  286. } catch {
  287. return download(nil, to: destination, failedWith: error)
  288. }
  289. }
  290. /// Creates a `DownloadRequest` to retrieve the contents of a URL based on the specified `urlRequest` and save
  291. /// them to the `destination`.
  292. ///
  293. /// If `destination` is not specified, the contents will remain in the temporary location determined by the
  294. /// underlying URL session.
  295. ///
  296. /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  297. ///
  298. /// - parameter urlRequest: The URL request
  299. /// - parameter destination: The closure used to determine the destination of the downloaded file. `nil` by default.
  300. ///
  301. /// - returns: The created `DownloadRequest`.
  302. @discardableResult
  303. open func download(
  304. _ urlRequest: URLRequestConvertible,
  305. to destination: DownloadRequest.DownloadFileDestination? = nil)
  306. -> DownloadRequest
  307. {
  308. do {
  309. let urlRequest = try urlRequest.asURLRequest()
  310. return download(.request(urlRequest), to: destination)
  311. } catch {
  312. return download(nil, to: destination, failedWith: error)
  313. }
  314. }
  315. // MARK: Resume Data
  316. /// Creates a `DownloadRequest` from the `resumeData` produced from a previous request cancellation to retrieve
  317. /// the contents of the original request and save them to the `destination`.
  318. ///
  319. /// If `destination` is not specified, the contents will remain in the temporary location determined by the
  320. /// underlying URL session.
  321. ///
  322. /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  323. ///
  324. /// On the latest release of all the Apple platforms (iOS 10, macOS 10.12, tvOS 10, watchOS 3), `resumeData` is broken
  325. /// on background URL session configurations. There's an underlying bug in the `resumeData` generation logic where the
  326. /// data is written incorrectly and will always fail to resume the download. For more information about the bug and
  327. /// possible workarounds, please refer to the following Stack Overflow post:
  328. ///
  329. /// - http://stackoverflow.com/a/39347461/1342462
  330. ///
  331. /// - parameter resumeData: The resume data. This is an opaque data blob produced by `URLSessionDownloadTask`
  332. /// when a task is cancelled. See `URLSession -downloadTask(withResumeData:)` for
  333. /// additional information.
  334. /// - parameter destination: The closure used to determine the destination of the downloaded file. `nil` by default.
  335. ///
  336. /// - returns: The created `DownloadRequest`.
  337. @discardableResult
  338. open func download(
  339. resumingWith resumeData: Data,
  340. to destination: DownloadRequest.DownloadFileDestination? = nil)
  341. -> DownloadRequest
  342. {
  343. return download(.resumeData(resumeData), to: destination)
  344. }
  345. // MARK: Private - Download Implementation
  346. private func download(
  347. _ downloadable: DownloadRequest.Downloadable,
  348. to destination: DownloadRequest.DownloadFileDestination?)
  349. -> DownloadRequest
  350. {
  351. do {
  352. let task = try downloadable.task(session: session, adapter: adapter, queue: queue)
  353. let download = DownloadRequest(session: session, requestTask: .download(downloadable, task))
  354. download.downloadDelegate.destination = destination
  355. delegate[task] = download
  356. if startRequestsImmediately { download.resume() }
  357. return download
  358. } catch {
  359. return download(downloadable, to: destination, failedWith: error)
  360. }
  361. }
  362. private func download(
  363. _ downloadable: DownloadRequest.Downloadable?,
  364. to destination: DownloadRequest.DownloadFileDestination?,
  365. failedWith error: Error)
  366. -> DownloadRequest
  367. {
  368. var downloadTask: Request.RequestTask = .download(nil, nil)
  369. if let downloadable = downloadable {
  370. downloadTask = .download(downloadable, nil)
  371. }
  372. let underlyingError = error.underlyingAdaptError ?? error
  373. let download = DownloadRequest(session: session, requestTask: downloadTask, error: underlyingError)
  374. download.downloadDelegate.destination = destination
  375. if let retrier = retrier, error is AdaptError {
  376. allowRetrier(retrier, toRetry: download, with: underlyingError)
  377. } else {
  378. if startRequestsImmediately { download.resume() }
  379. }
  380. return download
  381. }
  382. // MARK: - Upload Request
  383. // MARK: File
  384. /// Creates an `UploadRequest` from the specified `url`, `method` and `headers` for uploading the `file`.
  385. ///
  386. /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  387. ///
  388. /// - parameter file: The file to upload.
  389. /// - parameter url: The URL.
  390. /// - parameter method: The HTTP method. `.post` by default.
  391. /// - parameter headers: The HTTP headers. `nil` by default.
  392. ///
  393. /// - returns: The created `UploadRequest`.
  394. @discardableResult
  395. open func upload(
  396. _ fileURL: URL,
  397. to url: URLConvertible,
  398. method: HTTPMethod = .post,
  399. headers: HTTPHeaders? = nil)
  400. -> UploadRequest
  401. {
  402. do {
  403. let urlRequest = try URLRequest(url: url, method: method, headers: headers)
  404. return upload(fileURL, with: urlRequest)
  405. } catch {
  406. return upload(nil, failedWith: error)
  407. }
  408. }
  409. /// Creates a `UploadRequest` from the specified `urlRequest` for uploading the `file`.
  410. ///
  411. /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  412. ///
  413. /// - parameter file: The file to upload.
  414. /// - parameter urlRequest: The URL request.
  415. ///
  416. /// - returns: The created `UploadRequest`.
  417. @discardableResult
  418. open func upload(_ fileURL: URL, with urlRequest: URLRequestConvertible) -> UploadRequest {
  419. do {
  420. let urlRequest = try urlRequest.asURLRequest()
  421. return upload(.file(fileURL, urlRequest))
  422. } catch {
  423. return upload(nil, failedWith: error)
  424. }
  425. }
  426. // MARK: Data
  427. /// Creates an `UploadRequest` from the specified `url`, `method` and `headers` for uploading the `data`.
  428. ///
  429. /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  430. ///
  431. /// - parameter data: The data to upload.
  432. /// - parameter url: The URL.
  433. /// - parameter method: The HTTP method. `.post` by default.
  434. /// - parameter headers: The HTTP headers. `nil` by default.
  435. ///
  436. /// - returns: The created `UploadRequest`.
  437. @discardableResult
  438. open func upload(
  439. _ data: Data,
  440. to url: URLConvertible,
  441. method: HTTPMethod = .post,
  442. headers: HTTPHeaders? = nil)
  443. -> UploadRequest
  444. {
  445. do {
  446. let urlRequest = try URLRequest(url: url, method: method, headers: headers)
  447. return upload(data, with: urlRequest)
  448. } catch {
  449. return upload(nil, failedWith: error)
  450. }
  451. }
  452. /// Creates an `UploadRequest` from the specified `urlRequest` for uploading the `data`.
  453. ///
  454. /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  455. ///
  456. /// - parameter data: The data to upload.
  457. /// - parameter urlRequest: The URL request.
  458. ///
  459. /// - returns: The created `UploadRequest`.
  460. @discardableResult
  461. open func upload(_ data: Data, with urlRequest: URLRequestConvertible) -> UploadRequest {
  462. do {
  463. let urlRequest = try urlRequest.asURLRequest()
  464. return upload(.data(data, urlRequest))
  465. } catch {
  466. return upload(nil, failedWith: error)
  467. }
  468. }
  469. // MARK: InputStream
  470. /// Creates an `UploadRequest` from the specified `url`, `method` and `headers` for uploading the `stream`.
  471. ///
  472. /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  473. ///
  474. /// - parameter stream: The stream to upload.
  475. /// - parameter url: The URL.
  476. /// - parameter method: The HTTP method. `.post` by default.
  477. /// - parameter headers: The HTTP headers. `nil` by default.
  478. ///
  479. /// - returns: The created `UploadRequest`.
  480. @discardableResult
  481. open func upload(
  482. _ stream: InputStream,
  483. to url: URLConvertible,
  484. method: HTTPMethod = .post,
  485. headers: HTTPHeaders? = nil)
  486. -> UploadRequest
  487. {
  488. do {
  489. let urlRequest = try URLRequest(url: url, method: method, headers: headers)
  490. return upload(stream, with: urlRequest)
  491. } catch {
  492. return upload(nil, failedWith: error)
  493. }
  494. }
  495. /// Creates an `UploadRequest` from the specified `urlRequest` for uploading the `stream`.
  496. ///
  497. /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  498. ///
  499. /// - parameter stream: The stream to upload.
  500. /// - parameter urlRequest: The URL request.
  501. ///
  502. /// - returns: The created `UploadRequest`.
  503. @discardableResult
  504. open func upload(_ stream: InputStream, with urlRequest: URLRequestConvertible) -> UploadRequest {
  505. do {
  506. let urlRequest = try urlRequest.asURLRequest()
  507. return upload(.stream(stream, urlRequest))
  508. } catch {
  509. return upload(nil, failedWith: error)
  510. }
  511. }
  512. // MARK: MultipartFormData
  513. /// Encodes `multipartFormData` using `encodingMemoryThreshold` and calls `encodingCompletion` with new
  514. /// `UploadRequest` using the `url`, `method` and `headers`.
  515. ///
  516. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cummulative
  517. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  518. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  519. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  520. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  521. /// used for larger payloads such as video content.
  522. ///
  523. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  524. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  525. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  526. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  527. /// technique was used.
  528. ///
  529. /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  530. ///
  531. /// - parameter multipartFormData: The closure used to append body parts to the `MultipartFormData`.
  532. /// - parameter encodingMemoryThreshold: The encoding memory threshold in bytes.
  533. /// `multipartFormDataEncodingMemoryThreshold` by default.
  534. /// - parameter url: The URL.
  535. /// - parameter method: The HTTP method. `.post` by default.
  536. /// - parameter headers: The HTTP headers. `nil` by default.
  537. /// - parameter encodingCompletion: The closure called when the `MultipartFormData` encoding is complete.
  538. open func upload(
  539. multipartFormData: @escaping (MultipartFormData) -> Void,
  540. usingThreshold encodingMemoryThreshold: UInt64 = SessionManager.multipartFormDataEncodingMemoryThreshold,
  541. to url: URLConvertible,
  542. method: HTTPMethod = .post,
  543. headers: HTTPHeaders? = nil,
  544. encodingCompletion: ((MultipartFormDataEncodingResult) -> Void)?)
  545. {
  546. do {
  547. let urlRequest = try URLRequest(url: url, method: method, headers: headers)
  548. return upload(
  549. multipartFormData: multipartFormData,
  550. usingThreshold: encodingMemoryThreshold,
  551. with: urlRequest,
  552. encodingCompletion: encodingCompletion
  553. )
  554. } catch {
  555. DispatchQueue.main.async { encodingCompletion?(.failure(error)) }
  556. }
  557. }
  558. /// Encodes `multipartFormData` using `encodingMemoryThreshold` and calls `encodingCompletion` with new
  559. /// `UploadRequest` using the `urlRequest`.
  560. ///
  561. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cummulative
  562. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  563. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  564. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  565. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  566. /// used for larger payloads such as video content.
  567. ///
  568. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  569. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  570. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  571. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  572. /// technique was used.
  573. ///
  574. /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  575. ///
  576. /// - parameter multipartFormData: The closure used to append body parts to the `MultipartFormData`.
  577. /// - parameter encodingMemoryThreshold: The encoding memory threshold in bytes.
  578. /// `multipartFormDataEncodingMemoryThreshold` by default.
  579. /// - parameter urlRequest: The URL request.
  580. /// - parameter encodingCompletion: The closure called when the `MultipartFormData` encoding is complete.
  581. open func upload(
  582. multipartFormData: @escaping (MultipartFormData) -> Void,
  583. usingThreshold encodingMemoryThreshold: UInt64 = SessionManager.multipartFormDataEncodingMemoryThreshold,
  584. with urlRequest: URLRequestConvertible,
  585. encodingCompletion: ((MultipartFormDataEncodingResult) -> Void)?)
  586. {
  587. DispatchQueue.global(qos: .utility).async {
  588. let formData = MultipartFormData()
  589. multipartFormData(formData)
  590. var tempFileURL: URL?
  591. do {
  592. var urlRequestWithContentType = try urlRequest.asURLRequest()
  593. urlRequestWithContentType.setValue(formData.contentType, forHTTPHeaderField: "Content-Type")
  594. let isBackgroundSession = self.session.configuration.identifier != nil
  595. if formData.contentLength < encodingMemoryThreshold && !isBackgroundSession {
  596. let data = try formData.encode()
  597. let encodingResult = MultipartFormDataEncodingResult.success(
  598. request: self.upload(data, with: urlRequestWithContentType),
  599. streamingFromDisk: false,
  600. streamFileURL: nil
  601. )
  602. DispatchQueue.main.async { encodingCompletion?(encodingResult) }
  603. } else {
  604. let fileManager = FileManager.default
  605. let tempDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory())
  606. let directoryURL = tempDirectoryURL.appendingPathComponent("org.alamofire.manager/multipart.form.data")
  607. let fileName = UUID().uuidString
  608. let fileURL = directoryURL.appendingPathComponent(fileName)
  609. tempFileURL = fileURL
  610. var directoryError: Error?
  611. // Create directory inside serial queue to ensure two threads don't do this in parallel
  612. self.queue.sync {
  613. do {
  614. try fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
  615. } catch {
  616. directoryError = error
  617. }
  618. }
  619. if let directoryError = directoryError { throw directoryError }
  620. try formData.writeEncodedData(to: fileURL)
  621. let upload = self.upload(fileURL, with: urlRequestWithContentType)
  622. // Cleanup the temp file once the upload is complete
  623. upload.delegate.queue.addOperation {
  624. do {
  625. try FileManager.default.removeItem(at: fileURL)
  626. } catch {
  627. // No-op
  628. }
  629. }
  630. DispatchQueue.main.async {
  631. let encodingResult = MultipartFormDataEncodingResult.success(
  632. request: upload,
  633. streamingFromDisk: true,
  634. streamFileURL: fileURL
  635. )
  636. encodingCompletion?(encodingResult)
  637. }
  638. }
  639. } catch {
  640. // Cleanup the temp file in the event that the multipart form data encoding failed
  641. if let tempFileURL = tempFileURL {
  642. do {
  643. try FileManager.default.removeItem(at: tempFileURL)
  644. } catch {
  645. // No-op
  646. }
  647. }
  648. DispatchQueue.main.async { encodingCompletion?(.failure(error)) }
  649. }
  650. }
  651. }
  652. // MARK: Private - Upload Implementation
  653. private func upload(_ uploadable: UploadRequest.Uploadable) -> UploadRequest {
  654. do {
  655. let task = try uploadable.task(session: session, adapter: adapter, queue: queue)
  656. let upload = UploadRequest(session: session, requestTask: .upload(uploadable, task))
  657. if case let .stream(inputStream, _) = uploadable {
  658. upload.delegate.taskNeedNewBodyStream = { _, _ in inputStream }
  659. }
  660. delegate[task] = upload
  661. if startRequestsImmediately { upload.resume() }
  662. return upload
  663. } catch {
  664. return upload(uploadable, failedWith: error)
  665. }
  666. }
  667. private func upload(_ uploadable: UploadRequest.Uploadable?, failedWith error: Error) -> UploadRequest {
  668. var uploadTask: Request.RequestTask = .upload(nil, nil)
  669. if let uploadable = uploadable {
  670. uploadTask = .upload(uploadable, nil)
  671. }
  672. let underlyingError = error.underlyingAdaptError ?? error
  673. let upload = UploadRequest(session: session, requestTask: uploadTask, error: underlyingError)
  674. if let retrier = retrier, error is AdaptError {
  675. allowRetrier(retrier, toRetry: upload, with: underlyingError)
  676. } else {
  677. if startRequestsImmediately { upload.resume() }
  678. }
  679. return upload
  680. }
  681. #if !os(watchOS)
  682. // MARK: - Stream Request
  683. // MARK: Hostname and Port
  684. /// Creates a `StreamRequest` for bidirectional streaming using the `hostname` and `port`.
  685. ///
  686. /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  687. ///
  688. /// - parameter hostName: The hostname of the server to connect to.
  689. /// - parameter port: The port of the server to connect to.
  690. ///
  691. /// - returns: The created `StreamRequest`.
  692. @discardableResult
  693. open func stream(withHostName hostName: String, port: Int) -> StreamRequest {
  694. return stream(.stream(hostName: hostName, port: port))
  695. }
  696. // MARK: NetService
  697. /// Creates a `StreamRequest` for bidirectional streaming using the `netService`.
  698. ///
  699. /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  700. ///
  701. /// - parameter netService: The net service used to identify the endpoint.
  702. ///
  703. /// - returns: The created `StreamRequest`.
  704. @discardableResult
  705. open func stream(with netService: NetService) -> StreamRequest {
  706. return stream(.netService(netService))
  707. }
  708. // MARK: Private - Stream Implementation
  709. private func stream(_ streamable: StreamRequest.Streamable) -> StreamRequest {
  710. do {
  711. let task = try streamable.task(session: session, adapter: adapter, queue: queue)
  712. let request = StreamRequest(session: session, requestTask: .stream(streamable, task))
  713. delegate[task] = request
  714. if startRequestsImmediately { request.resume() }
  715. return request
  716. } catch {
  717. return stream(failedWith: error)
  718. }
  719. }
  720. private func stream(failedWith error: Error) -> StreamRequest {
  721. let stream = StreamRequest(session: session, requestTask: .stream(nil, nil), error: error)
  722. if startRequestsImmediately { stream.resume() }
  723. return stream
  724. }
  725. #endif
  726. // MARK: - Internal - Retry Request
  727. func retry(_ request: Request) -> Bool {
  728. guard let originalTask = request.originalTask else { return false }
  729. do {
  730. let task = try originalTask.task(session: session, adapter: adapter, queue: queue)
  731. request.delegate.task = task // resets all task delegate data
  732. request.retryCount += 1
  733. request.startTime = CFAbsoluteTimeGetCurrent()
  734. request.endTime = nil
  735. task.resume()
  736. return true
  737. } catch {
  738. request.delegate.error = error.underlyingAdaptError ?? error
  739. return false
  740. }
  741. }
  742. private func allowRetrier(_ retrier: RequestRetrier, toRetry request: Request, with error: Error) {
  743. DispatchQueue.utility.async { [weak self] in
  744. guard let strongSelf = self else { return }
  745. retrier.should(strongSelf, retry: request, with: error) { shouldRetry, timeDelay in
  746. guard let strongSelf = self else { return }
  747. guard shouldRetry else {
  748. if strongSelf.startRequestsImmediately { request.resume() }
  749. return
  750. }
  751. DispatchQueue.utility.after(timeDelay) {
  752. guard let strongSelf = self else { return }
  753. let retrySucceeded = strongSelf.retry(request)
  754. if retrySucceeded, let task = request.task {
  755. strongSelf.delegate[task] = request
  756. } else {
  757. if strongSelf.startRequestsImmediately { request.resume() }
  758. }
  759. }
  760. }
  761. }
  762. }
  763. }