mirror of
https://github.com/ProxymanApp/atlantis.git
synced 2026-05-20 20:20:35 +00:00
Make sure all accesses to URLSessionTask.currentRequest are crash safe (#172)
This commit is contained in:
@@ -253,7 +253,7 @@ extension Atlantis {
|
||||
// If not found, just generate and cache
|
||||
switch taskOrConnection {
|
||||
case let task as URLSessionTask:
|
||||
guard let request = task.currentRequest,
|
||||
guard let request = task.currentRequestSafe,
|
||||
let package = TrafficPackage.buildRequest(sessionTask: task, id: id) else {
|
||||
print("[Atlantis] ❌ Error: Should build package from URLSessionTask")
|
||||
return nil
|
||||
|
||||
+2
-15
@@ -6,10 +6,6 @@
|
||||
// Copyright © 2020 Proxyman. All rights reserved.
|
||||
//
|
||||
|
||||
#if os(iOS) || os(macOS)
|
||||
import class AVFoundation.AVAggregateAssetDownloadTask
|
||||
#endif
|
||||
|
||||
import Foundation
|
||||
|
||||
#if os(OSX)
|
||||
@@ -105,17 +101,8 @@ public final class TrafficPackage: Codable, CustomDebugStringConvertible, Serial
|
||||
// MARK: - Builder
|
||||
|
||||
static func buildRequest(sessionTask: URLSessionTask, id: String) -> TrafficPackage? {
|
||||
// If sessionTask is AVAggregateAssetDownloadTask,
|
||||
// accessing currentRequest crashes with not supported error,
|
||||
// so we need to check for it in advance.
|
||||
#if os(iOS) || os(macOS)
|
||||
if sessionTask is AVAggregateAssetDownloadTask {
|
||||
return nil
|
||||
}
|
||||
#endif
|
||||
|
||||
guard let currentRequest = sessionTask.currentRequest,
|
||||
let request = Request(currentRequest) else { return nil }
|
||||
guard let currentRequest = sessionTask.currentRequestSafe,
|
||||
let request = Request(currentRequest) else { return nil }
|
||||
|
||||
// Check if it's a websocket
|
||||
if let websocketClass = NSClassFromString("__NSURLSessionWebSocketTask"),
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
|
||||
import Foundation
|
||||
#if os(iOS) || os(macOS)
|
||||
import class AVFoundation.AVAggregateAssetDownloadTask
|
||||
#endif
|
||||
|
||||
extension URLSessionTask {
|
||||
var currentRequestSafe: URLRequest? {
|
||||
// If sessionTask is AVAggregateAssetDownloadTask,
|
||||
// accessing currentRequest crashes with not supported error,
|
||||
// so we need to check for it in advance.
|
||||
#if os(iOS) || os(macOS)
|
||||
if self is AVAggregateAssetDownloadTask {
|
||||
return nil
|
||||
}
|
||||
#endif
|
||||
|
||||
return currentRequest
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user