DataRequest.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. //
  2. // DataRequest.swift
  3. //
  4. // Copyright (c) 2014-2024 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. /// `Request` subclass which handles in-memory `Data` download using `URLSessionDataTask`.
  26. public class DataRequest: Request, @unchecked Sendable {
  27. /// `URLRequestConvertible` value used to create `URLRequest`s for this instance.
  28. public let convertible: any URLRequestConvertible
  29. /// `Data` read from the server so far.
  30. public var data: Data? { dataMutableState.read(\.data) }
  31. private struct DataMutableState {
  32. var data: Data?
  33. var httpResponseHandler: (queue: DispatchQueue,
  34. handler: @Sendable (_ response: HTTPURLResponse,
  35. _ completionHandler: @escaping @Sendable (ResponseDisposition) -> Void) -> Void)?
  36. }
  37. private let dataMutableState = Protected(DataMutableState())
  38. /// Creates a `DataRequest` using the provided parameters.
  39. ///
  40. /// - Parameters:
  41. /// - id: `UUID` used for the `Hashable` and `Equatable` implementations. `UUID()` by default.
  42. /// - convertible: `URLRequestConvertible` value used to create `URLRequest`s for this instance.
  43. /// - underlyingQueue: `DispatchQueue` on which all internal `Request` work is performed.
  44. /// - serializationQueue: `DispatchQueue` on which all serialization work is performed. By default targets
  45. /// `underlyingQueue`, but can be passed another queue from a `Session`.
  46. /// - eventMonitor: `EventMonitor` called for event callbacks from internal `Request` actions.
  47. /// - interceptor: `RequestInterceptor` used throughout the request lifecycle.
  48. /// - delegate: `RequestDelegate` that provides an interface to actions not performed by the `Request`.
  49. init(id: UUID = UUID(),
  50. convertible: any URLRequestConvertible,
  51. underlyingQueue: DispatchQueue,
  52. serializationQueue: DispatchQueue,
  53. eventMonitor: (any EventMonitor)?,
  54. interceptor: (any RequestInterceptor)?,
  55. shouldAutomaticallyResume: Bool?,
  56. delegate: any RequestDelegate) {
  57. self.convertible = convertible
  58. super.init(id: id,
  59. underlyingQueue: underlyingQueue,
  60. serializationQueue: serializationQueue,
  61. eventMonitor: eventMonitor,
  62. interceptor: interceptor,
  63. shouldAutomaticallyResume: shouldAutomaticallyResume,
  64. delegate: delegate)
  65. }
  66. override func reset() {
  67. super.reset()
  68. dataMutableState.write { mutableState in
  69. mutableState.data = nil
  70. }
  71. }
  72. /// Called when `Data` is received by this instance.
  73. ///
  74. /// - Note: Also calls `updateDownloadProgress`.
  75. ///
  76. /// - Parameter data: The `Data` received.
  77. func didReceive(data: Data) {
  78. dataMutableState.write { mutableState in
  79. if mutableState.data == nil {
  80. mutableState.data = data
  81. } else {
  82. mutableState.data?.append(data)
  83. }
  84. }
  85. updateDownloadProgress()
  86. }
  87. func didReceiveResponse(_ response: HTTPURLResponse, completionHandler: @escaping @Sendable (URLSession.ResponseDisposition) -> Void) {
  88. dataMutableState.read { dataMutableState in
  89. guard let httpResponseHandler = dataMutableState.httpResponseHandler else {
  90. underlyingQueue.async { completionHandler(.allow) }
  91. return
  92. }
  93. httpResponseHandler.queue.async {
  94. httpResponseHandler.handler(response) { disposition in
  95. if disposition == .cancel {
  96. self.mutableState.write { mutableState in
  97. mutableState.state = .cancelled
  98. mutableState.error = mutableState.error ?? AFError.explicitlyCancelled
  99. }
  100. }
  101. self.underlyingQueue.async {
  102. completionHandler(disposition.sessionDisposition)
  103. }
  104. }
  105. }
  106. }
  107. }
  108. override func task(for request: URLRequest, using session: URLSession) -> URLSessionTask {
  109. let copiedRequest = request
  110. return session.dataTask(with: copiedRequest)
  111. }
  112. /// Called to update the `downloadProgress` of the instance.
  113. func updateDownloadProgress() {
  114. let totalBytesReceived = Int64(data?.count ?? 0)
  115. let totalBytesExpected = task?.response?.expectedContentLength ?? NSURLSessionTransferSizeUnknown
  116. downloadProgress.totalUnitCount = totalBytesExpected
  117. downloadProgress.completedUnitCount = totalBytesReceived
  118. downloadProgressHandler?.queue.async { self.downloadProgressHandler?.handler(self.downloadProgress) }
  119. }
  120. /// Validates the request, using the specified closure.
  121. ///
  122. /// - Note: If validation fails, subsequent calls to response handlers will have an associated error.
  123. ///
  124. /// - Parameter validation: `Validation` closure used to validate the response.
  125. ///
  126. /// - Returns: The instance.
  127. @preconcurrency
  128. @discardableResult
  129. public func validate(_ validation: @escaping Validation) -> Self {
  130. let validator: @Sendable () -> Void = { [unowned self] in
  131. guard error == nil, let response else { return }
  132. let result = validation(request, response, data)
  133. if case let .failure(error) = result {
  134. self.error = error.asAFError(or: .responseValidationFailed(reason: .customValidationFailed(error: error)))
  135. }
  136. eventMonitor?.request(self,
  137. didValidateRequest: request,
  138. response: response,
  139. data: data,
  140. withResult: result)
  141. }
  142. validators.write { $0.append(validator) }
  143. return self
  144. }
  145. /// Sets a closure called whenever the `DataRequest` produces an `HTTPURLResponse` and providing a completion
  146. /// handler to return a `ResponseDisposition` value.
  147. ///
  148. /// - Parameters:
  149. /// - queue: `DispatchQueue` on which the closure will be called. `.main` by default.
  150. /// - handler: Closure called when the instance produces an `HTTPURLResponse`. The `completionHandler` provided
  151. /// MUST be called, otherwise the request will never complete.
  152. ///
  153. /// - Returns: The instance.
  154. @_disfavoredOverload
  155. @preconcurrency
  156. @discardableResult
  157. public func onHTTPResponse(
  158. on queue: DispatchQueue = .main,
  159. perform handler: @escaping @Sendable (_ response: HTTPURLResponse,
  160. _ completionHandler: @escaping @Sendable (ResponseDisposition) -> Void) -> Void
  161. ) -> Self {
  162. dataMutableState.write { mutableState in
  163. mutableState.httpResponseHandler = (queue, handler)
  164. }
  165. return self
  166. }
  167. /// Sets a closure called whenever the `DataRequest` produces an `HTTPURLResponse`.
  168. ///
  169. /// - Parameters:
  170. /// - queue: `DispatchQueue` on which the closure will be called. `.main` by default.
  171. /// - handler: Closure called when the instance produces an `HTTPURLResponse`.
  172. ///
  173. /// - Returns: The instance.
  174. @preconcurrency
  175. @discardableResult
  176. public func onHTTPResponse(on queue: DispatchQueue = .main,
  177. perform handler: @escaping @Sendable (HTTPURLResponse) -> Void) -> Self {
  178. onHTTPResponse(on: queue) { response, completionHandler in
  179. handler(response)
  180. completionHandler(.allow)
  181. }
  182. return self
  183. }
  184. // MARK: Response Serialization
  185. /// Adds a handler to be called once the request has finished.
  186. ///
  187. /// - Parameters:
  188. /// - queue: The queue on which the completion handler is dispatched. `.main` by default.
  189. /// - completionHandler: The code to be executed once the request has finished.
  190. ///
  191. /// - Returns: The request.
  192. @preconcurrency
  193. @discardableResult
  194. public func response(queue: DispatchQueue = .main, completionHandler: @escaping @Sendable (AFDataResponse<Data?>) -> Void) -> Self {
  195. appendResponseSerializer {
  196. // Start work that should be on the serialization queue.
  197. let result = AFResult<Data?>(value: self.data, error: self.error)
  198. // End work that should be on the serialization queue.
  199. self.underlyingQueue.async {
  200. let response = DataResponse(request: self.request,
  201. response: self.response,
  202. data: self.data,
  203. metrics: self.metrics,
  204. serializationDuration: 0,
  205. result: result)
  206. self.eventMonitor?.request(self, didParseResponse: response)
  207. self.responseSerializerDidComplete { queue.async { completionHandler(response) } }
  208. }
  209. }
  210. return self
  211. }
  212. private func _response<Serializer: DataResponseSerializerProtocol>(queue: DispatchQueue = .main,
  213. responseSerializer: Serializer,
  214. completionHandler: @escaping @Sendable (AFDataResponse<Serializer.SerializedObject>) -> Void)
  215. -> Self {
  216. appendResponseSerializer {
  217. // Start work that should be on the serialization queue.
  218. let start = ProcessInfo.processInfo.systemUptime
  219. let result: AFResult<Serializer.SerializedObject> = Result {
  220. try responseSerializer.serialize(request: self.request,
  221. response: self.response,
  222. data: self.data,
  223. error: self.error)
  224. }.mapError { error in
  225. error.asAFError(or: .responseSerializationFailed(reason: .customSerializationFailed(error: error)))
  226. }
  227. let end = ProcessInfo.processInfo.systemUptime
  228. // End work that should be on the serialization queue.
  229. self.underlyingQueue.async {
  230. let response = DataResponse(request: self.request,
  231. response: self.response,
  232. data: self.data,
  233. metrics: self.metrics,
  234. serializationDuration: end - start,
  235. result: result)
  236. self.eventMonitor?.request(self, didParseResponse: response)
  237. guard !self.isCancelled, let serializerError = result.failure, let delegate = self.delegate else {
  238. self.responseSerializerDidComplete { queue.async { completionHandler(response) } }
  239. return
  240. }
  241. delegate.retryResult(for: self, dueTo: serializerError) { retryResult in
  242. var didComplete: (@Sendable () -> Void)?
  243. defer {
  244. if let didComplete {
  245. self.responseSerializerDidComplete { queue.async { didComplete() } }
  246. }
  247. }
  248. switch retryResult {
  249. case .doNotRetry:
  250. didComplete = { completionHandler(response) }
  251. case let .doNotRetryWithError(retryError):
  252. let result: AFResult<Serializer.SerializedObject> = .failure(retryError.asAFError(orFailWith: "Received retryError was not already AFError"))
  253. let response = DataResponse(request: self.request,
  254. response: self.response,
  255. data: self.data,
  256. metrics: self.metrics,
  257. serializationDuration: end - start,
  258. result: result)
  259. didComplete = { completionHandler(response) }
  260. case .retry, .retryWithDelay:
  261. delegate.retryRequest(self, withDelay: retryResult.delay)
  262. }
  263. }
  264. }
  265. }
  266. return self
  267. }
  268. /// Adds a handler to be called once the request has finished.
  269. ///
  270. /// - Parameters:
  271. /// - queue: The queue on which the completion handler is dispatched. `.main` by default
  272. /// - responseSerializer: The response serializer responsible for serializing the request, response, and data.
  273. /// - completionHandler: The code to be executed once the request has finished.
  274. ///
  275. /// - Returns: The request.
  276. @preconcurrency
  277. @discardableResult
  278. public func response<Serializer: DataResponseSerializerProtocol>(queue: DispatchQueue = .main,
  279. responseSerializer: Serializer,
  280. completionHandler: @escaping @Sendable (AFDataResponse<Serializer.SerializedObject>) -> Void)
  281. -> Self {
  282. _response(queue: queue, responseSerializer: responseSerializer, completionHandler: completionHandler)
  283. }
  284. /// Adds a handler to be called once the request has finished.
  285. ///
  286. /// - Parameters:
  287. /// - queue: The queue on which the completion handler is dispatched. `.main` by default
  288. /// - responseSerializer: The response serializer responsible for serializing the request, response, and data.
  289. /// - completionHandler: The code to be executed once the request has finished.
  290. ///
  291. /// - Returns: The request.
  292. @preconcurrency
  293. @discardableResult
  294. public func response<Serializer: ResponseSerializer>(queue: DispatchQueue = .main,
  295. responseSerializer: Serializer,
  296. completionHandler: @escaping @Sendable (AFDataResponse<Serializer.SerializedObject>) -> Void)
  297. -> Self {
  298. _response(queue: queue, responseSerializer: responseSerializer, completionHandler: completionHandler)
  299. }
  300. /// Adds a handler using a `DataResponseSerializer` to be called once the request has finished.
  301. ///
  302. /// - Parameters:
  303. /// - queue: The queue on which the completion handler is called. `.main` by default.
  304. /// - dataPreprocessor: `DataPreprocessor` which processes the received `Data` before calling the
  305. /// `completionHandler`. `PassthroughPreprocessor()` by default.
  306. /// - emptyResponseCodes: HTTP status codes for which empty responses are always valid. `[204, 205]` by default.
  307. /// - emptyRequestMethods: `HTTPMethod`s for which empty responses are always valid. `[.head]` by default.
  308. /// - completionHandler: A closure to be executed once the request has finished.
  309. ///
  310. /// - Returns: The request.
  311. @preconcurrency
  312. @discardableResult
  313. public func responseData(queue: DispatchQueue = .main,
  314. dataPreprocessor: any DataPreprocessor = DataResponseSerializer.defaultDataPreprocessor,
  315. emptyResponseCodes: Set<Int> = DataResponseSerializer.defaultEmptyResponseCodes,
  316. emptyRequestMethods: Set<HTTPMethod> = DataResponseSerializer.defaultEmptyRequestMethods,
  317. completionHandler: @escaping @Sendable (AFDataResponse<Data>) -> Void) -> Self {
  318. response(queue: queue,
  319. responseSerializer: DataResponseSerializer(dataPreprocessor: dataPreprocessor,
  320. emptyResponseCodes: emptyResponseCodes,
  321. emptyRequestMethods: emptyRequestMethods),
  322. completionHandler: completionHandler)
  323. }
  324. /// Adds a handler using a `StringResponseSerializer` to be called once the request has finished.
  325. ///
  326. /// - Parameters:
  327. /// - queue: The queue on which the completion handler is dispatched. `.main` by default.
  328. /// - dataPreprocessor: `DataPreprocessor` which processes the received `Data` before calling the
  329. /// `completionHandler`. `PassthroughPreprocessor()` by default.
  330. /// - encoding: The string encoding. Defaults to `nil`, in which case the encoding will be determined
  331. /// from the server response, falling back to the default HTTP character set, `ISO-8859-1`.
  332. /// - emptyResponseCodes: HTTP status codes for which empty responses are always valid. `[204, 205]` by default.
  333. /// - emptyRequestMethods: `HTTPMethod`s for which empty responses are always valid. `[.head]` by default.
  334. /// - completionHandler: A closure to be executed once the request has finished.
  335. ///
  336. /// - Returns: The request.
  337. @preconcurrency
  338. @discardableResult
  339. public func responseString(queue: DispatchQueue = .main,
  340. dataPreprocessor: any DataPreprocessor = StringResponseSerializer.defaultDataPreprocessor,
  341. encoding: String.Encoding? = nil,
  342. emptyResponseCodes: Set<Int> = StringResponseSerializer.defaultEmptyResponseCodes,
  343. emptyRequestMethods: Set<HTTPMethod> = StringResponseSerializer.defaultEmptyRequestMethods,
  344. completionHandler: @escaping @Sendable (AFDataResponse<String>) -> Void) -> Self {
  345. response(queue: queue,
  346. responseSerializer: StringResponseSerializer(dataPreprocessor: dataPreprocessor,
  347. encoding: encoding,
  348. emptyResponseCodes: emptyResponseCodes,
  349. emptyRequestMethods: emptyRequestMethods),
  350. completionHandler: completionHandler)
  351. }
  352. /// Adds a handler using a `JSONResponseSerializer` to be called once the request has finished.
  353. ///
  354. /// - Parameters:
  355. /// - queue: The queue on which the completion handler is dispatched. `.main` by default.
  356. /// - dataPreprocessor: `DataPreprocessor` which processes the received `Data` before calling the
  357. /// `completionHandler`. `PassthroughPreprocessor()` by default.
  358. /// - emptyResponseCodes: HTTP status codes for which empty responses are always valid. `[204, 205]` by default.
  359. /// - emptyRequestMethods: `HTTPMethod`s for which empty responses are always valid. `[.head]` by default.
  360. /// - options: `JSONSerialization.ReadingOptions` used when parsing the response. `.allowFragments`
  361. /// by default.
  362. /// - completionHandler: A closure to be executed once the request has finished.
  363. ///
  364. /// - Returns: The request.
  365. @available(*, deprecated, message: "responseJSON deprecated and will be removed in Alamofire 6. Use responseDecodable instead.")
  366. @preconcurrency
  367. @discardableResult
  368. public func responseJSON(queue: DispatchQueue = .main,
  369. dataPreprocessor: any DataPreprocessor = JSONResponseSerializer.defaultDataPreprocessor,
  370. emptyResponseCodes: Set<Int> = JSONResponseSerializer.defaultEmptyResponseCodes,
  371. emptyRequestMethods: Set<HTTPMethod> = JSONResponseSerializer.defaultEmptyRequestMethods,
  372. options: JSONSerialization.ReadingOptions = .allowFragments,
  373. completionHandler: @escaping @Sendable (AFDataResponse<Any>) -> Void) -> Self {
  374. response(queue: queue,
  375. responseSerializer: JSONResponseSerializer(dataPreprocessor: dataPreprocessor,
  376. emptyResponseCodes: emptyResponseCodes,
  377. emptyRequestMethods: emptyRequestMethods,
  378. options: options),
  379. completionHandler: completionHandler)
  380. }
  381. /// Adds a handler using a `DecodableResponseSerializer` to be called once the request has finished.
  382. ///
  383. /// - Parameters:
  384. /// - type: `Decodable` type to decode from response data.
  385. /// - queue: The queue on which the completion handler is dispatched. `.main` by default.
  386. /// - dataPreprocessor: `DataPreprocessor` which processes the received `Data` before calling the
  387. /// `completionHandler`. `PassthroughPreprocessor()` by default.
  388. /// - decoder: `DataDecoder` to use to decode the response. `JSONDecoder()` by default.
  389. /// - emptyResponseCodes: HTTP status codes for which empty responses are always valid. `[204, 205]` by default.
  390. /// - emptyRequestMethods: `HTTPMethod`s for which empty responses are always valid. `[.head]` by default.
  391. /// - completionHandler: A closure to be executed once the request has finished.
  392. ///
  393. /// - Returns: The request.
  394. @preconcurrency
  395. @discardableResult
  396. public func responseDecodable<Value>(of type: Value.Type = Value.self,
  397. queue: DispatchQueue = .main,
  398. dataPreprocessor: any DataPreprocessor = DecodableResponseSerializer<Value>.defaultDataPreprocessor,
  399. decoder: any DataDecoder = JSONDecoder(),
  400. emptyResponseCodes: Set<Int> = DecodableResponseSerializer<Value>.defaultEmptyResponseCodes,
  401. emptyRequestMethods: Set<HTTPMethod> = DecodableResponseSerializer<Value>.defaultEmptyRequestMethods,
  402. completionHandler: @escaping @Sendable (AFDataResponse<Value>) -> Void) -> Self where Value: Decodable, Value: Sendable {
  403. response(queue: queue,
  404. responseSerializer: DecodableResponseSerializer(dataPreprocessor: dataPreprocessor,
  405. decoder: decoder,
  406. emptyResponseCodes: emptyResponseCodes,
  407. emptyRequestMethods: emptyRequestMethods),
  408. completionHandler: completionHandler)
  409. }
  410. }