Skip to content

Commit

Permalink
accept null utf char (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Lees11 authored and ianpartridge committed Apr 16, 2018
1 parent 5eaae44 commit 592bd2a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
19 changes: 7 additions & 12 deletions Sources/KituraWebSocket/WebSocketConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,20 +297,15 @@ public class WebSocketConnection {
}

private func fireReceivedString(from: NSMutableData) {
var zero: CChar = 0
from.append(&zero, length: 1)
let bytes = from.bytes.bindMemory(to: CChar.self, capacity: 1)
if let text = String(cString: bytes, encoding: .utf8) {
callbackQueue.async { [weak self] in
if let strongSelf = self {
strongSelf.service?.received(message: text, from: strongSelf)
}
}
}
else {
guard let text = String(data: Data(referencing: from), encoding: .utf8) else {
closeConnection(reason: .invalidDataContents, description: "Failed to convert received payload to UTF-8 String", hard: true)
return
}
callbackQueue.async { [weak self] in
if let strongSelf = self {
strongSelf.service?.received(message: text, from: strongSelf)
}
}
from.length -= 1
}

private func sendMessage(withOpCode: WSFrame.FrameOpcode, payload: UnsafeRawPointer?, payloadLength: Int) {
Expand Down
13 changes: 13 additions & 0 deletions Tests/KituraWebSocketTests/BasicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,19 @@ class BasicTests: KituraTest {
}
}

func testNullCharacter() {
register(closeReason: .noReasonCodeSent)

performServerTest() { expectation in

let textPayload = self.payload(text: "\u{0}")

self.performTest(framesToSend: [(true, self.opcodeText, textPayload)],
expectedFrames: [(true, self.opcodeText, textPayload)],
expectation: expectation)
}
}

func testUserDefinedCloseMessage() {
register(closeReason: .userDefined(65535))

Expand Down

0 comments on commit 592bd2a

Please sign in to comment.