EventMonitor.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. //
  2. // EventMonitor.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. public protocol URLSessionEventMonitor {
  26. var queue: DispatchQueue { get }
  27. // Session
  28. func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?)
  29. // URLSessionTask
  30. func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge)
  31. func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64)
  32. func urlSession(_ session: URLSession, taskNeedsNewBodyStream task: URLSessionTask)
  33. func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest)
  34. func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics)
  35. func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
  36. @available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
  37. func urlSession(_ session: URLSession, taskIsWaitingForConnectivity task: URLSessionTask)
  38. func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)
  39. func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, willCacheResponse proposedResponse: CachedURLResponse)
  40. // Downloads
  41. func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64)
  42. func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)
  43. func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL)
  44. }
  45. public extension URLSessionEventMonitor {
  46. var queue: DispatchQueue { return .main }
  47. }
  48. public protocol RequestEventMonitor {
  49. var queue: DispatchQueue { get }
  50. func request(_ request: Request, didCreateURLRequest urlRequest: URLRequest)
  51. func request(_ request: Request, didFailToCreateURLRequestWithError error: Error)
  52. func request(_ request: Request, didAdaptInitialRequest initialRequest: URLRequest, to adaptedRequest: URLRequest)
  53. func request(_ request: Request, didFailToAdaptURLRequest initialRequest: URLRequest, withError error: Error)
  54. func request(_ request: Request, didCreateTask task: URLSessionTask)
  55. func requestDidResume(_ request: Request)
  56. func requestDidSuspend(_ request: Request)
  57. func requestDidCancel(_ request: Request)
  58. func request(_ request: Request, didGatherMetrics metrics: URLSessionTaskMetrics)
  59. func request(_ request: Request, didFailTask task: URLSessionTask, earlyWithError error: Error)
  60. func request(_ request: Request, didCompleteTask task: URLSessionTask, with error: Error?)
  61. func requestDidFinish(_ request: Request)
  62. }
  63. public extension RequestEventMonitor {
  64. var queue: DispatchQueue { return .main }
  65. }
  66. public protocol EventMonitor: URLSessionEventMonitor & RequestEventMonitor { }
  67. public extension EventMonitor {
  68. var queue: DispatchQueue { return .main }
  69. }
  70. public final class CompositeEventMonitor: EventMonitor {
  71. // TODO: Give composite its own queue?
  72. let monitors: [EventMonitor]
  73. init(monitors: [EventMonitor]) {
  74. self.monitors = monitors
  75. }
  76. func performEvent(_ event: @escaping (EventMonitor) -> Void) {
  77. for monitor in monitors {
  78. monitor.queue.async { event(monitor) }
  79. }
  80. }
  81. public func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
  82. performEvent { $0.urlSession(session, didBecomeInvalidWithError: error) }
  83. }
  84. public func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge) {
  85. performEvent { $0.urlSession(session, task: task, didReceive: challenge) }
  86. }
  87. public func urlSession(_ session: URLSession,
  88. task: URLSessionTask,
  89. didSendBodyData bytesSent: Int64,
  90. totalBytesSent: Int64,
  91. totalBytesExpectedToSend: Int64) {
  92. performEvent {
  93. $0.urlSession(session,
  94. task: task,
  95. didSendBodyData: bytesSent,
  96. totalBytesSent: totalBytesSent,
  97. totalBytesExpectedToSend: totalBytesExpectedToSend)
  98. }
  99. }
  100. public func urlSession(_ session: URLSession, taskNeedsNewBodyStream task: URLSessionTask) {
  101. performEvent {
  102. $0.urlSession(session, taskNeedsNewBodyStream: task)
  103. }
  104. }
  105. public func urlSession(_ session: URLSession,
  106. task: URLSessionTask,
  107. willPerformHTTPRedirection response: HTTPURLResponse,
  108. newRequest request: URLRequest) {
  109. performEvent {
  110. $0.urlSession(session,
  111. task: task,
  112. willPerformHTTPRedirection: response,
  113. newRequest: request)
  114. }
  115. }
  116. public func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
  117. performEvent { $0.urlSession(session, task: task, didFinishCollecting: metrics) }
  118. }
  119. public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
  120. performEvent { $0.urlSession(session, task: task, didCompleteWithError: error) }
  121. }
  122. @available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
  123. public func urlSession(_ session: URLSession, taskIsWaitingForConnectivity task: URLSessionTask) {
  124. performEvent { $0.urlSession(session, taskIsWaitingForConnectivity: task) }
  125. }
  126. public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
  127. performEvent { $0.urlSession(session, dataTask: dataTask, didReceive: data) }
  128. }
  129. public func urlSession(_ session: URLSession,
  130. dataTask: URLSessionDataTask,
  131. willCacheResponse proposedResponse: CachedURLResponse) {
  132. performEvent { $0.urlSession(session, dataTask: dataTask, willCacheResponse: proposedResponse) }
  133. }
  134. public func urlSession(_ session: URLSession,
  135. downloadTask: URLSessionDownloadTask,
  136. didResumeAtOffset fileOffset: Int64,
  137. expectedTotalBytes: Int64) {
  138. performEvent {
  139. $0.urlSession(session,
  140. downloadTask: downloadTask,
  141. didResumeAtOffset: fileOffset,
  142. expectedTotalBytes: expectedTotalBytes)
  143. }
  144. }
  145. public func urlSession(_ session: URLSession,
  146. downloadTask: URLSessionDownloadTask,
  147. didWriteData bytesWritten: Int64,
  148. totalBytesWritten: Int64,
  149. totalBytesExpectedToWrite: Int64) {
  150. performEvent {
  151. $0.urlSession(session,
  152. downloadTask: downloadTask,
  153. didWriteData: bytesWritten,
  154. totalBytesWritten: totalBytesWritten,
  155. totalBytesExpectedToWrite: totalBytesExpectedToWrite)
  156. }
  157. }
  158. public func urlSession(_ session: URLSession,
  159. downloadTask: URLSessionDownloadTask,
  160. didFinishDownloadingTo location: URL) {
  161. performEvent { $0.urlSession(session, downloadTask: downloadTask, didFinishDownloadingTo: location) }
  162. }
  163. public func request(_ request: Request, didCreateURLRequest urlRequest: URLRequest) {
  164. performEvent { $0.request(request, didCreateURLRequest: urlRequest) }
  165. }
  166. public func request(_ request: Request, didFailToCreateURLRequestWithError error: Error) {
  167. performEvent { $0.request(request, didFailToCreateURLRequestWithError: error) }
  168. }
  169. public func request(_ request: Request, didAdaptInitialRequest initialRequest: URLRequest, to adaptedRequest: URLRequest) {
  170. performEvent { $0.request(request, didAdaptInitialRequest: initialRequest, to: adaptedRequest) }
  171. }
  172. public func request(_ request: Request, didFailToAdaptURLRequest initialRequest: URLRequest, withError error: Error) {
  173. performEvent { $0.request(request, didFailToAdaptURLRequest: initialRequest, withError: error) }
  174. }
  175. public func request(_ request: Request, didCreateTask task: URLSessionTask) {
  176. performEvent { $0.request(request, didCreateTask: task) }
  177. }
  178. public func request(_ request: Request, didGatherMetrics metrics: URLSessionTaskMetrics) {
  179. performEvent { $0.request(request, didGatherMetrics: metrics) }
  180. }
  181. public func request(_ request: Request, didFailTask task: URLSessionTask, earlyWithError error: Error) {
  182. performEvent { $0.request(request, didFailTask: task, earlyWithError: error) }
  183. }
  184. public func request(_ request: Request, didCompleteTask task: URLSessionTask, with error: Error?) {
  185. performEvent { $0.request(request, didCompleteTask: task, with: error) }
  186. }
  187. public func requestDidFinish(_ request: Request) {
  188. performEvent { $0.requestDidFinish(request) }
  189. }
  190. public func requestDidResume(_ request: Request) {
  191. performEvent { $0.requestDidResume(request) }
  192. }
  193. public func requestDidSuspend(_ request: Request) {
  194. performEvent { $0.requestDidSuspend(request) }
  195. }
  196. public func requestDidCancel(_ request: Request) {
  197. performEvent { $0.requestDidCancel(request) }
  198. }
  199. }
  200. public final class NSLoggingEventMonitor: EventMonitor {
  201. public let queue = DispatchQueue(label: "org.alamofire.nsLoggingEventMonitorQueue", qos: .background)
  202. public func request(_ request: Request, didCreateURLRequest urlRequest: URLRequest) {
  203. NSLog("Request: \(request) didCreateURLRequest: \(urlRequest)")
  204. }
  205. public func request(_ request: Request, didFailToCreateURLRequestWithError error: Error) {
  206. NSLog("Request: \(request) didFailToCreateURLRequestWithError: \(error)")
  207. }
  208. public func request(_ request: Request, didAdaptInitialRequest initialRequest: URLRequest, to adaptedRequest: URLRequest) {
  209. NSLog("Request: \(request) didAdaptInitialRequest \(initialRequest) to \(adaptedRequest)")
  210. }
  211. public func request(_ request: Request, didFailToAdaptURLRequest initialRequest: URLRequest, withError error: Error) {
  212. NSLog("Request: \(request) didFailToAdaptURLRequest \(initialRequest) withError \(error)")
  213. }
  214. public func request(_ request: Request, didCreateTask task: URLSessionTask) {
  215. NSLog("Request: \(request) didCreateTask \(task)")
  216. }
  217. public func request(_ request: Request, didGatherMetrics metrics: URLSessionTaskMetrics) {
  218. NSLog("Request: \(request) didGatherMetrics \(metrics)")
  219. }
  220. public func request(_ request: Request, didFailTask task: URLSessionTask, earlyWithError error: Error) {
  221. NSLog("Request: \(request) didFailTask \(task) earlyWithError \(error)")
  222. }
  223. public func request(_ request: Request, didCompleteTask task: URLSessionTask, with error: Error?) {
  224. NSLog("Request: \(request) didCompleteTask \(task) withError: \(error?.localizedDescription ?? "None")")
  225. }
  226. public func requestDidFinish(_ request: Request) {
  227. NSLog("Request: \(request) didFinish")
  228. }
  229. public func requestDidResume(_ request: Request) {
  230. NSLog("Request: \(request) didResume")
  231. }
  232. public func requestDidSuspend(_ request: Request) {
  233. NSLog("Request: \(request) didSuspend")
  234. }
  235. public func requestDidCancel(_ request: Request) {
  236. NSLog("Request: \(request) didCancel")
  237. }
  238. public func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
  239. NSLog("URLSession: \(session), didBecomeInvalidWithError: \(error?.localizedDescription ?? "None")")
  240. }
  241. public func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge) {
  242. NSLog("URLSession: \(session), task: \(task), didReceiveChallenge: \(challenge)")
  243. }
  244. public func urlSession(_ session: URLSession,
  245. task: URLSessionTask,
  246. didSendBodyData bytesSent: Int64,
  247. totalBytesSent: Int64,
  248. totalBytesExpectedToSend: Int64) {
  249. NSLog("URLSession: \(session), task: \(task), didSendBodyData: \(bytesSent), totalBytesSent: \(totalBytesSent), totalBytesExpectedToSent: \(totalBytesExpectedToSend)")
  250. }
  251. public func urlSession(_ session: URLSession, taskNeedsNewBodyStream task: URLSessionTask) {
  252. NSLog("URLSession: \(session), taskNeedsNewBodyStream: \(task)")
  253. }
  254. public func urlSession(_ session: URLSession,
  255. task: URLSessionTask,
  256. willPerformHTTPRedirection response: HTTPURLResponse,
  257. newRequest request: URLRequest) {
  258. NSLog("URLSession: \(session), task: \(task), willPerformHTTPRedirection: \(response), newRequest: \(request)")
  259. }
  260. public func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
  261. NSLog("URLSession: \(session), task: \(task), didFinishCollecting: \(metrics)")
  262. }
  263. public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
  264. NSLog("URLSession: \(session), task: \(task), didCompleteWithError: \(error?.localizedDescription ?? "None")")
  265. }
  266. public func urlSession(_ session: URLSession, taskIsWaitingForConnectivity task: URLSessionTask) {
  267. NSLog("URLSession: \(session), taskIsWaitingForConnectivity: \(task)")
  268. }
  269. public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
  270. NSLog("URLSession: \(session), dataTask: \(dataTask), didReceiveDataOfLength: \(data.count)")
  271. }
  272. public func urlSession(_ session: URLSession,
  273. dataTask: URLSessionDataTask,
  274. willCacheResponse proposedResponse: CachedURLResponse) {
  275. NSLog("URLSession: \(session), dataTask: \(dataTask), willCacheResponse: \(proposedResponse)")
  276. }
  277. public func urlSession(_ session: URLSession,
  278. downloadTask: URLSessionDownloadTask,
  279. didResumeAtOffset fileOffset: Int64,
  280. expectedTotalBytes: Int64) {
  281. NSLog("URLSession: \(session), downloadTask: \(downloadTask), didResumeAtOffset: \(fileOffset), expectedTotalBytes: \(expectedTotalBytes)")
  282. }
  283. public func urlSession(_ session: URLSession,
  284. downloadTask: URLSessionDownloadTask,
  285. didWriteData bytesWritten: Int64,
  286. totalBytesWritten: Int64,
  287. totalBytesExpectedToWrite: Int64) {
  288. NSLog("URLSession: \(session), downloadTask: \(downloadTask), didWriteData bytesWritten: \(bytesWritten), totalBytesWritten: \(totalBytesWritten), totalBytesExpectedToWrite: \(totalBytesExpectedToWrite)")
  289. }
  290. public func urlSession(_ session: URLSession,
  291. downloadTask: URLSessionDownloadTask,
  292. didFinishDownloadingTo location: URL) {
  293. NSLog("URLSession: \(session), downloadTask: \(downloadTask), didFinishDownloadingTo: \(location)")
  294. }
  295. }
  296. public final class ClosureEventMonitor: EventMonitor {
  297. /// Overrides default behavior for URLSessionDelegate method `urlSession(_:didBecomeInvalidWithError:)`.
  298. open var sessionDidBecomeInvalidWithError: ((URLSession, Error?) -> Void)?
  299. /// Overrides default behavior for URLSessionTaskDelegate method `urlSession(_:task:didReceive:completionHandler:)`.
  300. open var taskDidReceiveChallenge: ((URLSession, URLSessionTask, URLAuthenticationChallenge) -> Void)?
  301. /// Overrides default behavior for URLSessionTaskDelegate method `urlSession(_:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:)`.
  302. open var taskDidSendBodyData: ((URLSession, URLSessionTask, Int64, Int64, Int64) -> Void)?
  303. /// Overrides default behavior for URLSessionTaskDelegate method `urlSession(_:task:needNewBodyStream:)`.
  304. open var taskNeedNewBodyStream: ((URLSession, URLSessionTask) -> Void)?
  305. /// Overrides default behavior for URLSessionTaskDelegate method `urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)`.
  306. open var taskWillPerformHTTPRedirection: ((URLSession, URLSessionTask, HTTPURLResponse, URLRequest) -> Void)?
  307. open var taskDidFinishCollectingMetrics: ((URLSession, URLSessionTask, URLSessionTaskMetrics) -> Void)?
  308. /// Overrides default behavior for URLSessionTaskDelegate method `urlSession(_:task:didCompleteWithError:)`.
  309. open var taskDidComplete: ((URLSession, URLSessionTask, Error?) -> Void)?
  310. open var taskIsWaitingForConnectivity: ((URLSession, URLSessionTask) -> Void)?
  311. /// Overrides default behavior for URLSessionDataDelegate method `urlSession(_:dataTask:didReceive:)`.
  312. open var dataTaskDidReceiveData: ((URLSession, URLSessionDataTask, Data) -> Void)?
  313. /// Overrides default behavior for URLSessionDataDelegate method `urlSession(_:dataTask:willCacheResponse:completionHandler:)`.
  314. open var dataTaskWillCacheResponse: ((URLSession, URLSessionDataTask, CachedURLResponse) -> Void)?
  315. // MARK: URLSessionDownloadDelegate Overrides
  316. /// Overrides default behavior for URLSessionDownloadDelegate method `urlSession(_:downloadTask:didFinishDownloadingTo:)`.
  317. open var downloadTaskDidFinishDownloadingToURL: ((URLSession, URLSessionDownloadTask, URL) -> Void)?
  318. /// Overrides default behavior for URLSessionDownloadDelegate method `urlSession(_:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:)`.
  319. open var downloadTaskDidWriteData: ((URLSession, URLSessionDownloadTask, Int64, Int64, Int64) -> Void)?
  320. /// Overrides default behavior for URLSessionDownloadDelegate method `urlSession(_:downloadTask:didResumeAtOffset:expectedTotalBytes:)`.
  321. open var downloadTaskDidResumeAtOffset: ((URLSession, URLSessionDownloadTask, Int64, Int64) -> Void)?
  322. open var requestDidCreateURLRequest: ((Request, URLRequest) -> Void)?
  323. open var requestDidFailToCreateURLRequestWithError: ((Request, Error) -> Void)?
  324. open var requestDidAdaptInitialRequestToAdaptedRequest: ((Request, URLRequest, URLRequest) -> Void)?
  325. open var requestDidFailToAdaptURLRequestWithError: ((Request, URLRequest, Error) -> Void)?
  326. open var requestDidCreateTask: ((Request, URLSessionTask) -> Void)?
  327. open var requestDidGatherMetrics: ((Request, URLSessionTaskMetrics) -> Void)?
  328. open var requestDidFailTaskEarlyWithError: ((Request, URLSessionTask, Error) -> Void)?
  329. open var requestDidCompleteTaskWithError: ((Request, URLSessionTask, Error?) -> Void)?
  330. open var requestDidResume: ((Request) -> Void)?
  331. open var requestDidSuspend: ((Request) -> Void)?
  332. open var requestDidCancel: ((Request) -> Void)?
  333. open var requestDidFinish: ((Request) -> Void)?
  334. public func request(_ request: Request, didCreateURLRequest urlRequest: URLRequest) {
  335. requestDidCreateURLRequest?(request, urlRequest)
  336. }
  337. public func request(_ request: Request, didFailToCreateURLRequestWithError error: Error) {
  338. requestDidFailToCreateURLRequestWithError?(request, error)
  339. }
  340. public func request(_ request: Request, didAdaptInitialRequest initialRequest: URLRequest, to adaptedRequest: URLRequest) {
  341. requestDidAdaptInitialRequestToAdaptedRequest?(request, initialRequest, adaptedRequest)
  342. }
  343. public func request(_ request: Request, didFailToAdaptURLRequest initialRequest: URLRequest, withError error: Error) {
  344. requestDidFailToAdaptURLRequestWithError?(request, initialRequest, error)
  345. }
  346. public func request(_ request: Request, didCreateTask task: URLSessionTask) {
  347. requestDidCreateTask?(request, task)
  348. }
  349. public func request(_ request: Request, didGatherMetrics metrics: URLSessionTaskMetrics) {
  350. requestDidGatherMetrics?(request, metrics)
  351. }
  352. public func request(_ request: Request, didFailTask task: URLSessionTask, earlyWithError error: Error) {
  353. requestDidFailTaskEarlyWithError?(request, task, error)
  354. }
  355. public func request(_ request: Request, didCompleteTask task: URLSessionTask, with error: Error?) {
  356. requestDidCompleteTaskWithError?(request, task, error)
  357. }
  358. public func requestDidFinish(_ request: Request) {
  359. requestDidFinish?(request)
  360. }
  361. public func requestDidResume(_ request: Request) {
  362. requestDidResume?(request)
  363. }
  364. public func requestDidSuspend(_ request: Request) {
  365. requestDidSuspend?(request)
  366. }
  367. public func requestDidCancel(_ request: Request) {
  368. requestDidCancel?(request)
  369. }
  370. public func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
  371. sessionDidBecomeInvalidWithError?(session, error)
  372. }
  373. public func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge) {
  374. taskDidReceiveChallenge?(session, task, challenge)
  375. }
  376. public func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
  377. taskDidSendBodyData?(session, task, bytesSent, totalBytesSent, totalBytesExpectedToSend)
  378. }
  379. public func urlSession(_ session: URLSession, taskNeedsNewBodyStream task: URLSessionTask) {
  380. taskNeedNewBodyStream?(session, task)
  381. }
  382. public func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest) {
  383. taskWillPerformHTTPRedirection?(session, task, response, request)
  384. }
  385. public func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
  386. taskDidFinishCollectingMetrics?(session, task, metrics)
  387. }
  388. public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
  389. taskDidComplete?(session, task, error)
  390. }
  391. public func urlSession(_ session: URLSession, taskIsWaitingForConnectivity task: URLSessionTask) {
  392. taskIsWaitingForConnectivity?(session, task)
  393. }
  394. public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
  395. dataTaskDidReceiveData?(session, dataTask, data)
  396. }
  397. public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, willCacheResponse proposedResponse: CachedURLResponse) {
  398. dataTaskWillCacheResponse?(session, dataTask, proposedResponse)
  399. }
  400. public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
  401. downloadTaskDidResumeAtOffset?(session, downloadTask, fileOffset, expectedTotalBytes)
  402. }
  403. public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
  404. downloadTaskDidWriteData?(session, downloadTask, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite)
  405. }
  406. public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
  407. downloadTaskDidFinishDownloadingToURL?(session, downloadTask, location)
  408. }
  409. }