daltoniam/Skeets

Loading image with ssl

giangqaz opened this issue · 5 comments

Hi, I'm using httpswift with skeets in my apps
with httpswift
I accept all certificate with

var attempted = false
request.auth = {(challenge: NSURLAuthenticationChallenge) in
    if !attempted {
        attempted = true
        return NSURLCredential(forTrust: challenge.protectionSpace.serverTrust)
    }
    return nil
}

It worked.

but when I loading image
I do the same with

let task = HTTPTask()
                    var attempted = false
                    task.auth = {(challenge: NSURLAuthenticationChallenge) in
                        if !attempted {
                            attempted = true
                            return NSURLCredential(forTrust: challenge.protectionSpace.serverTrust)
                        }
                        return nil
                    }

                    task.download(url, parameters: nil, progress: { (status: Double) in
                        dispatch_async(dispatch_get_main_queue(), {
                            self.doProgress(hash, status: status)
                        })
                        }, completionHandler: { (response: HTTPResponse) in
                            if let err = response.error {
                                dispatch_async(dispatch_get_main_queue(), {
                                    self.doFailure(hash, error: err)
                                })
                            }
                            self.cache.add(hash, url: response.responseObject! as! NSURL)
                            dispatch_async(dispatch_get_main_queue(), {
                                if let d = self.cache.fromMemory(hash) {
                                    self.doSuccess(hash, data: d)
                                }
                            })
                        })

in ImageManager.swift file
but I get this error

fatal error: unexpectedly found nil while unwrapping an Optional value
failed to get an image: Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “merchant.jamja.vn” which could put your confidential information at risk." UserInfo=0x78f840e0 {NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorCodeKey=-9807, NSErrorFailingURLKey=https://merchant.jamja.vn/image/5555b0b3f3c0620faa69b7f3/thumbnail/, NSErrorFailingURLStringKey=https://merchant.jamja.vn/image/5555b0b3f3c0620faa69b7f3/thumbnail/, _kCFStreamErrorDomainKey=3, NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “merchant.jamja.vn” which could put your confidential information at risk.}

could you tell me how to load type of image?
thanks

I would check and see if the NSURLDownloadSessionTask even calls the authentication delegates. Might just be the way the Apple API is designed. Try putting a few println() in your task.auth and inside SwiftHTTP delegate method:

public func URLSession(session: NSURLSession, task: NSURLSessionTask, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) 

yes, I checked, and NSURLDownloadSessionTask seem to be not to be call authentication delegates.

@daltoniam
It also not run into here

if let sec = security where challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust { let space = challenge.protectionSpace if let trust = space.serverTrust { if sec.isValid(trust, domain: space.host) { completionHandler(.UseCredential, NSURLCredential(trust: trust)) return } } completionHandler(.CancelAuthenticationChallenge, nil)
        return

    } else if let a = auth {

        let cred = a(challenge)
        if let c = cred {
            completionHandler(.UseCredential, c)
            return
        }
        completionHandler(.RejectProtectionSpace, nil)
        return
    }

so, do you give me some advice to load that image?

Well there isn't much we can do in Skeets if the authentication delegates aren't getting called. If the underlining NSURLDownloadSessionTask doesn't call the delegates, then the auth closure wouldn't be called either. My only suggestion would be to try replacing the download method calls with GET. You probably wouldn't be able to uses Skeets proper and have to use that custom implementation of it, since the NSURLDownloadSessionTask API doesn't call the authentication delegates and we can't change the way Apple APIs work (other than opening a radar on it).

@daltoniam thank you.