Skip to content

Commit 57d1f3c

Browse files
committed
bump websockets
1 parent 499c0d6 commit 57d1f3c

File tree

1 file changed

+60
-12
lines changed

1 file changed

+60
-12
lines changed

Source/WebSocket.swift

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,31 @@ import Foundation
1010
import CoreFoundation
1111
import Security
1212

13+
//////////////////////////////////////////////////////////////////////////////////////////////////
14+
//
15+
// Websocket.swift
16+
//
17+
// Created by Dalton Cherry on 7/16/14.
18+
// Copyright (c) 2014-2015 Dalton Cherry.
19+
//
20+
// Licensed under the Apache License, Version 2.0 (the "License");
21+
// you may not use this file except in compliance with the License.
22+
// You may obtain a copy of the License at
23+
//
24+
// http://www.apache.org/licenses/LICENSE-2.0
25+
//
26+
// Unless required by applicable law or agreed to in writing, software
27+
// distributed under the License is distributed on an "AS IS" BASIS,
28+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29+
// See the License for the specific language governing permissions and
30+
// limitations under the License.
31+
//
32+
//////////////////////////////////////////////////////////////////////////////////////////////////
33+
34+
import Foundation
35+
import CoreFoundation
36+
import Security
37+
1338
public protocol WebSocketDelegate: class {
1439
func websocketDidConnect(socket: WebSocket)
1540
func websocketDidDisconnect(socket: WebSocket, error: NSError?)
@@ -421,6 +446,33 @@ public class WebSocket : NSObject, NSStreamDelegate {
421446
return false
422447
}
423448

449+
///read a 16 bit big endian value from a buffer
450+
private static func readUint16(buffer: UnsafePointer<UInt8>, offset: Int) -> UInt16 {
451+
return (UInt16(buffer[offset + 0]) << 8) | UInt16(buffer[offset + 1])
452+
}
453+
454+
///read a 64 bit big endian value from a buffer
455+
private static func readUint64(buffer: UnsafePointer<UInt8>, offset: Int) -> UInt64 {
456+
var value = UInt64(0)
457+
for i in 0...7 {
458+
value = (value << 8) | UInt64(buffer[offset + i])
459+
}
460+
return value
461+
}
462+
463+
///write a 16 bit big endian value to a buffer
464+
private static func writeUint16(buffer: UnsafeMutablePointer<UInt8>, offset: Int, value: UInt16) {
465+
buffer[offset + 0] = UInt8(value >> 8)
466+
buffer[offset + 1] = UInt8(value & 0xff)
467+
}
468+
469+
///write a 64 bit big endian value to a buffer
470+
private static func writeUint64(buffer: UnsafeMutablePointer<UInt8>, offset: Int, value: UInt64) {
471+
for i in 0...7 {
472+
buffer[offset + i] = UInt8((value >> (8*UInt64(7 - i))) & 0xff)
473+
}
474+
}
475+
424476
///process the websocket data
425477
private func processRawMessage(buffer: UnsafePointer<UInt8>, bufferLen: Int) {
426478
let response = readStack.last
@@ -474,8 +526,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
474526
if payloadLen == 1 {
475527
code = CloseCode.ProtocolError.rawValue
476528
} else if payloadLen > 1 {
477-
let codeBuffer = UnsafePointer<UInt16>((buffer+offset))
478-
code = codeBuffer[0].bigEndian
529+
code = WebSocket.readUint16(buffer, offset: offset)
479530
if code < 1000 || (code > 1003 && code < 1007) || (code > 1011 && code < 3000) {
480531
code = CloseCode.ProtocolError.rawValue
481532
}
@@ -501,12 +552,10 @@ public class WebSocket : NSObject, NSStreamDelegate {
501552
}
502553
var dataLength = UInt64(payloadLen)
503554
if dataLength == 127 {
504-
let bytes = UnsafePointer<UInt64>((buffer+offset))
505-
dataLength = bytes[0].bigEndian
555+
dataLength = WebSocket.readUint64(buffer, offset: offset)
506556
offset += sizeof(UInt64)
507557
} else if dataLength == 126 {
508-
let bytes = UnsafePointer<UInt16>((buffer+offset))
509-
dataLength = UInt64(bytes[0].bigEndian)
558+
dataLength = UInt64(WebSocket.readUint16(buffer, offset: offset))
510559
offset += sizeof(UInt16)
511560
}
512561
if bufferLen < offset || UInt64(bufferLen - offset) < dataLength {
@@ -644,8 +693,8 @@ public class WebSocket : NSObject, NSStreamDelegate {
644693
///write a an error to the socket
645694
private func writeError(code: UInt16) {
646695
let buf = NSMutableData(capacity: sizeof(UInt16))
647-
let buffer = UnsafeMutablePointer<UInt16>(buf!.bytes)
648-
buffer[0] = code.bigEndian
696+
let buffer = UnsafeMutablePointer<UInt8>(buf!.bytes)
697+
WebSocket.writeUint16(buffer, offset: 0, value: code)
649698
dequeueWrite(NSData(bytes: buffer, length: sizeof(UInt16)), code: .ConnectionClose)
650699
}
651700
///used to write things to the stream
@@ -665,13 +714,11 @@ public class WebSocket : NSObject, NSStreamDelegate {
665714
buffer[1] = CUnsignedChar(dataLength)
666715
} else if dataLength <= Int(UInt16.max) {
667716
buffer[1] = 126
668-
let sizeBuffer = UnsafeMutablePointer<UInt16>((buffer+offset))
669-
sizeBuffer[0] = UInt16(dataLength).bigEndian
717+
WebSocket.writeUint16(buffer, offset: offset, value: UInt16(dataLength))
670718
offset += sizeof(UInt16)
671719
} else {
672720
buffer[1] = 127
673-
let sizeBuffer = UnsafeMutablePointer<UInt64>((buffer+offset))
674-
sizeBuffer[0] = UInt64(dataLength).bigEndian
721+
WebSocket.writeUint64(buffer, offset: offset, value: UInt64(dataLength))
675722
offset += sizeof(UInt64)
676723
}
677724
buffer[1] |= s.MaskMask
@@ -725,6 +772,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
725772
}
726773

727774
}
775+
728776
private class SSLCert {
729777
var certData: NSData?
730778
var key: SecKeyRef?

0 commit comments

Comments
 (0)