Skip to content

Commit 3434b45

Browse files
authored
Merge pull request #269 from zapcannon87/developer
fix(IM): WebSocket Request should not include "Origin" Header
2 parents da881c9 + 4b4b8c3 commit 3434b45

File tree

4 files changed

+77
-39
lines changed

4 files changed

+77
-39
lines changed

LeanCloudTests/LCInstallationTestCase.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,16 @@ class LCInstallationTestCase: BaseTestCase {
5252
try! FileManager.default.removeItem(at: fileURL)
5353
}
5454
}
55-
55+
56+
func testSetDeviceTokenAndTeamID() {
57+
let installation = LCInstallation()
58+
let deviceToken = UUID().uuidString
59+
try! installation.set("deviceToken", value: deviceToken)
60+
try! installation.set("apnsTeamId", value: "LeanCloud")
61+
installation.badge = 0
62+
installation.channels = LCArray(["test"])
63+
XCTAssertEqual(installation.deviceToken?.value, deviceToken)
64+
XCTAssertEqual(installation.apnsTeamId?.value, "LeanCloud")
65+
XCTAssertTrue(installation.save().isSuccess)
66+
}
5667
}

LeanCloudTests/RTMConnectionTestCase.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import XCTest
1010
@testable import LeanCloud
11+
@testable import Starscream
1112

1213
class RTMConnectionTestCase: RTMBaseTestCase {
1314

@@ -289,7 +290,7 @@ class RTMConnectionTestCase: RTMBaseTestCase {
289290
var goaway = IMGenericCommand()
290291
goaway.cmd = .goaway
291292
return try! goaway.serializedData()
292-
}())
293+
}(), response: WebSocket.WSResponse())
293294
}
294295
}
295296

Sources/RTM/RTMConnection.swift

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ class RTMConnection {
369369
private(set) var instantMessagingDelegatorMap: [IMClient.Identifier: Delegator] = [:]
370370
private(set) var liveQueryDelegatorMap: [LiveQueryClient.Identifier: Delegator] = [:]
371371
var allDelegators: [Delegator] {
372-
return Array(self.instantMessagingDelegatorMap.values) + Array(self.liveQueryDelegatorMap.values)
372+
return Array(self.instantMessagingDelegatorMap.values)
373+
+ Array(self.liveQueryDelegatorMap.values)
373374
}
374375
private(set) var socket: WebSocket? = nil
375376
private(set) var timer: Timer? = nil
@@ -665,17 +666,12 @@ extension RTMConnection {
665666
var request = URLRequest(url: url)
666667
request.timeoutInterval = self.application.configuration.RTMConnectingTimeoutInterval
667668
let socket = WebSocket(request: request, protocols: [self.lcimProtocol.rawValue])
668-
socket.delegate = self
669+
socket.request.setValue(nil, forHTTPHeaderField: "Origin")
670+
socket.advancedDelegate = self
669671
socket.pongDelegate = self
670672
socket.callbackQueue = self.serialQueue
671673
socket.connect()
672674
self.socket = socket
673-
Logger.shared.verbose("""
674-
\n\(socket)
675-
In Connecting.
676-
\tURL: \(url)
677-
\tProtocol: \(self.lcimProtocol.rawValue)
678-
""")
679675
case .failure(error: let error):
680676
for item in self.allDelegators {
681677
item.queue.async {
@@ -792,11 +788,11 @@ extension RTMConnection {
792788

793789
}
794790

795-
extension RTMConnection: WebSocketDelegate, WebSocketPongDelegate {
791+
extension RTMConnection: WebSocketAdvancedDelegate, WebSocketPongDelegate {
796792

797793
// MARK: WebSocketDelegate
798794

799-
func websocketDidConnect(socket: WebSocketClient) {
795+
func websocketDidConnect(socket: WebSocket) {
800796
assert(self.specificAssertion)
801797
assert(self.socket === socket && self.timer == nil)
802798
Logger.shared.verbose("""
@@ -813,40 +809,51 @@ extension RTMConnection: WebSocketDelegate, WebSocketPongDelegate {
813809
}
814810
}
815811

816-
func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
812+
func websocketDidDisconnect(socket: WebSocket, error: Error?) {
817813
assert(self.specificAssertion)
818814
assert(self.socket === socket)
819-
Logger.shared.error("\(socket) disconnect with error: \(String(describing: error))")
815+
Logger.shared.error("""
816+
\n\(socket)
817+
Disconnect with error: \(String(describing: error))
818+
""")
820819
self.tryClearConnection(with: LCError(error: error ?? LCError.RTMConnectionClosedByRemote))
821820
self.useSecondaryServer.toggle()
822821
self.tryConnecting(delay: self.reconnectingDelay)
823822
}
824823

825-
func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
824+
func websocketDidReceiveMessage(socket: WebSocket, text: String, response: WebSocket.WSResponse) {
825+
Logger.shared.error("should never be invoked.")
826+
}
827+
828+
func websocketDidReceiveData(socket: WebSocket, data: Data, response: WebSocket.WSResponse) {
826829
assert(self.specificAssertion)
827830
assert(self.socket === socket && self.timer != nil)
828-
let inCommand: IMGenericCommand
829831
do {
830-
inCommand = try IMGenericCommand(serializedData: data)
831-
} catch {
832-
Logger.shared.error(error)
833-
return
834-
}
835-
Logger.shared.debug("\n------ BEGIN LeanCloud In Command\n\(socket)\n\(inCommand)------ END")
836-
if inCommand.hasI {
837-
self.timer?.handle(callbackCommand: inCommand)
838-
} else {
839-
var delegator: Delegator?
840-
if let peerID = (inCommand.hasPeerID ? inCommand.peerID : nil) {
841-
delegator = self.instantMessagingDelegatorMap[peerID]
842-
} else if let installationID = (inCommand.hasInstallationID ? inCommand.installationID : nil) {
843-
delegator = self.liveQueryDelegatorMap[installationID]
832+
let inCommand = try IMGenericCommand(serializedData: data)
833+
Logger.shared.debug("""
834+
\n------ BEGIN LeanCloud In Command
835+
\(socket)
836+
\(inCommand)
837+
\(response.lcDescription)
838+
------ END
839+
""")
840+
if inCommand.hasI {
841+
self.timer?.handle(callbackCommand: inCommand)
844842
} else {
845-
self.handleGoaway(inCommand: inCommand)
846-
}
847-
delegator?.queue.async {
848-
delegator?.delegate?.connection(self, didReceiveCommand: inCommand)
843+
var delegator: Delegator?
844+
if let peerID = (inCommand.hasPeerID ? inCommand.peerID : nil) {
845+
delegator = self.instantMessagingDelegatorMap[peerID]
846+
} else if let installationID = (inCommand.hasInstallationID ? inCommand.installationID : nil) {
847+
delegator = self.liveQueryDelegatorMap[installationID]
848+
} else {
849+
self.handleGoaway(inCommand: inCommand)
850+
}
851+
delegator?.queue.async {
852+
delegator?.delegate?.connection(self, didReceiveCommand: inCommand)
853+
}
849854
}
855+
} catch {
856+
Logger.shared.error(error)
850857
}
851858
}
852859

@@ -856,13 +863,32 @@ extension RTMConnection: WebSocketDelegate, WebSocketPongDelegate {
856863
self.timer?.receivePong()
857864
}
858865

859-
func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
860-
fatalError("should never be invoked.")
866+
func websocketHttpUpgrade(socket: WebSocket, request: String) {
867+
Logger.shared.verbose("""
868+
\n\(socket)
869+
\(request)
870+
""")
861871
}
862872

873+
func websocketHttpUpgrade(socket: WebSocket, response: String) {
874+
Logger.shared.verbose("""
875+
\n\(socket)
876+
\(response)
877+
""")
878+
}
879+
}
880+
881+
private extension WebSocket.WSResponse {
882+
883+
var lcDescription: String {
884+
return """
885+
code: \(self.code)
886+
frameCount: \(self.frameCount)
887+
"""
888+
}
863889
}
864890

865-
extension IMGenericCommand {
891+
private extension IMGenericCommand {
866892

867893
var lcEncounteredError: LCError? {
868894
if self.cmd == .error {
@@ -957,7 +983,7 @@ extension IMAckCommand {
957983
}
958984
}
959985

960-
extension LCError {
986+
private extension LCError {
961987

962988
// MARK: Connection Lost Error
963989

main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ class JazzyTask: Task {
471471
try generateDocumentation(currentVersion: currentVersion)
472472
try moveGeneratedDocumentationToRepo()
473473
try commitPush()
474-
try OpenTask.url("https://jenkins.avoscloud.com/job/cn-api-doc-prod-ucloud/build")
474+
try OpenTask.url("http://jenkins.avoscloud.com/job/cn-api-doc-prod-ucloud/build")
475475
}
476476
}
477477

0 commit comments

Comments
 (0)