Skip to content

Commit

Permalink
Merge pull request #2 from readdle/fix/call-auth-challenge-if-delegat…
Browse files Browse the repository at this point in the history
…e-is-presented

Do not use AuthChallenge if no task delegate presented
  • Loading branch information
andriydruk authored Nov 19, 2018
2 parents c577fda + 4d46ec4 commit 9d3eb03
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
24 changes: 20 additions & 4 deletions Foundation/URLSession/URLSessionTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,8 @@ extension _ProtocolClient: URLProtocolClient {
guard let session = task.session as? URLSession else { fatalError("session cannot be nil") }
guard let response = task.response as? HTTPURLResponse else { fatalError("No response") }

if response.statusCode == 401 {

if response.statusCode == 401, `protocol`.containsTaskDelegate() {
// Concat protection space from header with all possibles protection spaces
if !task.protectionSpacesInited { // init protection spaces
var allPossibleProtectionSpaces = AuthProtectionSpace.createAllPossible(using: response)
Expand All @@ -683,6 +684,7 @@ extension _ProtocolClient: URLProtocolClient {
return
}
}


switch session.behaviour(for: task) {
case .taskDelegate(let delegate):
Expand Down Expand Up @@ -746,7 +748,7 @@ extension _ProtocolClient: URLProtocolClient {
}
let certificateErrors = [NSURLErrorServerCertificateUntrusted, NSURLErrorServerCertificateWrongHost]

if certificateErrors.contains(error._code) {
if certificateErrors.contains(error._code) && `protocol`.containsTaskDelegate() {
let protectionSpace = URLProtectionSpace(host: "",
port: 443,
protocol: "https",
Expand All @@ -763,10 +765,9 @@ extension _ProtocolClient: URLProtocolClient {

task.previousFailureCount += 1
urlProtocol(`protocol`, didReceive: authenticationChallenge)
return
}
return
}

urlProtocol(task: task, didFailWithError: error)
}

Expand Down Expand Up @@ -838,6 +839,21 @@ private extension URLSessionTask {
}
}

fileprivate extension URLProtocol {
func containsTaskDelegate() -> Bool {
guard let task = self.task else { return false }
guard let session = task.session as? URLSession else { return false }

switch session.behaviour(for: task) {
case .taskDelegate(_):
return true

default:
return false
}
}
}

extension URLProtocol {
enum _PropertyKey: String {
case responseData
Expand Down
1 change: 1 addition & 0 deletions Foundation/URLSession/http/HTTPBodySource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ extension _HTTPBodyStreamSource: _HTTPBodySource {
if inputStream.hasBytesAvailable {
let buffer = UnsafeMutableRawBufferPointer.allocate(count: length)
guard let pointer = buffer.baseAddress?.assumingMemoryBound(to: UInt8.self) else {
buffer.deallocate()
return .error
}
let readBytes = self.inputStream.read(pointer, maxLength: length)
Expand Down

0 comments on commit 9d3eb03

Please sign in to comment.