Skip to content

[URLSession] Move the default URLProtocolClient implementation to URLSessionTask.swift #1053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 1 addition & 92 deletions Foundation/NSURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,98 +145,7 @@ public protocol URLProtocolClient : NSObjectProtocol {
func urlProtocol(_ protocol: URLProtocol, didCancel challenge: URLAuthenticationChallenge)
}

internal class _ProtocolClient : NSObject, URLProtocolClient {

func urlProtocol(_ protocol: URLProtocol, didReceive response: URLResponse, cacheStoragePolicy policy: URLCache.StoragePolicy) {
`protocol`.task?.response = response
}

func urlProtocolDidFinishLoading(_ protocol: URLProtocol) {
guard let task = `protocol`.task else { fatalError() }
guard let session = task.session as? URLSession else { fatalError() }
switch session.behaviour(for: task) {
case .taskDelegate(let delegate):
session.delegateQueue.addOperation {
delegate.urlSession(session, task: task, didCompleteWithError: nil)
task.state = .completed
session.taskRegistry.remove(task)
}
case .noDelegate:
task.state = .completed
session.taskRegistry.remove(task)
case .dataCompletionHandler:
let data = Data()
guard let client = `protocol`.client else { fatalError() }
client.urlProtocol(`protocol`, didLoad: data)
return
case .downloadCompletionHandler(let completion):
session.delegateQueue.addOperation {
completion(task.currentRequest?.url, task.response, nil)
task.state = .completed
session.taskRegistry.remove(task)
}
}
}

func urlProtocol(_ protocol: URLProtocol, didCancel challenge: URLAuthenticationChallenge) {
NSUnimplemented()
}

func urlProtocol(_ protocol: URLProtocol, didReceive challenge: URLAuthenticationChallenge) {
NSUnimplemented()
}

func urlProtocol(_ protocol: URLProtocol, didLoad data: Data) {
guard let task = `protocol`.task else { fatalError() }
guard let session = task.session as? URLSession else { fatalError() }
switch session.behaviour(for: task) {
case .dataCompletionHandler(let completion):
guard let s = task.session as? URLSession else { fatalError() }
s.delegateQueue.addOperation {
completion(data, task.response, nil)
task.state = .completed
s.taskRegistry.remove(task)
}
default: return
}
}

func urlProtocol(_ protocol: URLProtocol, didFailWithError error: Error) {
guard let task = `protocol`.task else { fatalError() }
guard let session = task.session as? URLSession else { fatalError() }
switch session.behaviour(for: task) {
case .taskDelegate(let delegate):
session.delegateQueue.addOperation {
delegate.urlSession(session, task: task, didCompleteWithError: error as Error)
task.state = .completed
session.taskRegistry.remove(task)
}
case .noDelegate:
task.state = .completed
session.taskRegistry.remove(task)
case .dataCompletionHandler(let completion):
session.delegateQueue.addOperation {
completion(nil, nil, error)
task.state = .completed
session.taskRegistry.remove(task)
}
case .downloadCompletionHandler(let completion):
session.delegateQueue.addOperation {
completion(nil, nil, error)
task.state = .completed
session.taskRegistry.remove(task)
}
}
}

func urlProtocol(_ protocol: URLProtocol, cachedResponseIsValid cachedResponse: CachedURLResponse) {
NSUnimplemented()
}

func urlProtocol(_ protocol: URLProtocol, wasRedirectedTo request: URLRequest, redirectResponse: URLResponse) {
NSUnimplemented()
}
}
internal class _ProtocolClient : NSObject { }

/*!
@class NSURLProtocol
Expand Down
93 changes: 93 additions & 0 deletions Foundation/NSURLSession/NSURLSessionTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,96 @@ open class URLSessionStreamTask : URLSessionTask {

/* Key in the userInfo dictionary of an NSError received during a failed download. */
public let URLSessionDownloadTaskResumeData: String = "NSURLSessionDownloadTaskResumeData"

extension _ProtocolClient : URLProtocolClient {

func urlProtocol(_ protocol: URLProtocol, didReceive response: URLResponse, cacheStoragePolicy policy: URLCache.StoragePolicy) {
`protocol`.task?.response = response
}

func urlProtocolDidFinishLoading(_ protocol: URLProtocol) {
guard let task = `protocol`.task else { fatalError() }
guard let session = task.session as? URLSession else { fatalError() }
switch session.behaviour(for: task) {
case .taskDelegate(let delegate):
session.delegateQueue.addOperation {
delegate.urlSession(session, task: task, didCompleteWithError: nil)
task.state = .completed
session.taskRegistry.remove(task)
}
case .noDelegate:
task.state = .completed
session.taskRegistry.remove(task)
case .dataCompletionHandler(let completion):
let data = Data()
guard let client = `protocol`.client else { fatalError() }
client.urlProtocol(`protocol`, didLoad: data)
return
case .downloadCompletionHandler(let completion):
session.delegateQueue.addOperation {
completion(task.currentRequest?.url, task.response, nil)
task.state = .completed
session.taskRegistry.remove(task)
}
}
}

func urlProtocol(_ protocol: URLProtocol, didCancel challenge: URLAuthenticationChallenge) {
NSUnimplemented()
}

func urlProtocol(_ protocol: URLProtocol, didReceive challenge: URLAuthenticationChallenge) {
NSUnimplemented()
}

func urlProtocol(_ protocol: URLProtocol, didLoad data: Data) {
guard let task = `protocol`.task else { fatalError() }
guard let session = task.session as? URLSession else { fatalError() }
switch session.behaviour(for: task) {
case .dataCompletionHandler(let completion):
guard let s = task.session as? URLSession else { fatalError() }
s.delegateQueue.addOperation {
completion(data, task.response, nil)
task.state = .completed
s.taskRegistry.remove(task)
}
default: return
}
}

func urlProtocol(_ protocol: URLProtocol, didFailWithError error: Error) {
guard let task = `protocol`.task else { fatalError() }
guard let session = task.session as? URLSession else { fatalError() }
switch session.behaviour(for: task) {
case .taskDelegate(let delegate):
session.delegateQueue.addOperation {
delegate.urlSession(session, task: task, didCompleteWithError: error as Error)
task.state = .completed
session.taskRegistry.remove(task)
}
case .noDelegate:
task.state = .completed
session.taskRegistry.remove(task)
case .dataCompletionHandler(let completion):
session.delegateQueue.addOperation {
completion(nil, nil, error)
task.state = .completed
session.taskRegistry.remove(task)
}
case .downloadCompletionHandler(let completion):
session.delegateQueue.addOperation {
completion(nil, nil, error)
task.state = .completed
session.taskRegistry.remove(task)
}
}
}

func urlProtocol(_ protocol: URLProtocol, cachedResponseIsValid cachedResponse: CachedURLResponse) {
NSUnimplemented()
}

func urlProtocol(_ protocol: URLProtocol, wasRedirectedTo request: URLRequest, redirectResponse: URLResponse) {
NSUnimplemented()
}
}