From 5976ef1752f58f863da8a074dc54199a5083b3f6 Mon Sep 17 00:00:00 2001 From: Erik Date: Sat, 27 Feb 2016 19:43:21 -0500 Subject: [PATCH] bump websocket version --- Source/WebSocket.swift | 48 ++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/Source/WebSocket.swift b/Source/WebSocket.swift index fce6209..163ce7a 100644 --- a/Source/WebSocket.swift +++ b/Source/WebSocket.swift @@ -109,7 +109,7 @@ public class WebSocket : NSObject, NSStreamDelegate { public var headers = [String: String]() public var voipEnabled = false public var selfSignedSSL = false - private var security: SSLSecurity? + public var security: SSLSecurity? public var enabledSSLCipherSuites: [SSLCipherSuite]? public var origin: String? public var isConnected :Bool { @@ -167,8 +167,8 @@ public class WebSocket : NSObject, NSStreamDelegate { public func disconnect(forceTimeout forceTimeout: NSTimeInterval? = nil) { switch forceTimeout { case .Some(let seconds) where seconds > 0: - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC))), queue) { [unowned self] in - self.disconnectStream(nil) + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC))), queue) { [weak self] in + self?.disconnectStream(nil) } fallthrough case .None: @@ -309,13 +309,12 @@ public class WebSocket : NSObject, NSStreamDelegate { let bytes = UnsafePointer(data.bytes) var timeout = 5000000 //wait 5 seconds before giving up writeQueue.addOperationWithBlock { [weak self] in - guard let this = self else { return } while !outStream.hasSpaceAvailable { usleep(100) //wait until the socket is ready timeout -= 100 if timeout < 0 { - this.cleanupStream() - this.doDisconnect(this.errorWithDetail("write wait timed out", code: 2)) + self?.cleanupStream() + self?.doDisconnect(self?.errorWithDetail("write wait timed out", code: 2)) return } else if outStream.streamError != nil { return //disconnectStream will be called. @@ -352,7 +351,11 @@ public class WebSocket : NSObject, NSStreamDelegate { } //disconnect the stream object private func disconnectStream(error: NSError?) { - writeQueue.waitUntilAllOperationsAreFinished() + if error == nil { + writeQueue.waitUntilAllOperationsAreFinished() + } else { + writeQueue.cancelAllOperations() + } cleanupStream() doDisconnect(error) } @@ -814,7 +817,7 @@ public class WebSocket : NSObject, NSStreamDelegate { } -private class SSLCert { +public class SSLCert { var certData: NSData? var key: SecKeyRef? @@ -825,7 +828,7 @@ private class SSLCert { - returns: a representation security object to be used with */ - init(data: NSData) { + public init(data: NSData) { self.certData = data } @@ -836,13 +839,13 @@ private class SSLCert { - returns: a representation security object to be used with */ - init(key: SecKeyRef) { + public init(key: SecKeyRef) { self.key = key } } -private class SSLSecurity { - var validatedDN = true //should the domain name be validated? +public class SSLSecurity { + public var validatedDN = true //should the domain name be validated? var isReady = false //is the key processing done? var certificates: [NSData]? //the certificates @@ -856,10 +859,11 @@ private class SSLSecurity { - returns: a representation security object to be used with */ - convenience init(usePublicKeys: Bool = false) { + public convenience init(usePublicKeys: Bool = false) { let paths = NSBundle.mainBundle().pathsForResourcesOfType("cer", inDirectory: ".") - let certs = paths.reduce([SSLCert]()) { (var certs: [SSLCert], path: String) -> [SSLCert] in + let certs = paths.reduce([SSLCert]()) { (certs: [SSLCert], path: String) -> [SSLCert] in + var certs = certs if let data = NSData(contentsOfFile: path) { certs.append(SSLCert(data: data)) } @@ -877,12 +881,13 @@ private class SSLSecurity { - returns: a representation security object to be used with */ - init(certs: [SSLCert], usePublicKeys: Bool) { + public init(certs: [SSLCert], usePublicKeys: Bool) { self.usePublicKeys = usePublicKeys if self.usePublicKeys { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)) { - let pubKeys = certs.reduce([SecKeyRef]()) { (var pubKeys: [SecKeyRef], cert: SSLCert) -> [SecKeyRef] in + let pubKeys = certs.reduce([SecKeyRef]()) { (pubKeys: [SecKeyRef], cert: SSLCert) -> [SecKeyRef] in + var pubKeys = pubKeys if let data = cert.certData where cert.key == nil { cert.key = self.extractPublicKey(data) } @@ -896,7 +901,8 @@ private class SSLSecurity { self.isReady = true } } else { - let certificates = certs.reduce([NSData]()) { (var certificates: [NSData], cert: SSLCert) -> [NSData] in + let certificates = certs.reduce([NSData]()) { (certificates: [NSData], cert: SSLCert) -> [NSData] in + var certificates = certificates if let data = cert.certData { certificates.append(data) } @@ -915,7 +921,7 @@ private class SSLSecurity { - returns: if the key was successfully validated */ - func isValid(trust: SecTrustRef, domain: String?) -> Bool { + public func isValid(trust: SecTrustRef, domain: String?) -> Bool { var tries = 0 while(!self.isReady) { @@ -1010,7 +1016,8 @@ private class SSLSecurity { - returns: the certificate chain for the trust */ func certificateChainForTrust(trust: SecTrustRef) -> [NSData] { - let certificates = (0.. [NSData] in + let certificates = (0.. [NSData] in + var certificates = certificates let cert = SecTrustGetCertificateAtIndex(trust, index) certificates.append(SecCertificateCopyData(cert!)) return certificates @@ -1028,7 +1035,8 @@ private class SSLSecurity { */ func publicKeyChainForTrust(trust: SecTrustRef) -> [SecKeyRef] { let policy = SecPolicyCreateBasicX509() - let keys = (0.. [SecKeyRef] in + let keys = (0.. [SecKeyRef] in + var keys = keys let cert = SecTrustGetCertificateAtIndex(trust, index) if let key = extractPublicKeyFromCert(cert!, policy: policy) { keys.append(key)