Skip to content

Fix trust certificates #8

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 4 commits into from
Apr 12, 2019
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
16 changes: 7 additions & 9 deletions Foundation/URLSession/URLSessionTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ extension _ProtocolClient: URLProtocolClient {
// URLCredentials holds credentials OR trustAllCertificates
if let credential = credential {
if let trust = credential._trustAllCertificated { // Trust field is set
task.previousFailureCount = 0
protocolTrust = trust
} else {
protocolCredentials = credential
Expand All @@ -632,7 +633,7 @@ extension _ProtocolClient: URLProtocolClient {
task.setTrustAllCertificates(trustAllCertificate)
}

if !task.setAuthMethod(authScheme) {
if authScheme != NSURLAuthenticationMethodServerTrust && !task.setAuthMethod(authScheme) {
NSLog("\(authScheme) is not supported")
}

Expand All @@ -641,7 +642,8 @@ extension _ProtocolClient: URLProtocolClient {
task.protectionSpaces = []

session.workQueue.async {
task._protocol?.client?.urlProtocolDidFinishLoading(`protocol`)
let error = challenge.error ?? NSError(domain: NSURLErrorDomain, code: NSURLErrorUnknown)
self.urlProtocol(task: task, didFailWithError: error)
}
case .rejectProtectionSpace:
session.workQueue.async {
Expand Down Expand Up @@ -683,7 +685,7 @@ extension _ProtocolClient: URLProtocolClient {
proposedCredential: nil,
previousFailureCount: task.previousFailureCount,
failureResponse: response,
error: nil,
error: NSError(domain: NSURLErrorDomain, code: NSURLErrorUserAuthenticationRequired),
sender: sender)

urlProtocol(`protocol`, didReceive: authenticationChallenge)
Expand All @@ -708,9 +710,7 @@ extension _ProtocolClient: URLProtocolClient {
}
case .noDelegate:
task.state = .completed
session.workQueue.async {
session.taskRegistry.remove(task)
}
session.taskRegistry.remove(task)
case .dataCompletionHandler(let completion):
session.delegateQueue.addOperation {
completion(`protocol`.properties[URLProtocol._PropertyKey.responseData] as? Data ?? Data(), task.response, nil)
Expand Down Expand Up @@ -798,9 +798,7 @@ extension _ProtocolClient: URLProtocolClient {
}
case .noDelegate:
task.state = .completed
session.workQueue.async {
session.taskRegistry.remove(task)
}
session.taskRegistry.remove(task)
case .dataCompletionHandler(let completion):
session.delegateQueue.addOperation {
completion(nil, nil, error)
Expand Down
3 changes: 2 additions & 1 deletion Foundation/URLSession/http/HTTPURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ internal extension _HTTPURLProtocol {
NSUnderlyingErrorKey: error,
NSURLErrorFailingURLErrorKey: $0,
NSURLErrorFailingURLStringErrorKey: $0.absoluteString,
NSLocalizedDescriptionKey: NSLocalizedString(error.localizedDescription, comment: "N/A")
NSLocalizedDescriptionKey: error.localizedDescription
]
}
let urlError = URLError(_nsError: NSError(domain: NSURLErrorDomain, code: error.code, userInfo: userInfo))
Expand Down Expand Up @@ -745,6 +745,7 @@ extension _HTTPURLProtocol {
fatalError("Trying to complete the task, but its transfer isn't complete.")
}
task?.response = response
task?.error = nil

//We don't want a timeout to be triggered after this. The timeout timer needs to be cancelled.
easyHandle.timeoutTimer = nil
Expand Down