@@ -109,7 +109,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
109109 public var headers = [ String: String] ( )
110110 public var voipEnabled = false
111111 public var selfSignedSSL = false
112- private var security : SSLSecurity ?
112+ public var security : SSLSecurity ?
113113 public var enabledSSLCipherSuites : [ SSLCipherSuite ] ?
114114 public var origin : String ?
115115 public var isConnected : Bool {
@@ -167,8 +167,8 @@ public class WebSocket : NSObject, NSStreamDelegate {
167167 public func disconnect( forceTimeout forceTimeout: NSTimeInterval ? = nil ) {
168168 switch forceTimeout {
169169 case . Some( let seconds) where seconds > 0 :
170- dispatch_after ( dispatch_time ( DISPATCH_TIME_NOW, Int64 ( seconds * Double( NSEC_PER_SEC) ) ) , queue) { [ unowned self] in
171- self . disconnectStream ( nil )
170+ dispatch_after ( dispatch_time ( DISPATCH_TIME_NOW, Int64 ( seconds * Double( NSEC_PER_SEC) ) ) , queue) { [ weak self] in
171+ self ? . disconnectStream ( nil )
172172 }
173173 fallthrough
174174 case . None:
@@ -309,13 +309,12 @@ public class WebSocket : NSObject, NSStreamDelegate {
309309 let bytes = UnsafePointer < UInt8 > ( data. bytes)
310310 var timeout = 5000000 //wait 5 seconds before giving up
311311 writeQueue. addOperationWithBlock { [ weak self] in
312- guard let this = self else { return }
313312 while !outStream. hasSpaceAvailable {
314313 usleep ( 100 ) //wait until the socket is ready
315314 timeout -= 100
316315 if timeout < 0 {
317- this . cleanupStream ( )
318- this . doDisconnect ( this . errorWithDetail ( " write wait timed out " , code: 2 ) )
316+ self ? . cleanupStream ( )
317+ self ? . doDisconnect ( self ? . errorWithDetail ( " write wait timed out " , code: 2 ) )
319318 return
320319 } else if outStream. streamError != nil {
321320 return //disconnectStream will be called.
@@ -352,7 +351,11 @@ public class WebSocket : NSObject, NSStreamDelegate {
352351 }
353352 //disconnect the stream object
354353 private func disconnectStream( error: NSError? ) {
355- writeQueue. waitUntilAllOperationsAreFinished ( )
354+ if error == nil {
355+ writeQueue. waitUntilAllOperationsAreFinished ( )
356+ } else {
357+ writeQueue. cancelAllOperations ( )
358+ }
356359 cleanupStream ( )
357360 doDisconnect ( error)
358361 }
@@ -814,7 +817,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
814817
815818}
816819
817- private class SSLCert {
820+ public class SSLCert {
818821 var certData : NSData ?
819822 var key : SecKeyRef ?
820823
@@ -825,7 +828,7 @@ private class SSLCert {
825828
826829 - returns: a representation security object to be used with
827830 */
828- init ( data: NSData) {
831+ public init( data: NSData) {
829832 self . certData = data
830833 }
831834
@@ -836,13 +839,13 @@ private class SSLCert {
836839
837840 - returns: a representation security object to be used with
838841 */
839- init ( key: SecKeyRef) {
842+ public init( key: SecKeyRef) {
840843 self . key = key
841844 }
842845}
843846
844- private class SSLSecurity {
845- var validatedDN = true //should the domain name be validated?
847+ public class SSLSecurity {
848+ public var validatedDN = true //should the domain name be validated?
846849
847850 var isReady = false //is the key processing done?
848851 var certificates : [ NSData ] ? //the certificates
@@ -856,10 +859,11 @@ private class SSLSecurity {
856859
857860 - returns: a representation security object to be used with
858861 */
859- convenience init( usePublicKeys: Bool = false ) {
862+ public convenience init( usePublicKeys: Bool = false ) {
860863 let paths = NSBundle . mainBundle ( ) . pathsForResourcesOfType ( " cer " , inDirectory: " . " )
861864
862- let certs = paths. reduce ( [ SSLCert] ( ) ) { ( var certs: [ SSLCert ] , path: String ) -> [ SSLCert ] in
865+ let certs = paths. reduce ( [ SSLCert] ( ) ) { ( certs: [ SSLCert ] , path: String ) -> [ SSLCert ] in
866+ var certs = certs
863867 if let data = NSData ( contentsOfFile: path) {
864868 certs. append ( SSLCert ( data: data) )
865869 }
@@ -877,12 +881,13 @@ private class SSLSecurity {
877881
878882 - returns: a representation security object to be used with
879883 */
880- init ( certs: [ SSLCert] , usePublicKeys: Bool) {
884+ public init( certs: [ SSLCert] , usePublicKeys: Bool) {
881885 self . usePublicKeys = usePublicKeys
882886
883887 if self . usePublicKeys {
884888 dispatch_async ( dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ) ) {
885- let pubKeys = certs. reduce ( [ SecKeyRef] ( ) ) { ( var pubKeys: [ SecKeyRef ] , cert: SSLCert ) -> [ SecKeyRef ] in
889+ let pubKeys = certs. reduce ( [ SecKeyRef] ( ) ) { ( pubKeys: [ SecKeyRef ] , cert: SSLCert ) -> [ SecKeyRef ] in
890+ var pubKeys = pubKeys
886891 if let data = cert. certData where cert. key == nil {
887892 cert. key = self . extractPublicKey ( data)
888893 }
@@ -896,7 +901,8 @@ private class SSLSecurity {
896901 self . isReady = true
897902 }
898903 } else {
899- let certificates = certs. reduce ( [ NSData] ( ) ) { ( var certificates: [ NSData ] , cert: SSLCert ) -> [ NSData ] in
904+ let certificates = certs. reduce ( [ NSData] ( ) ) { ( certificates: [ NSData ] , cert: SSLCert ) -> [ NSData ] in
905+ var certificates = certificates
900906 if let data = cert. certData {
901907 certificates. append ( data)
902908 }
@@ -915,7 +921,7 @@ private class SSLSecurity {
915921
916922 - returns: if the key was successfully validated
917923 */
918- func isValid( trust: SecTrustRef , domain: String ? ) -> Bool {
924+ public func isValid( trust: SecTrustRef , domain: String ? ) -> Bool {
919925
920926 var tries = 0
921927 while ( !self . isReady) {
@@ -1010,7 +1016,8 @@ private class SSLSecurity {
10101016 - returns: the certificate chain for the trust
10111017 */
10121018 func certificateChainForTrust( trust: SecTrustRef ) -> [ NSData ] {
1013- let certificates = ( 0 ..< SecTrustGetCertificateCount ( trust) ) . reduce ( [ NSData] ( ) ) { ( var certificates: [ NSData ] , index: Int ) -> [ NSData ] in
1019+ let certificates = ( 0 ..< SecTrustGetCertificateCount ( trust) ) . reduce ( [ NSData] ( ) ) { ( certificates: [ NSData ] , index: Int ) -> [ NSData ] in
1020+ var certificates = certificates
10141021 let cert = SecTrustGetCertificateAtIndex ( trust, index)
10151022 certificates. append ( SecCertificateCopyData ( cert!) )
10161023 return certificates
@@ -1028,7 +1035,8 @@ private class SSLSecurity {
10281035 */
10291036 func publicKeyChainForTrust( trust: SecTrustRef ) -> [ SecKeyRef ] {
10301037 let policy = SecPolicyCreateBasicX509 ( )
1031- let keys = ( 0 ..< SecTrustGetCertificateCount ( trust) ) . reduce ( [ SecKeyRef] ( ) ) { ( var keys: [ SecKeyRef ] , index: Int ) -> [ SecKeyRef ] in
1038+ let keys = ( 0 ..< SecTrustGetCertificateCount ( trust) ) . reduce ( [ SecKeyRef] ( ) ) { ( keys: [ SecKeyRef ] , index: Int ) -> [ SecKeyRef ] in
1039+ var keys = keys
10321040 let cert = SecTrustGetCertificateAtIndex ( trust, index)
10331041 if let key = extractPublicKeyFromCert ( cert!, policy: policy) {
10341042 keys. append ( key)
0 commit comments