Skip to content

Commit 8042053

Browse files
committed
bump websocket version
1 parent 3e3ba60 commit 8042053

File tree

4 files changed

+59
-67
lines changed

4 files changed

+59
-67
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Carthage
9595
-----------------
9696
Add this line to your `Cartfile`:
9797
```
98-
github "socketio/socket.io-client-swift" ~> 8.0.2 # Or latest version
98+
github "socketio/socket.io-client-swift" ~> 8.0.3 # Or latest version
9999
```
100100

101101
Run `carthage update --platform ios,macosx`.
@@ -108,7 +108,7 @@ Create `Podfile` and add `pod 'Socket.IO-Client-Swift'`:
108108
use_frameworks!
109109

110110
target 'YourApp' do
111-
pod 'Socket.IO-Client-Swift', '~> 8.0.2' # Or latest version
111+
pod 'Socket.IO-Client-Swift', '~> 8.0.3' # Or latest version
112112
end
113113
```
114114

@@ -137,7 +137,7 @@ CocoaSeeds
137137
Add this line to your `Seedfile`:
138138

139139
```
140-
github "socketio/socket.io-client-swift", "v8.0.2", :files => "Source/*.swift" # Or latest version
140+
github "socketio/socket.io-client-swift", "v8.0.3", :files => "Source/*.swift" # Or latest version
141141
```
142142

143143
Run `seed install`.

Socket.IO-Client-Swift.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22
s.name = "Socket.IO-Client-Swift"
33
s.module_name = "SocketIO"
4-
s.version = "8.0.2"
4+
s.version = "8.0.3"
55
s.summary = "Socket.IO-client for iOS and OS X"
66
s.description = <<-DESC
77
Socket.IO-client for iOS and OS X.
@@ -14,7 +14,7 @@ Pod::Spec.new do |s|
1414
s.ios.deployment_target = '8.0'
1515
s.osx.deployment_target = '10.10'
1616
s.tvos.deployment_target = '9.0'
17-
s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v8.0.2' }
17+
s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v8.0.3' }
1818
s.source_files = "Source/**/*.swift"
1919
s.requires_arc = true
2020
# s.dependency 'Starscream', '~> 0.9' # currently this repo includes Starscream swift files

Source/SSLSecurity.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
// limitations under the License.
2020
//
2121
//////////////////////////////////////////////////////////////////////////////////////////////////
22-
2322
import Foundation
2423
import Security
2524

25+
public protocol SSLTrustValidator {
26+
func isValid(_ trust: SecTrust, domain: String?) -> Bool
27+
}
28+
2629
open class SSLCert {
2730
var certData: Data?
2831
var key: SecKey?
@@ -50,7 +53,7 @@ open class SSLCert {
5053
}
5154
}
5255

53-
open class SSLSecurity {
56+
open class SSLSecurity : SSLTrustValidator {
5457
public var validatedDN = true //should the domain name be validated?
5558

5659
var isReady = false //is the key processing done?

Source/WebSocket.swift

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
// limitations under the License.
1919
//
2020
//////////////////////////////////////////////////////////////////////////////////////////////////
21-
2221
import Foundation
2322
import CoreFoundation
2423
import Security
@@ -77,7 +76,6 @@ open class WebSocket : NSObject, StreamDelegate {
7776
var optionalProtocols: [String]?
7877

7978
// MARK: - Constants
80-
8179
let headerWSUpgradeName = "Upgrade"
8280
let headerWSUpgradeValue = "websocket"
8381
let headerWSHostName = "Host"
@@ -108,7 +106,6 @@ open class WebSocket : NSObject, StreamDelegate {
108106
}
109107

110108
// MARK: - Delegates
111-
112109
/// Responds to callback about new messages coming in over the WebSocket
113110
/// and also connection/disconnect messages.
114111
public weak var delegate: WebSocketDelegate?
@@ -118,7 +115,6 @@ open class WebSocket : NSObject, StreamDelegate {
118115

119116

120117
// MARK: - Block based API.
121-
122118
public var onConnect: ((Void) -> Void)?
123119
public var onDisconnect: ((NSError?) -> Void)?
124120
public var onText: ((String) -> Void)?
@@ -128,7 +124,7 @@ open class WebSocket : NSObject, StreamDelegate {
128124
public var headers = [String: String]()
129125
public var voipEnabled = false
130126
public var disableSSLCertValidation = false
131-
public var security: SSLSecurity?
127+
public var security: SSLTrustValidator?
132128
public var enabledSSLCipherSuites: [SSLCipherSuite]?
133129
public var origin: String?
134130
public var timeout = 5
@@ -139,7 +135,6 @@ open class WebSocket : NSObject, StreamDelegate {
139135
public var currentURL: URL { return url }
140136

141137
// MARK: - Private
142-
143138
private var url: URL
144139
private var inputStream: InputStream?
145140
private var outputStream: OutputStream?
@@ -190,11 +185,8 @@ open class WebSocket : NSObject, StreamDelegate {
190185

191186
/**
192187
Disconnect from the server. I send a Close control frame to the server, then expect the server to respond with a Close control frame and close the socket from its end. I notify my delegate once the socket has been closed.
193-
194188
If you supply a non-nil `forceTimeout`, I wait at most that long (in seconds) for the server to close the socket. After the timeout expires, I close the socket and notify my delegate.
195-
196189
If you supply a zero (or negative) `forceTimeout`, I immediately close the socket (without sending a Close control frame) and notify my delegate.
197-
198190
- Parameter forceTimeout: Maximum time to wait for the server to close the socket.
199191
- Parameter closeCode: The code to send on disconnect. The default is the normal close code for cleanly disconnecting a webSocket.
200192
*/
@@ -216,9 +208,7 @@ open class WebSocket : NSObject, StreamDelegate {
216208

217209
/**
218210
Write a string to the websocket. This sends it as a text frame.
219-
220211
If you supply a non-nil completion block, I will perform it when the write completes.
221-
222212
- parameter string: The string to write.
223213
- parameter completion: The (optional) completion handler.
224214
*/
@@ -229,9 +219,7 @@ open class WebSocket : NSObject, StreamDelegate {
229219

230220
/**
231221
Write binary data to the websocket. This sends it as a binary frame.
232-
233222
If you supply a non-nil completion block, I will perform it when the write completes.
234-
235223
- parameter data: The data to write.
236224
- parameter completion: The (optional) completion handler.
237225
*/
@@ -313,7 +301,6 @@ open class WebSocket : NSObject, StreamDelegate {
313301
private func initStreamsWithData(_ data: Data, _ port: Int) {
314302
//higher level API we will cut over to at some point
315303
//NSStream.getStreamsToHostWithName(url.host, port: url.port.integerValue, inputStream: &inputStream, outputStream: &outputStream)
316-
317304
var readStream: Unmanaged<CFReadStream>?
318305
var writeStream: Unmanaged<CFWriteStream>?
319306
let h = url.host! as NSString
@@ -326,35 +313,35 @@ open class WebSocket : NSObject, StreamDelegate {
326313
if supportedSSLSchemes.contains(url.scheme!) {
327314
inStream.setProperty(StreamSocketSecurityLevel.negotiatedSSL as AnyObject, forKey: Stream.PropertyKey.socketSecurityLevelKey)
328315
outStream.setProperty(StreamSocketSecurityLevel.negotiatedSSL as AnyObject, forKey: Stream.PropertyKey.socketSecurityLevelKey)
316+
if disableSSLCertValidation {
317+
let settings: [NSObject: NSObject] = [kCFStreamSSLValidatesCertificateChain: NSNumber(value: false), kCFStreamSSLPeerName: kCFNull]
318+
inStream.setProperty(settings, forKey: kCFStreamPropertySSLSettings as Stream.PropertyKey)
319+
outStream.setProperty(settings, forKey: kCFStreamPropertySSLSettings as Stream.PropertyKey)
320+
}
321+
if let cipherSuites = self.enabledSSLCipherSuites {
322+
if let sslContextIn = CFReadStreamCopyProperty(inputStream, CFStreamPropertyKey(rawValue: kCFStreamPropertySSLContext)) as! SSLContext?,
323+
let sslContextOut = CFWriteStreamCopyProperty(outputStream, CFStreamPropertyKey(rawValue: kCFStreamPropertySSLContext)) as! SSLContext? {
324+
let resIn = SSLSetEnabledCiphers(sslContextIn, cipherSuites, cipherSuites.count)
325+
let resOut = SSLSetEnabledCiphers(sslContextOut, cipherSuites, cipherSuites.count)
326+
if resIn != errSecSuccess {
327+
let error = self.errorWithDetail("Error setting ingoing cypher suites", code: UInt16(resIn))
328+
disconnectStream(error)
329+
return
330+
}
331+
if resOut != errSecSuccess {
332+
let error = self.errorWithDetail("Error setting outgoing cypher suites", code: UInt16(resOut))
333+
disconnectStream(error)
334+
return
335+
}
336+
}
337+
}
329338
} else {
330339
certValidated = true //not a https session, so no need to check SSL pinning
331340
}
332341
if voipEnabled {
333342
inStream.setProperty(StreamNetworkServiceTypeValue.voIP as AnyObject, forKey: Stream.PropertyKey.networkServiceType)
334343
outStream.setProperty(StreamNetworkServiceTypeValue.voIP as AnyObject, forKey: Stream.PropertyKey.networkServiceType)
335344
}
336-
if disableSSLCertValidation {
337-
let settings: [NSObject: NSObject] = [kCFStreamSSLValidatesCertificateChain: NSNumber(value: false), kCFStreamSSLPeerName: kCFNull]
338-
inStream.setProperty(settings, forKey: kCFStreamPropertySSLSettings as Stream.PropertyKey)
339-
outStream.setProperty(settings, forKey: kCFStreamPropertySSLSettings as Stream.PropertyKey)
340-
}
341-
if let cipherSuites = self.enabledSSLCipherSuites {
342-
if let sslContextIn = CFReadStreamCopyProperty(inputStream, CFStreamPropertyKey(rawValue: kCFStreamPropertySSLContext)) as! SSLContext?,
343-
let sslContextOut = CFWriteStreamCopyProperty(outputStream, CFStreamPropertyKey(rawValue: kCFStreamPropertySSLContext)) as! SSLContext? {
344-
let resIn = SSLSetEnabledCiphers(sslContextIn, cipherSuites, cipherSuites.count)
345-
let resOut = SSLSetEnabledCiphers(sslContextOut, cipherSuites, cipherSuites.count)
346-
if resIn != errSecSuccess {
347-
let error = self.errorWithDetail("Error setting ingoing cypher suites", code: UInt16(resIn))
348-
disconnectStream(error)
349-
return
350-
}
351-
if resOut != errSecSuccess {
352-
let error = self.errorWithDetail("Error setting outgoing cypher suites", code: UInt16(resOut))
353-
disconnectStream(error)
354-
return
355-
}
356-
}
357-
}
358345

359346
CFReadStreamSetDispatchQueue(inStream, WebSocket.sharedWorkQueue)
360347
CFWriteStreamSetDispatchQueue(outStream, WebSocket.sharedWorkQueue)
@@ -447,7 +434,6 @@ open class WebSocket : NSObject, StreamDelegate {
447434
let buf = NSMutableData(capacity: BUFFER_MAX)
448435
let buffer = UnsafeMutableRawPointer(mutating: buf!.bytes).assumingMemoryBound(to: UInt8.self)
449436
let length = inputStream!.read(buffer, maxLength: BUFFER_MAX)
450-
451437
guard length > 0 else { return }
452438
var process = false
453439
if inputQueue.count == 0 {
@@ -643,34 +629,22 @@ open class WebSocket : NSObject, StreamDelegate {
643629
writeError(errCode)
644630
return emptyBuffer
645631
}
632+
var closeCode = CloseCode.normal.rawValue
646633
if receivedOpcode == .connectionClose {
647-
var code = CloseCode.normal.rawValue
648634
if payloadLen == 1 {
649-
code = CloseCode.protocolError.rawValue
635+
closeCode = CloseCode.protocolError.rawValue
650636
} else if payloadLen > 1 {
651-
code = WebSocket.readUint16(baseAddress, offset: offset)
652-
if code < 1000 || (code > 1003 && code < 1007) || (code > 1011 && code < 3000) {
653-
code = CloseCode.protocolError.rawValue
637+
closeCode = WebSocket.readUint16(baseAddress, offset: offset)
638+
if closeCode < 1000 || (closeCode > 1003 && closeCode < 1007) || (closeCode > 1011 && closeCode < 3000) {
639+
closeCode = CloseCode.protocolError.rawValue
654640
}
655-
offset += 2
656641
}
657-
var closeReason = "connection closed by server"
658-
if payloadLen > 2 {
659-
let len = Int(payloadLen - 2)
660-
if len > 0 {
661-
let bytes = baseAddress + offset
662-
if let customCloseReason = String(data: Data(bytes: bytes, count: len), encoding: .utf8) {
663-
closeReason = customCloseReason
664-
} else {
665-
code = CloseCode.protocolError.rawValue
666-
}
667-
}
642+
if payloadLen < 2 {
643+
doDisconnect(errorWithDetail("connection closed by server", code: closeCode))
644+
writeError(closeCode)
645+
return emptyBuffer
668646
}
669-
doDisconnect(errorWithDetail(closeReason, code: code))
670-
writeError(code)
671-
return emptyBuffer
672-
}
673-
if isControlFrame && payloadLen > 125 {
647+
} else if isControlFrame && payloadLen > 125 {
674648
writeError(CloseCode.protocolError.rawValue)
675649
return emptyBuffer
676650
}
@@ -695,8 +669,24 @@ open class WebSocket : NSObject, StreamDelegate {
695669
len = 0
696670
data = Data()
697671
} else {
672+
if receivedOpcode == .connectionClose && len > 0 {
673+
let size = MemoryLayout<UInt16>.size
674+
offset += size
675+
len -= UInt64(size)
676+
}
698677
data = Data(bytes: baseAddress+offset, count: Int(len))
699678
}
679+
if receivedOpcode == .connectionClose {
680+
var closeReason = "connection closed by server"
681+
if let customCloseReason = String(data: data, encoding: .utf8) {
682+
closeReason = customCloseReason
683+
} else {
684+
closeCode = CloseCode.protocolError.rawValue
685+
}
686+
doDisconnect(errorWithDetail(closeReason, code: closeCode))
687+
writeError(closeCode)
688+
return emptyBuffer
689+
}
700690
if receivedOpcode == .pong {
701691
if canDispatch {
702692
callbackQueue.async { [weak self] in
@@ -910,7 +900,6 @@ open class WebSocket : NSObject, StreamDelegate {
910900
}
911901

912902
// MARK: - Deinit
913-
914903
deinit {
915904
mutex.lock()
916905
readyToWrite = false

0 commit comments

Comments
 (0)