This repository has been archived by the owner on Oct 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
NetURLSessionDelegate.swift
139 lines (108 loc) · 5.17 KB
/
NetURLSessionDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// NetURLSessionDelegate.swift
// Net
//
// Created by Alex Rupérez on 17/3/17.
//
//
import Foundation
class NetURLSessionDelegate: NSObject {
weak var netURLSession: NetURLSession?
final var tasks = [URLSessionTask: NetTask]()
init(_ urlSession: NetURLSession) {
netURLSession = urlSession
super.init()
}
func add(_ task: URLSessionTask, _ netTask: NetTask?) {
tasks[task] = netTask
}
deinit {
tasks.removeAll()
netURLSession = nil
}
}
extension NetURLSessionDelegate: URLSessionDelegate {}
extension NetURLSessionDelegate: URLSessionTaskDelegate {
func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void) {
handle(challenge, tasks[task], completion: completionHandler)
}
@available(iOS 10.0, tvOS 10.0, watchOS 3.0, macOS 10.12, *)
func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting taskMetrics: URLSessionTaskMetrics) {
if let netTask = tasks[task] {
netTask.metrics = NetTaskMetrics(taskMetrics, request: netTask.request, response: netTask.response)
tasks[task] = nil
}
}
@available(iOS 11.0, tvOS 11.0, watchOS 4.0, macOS 10.13, *)
func urlSession(_ session: URLSession, task: URLSessionTask, willBeginDelayedRequest request: URLRequest, completionHandler: @escaping (URLSession.DelayedRequestDisposition, URLRequest?) -> Void) {
completionHandler(.continueLoading, nil)
}
@available(iOS 11.0, tvOS 11.0, watchOS 4.0, macOS 10.13, *)
func urlSession(_ session: URLSession, taskIsWaitingForConnectivity task: URLSessionTask) {
if let netTask = tasks[task] {
netTask.state = .waitingForConnectivity
}
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if let netTask = tasks[task] {
let netError = netURLSession?.netError(error, netTask.response?.responseObject, task.response)
netURLSession?.process(netTask, netTask.response, netError)
tasks[task] = nil
}
}
}
extension NetURLSessionDelegate: URLSessionDataDelegate {
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
if let netTask = tasks[dataTask] {
guard let response = netTask.response, var oldData = try? response.object() as Data else {
tasks[dataTask]?.response = netURLSession?.netResponse(dataTask.response, netTask, data)
return
}
tasks[dataTask]?.response = netURLSession?.netResponse(dataTask.response, netTask, oldData.append(data))
}
}
}
extension NetURLSessionDelegate: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {}
}
@available(iOS 9.0, *)
extension NetURLSessionDelegate: URLSessionStreamDelegate {}
extension NetURLSessionDelegate {
func handle(_ challenge: URLAuthenticationChallenge, _ netTask: NetTask? = nil, completion: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void) {
guard let authChallenge = netURLSession?.authChallenge else {
guard challenge.previousFailureCount == 0 else {
challenge.sender?.cancel(challenge)
if let realm = challenge.protectionSpace.realm {
print(realm)
print(challenge.protectionSpace.authenticationMethod)
}
completion(.cancelAuthenticationChallenge, nil)
return
}
var credential: URLCredential? = challenge.proposedCredential
if credential?.hasPassword != true, challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic || challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPDigest, let request = netTask?.request {
switch request.authorization {
case .basic(let user, let password):
credential = URLCredential(user: user, password: password, persistence: .forSession)
default:
break
}
}
if credential == nil, challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust, let serverTrust = challenge.protectionSpace.serverTrust {
let host = challenge.protectionSpace.host
if let policy = netURLSession?.serverTrust[host] {
if policy.evaluate(serverTrust, host: host) {
credential = URLCredential(trust: serverTrust)
} else {
credential = nil
}
} else {
credential = URLCredential(trust: serverTrust)
}
}
completion(credential != nil ? .useCredential : .cancelAuthenticationChallenge, credential)
return
}
authChallenge(challenge, completion)
}
}