forked from PerfectlySoft/Perfect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebSocketHandler.swift
377 lines (299 loc) · 11 KB
/
WebSocketHandler.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//
// WebSocketHandler.swift
// PerfectLib
//
// Created by Kyle Jessup on 2016-01-06.
// Copyright © 2016 PerfectlySoft. All rights reserved.
//
//===----------------------------------------------------------------------===//
//
// This source file is part of the Perfect.org open source project
//
// Copyright (c) 2015 - 2016 PerfectlySoft Inc. and the Perfect project authors
// Licensed under Apache License v2.0
//
// See http://perfect.org/licensing.html for license information
//
//===----------------------------------------------------------------------===//
//
#if os(Linux)
import SwiftGlibc
import LinuxBridge
private let UINT16_MAX = UInt(0xFFFF)
#endif
private let smallPayloadSize = 126
/// This class represents the communications channel for a WebSocket session.
public class WebSocket {
/// The various types of WebSocket messages.
public enum OpcodeType: UInt8 {
case Continuation = 0x0, Text = 0x1, Binary = 0x2, Close = 0x8, Ping = 0x9, Pong = 0xA, Invalid
}
private struct Frame {
let fin: Bool
let rsv1: Bool
let rsv2: Bool
let rsv3: Bool
let opCode: OpcodeType
let bytesPayload: [UInt8]
var stringPayload: String? {
return UTF8Encoding.encode(self.bytesPayload)
}
}
private let connection: WebConnection
/// The read timeout, in seconds. By default this is -1, which means no timeout.
public var readTimeoutSeconds: Double = -1.0
private var socket: NetTCP { return self.connection.connection }
/// Indicates if the socket is still likely connected or if it has been closed.
public var isConnected: Bool { return self.socket.fd.isValid }
private var nextIsContinuation = false
private let readBuffer = Bytes()
init(connection: WebConnection) {
self.connection = connection
}
/// Close the connection.
public func close() {
if self.socket.fd.isValid {
self.sendMessage(.Close, bytes: [UInt8](), final: true) {
self.socket.close()
}
}
}
private func clearFrame() {
let position = self.readBuffer.position
self.readBuffer.data.removeFirst(position)
self.readBuffer.position = 0
}
private func fillFrame() -> Frame? {
guard self.readBuffer.availableExportBytes >= 2 else {
return nil
}
// we know we potentially have a valid frame here
// for to be resetting the position if we don't have a valid frame yet
let oldPosition = self.readBuffer.position
let byte1 = self.readBuffer.export8Bits()
let byte2 = self.readBuffer.export8Bits()
let fin = (byte1 & 0x80) != 0
let rsv1 = (byte1 & 0x40) != 0
let rsv2 = (byte1 & 0x20) != 0
let rsv3 = (byte1 & 0x10) != 0
let opcode = OpcodeType(rawValue: byte1 & 0xF) ?? .Invalid
let maskBit = (byte2 & 0x80) != 0
guard maskBit else {
self.close()
return nil
}
var unmaskedLength = Int(byte2 ^ 0x80)
if unmaskedLength == smallPayloadSize {
if self.readBuffer.availableExportBytes >= 2 {
unmaskedLength = Int(ntohs(self.readBuffer.export16Bits()))
}
} else if unmaskedLength > smallPayloadSize {
if self.readBuffer.availableExportBytes >= 8 {
unmaskedLength = Int(ntohll(self.readBuffer.export64Bits()))
}
} // else small payload
if self.readBuffer.availableExportBytes >= 4 {
let maskingKey = self.readBuffer.exportBytes(4)
if self.readBuffer.availableExportBytes >= unmaskedLength {
var exported = self.readBuffer.exportBytes(unmaskedLength)
for i in 0..<exported.count {
exported[i] = exported[i] ^ maskingKey[i % 4]
}
self.clearFrame()
return Frame(fin: fin, rsv1: rsv1, rsv2: rsv2, rsv3: rsv3, opCode: opcode, bytesPayload: exported)
}
}
self.readBuffer.position = oldPosition
return nil
}
func fillBuffer(demand: Int, completion: (Bool) -> ()) {
self.socket.readBytesFully(demand, timeoutSeconds: self.readTimeoutSeconds) {
[weak self] (b:[UInt8]?) -> () in
if let b = b {
self?.readBuffer.data.appendContentsOf(b)
}
completion(b != nil)
}
}
func fillBufferSome(suggestion: Int, completion: () -> ()) {
self.socket.readSomeBytes(suggestion) {
[weak self] (b:[UInt8]?) -> () in
if let b = b {
self?.readBuffer.data.appendContentsOf(b)
}
completion()
}
}
private func readFrame(completion: (Frame?) -> ()) {
if let frame = self.fillFrame() {
switch frame.opCode {
// check for and handle ping/pong
case .Ping:
self.sendMessage(.Pong, bytes: frame.bytesPayload, final: true) {
self.readFrame(completion)
}
return
// check for and handle close
case .Close:
self.close()
return completion(nil)
default:
return completion(frame)
}
}
self.fillBuffer(1) {
b in
guard b != false else {
return completion(nil)
}
self.fillBufferSome(1024 * 32) { // some arbitrary read-ahead amount
self.readFrame(completion)
}
}
}
/// Read string data from the client.
public func readStringMessage(continuation: (String?, opcode: OpcodeType, final: Bool) -> ()) {
self.readFrame {
frame in
continuation(frame?.stringPayload, opcode: frame?.opCode ?? .Invalid, final: frame?.fin ?? true)
}
}
/// Read binary data from the client.
public func readBytesMessage(continuation: ([UInt8]?, opcode: OpcodeType, final: Bool) -> ()) {
self.readFrame {
frame in
continuation(frame?.bytesPayload, opcode: frame?.opCode ?? .Invalid, final: frame?.fin ?? true)
}
}
/// Send binary data to thew client.
public func sendBinaryMessage(bytes: [UInt8], final: Bool, completion: () -> ()) {
self.sendMessage(.Binary, bytes: bytes, final: final, completion: completion)
}
/// Send string data to the client.
public func sendStringMessage(string: String, final: Bool, completion: () -> ()) {
self.sendMessage(.Text, bytes: UTF8Encoding.decode(string), final: final, completion: completion)
}
/// Send a "pong" message to the client.
public func sendPong(completion: () -> ()) {
self.sendMessage(.Pong, bytes: [UInt8](), final: true, completion: completion)
}
/// Send a "ping" message to the client.
/// Expect a "pong" message to follow.
public func sendPing(completion: () -> ()) {
self.sendMessage(.Ping, bytes: [UInt8](), final: true, completion: completion)
}
private func sendMessage(opcode: OpcodeType, bytes: [UInt8], final: Bool, completion: () -> ()) {
let sendBuffer = Bytes()
let byte1 = UInt8(final ? 0x80 : 0x0) | (self.nextIsContinuation ? 0 : opcode.rawValue)
self.nextIsContinuation = !final
sendBuffer.import8Bits(byte1)
let payloadSize = bytes.count
if payloadSize < smallPayloadSize {
let byte2 = UInt8(payloadSize)
sendBuffer.import8Bits(byte2)
} else if payloadSize <= Int(UINT16_MAX) {
sendBuffer.import8Bits(UInt8(smallPayloadSize))
.import16Bits(htons(UInt16(payloadSize)))
} else {
sendBuffer.import8Bits(UInt8(1+smallPayloadSize))
.import64Bits(htonll(UInt64(payloadSize)))
}
sendBuffer.importBytes(bytes)
self.socket.writeBytes(sendBuffer.data) {
_ in
completion()
}
}
}
/// The protocol that all WebSocket handlers must implement.
public protocol WebSocketSessionHandler {
/// Optionally indicate the name of the protocol the handler implements.
/// If this has a valid, the protocol name will be validated against what the client is requesting.
var socketProtocol: String? { get }
/// This function is called once the WebSocket session has been initiated.
func handleSession(request: WebRequest, socket: WebSocket)
}
private let acceptableProtocolVersions = [13]
private let webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
/// This request handler accepts WebSocket requests from client.
/// It will initialize the session and then deliver it to the `WebSocketSessionHandler`.
public class WebSocketHandler : RequestHandler {
public typealias HandlerProducer = (request: WebRequest, protocols: [String]) -> WebSocketSessionHandler?
private let handlerProducer: HandlerProducer
public init(handlerProducer: HandlerProducer) {
self.handlerProducer = handlerProducer
}
public func handleRequest(request: WebRequest, response: WebResponse) {
guard let upgrade = request.header("Upgrade"),
connection = request.header("Connection"),
secWebSocketKey = request.header("Sec-WebSocket-Key"),
secWebSocketVersion = request.header("Sec-WebSocket-Version")
where upgrade.lowercaseString == "websocket" && connection.lowercaseString == "upgrade" else {
response.setStatus(400, message: "Bad Request")
response.requestCompletedCallback()
return
}
guard acceptableProtocolVersions.contains(Int(secWebSocketVersion) ?? 0) else {
response.setStatus(400, message: "Bad Request")
response.addHeader("Sec-WebSocket-Version", value: "\(acceptableProtocolVersions[0])")
response.appendBodyString("WebSocket protocol version \(secWebSocketVersion) not supported. Supported protocol versions are: \(acceptableProtocolVersions.map { String($0) }.joinWithSeparator(","))")
response.requestCompletedCallback()
return
}
let secWebSocketProtocol = request.header("Sec-WebSocket-Protocol") ?? ""
let protocolList = secWebSocketProtocol.characters.split(",").flatMap {
i -> String? in
var s = String(i)
while s.characters.count > 0 && s.characters[s.characters.startIndex] == " " {
s.removeAtIndex(s.startIndex)
}
return s.characters.count > 0 ? s : nil
}
guard let handler = self.handlerProducer(request: request, protocols: protocolList) else {
response.setStatus(400, message: "Bad Request")
response.appendBodyString("WebSocket protocols not supported.")
response.requestCompletedCallback()
return
}
response.requestCompletedCallback = {} // this is no longer a normal request, eligible for keep-alive
response.setStatus(101, message: "Switching Protocols")
response.addHeader("Upgrade", value: "websocket")
response.addHeader("Connection", value: "Upgrade")
response.addHeader("Sec-WebSocket-Accept", value: self.base64((secWebSocketKey + webSocketGUID).utf8.sha1))
if let chosenProtocol = handler.socketProtocol {
response.addHeader("Sec-WebSocket-Protocol", value: chosenProtocol)
}
for (key, value) in response.headersArray {
response.connection.writeHeaderLine(key + ": " + value)
}
response.connection.writeBodyBytes([UInt8]())
handler.handleSession(request, socket: WebSocket(connection: response.connection))
}
private func base64(a: [UInt8]) -> String {
let bio = BIO_push(BIO_new(BIO_f_base64()), BIO_new(BIO_s_mem()))
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL)
BIO_write(bio, a, Int32(a.count))
BIO_ctrl(bio, BIO_CTRL_FLUSH, 0, nil)
var mem = UnsafeMutablePointer<BUF_MEM>()
BIO_ctrl(bio, BIO_C_GET_BUF_MEM_PTR, 0, &mem)
BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, Int(BIO_NOCLOSE), nil)
BIO_free_all(bio)
let txt = UnsafeMutablePointer<UInt8>(mem.memory.data)
let ret = UTF8Encoding.encode(GenerateFromPointer(from: txt, count: mem.memory.length))
free(mem.memory.data)
return ret
}
}
import OpenSSL
extension String.UTF8View {
var sha1: [UInt8] {
let bytes = UnsafeMutablePointer<UInt8>.alloc(Int(SHA_DIGEST_LENGTH))
defer { bytes.destroy() ; bytes.dealloc(Int(SHA_DIGEST_LENGTH)) }
SHA1(Array<UInt8>(self), (self.count), bytes)
var r = [UInt8]()
for idx in 0..<Int(SHA_DIGEST_LENGTH) {
r.append(bytes[idx])
}
return r
}
}