diff --git a/README.md b/README.md index 73f2882..de43c9d 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ import PackageDescription let package = Package( name: "YourSocketIOProject", dependencies: [ - .Package(url: "https://github.com/socketio/socket.io-client-swift", majorVersion: 5) + .Package(url: "https://github.com/socketio/socket.io-client-swift", majorVersion: 6) ] ) ``` @@ -88,7 +88,7 @@ Carthage ----------------- Add this line to your `Cartfile`: ``` -github "socketio/socket.io-client-swift" ~> 6.0.0 # Or latest version +github "socketio/socket.io-client-swift" ~> 6.1.1 # Or latest version ``` Run `carthage update --platform ios,macosx`. @@ -102,7 +102,7 @@ source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' use_frameworks! -pod 'Socket.IO-Client-Swift', '~> 6.0.0' # Or latest version +pod 'Socket.IO-Client-Swift', '~> 6.1.1' # Or latest version ``` Install pods: @@ -130,7 +130,7 @@ CocoaSeeds Add this line to your `Seedfile`: ``` -github "socketio/socket.io-client-swift", "v6.0.0", :files => "Source/*.swift" # Or latest version +github "socketio/socket.io-client-swift", "v6.1.1", :files => "Source/*.swift" # Or latest version ``` Run `seed install`. diff --git a/Socket.IO-Client-Swift.podspec b/Socket.IO-Client-Swift.podspec index 42a8ef9..248adc5 100644 --- a/Socket.IO-Client-Swift.podspec +++ b/Socket.IO-Client-Swift.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "Socket.IO-Client-Swift" s.module_name = "SocketIOClientSwift" - s.version = "6.0.0" + s.version = "6.1.1" s.summary = "Socket.IO-client for iOS and OS X" s.description = <<-DESC Socket.IO-client for iOS and OS X. @@ -14,7 +14,7 @@ Pod::Spec.new do |s| s.ios.deployment_target = '8.0' s.osx.deployment_target = '10.10' s.tvos.deployment_target = '9.0' - s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v6.0.0' } + s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v6.1.1' } s.source_files = "Source/**/*.swift" s.requires_arc = true # s.dependency 'Starscream', '~> 0.9' # currently this repo includes Starscream swift files diff --git a/Socket.IO-Client-Swift.xcodeproj/project.pbxproj b/Socket.IO-Client-Swift.xcodeproj/project.pbxproj index f209032..c373933 100644 --- a/Socket.IO-Client-Swift.xcodeproj/project.pbxproj +++ b/Socket.IO-Client-Swift.xcodeproj/project.pbxproj @@ -101,11 +101,6 @@ 74171EC51C10CD240062D398 /* SocketTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E601C10CD240062D398 /* SocketTypes.swift */; }; 74171EC71C10CD240062D398 /* SocketTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E601C10CD240062D398 /* SocketTypes.swift */; }; 74171EC81C10CD240062D398 /* SocketTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E601C10CD240062D398 /* SocketTypes.swift */; }; - 74171EC91C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; }; - 74171ECA1C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; }; - 74171ECB1C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; }; - 74171ECD1C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; }; - 74171ECE1C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; }; 74171ECF1C10CD240062D398 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E621C10CD240062D398 /* WebSocket.swift */; }; 74171ED01C10CD240062D398 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E621C10CD240062D398 /* WebSocket.swift */; }; 74171ED11C10CD240062D398 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E621C10CD240062D398 /* WebSocket.swift */; }; @@ -128,6 +123,12 @@ 74ABF7771C3991C10078C657 /* SocketIOClientSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74ABF7761C3991C10078C657 /* SocketIOClientSpec.swift */; }; 74F124F01BC574CF002966F4 /* SocketBasicPacketTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74F124EF1BC574CF002966F4 /* SocketBasicPacketTest.swift */; }; 74F124F11BC574CF002966F4 /* SocketBasicPacketTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74F124EF1BC574CF002966F4 /* SocketBasicPacketTest.swift */; }; + CEBA56961CDA0B7700BA0389 /* NSCharacterSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEBA56951CDA0B7700BA0389 /* NSCharacterSet.swift */; }; + CEBA56971CDA0B7700BA0389 /* NSCharacterSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEBA56951CDA0B7700BA0389 /* NSCharacterSet.swift */; }; + CEBA56981CDA0B7700BA0389 /* NSCharacterSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEBA56951CDA0B7700BA0389 /* NSCharacterSet.swift */; }; + CEBA569A1CDA0B8200BA0389 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEBA56991CDA0B8200BA0389 /* String.swift */; }; + CEBA569B1CDA0B8200BA0389 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEBA56991CDA0B8200BA0389 /* String.swift */; }; + CEBA569C1CDA0B8200BA0389 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEBA56991CDA0B8200BA0389 /* String.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -184,7 +185,6 @@ 74171E5E1C10CD240062D398 /* SocketParsable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketParsable.swift; path = Source/SocketParsable.swift; sourceTree = ""; }; 74171E5F1C10CD240062D398 /* SocketStringReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketStringReader.swift; path = Source/SocketStringReader.swift; sourceTree = ""; }; 74171E601C10CD240062D398 /* SocketTypes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketTypes.swift; path = Source/SocketTypes.swift; sourceTree = ""; }; - 74171E611C10CD240062D398 /* SwiftRegex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SwiftRegex.swift; path = Source/SwiftRegex.swift; sourceTree = ""; }; 74171E621C10CD240062D398 /* WebSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WebSocket.swift; path = Source/WebSocket.swift; sourceTree = ""; }; 741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketEngineTest.swift; sourceTree = ""; }; 7420CB781C49629E00956AA4 /* SocketEnginePollable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketEnginePollable.swift; path = Source/SocketEnginePollable.swift; sourceTree = ""; }; @@ -195,6 +195,8 @@ 7472C65E1BCAC46E003CA70D /* SocketSideEffectTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketSideEffectTest.swift; sourceTree = ""; }; 74ABF7761C3991C10078C657 /* SocketIOClientSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketIOClientSpec.swift; path = Source/SocketIOClientSpec.swift; sourceTree = ""; }; 74F124EF1BC574CF002966F4 /* SocketBasicPacketTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketBasicPacketTest.swift; sourceTree = ""; }; + CEBA56951CDA0B7700BA0389 /* NSCharacterSet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NSCharacterSet.swift; path = Source/NSCharacterSet.swift; sourceTree = ""; }; + CEBA56991CDA0B8200BA0389 /* String.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = String.swift; path = Source/String.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -347,6 +349,7 @@ 5764DF7B1B51F24A004FF46E /* Source */ = { isa = PBXGroup; children = ( + CEBA569E1CDA0C0C00BA0389 /* utils */, 74171E501C10CD240062D398 /* SocketAckEmitter.swift */, 74171E511C10CD240062D398 /* SocketAckManager.swift */, 74171E521C10CD240062D398 /* SocketAnyEvent.swift */, @@ -366,12 +369,20 @@ 74171E5E1C10CD240062D398 /* SocketParsable.swift */, 74171E5F1C10CD240062D398 /* SocketStringReader.swift */, 74171E601C10CD240062D398 /* SocketTypes.swift */, - 74171E611C10CD240062D398 /* SwiftRegex.swift */, 74171E621C10CD240062D398 /* WebSocket.swift */, ); name = Source; sourceTree = ""; }; + CEBA569E1CDA0C0C00BA0389 /* utils */ = { + isa = PBXGroup; + children = ( + CEBA56951CDA0B7700BA0389 /* NSCharacterSet.swift */, + CEBA56991CDA0B8200BA0389 /* String.swift */, + ); + name = utils; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ @@ -607,9 +618,11 @@ files = ( 740CA1221C496EF700CB98F4 /* SocketEngineWebsocket.swift in Sources */, 74171EA51C10CD240062D398 /* SocketIOClientStatus.swift in Sources */, + CEBA569A1CDA0B8200BA0389 /* String.swift in Sources */, 74171E751C10CD240062D398 /* SocketEngine.swift in Sources */, 74171E691C10CD240062D398 /* SocketAckManager.swift in Sources */, 7420CB791C49629E00956AA4 /* SocketEnginePollable.swift in Sources */, + CEBA56961CDA0B7700BA0389 /* NSCharacterSet.swift in Sources */, 74ABF7771C3991C10078C657 /* SocketIOClientSpec.swift in Sources */, 74171E871C10CD240062D398 /* SocketEngineSpec.swift in Sources */, 74171E631C10CD240062D398 /* SocketAckEmitter.swift in Sources */, @@ -618,7 +631,6 @@ 74171EAB1C10CD240062D398 /* SocketLogger.swift in Sources */, 74171E991C10CD240062D398 /* SocketIOClient.swift in Sources */, 74171E8D1C10CD240062D398 /* SocketEventHandler.swift in Sources */, - 74171EC91C10CD240062D398 /* SwiftRegex.swift in Sources */, 74171E7B1C10CD240062D398 /* SocketEngineClient.swift in Sources */, 74171ECF1C10CD240062D398 /* WebSocket.swift in Sources */, 74171EB11C10CD240062D398 /* SocketPacket.swift in Sources */, @@ -643,7 +655,6 @@ 74171E8E1C10CD240062D398 /* SocketEventHandler.swift in Sources */, 74171E7C1C10CD240062D398 /* SocketEngineClient.swift in Sources */, 74171E821C10CD240062D398 /* SocketEnginePacketType.swift in Sources */, - 74171ECA1C10CD240062D398 /* SwiftRegex.swift in Sources */, 74171EB21C10CD240062D398 /* SocketPacket.swift in Sources */, 741F39EE1BD025D80026C9CC /* SocketEngineTest.swift in Sources */, 74171EBE1C10CD240062D398 /* SocketStringReader.swift in Sources */, @@ -664,9 +675,11 @@ files = ( 740CA1211C496EF200CB98F4 /* SocketEngineWebsocket.swift in Sources */, 7471CCEA1C39926300364B59 /* SocketIOClientSpec.swift in Sources */, + CEBA569B1CDA0B8200BA0389 /* String.swift in Sources */, 74171EA71C10CD240062D398 /* SocketIOClientStatus.swift in Sources */, 74171E771C10CD240062D398 /* SocketEngine.swift in Sources */, 7420CB7A1C49629E00956AA4 /* SocketEnginePollable.swift in Sources */, + CEBA56971CDA0B7700BA0389 /* NSCharacterSet.swift in Sources */, 74171E6B1C10CD240062D398 /* SocketAckManager.swift in Sources */, 74171E891C10CD240062D398 /* SocketEngineSpec.swift in Sources */, 74171E651C10CD240062D398 /* SocketAckEmitter.swift in Sources */, @@ -675,7 +688,6 @@ 74171EAD1C10CD240062D398 /* SocketLogger.swift in Sources */, 74171E9B1C10CD240062D398 /* SocketIOClient.swift in Sources */, 74171E8F1C10CD240062D398 /* SocketEventHandler.swift in Sources */, - 74171ECB1C10CD240062D398 /* SwiftRegex.swift in Sources */, 74171E7D1C10CD240062D398 /* SocketEngineClient.swift in Sources */, 74171ED11C10CD240062D398 /* WebSocket.swift in Sources */, 74171EB31C10CD240062D398 /* SocketPacket.swift in Sources */, @@ -707,9 +719,11 @@ files = ( 740CA1201C496EEB00CB98F4 /* SocketEngineWebsocket.swift in Sources */, 7471CCEB1C39926C00364B59 /* SocketIOClientSpec.swift in Sources */, + CEBA569C1CDA0B8200BA0389 /* String.swift in Sources */, 74171EA91C10CD240062D398 /* SocketIOClientStatus.swift in Sources */, 74171E791C10CD240062D398 /* SocketEngine.swift in Sources */, 7420CB7B1C49629E00956AA4 /* SocketEnginePollable.swift in Sources */, + CEBA56981CDA0B7700BA0389 /* NSCharacterSet.swift in Sources */, 74171E6D1C10CD240062D398 /* SocketAckManager.swift in Sources */, 74171E8B1C10CD240062D398 /* SocketEngineSpec.swift in Sources */, 74171E671C10CD240062D398 /* SocketAckEmitter.swift in Sources */, @@ -718,7 +732,6 @@ 74171EAF1C10CD240062D398 /* SocketLogger.swift in Sources */, 74171E9D1C10CD240062D398 /* SocketIOClient.swift in Sources */, 74171E911C10CD240062D398 /* SocketEventHandler.swift in Sources */, - 74171ECD1C10CD240062D398 /* SwiftRegex.swift in Sources */, 74171E7F1C10CD240062D398 /* SocketEngineClient.swift in Sources */, 74171ED31C10CD240062D398 /* WebSocket.swift in Sources */, 74171EB51C10CD240062D398 /* SocketPacket.swift in Sources */, @@ -743,7 +756,6 @@ 74171E921C10CD240062D398 /* SocketEventHandler.swift in Sources */, 74171E801C10CD240062D398 /* SocketEngineClient.swift in Sources */, 74171E861C10CD240062D398 /* SocketEnginePacketType.swift in Sources */, - 74171ECE1C10CD240062D398 /* SwiftRegex.swift in Sources */, 74171EB61C10CD240062D398 /* SocketPacket.swift in Sources */, 57634A2A1BD9B46D00E19CD7 /* SocketEngineTest.swift in Sources */, 74171EC21C10CD240062D398 /* SocketStringReader.swift in Sources */, diff --git a/SocketIO-MacTests/SocketBasicPacketTest.swift b/SocketIO-MacTests/SocketBasicPacketTest.swift index c8ef466..91fe75e 100644 --- a/SocketIO-MacTests/SocketBasicPacketTest.swift +++ b/SocketIO-MacTests/SocketBasicPacketTest.swift @@ -145,4 +145,20 @@ class SocketBasicPacketTest: XCTestCase { XCTAssertEqual(packet.packetString, expectedSendString) XCTAssertEqual(packet.binary, [data2, data]) } + + func testBinaryStringPlaceholderInMessage() { + let engineString = "52-[\"test\",\"~~0\",{\"num\":0,\"_placeholder\":true},{\"num\":1,\"_placeholder\":true}]" + let socket = SocketIOClient(socketURL: NSURL()) + socket.setTestable() + + if case let .Right(packet) = socket.parseString(engineString) { + var packet = packet + XCTAssertEqual(packet.event, "test") + packet.addData(data) + packet.addData(data2) + XCTAssertEqual(packet.args[0] as? String, "~~0") + } else { + XCTFail() + } + } } diff --git a/SocketIO-MacTests/SocketEngineTest.swift b/SocketIO-MacTests/SocketEngineTest.swift index 24f46e0..38c08aa 100644 --- a/SocketIO-MacTests/SocketEngineTest.swift +++ b/SocketIO-MacTests/SocketEngineTest.swift @@ -86,4 +86,20 @@ class SocketEngineTest: XCTestCase { engine.parsePollingMessage("41:42[\"stringTest\",\"lïne one\\nlīne \\rtwo\"]") waitForExpectations(withTimeout: 3, handler: nil) } + + func testEncodeURLProperly() { + engine.connectParams = [ + "created": "2016-05-04T18:31:15+0200" + ] + + XCTAssertEqual(engine.urlPolling.query, "transport=polling&b64=1&created=2016-05-04T18%3A31%3A15%2B0200") + XCTAssertEqual(engine.urlWebSocket.query, "transport=websocket&created=2016-05-04T18%3A31%3A15%2B0200") + + engine.connectParams = [ + "forbidden": "!*'();:@&=+$,/?%#[]\" {}" + ] + + XCTAssertEqual(engine.urlPolling.query, "transport=polling&b64=1&forbidden=%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%25%23%5B%5D%22%20%7B%7D") + XCTAssertEqual(engine.urlWebSocket.query, "transport=websocket&forbidden=%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%25%23%5B%5D%22%20%7B%7D") + } } diff --git a/SocketIO-MacTests/SocketObjectiveCTest.m b/SocketIO-MacTests/SocketObjectiveCTest.m index 95b49be..7a3b610 100644 --- a/SocketIO-MacTests/SocketObjectiveCTest.m +++ b/SocketIO-MacTests/SocketObjectiveCTest.m @@ -35,7 +35,9 @@ } - (void)testEmitWithAckSyntax { - [self.socket emitWithAck:@"testAckEmit" with:@[@YES]]; + [self.socket emitWithAck:@"testAckEmit" with:@[@YES]](0, ^(NSArray* data) { + + }); } - (void)testOffSyntax { diff --git a/SocketIO-MacTests/SocketParserTest.swift b/SocketIO-MacTests/SocketParserTest.swift index 44a8fca..43e66d1 100644 --- a/SocketIO-MacTests/SocketParserTest.swift +++ b/SocketIO-MacTests/SocketParserTest.swift @@ -18,9 +18,10 @@ class SocketParserTest: XCTestCase { "25[\"test\"]": ("/", ["test"], [], 5), "2[\"test\",\"~~0\"]": ("/", ["test", "~~0"], [], -1), "2/swift,[\"testArrayEmitReturn\",[\"test3\",\"test4\"]]": ("/swift", ["testArrayEmitReturn", ["test3", "test4"]], [], -1), - "51-/swift,[\"testMultipleItemsWithBufferEmitReturn\",[1,2],{\"test\":\"bob\"},25,\"polo\",{\"_placeholder\":true,\"num\":0}]": ("/swift", ["testMultipleItemsWithBufferEmitReturn", [1, 2], ["test": "bob"], 25, "polo", "~~0"], [], -1), + "51-/swift,[\"testMultipleItemsWithBufferEmitReturn\",[1,2],{\"test\":\"bob\"},25,\"polo\",{\"_placeholder\":true,\"num\":0}]": ("/swift", ["testMultipleItemsWithBufferEmitReturn", [1, 2], ["test": "bob"], 25, "polo", ["_placeholder": true, "num": 0]], [], -1), "3/swift,0[[\"test3\",\"test4\"]]": ("/swift", [["test3", "test4"]], [], 0), - "61-/swift,19[[1,2],{\"test\":\"bob\"},25,\"polo\",{\"_placeholder\":true,\"num\":0}]": ("/swift", [ [1, 2], ["test": "bob"], 25, "polo", "~~0"], [], 19), + "61-/swift,19[[1,2],{\"test\":\"bob\"},25,\"polo\",{\"_placeholder\":true,\"num\":0}]": + ("/swift", [ [1, 2], ["test": "bob"], 25, "polo", ["_placeholder": true, "num": 0]], [], 19), "4/swift,": ("/swift", [], [], -1), "0/swift": ("/swift", [], [], -1), "1/swift": ("/swift", [], [], -1), @@ -123,8 +124,8 @@ class SocketParserTest: XCTestCase { if case let .Right(packet) = packet { XCTAssertEqual(packet.type, SocketPacket.PacketType(rawValue: Int(type) ?? -1)!) XCTAssertEqual(packet.nsp, validValues.0) - XCTAssertTrue((packet.data as NSArray).isEqual(to: validValues.1)) - XCTAssertTrue((packet.binary as NSArray).isEqual(to: validValues.2)) + XCTAssertTrue((packet.data as NSArray).isEqual(to: validValues.1), "\(packet.data)") + XCTAssertTrue((packet.binary as NSArray).isEqual(to: validValues.2), "\(packet.binary)") XCTAssertEqual(packet.id, validValues.3) } else { XCTFail() diff --git a/Source/NSCharacterSet.swift b/Source/NSCharacterSet.swift new file mode 100644 index 0000000..71a93b2 --- /dev/null +++ b/Source/NSCharacterSet.swift @@ -0,0 +1,31 @@ +// +// NSCharacterSet.swift +// Socket.IO-Client-Swift +// +// Created by Yannick Loriot on 5/4/16. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +import Foundation + +extension NSCharacterSet { + class var allowedURLCharacterSet: NSCharacterSet { + return NSCharacterSet(charactersIn: "!*'();:@&=+$,/?%#[]\" {}").inverted + } +} diff --git a/Source/SocketEngine.swift b/Source/SocketEngine.swift index 1583792..abd0bb4 100644 --- a/Source/SocketEngine.swift +++ b/Source/SocketEngine.swift @@ -34,6 +34,7 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe (urlPolling, urlWebSocket) = createURLs() } } + public var postWait = [String]() public var waitingForPoll = false public var waitingForPost = false @@ -133,23 +134,16 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe allowLossyConversion: false) else { return } do { - if let dict = try NSJSONSerialization.jsonObject(with: stringData, - options: NSJSONReadingOptions.mutableContainers) as? NSDictionary { - guard let code = dict["code"] as? Int else { return } - guard let error = dict["message"] as? String else { return } - - switch code { - case 0: // Unknown transport - didError(error: error) - case 1: // Unknown sid. - didError(error: error) - case 2: // Bad handshake request - didError(error: error) - case 3: // Bad request - didError(error: error) - default: - didError(error: error) - } + if let dict = try NSJSONSerialization.jsonObject(with: stringData, options: .mutableContainers) as? NSDictionary { + guard let error = dict["message"] as? String else { return } + + /* + 0: Unknown transport + 1: Unknown sid + 2: Bad handshake request + 3: Bad request + */ + didError(error: error) } } catch { didError(error: "Got unknown error from server \(msg)") @@ -204,7 +198,9 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe } } - doLongPoll(for: reqPolling) + dispatch_async(emitQueue) { + self.doLongPoll(for: reqPolling) + } } private func createURLs() -> (NSURL, NSURL) { @@ -218,8 +214,6 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe urlWebSocket.path = socketPath urlPolling.path = socketPath - urlWebSocket.query = "transport=websocket" - urlPolling.query = "transport=polling&b64=1" if secure { urlPolling.scheme = "https" @@ -231,12 +225,15 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe if connectParams != nil { for (key, value) in connectParams! { - queryString += "&\(key)=\(value)" + let keyEsc = key.urlEncode()! + let valueEsc = "\(value)".urlEncode()! + + queryString += "&\(keyEsc)=\(valueEsc)" } } - urlWebSocket.query = urlWebSocket.query! + queryString - urlPolling.query = urlPolling.query! + queryString + urlWebSocket.percentEncodedQuery = "transport=websocket" + queryString + urlPolling.percentEncodedQuery = "transport=polling&b64=1" + queryString return (urlPolling.url!, urlWebSocket.url!) } @@ -286,7 +283,8 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe DefaultSocketLogger.Logger.log("Engine is being closed.", type: logType) if closed { - return postSendClose(nil, nil, nil) + client?.engineDidClose(reason: reason) + return } if websocket { @@ -393,7 +391,6 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe } } catch { didError(error: "Error parsing open packet") - return } } diff --git a/Source/SocketEngineSpec.swift b/Source/SocketEngineSpec.swift index f1224bd..f2d8185 100644 --- a/Source/SocketEngineSpec.swift +++ b/Source/SocketEngineSpec.swift @@ -46,7 +46,7 @@ import Foundation var urlPolling: NSURL { get } var urlWebSocket: NSURL { get } var websocket: Bool { get } - optional var ws: WebSocket? { get } + var ws: WebSocket? { get } init(client: SocketEngineClient, url: NSURL, options: NSDictionary?) @@ -63,14 +63,14 @@ import Foundation extension SocketEngineSpec { var urlPollingWithSid: NSURL { let com = NSURLComponents(url: urlPolling, resolvingAgainstBaseURL: false)! - com.query = com.query! + "&sid=\(sid)" + com.percentEncodedQuery = com.percentEncodedQuery! + "&sid=\(sid.urlEncode()!)" return com.url! } var urlWebSocketWithSid: NSURL { let com = NSURLComponents(url: urlWebSocket, resolvingAgainstBaseURL: false)! - com.query = com.query! + (sid == "" ? "" : "&sid=\(sid)") + com.percentEncodedQuery = com.percentEncodedQuery! + (sid == "" ? "" : "&sid=\(sid.urlEncode()!)") return com.url! } diff --git a/Source/SocketEngineWebsocket.swift b/Source/SocketEngineWebsocket.swift index 1360a67..8132ed6 100644 --- a/Source/SocketEngineWebsocket.swift +++ b/Source/SocketEngineWebsocket.swift @@ -27,8 +27,6 @@ import Foundation /// Protocol that is used to implement socket.io WebSocket support public protocol SocketEngineWebsocket : SocketEngineSpec, WebSocketDelegate { - var ws: WebSocket? { get } - func sendWebSocketMessage(_ str: String, withType type: SocketEnginePacketType, withData datas: [NSData]) } diff --git a/Source/SocketIOClient.swift b/Source/SocketIOClient.swift index cc5fa0e..add4fce 100644 --- a/Source/SocketIOClient.swift +++ b/Source/SocketIOClient.swift @@ -131,8 +131,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable assert(timeoutAfter >= 0, "Invalid timeout: \(timeoutAfter)") guard status != .connected else { - DefaultSocketLogger.Logger.log("Tried connecting on an already connected socket", - type: logType) + DefaultSocketLogger.Logger.log("Tried connecting on an already connected socket", type: logType) return } @@ -149,8 +148,8 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeoutAfter) * Int64(NSEC_PER_SEC)) dispatch_after(time, handleQueue) {[weak self] in - if let this = self where this.status != .connected && this.status != .closed { - this.status = .closed + if let this = self where this.status != .connected && this.status != .disconnected { + this.status = .disconnected this.engine?.disconnect(reason: "Connect timeout") handler?() @@ -187,11 +186,11 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable } func didDisconnect(reason: String) { - guard status != .closed else { return } + guard status != .disconnected else { return } DefaultSocketLogger.Logger.log("Disconnected: %@", type: logType, args: reason) - status = .closed + status = .disconnected reconnects = false // Make sure the engine is actually dead. @@ -202,6 +201,8 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable /// Disconnects the socket. Only reconnect the same socket if you know what you're doing. /// Will turn off automatic reconnects. public func disconnect() { + assert(status != .notConnected, "Tried closing a NotConnected client") + DefaultSocketLogger.Logger.log("Closing socket", type: logType) reconnects = false @@ -267,11 +268,11 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable public func engineDidClose(reason: String) { waitingPackets.removeAll() - if status != .closed { + if status != .disconnected { status = .notConnected } - if status == .closed || !reconnects { + if status == .disconnected || !reconnects { didDisconnect(reason: reason) } else if !reconnecting { reconnecting = true diff --git a/Source/SocketIOClientStatus.swift b/Source/SocketIOClientStatus.swift index 7b91152..27574e6 100644 --- a/Source/SocketIOClientStatus.swift +++ b/Source/SocketIOClientStatus.swift @@ -24,19 +24,9 @@ import Foundation -@objc public enum SocketIOClientStatus : Int, CustomStringConvertible { - case notConnected, closed, connecting, connected - - public var description: String { - switch self { - case notConnected: - return "Not Connected" - case closed: - return "Closed" - case connecting: - return "Connecting" - case connected: - return "Connected" - } - } -} \ No newline at end of file +/// **NotConnected**: initial state +/// +/// **Disconnected**: connected before +@objc public enum SocketIOClientStatus : Int { + case notConnected, disconnected, connecting, connected +} diff --git a/Source/SocketPacket.swift b/Source/SocketPacket.swift index 8008909..23468f6 100644 --- a/Source/SocketPacket.swift +++ b/Source/SocketPacket.swift @@ -187,19 +187,21 @@ struct SocketPacket { data = data.map(_fillInPlaceholders) } - // Helper method that looks for placeholder strings + // Helper method that looks for placeholders // If object is a collection it will recurse // Returns the object if it is not a placeholder string or the corresponding // binary data private func _fillInPlaceholders(_ object: AnyObject) -> AnyObject { switch object { - case let string as String where string["~~(\\d)"].groups() != nil: - return binary[Int(string["~~(\\d)"].groups()![1])!] case let dict as NSDictionary: - return dict.reduce(NSMutableDictionary(), combine: {cur, keyValue in - cur[keyValue.0 as! NSCopying] = _fillInPlaceholders(keyValue.1) - return cur - }) + if dict["_placeholder"] as? Bool ?? false { + return binary[dict["num"] as! Int] + } else { + return dict.reduce(NSMutableDictionary(), combine: {cur, keyValue in + cur[keyValue.0 as! NSCopying] = _fillInPlaceholders(keyValue.1) + return cur + }) + } case let arr as [AnyObject]: return arr.map(_fillInPlaceholders) default: diff --git a/Source/SocketParsable.swift b/Source/SocketParsable.swift index 2f93895..d4cf386 100644 --- a/Source/SocketParsable.swift +++ b/Source/SocketParsable.swift @@ -111,12 +111,11 @@ extension SocketParsable { } let d = message[parser.currentIndex.advanced(by: 1).. String? { + return addingPercentEncoding(withAllowedCharacters: .allowedURLCharacterSet) + } +}