-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathMBWebSocketServer.m
More file actions
243 lines (193 loc) · 7.73 KB
/
MBWebSocketServer.m
File metadata and controls
243 lines (193 loc) · 7.73 KB
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
// Originally created by Max Howell in October 2011.
// This class is in the public domain.
#import <CommonCrypto/CommonDigest.h>
#import "MBWebSocketServer.h"
@interface MBWebSocketServer () <GCDAsyncSocketDelegate>
@end
@interface NSString (MBWebSocketServer)
- (id)sha1base64;
@end
static unsigned long long ntohll(unsigned long long v) {
union { unsigned long lv[2]; unsigned long long llv; } u;
u.llv = v;
return ((unsigned long long)ntohl(u.lv[0]) << 32) | (unsigned long long)ntohl(u.lv[1]);
}
@implementation MBWebSocketServer
@dynamic connected;
@dynamic clientCount;
- (id)initWithPort:(NSUInteger)port delegate:(id<MBWebSocketServerDelegate>)delegate {
_port = port;
_delegate = delegate;
socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
connections = [NSMutableArray new];
NSError *error = nil;
[socket acceptOnPort:_port error:&error];
if (error) {
NSLog(@"MBWebSockerServer failed to initialize: %@", error);
return nil;
}
return self;
}
- (BOOL)connected {
return connections.count > 0;
}
- (NSUInteger)clientCount {
return connections.count;
}
- (void)send:(id)object {
id payload = [object webSocketFrameData];
for (GCDAsyncSocket *connection in connections)
[connection writeData:payload withTimeout:-1 tag:3];
}
- (NSString *)handshakeResponseForData:(NSData *)data {
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSArray *strings = [string componentsSeparatedByString:@"\r\n"];
if (strings.count && [strings[0] isEqualToString:@"GET / HTTP/1.1"])
for (NSString *line in strings) {
NSArray *parts = [line componentsSeparatedByString:@":"];
if (parts.count == 2 && [parts[0] isEqualToString:@"Sec-WebSocket-Key"]) {
id key = [parts[1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
id secWebSocketAccept = [[key stringByAppendingString:@"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"] sha1base64];
return [NSString stringWithFormat:
@"HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Accept: %@\r\n\r\n",
secWebSocketAccept];
}
}
@throw @"Invalid handshake from client";
}
#pragma mark - GCDAsyncSocketDelegate
- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)connection {
[connections addObject:connection];
[connection readDataWithTimeout:-1 tag:1];
}
- (void)socket:(GCDAsyncSocket *)connection didReadData:(NSData *)data withTag:(long)tag
{
@try {
const unsigned char *bytes = data.bytes;
switch (tag) {
case 1: {
NSString *handshake = [self handshakeResponseForData:data];
[connection writeData:[handshake dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:2];
break;
}
case 4: {
uint64_t const N = bytes[1] & 0x7f;
if (!bytes[0] & 0x81)
@throw @"Cannot handle this websocket frame format!";
if (!bytes[1] & 0x80)
@throw @"Can only handle websocket frames with masks!";
if (N >= 126)
[connection readDataToLength:N == 126 ? 2 : 8 withTimeout:-1 buffer:nil bufferOffset:0 tag:5];
else
[connection readDataToLength:N + 4 withTimeout:-1 buffer:nil bufferOffset:0 tag:6];
break;
}
case 5: {
if (data.length == 2) {
uint16_t *p = (uint16_t *)bytes;
[connection readDataToLength:ntohs(*p) + 4 withTimeout:-1 buffer:nil bufferOffset:0 tag:6];
} else {
uint64_t *p = (uint64_t *)bytes;
[connection readDataToLength:ntohll(*p) + 4 withTimeout:-1 buffer:nil bufferOffset:0 tag:6];
}
break;
}
case 6: {
NSMutableData *unmaskedData = [NSMutableData dataWithCapacity:data.length - 4];
for (int x = 4; x < data.length; ++x) {
char c = bytes[x] ^ bytes[x%4];
[unmaskedData appendBytes:&c length:1];
}
NSData *rangedData = [unmaskedData subdataWithRange:NSMakeRange(0, data.length - 4)];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[_delegate webSocketServer:self didReceiveData:rangedData fromConnection:connection];
}];
[connection readDataToLength:2 withTimeout:-1 buffer:nil bufferOffset:0 tag:4];
break;
}
}
}
@catch (id e) {
NSLog(@"MBWebSocketServer: %@", e);
[connection disconnect];
}
}
- (void)socket:(GCDAsyncSocket *)connection didWriteDataWithTag:(long)tag {
if (tag == 2) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[_delegate webSocketServer:self didAcceptConnection:connection];
}];
[connection readDataToLength:2 withTimeout:-1 buffer:nil bufferOffset:0 tag:4];
}
}
- (void)socketDidDisconnect:(GCDAsyncSocket *)connection withError:(NSError *)error {
[connections removeObjectIdenticalTo:connection];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[_delegate webSocketServer:self clientDisconnected:connection];
}];
}
@end
@implementation NSString (MBWebSocketServer)
- (id)sha1base64 {
NSMutableData* data = (id) [self dataUsingEncoding:NSUTF8StringEncoding];
unsigned char input[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, (unsigned)data.length, input);
//////
static const char map[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
data = [NSMutableData dataWithLength:28];
uint8_t* out = (uint8_t*) data.mutableBytes;
for (int i = 0; i < 20;) {
int v = 0;
for (const int N = i + 3; i < N; i++) {
v <<= 8;
v |= 0xFF & input[i];
}
*out++ = map[v >> 18 & 0x3F];
*out++ = map[v >> 12 & 0x3F];
*out++ = map[v >> 6 & 0x3F];
*out++ = map[v >> 0 & 0x3F];
}
out[-2] = map[(input[19] & 0x0F) << 2];
out[-1] = '=';
return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}
- (id)webSocketFrameData {
return [[self dataUsingEncoding:NSUTF8StringEncoding] webSocketFrameData];
}
@end
@implementation NSData (MBWebSocketServer)
- (id)webSocketFrameData {
NSMutableData *data = [NSMutableData dataWithLength:10];
char *header = data.mutableBytes;
header[0] = 0x81;
if (self.length > 65535) {
header[1] = 127;
header[2] = (self.length >> 56) & 255;
header[3] = (self.length >> 48) & 255;
header[4] = (self.length >> 40) & 255;
header[5] = (self.length >> 32) & 255;
header[6] = (self.length >> 24) & 255;
header[7] = (self.length >> 16) & 255;
header[8] = (self.length >> 8) & 255;
header[9] = self.length & 255;
} else if (self.length > 125) {
header[1] = 126;
header[2] = (self.length >> 8) & 255;
header[3] = self.length & 255;
data.length = 4;
} else {
header[1] = self.length;
data.length = 2;
}
[data appendData:self];
return data;
}
@end
@implementation GCDAsyncSocket (MBWebSocketServer)
- (void)writeWebSocketFrame:(id)object {
[self writeData:[object webSocketFrameData] withTimeout:-1 tag:3];
}
@end