Skip to content

Commit 226e078

Browse files
author
Pushkar N Kulkarni
authored
Merge pull request #1053 from nethraravindran/client-branch
Move the default URLProtocolClient implementation to URLSessionTask.swift
2 parents b4e40ce + d236a1e commit 226e078

File tree

2 files changed

+94
-92
lines changed

2 files changed

+94
-92
lines changed

Foundation/NSURLProtocol.swift

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -145,98 +145,7 @@ public protocol URLProtocolClient : NSObjectProtocol {
145145
func urlProtocol(_ protocol: URLProtocol, didCancel challenge: URLAuthenticationChallenge)
146146
}
147147

148-
internal class _ProtocolClient : NSObject, URLProtocolClient {
149-
150-
func urlProtocol(_ protocol: URLProtocol, didReceive response: URLResponse, cacheStoragePolicy policy: URLCache.StoragePolicy) {
151-
`protocol`.task?.response = response
152-
}
153-
154-
func urlProtocolDidFinishLoading(_ protocol: URLProtocol) {
155-
guard let task = `protocol`.task else { fatalError() }
156-
guard let session = task.session as? URLSession else { fatalError() }
157-
switch session.behaviour(for: task) {
158-
case .taskDelegate(let delegate):
159-
session.delegateQueue.addOperation {
160-
delegate.urlSession(session, task: task, didCompleteWithError: nil)
161-
task.state = .completed
162-
session.taskRegistry.remove(task)
163-
}
164-
case .noDelegate:
165-
task.state = .completed
166-
session.taskRegistry.remove(task)
167-
case .dataCompletionHandler:
168-
let data = Data()
169-
guard let client = `protocol`.client else { fatalError() }
170-
client.urlProtocol(`protocol`, didLoad: data)
171-
return
172-
case .downloadCompletionHandler(let completion):
173-
session.delegateQueue.addOperation {
174-
completion(task.currentRequest?.url, task.response, nil)
175-
task.state = .completed
176-
session.taskRegistry.remove(task)
177-
}
178-
}
179-
}
180-
181-
func urlProtocol(_ protocol: URLProtocol, didCancel challenge: URLAuthenticationChallenge) {
182-
NSUnimplemented()
183-
}
184-
185-
func urlProtocol(_ protocol: URLProtocol, didReceive challenge: URLAuthenticationChallenge) {
186-
NSUnimplemented()
187-
}
188-
189-
func urlProtocol(_ protocol: URLProtocol, didLoad data: Data) {
190-
guard let task = `protocol`.task else { fatalError() }
191-
guard let session = task.session as? URLSession else { fatalError() }
192-
switch session.behaviour(for: task) {
193-
case .dataCompletionHandler(let completion):
194-
guard let s = task.session as? URLSession else { fatalError() }
195-
s.delegateQueue.addOperation {
196-
completion(data, task.response, nil)
197-
task.state = .completed
198-
s.taskRegistry.remove(task)
199-
}
200-
default: return
201-
}
202-
}
203-
204-
func urlProtocol(_ protocol: URLProtocol, didFailWithError error: Error) {
205-
guard let task = `protocol`.task else { fatalError() }
206-
guard let session = task.session as? URLSession else { fatalError() }
207-
switch session.behaviour(for: task) {
208-
case .taskDelegate(let delegate):
209-
session.delegateQueue.addOperation {
210-
delegate.urlSession(session, task: task, didCompleteWithError: error as Error)
211-
task.state = .completed
212-
session.taskRegistry.remove(task)
213-
}
214-
case .noDelegate:
215-
task.state = .completed
216-
session.taskRegistry.remove(task)
217-
case .dataCompletionHandler(let completion):
218-
session.delegateQueue.addOperation {
219-
completion(nil, nil, error)
220-
task.state = .completed
221-
session.taskRegistry.remove(task)
222-
}
223-
case .downloadCompletionHandler(let completion):
224-
session.delegateQueue.addOperation {
225-
completion(nil, nil, error)
226-
task.state = .completed
227-
session.taskRegistry.remove(task)
228-
}
229-
}
230-
}
231-
232-
func urlProtocol(_ protocol: URLProtocol, cachedResponseIsValid cachedResponse: CachedURLResponse) {
233-
NSUnimplemented()
234-
}
235-
236-
func urlProtocol(_ protocol: URLProtocol, wasRedirectedTo request: URLRequest, redirectResponse: URLResponse) {
237-
NSUnimplemented()
238-
}
239-
}
148+
internal class _ProtocolClient : NSObject { }
240149

241150
/*!
242151
@class NSURLProtocol

Foundation/NSURLSession/NSURLSessionTask.swift

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,3 +522,96 @@ open class URLSessionStreamTask : URLSessionTask {
522522

523523
/* Key in the userInfo dictionary of an NSError received during a failed download. */
524524
public let URLSessionDownloadTaskResumeData: String = "NSURLSessionDownloadTaskResumeData"
525+
526+
extension _ProtocolClient : URLProtocolClient {
527+
528+
func urlProtocol(_ protocol: URLProtocol, didReceive response: URLResponse, cacheStoragePolicy policy: URLCache.StoragePolicy) {
529+
`protocol`.task?.response = response
530+
}
531+
532+
func urlProtocolDidFinishLoading(_ protocol: URLProtocol) {
533+
guard let task = `protocol`.task else { fatalError() }
534+
guard let session = task.session as? URLSession else { fatalError() }
535+
switch session.behaviour(for: task) {
536+
case .taskDelegate(let delegate):
537+
session.delegateQueue.addOperation {
538+
delegate.urlSession(session, task: task, didCompleteWithError: nil)
539+
task.state = .completed
540+
session.taskRegistry.remove(task)
541+
}
542+
case .noDelegate:
543+
task.state = .completed
544+
session.taskRegistry.remove(task)
545+
case .dataCompletionHandler(let completion):
546+
let data = Data()
547+
guard let client = `protocol`.client else { fatalError() }
548+
client.urlProtocol(`protocol`, didLoad: data)
549+
return
550+
case .downloadCompletionHandler(let completion):
551+
session.delegateQueue.addOperation {
552+
completion(task.currentRequest?.url, task.response, nil)
553+
task.state = .completed
554+
session.taskRegistry.remove(task)
555+
}
556+
}
557+
}
558+
559+
func urlProtocol(_ protocol: URLProtocol, didCancel challenge: URLAuthenticationChallenge) {
560+
NSUnimplemented()
561+
}
562+
563+
func urlProtocol(_ protocol: URLProtocol, didReceive challenge: URLAuthenticationChallenge) {
564+
NSUnimplemented()
565+
}
566+
567+
func urlProtocol(_ protocol: URLProtocol, didLoad data: Data) {
568+
guard let task = `protocol`.task else { fatalError() }
569+
guard let session = task.session as? URLSession else { fatalError() }
570+
switch session.behaviour(for: task) {
571+
case .dataCompletionHandler(let completion):
572+
guard let s = task.session as? URLSession else { fatalError() }
573+
s.delegateQueue.addOperation {
574+
completion(data, task.response, nil)
575+
task.state = .completed
576+
s.taskRegistry.remove(task)
577+
}
578+
default: return
579+
}
580+
}
581+
582+
func urlProtocol(_ protocol: URLProtocol, didFailWithError error: Error) {
583+
guard let task = `protocol`.task else { fatalError() }
584+
guard let session = task.session as? URLSession else { fatalError() }
585+
switch session.behaviour(for: task) {
586+
case .taskDelegate(let delegate):
587+
session.delegateQueue.addOperation {
588+
delegate.urlSession(session, task: task, didCompleteWithError: error as Error)
589+
task.state = .completed
590+
session.taskRegistry.remove(task)
591+
}
592+
case .noDelegate:
593+
task.state = .completed
594+
session.taskRegistry.remove(task)
595+
case .dataCompletionHandler(let completion):
596+
session.delegateQueue.addOperation {
597+
completion(nil, nil, error)
598+
task.state = .completed
599+
session.taskRegistry.remove(task)
600+
}
601+
case .downloadCompletionHandler(let completion):
602+
session.delegateQueue.addOperation {
603+
completion(nil, nil, error)
604+
task.state = .completed
605+
session.taskRegistry.remove(task)
606+
}
607+
}
608+
}
609+
610+
func urlProtocol(_ protocol: URLProtocol, cachedResponseIsValid cachedResponse: CachedURLResponse) {
611+
NSUnimplemented()
612+
}
613+
614+
func urlProtocol(_ protocol: URLProtocol, wasRedirectedTo request: URLRequest, redirectResponse: URLResponse) {
615+
NSUnimplemented()
616+
}
617+
}

0 commit comments

Comments
 (0)