@@ -109,7 +109,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
109
109
public var headers = [ String: String] ( )
110
110
public var voipEnabled = false
111
111
public var selfSignedSSL = false
112
- private var security : SSLSecurity ?
112
+ public var security : SSLSecurity ?
113
113
public var enabledSSLCipherSuites : [ SSLCipherSuite ] ?
114
114
public var origin : String ?
115
115
public var isConnected : Bool {
@@ -167,8 +167,8 @@ public class WebSocket : NSObject, NSStreamDelegate {
167
167
public func disconnect( forceTimeout forceTimeout: NSTimeInterval ? = nil ) {
168
168
switch forceTimeout {
169
169
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 )
172
172
}
173
173
fallthrough
174
174
case . None:
@@ -309,13 +309,12 @@ public class WebSocket : NSObject, NSStreamDelegate {
309
309
let bytes = UnsafePointer < UInt8 > ( data. bytes)
310
310
var timeout = 5000000 //wait 5 seconds before giving up
311
311
writeQueue. addOperationWithBlock { [ weak self] in
312
- guard let this = self else { return }
313
312
while !outStream. hasSpaceAvailable {
314
313
usleep ( 100 ) //wait until the socket is ready
315
314
timeout -= 100
316
315
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 ) )
319
318
return
320
319
} else if outStream. streamError != nil {
321
320
return //disconnectStream will be called.
@@ -352,7 +351,11 @@ public class WebSocket : NSObject, NSStreamDelegate {
352
351
}
353
352
//disconnect the stream object
354
353
private func disconnectStream( error: NSError? ) {
355
- writeQueue. waitUntilAllOperationsAreFinished ( )
354
+ if error == nil {
355
+ writeQueue. waitUntilAllOperationsAreFinished ( )
356
+ } else {
357
+ writeQueue. cancelAllOperations ( )
358
+ }
356
359
cleanupStream ( )
357
360
doDisconnect ( error)
358
361
}
@@ -814,7 +817,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
814
817
815
818
}
816
819
817
- private class SSLCert {
820
+ public class SSLCert {
818
821
var certData : NSData ?
819
822
var key : SecKeyRef ?
820
823
@@ -825,7 +828,7 @@ private class SSLCert {
825
828
826
829
- returns: a representation security object to be used with
827
830
*/
828
- init ( data: NSData) {
831
+ public init( data: NSData) {
829
832
self . certData = data
830
833
}
831
834
@@ -836,13 +839,13 @@ private class SSLCert {
836
839
837
840
- returns: a representation security object to be used with
838
841
*/
839
- init ( key: SecKeyRef) {
842
+ public init( key: SecKeyRef) {
840
843
self . key = key
841
844
}
842
845
}
843
846
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?
846
849
847
850
var isReady = false //is the key processing done?
848
851
var certificates : [ NSData ] ? //the certificates
@@ -856,10 +859,11 @@ private class SSLSecurity {
856
859
857
860
- returns: a representation security object to be used with
858
861
*/
859
- convenience init( usePublicKeys: Bool = false ) {
862
+ public convenience init( usePublicKeys: Bool = false ) {
860
863
let paths = NSBundle . mainBundle ( ) . pathsForResourcesOfType ( " cer " , inDirectory: " . " )
861
864
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
863
867
if let data = NSData ( contentsOfFile: path) {
864
868
certs. append ( SSLCert ( data: data) )
865
869
}
@@ -877,12 +881,13 @@ private class SSLSecurity {
877
881
878
882
- returns: a representation security object to be used with
879
883
*/
880
- init ( certs: [ SSLCert] , usePublicKeys: Bool) {
884
+ public init( certs: [ SSLCert] , usePublicKeys: Bool) {
881
885
self . usePublicKeys = usePublicKeys
882
886
883
887
if self . usePublicKeys {
884
888
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
886
891
if let data = cert. certData where cert. key == nil {
887
892
cert. key = self . extractPublicKey ( data)
888
893
}
@@ -896,7 +901,8 @@ private class SSLSecurity {
896
901
self . isReady = true
897
902
}
898
903
} 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
900
906
if let data = cert. certData {
901
907
certificates. append ( data)
902
908
}
@@ -915,7 +921,7 @@ private class SSLSecurity {
915
921
916
922
- returns: if the key was successfully validated
917
923
*/
918
- func isValid( trust: SecTrustRef , domain: String ? ) -> Bool {
924
+ public func isValid( trust: SecTrustRef , domain: String ? ) -> Bool {
919
925
920
926
var tries = 0
921
927
while ( !self . isReady) {
@@ -1010,7 +1016,8 @@ private class SSLSecurity {
1010
1016
- returns: the certificate chain for the trust
1011
1017
*/
1012
1018
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
1014
1021
let cert = SecTrustGetCertificateAtIndex ( trust, index)
1015
1022
certificates. append ( SecCertificateCopyData ( cert!) )
1016
1023
return certificates
@@ -1028,7 +1035,8 @@ private class SSLSecurity {
1028
1035
*/
1029
1036
func publicKeyChainForTrust( trust: SecTrustRef ) -> [ SecKeyRef ] {
1030
1037
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
1032
1040
let cert = SecTrustGetCertificateAtIndex ( trust, index)
1033
1041
if let key = extractPublicKeyFromCert ( cert!, policy: policy) {
1034
1042
keys. append ( key)
0 commit comments