xref: /aosp_15_r20/external/webrtc/examples/objc/AppRTCMobile/third_party/SocketRocket/SRWebSocket.m (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1//
2//   Copyright 2012 Square Inc.
3//
4//   Licensed under the Apache License, Version 2.0 (the "License");
5//   you may not use this file except in compliance with the License.
6//   You may obtain a copy of the License at
7//
8//       http://www.apache.org/licenses/LICENSE-2.0
9//
10//   Unless required by applicable law or agreed to in writing, software
11//   distributed under the License is distributed on an "AS IS" BASIS,
12//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13//   See the License for the specific language governing permissions and
14//   limitations under the License.
15//
16
17
18#import "SRWebSocket.h"
19
20#if TARGET_OS_IPHONE
21#define HAS_ICU
22#endif
23
24#ifdef HAS_ICU
25#import <unicode/utf8.h>
26#endif
27
28#if TARGET_OS_IPHONE
29#import <Endian.h>
30#else
31#import <CoreServices/CoreServices.h>
32#endif
33
34#import <CommonCrypto/CommonDigest.h>
35#import <Security/SecRandom.h>
36
37#if OS_OBJECT_USE_OBJC_RETAIN_RELEASE
38#define sr_dispatch_retain(x)
39#define sr_dispatch_release(x)
40#define maybe_bridge(x) ((__bridge void *) x)
41#else
42#define sr_dispatch_retain(x) dispatch_retain(x)
43#define sr_dispatch_release(x) dispatch_release(x)
44#define maybe_bridge(x) (x)
45#endif
46
47#if !__has_feature(objc_arc)
48#error SocketRocket must be compiled with ARC enabled
49#endif
50
51
52typedef enum  {
53    SROpCodeTextFrame = 0x1,
54    SROpCodeBinaryFrame = 0x2,
55    // 3-7 reserved.
56    SROpCodeConnectionClose = 0x8,
57    SROpCodePing = 0x9,
58    SROpCodePong = 0xA,
59    // B-F reserved.
60} SROpCode;
61
62typedef struct {
63    BOOL fin;
64//  BOOL rsv1;
65//  BOOL rsv2;
66//  BOOL rsv3;
67    uint8_t opcode;
68    BOOL masked;
69    uint64_t payload_length;
70} frame_header;
71
72static NSString *const SRWebSocketAppendToSecKeyString = @"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
73
74static inline int32_t validate_dispatch_data_partial_string(NSData *data);
75static inline void SRFastLog(NSString *format, ...);
76
77@interface NSData (SRWebSocket)
78
79- (NSString *)stringBySHA1ThenBase64Encoding;
80
81@end
82
83
84@interface NSString (SRWebSocket)
85
86- (NSString *)stringBySHA1ThenBase64Encoding;
87
88@end
89
90
91@interface NSURL (SRWebSocket)
92
93// The origin isn't really applicable for a native application.
94// So instead, just map ws -> http and wss -> https.
95- (NSString *)SR_origin;
96
97@end
98
99
100@interface _SRRunLoopThread : NSThread
101
102@property (nonatomic, readonly) NSRunLoop *runLoop;
103
104@end
105
106
107static NSString *newSHA1String(const char *bytes, size_t length) {
108    uint8_t md[CC_SHA1_DIGEST_LENGTH];
109
110    assert(length >= 0);
111    assert(length <= UINT32_MAX);
112    CC_SHA1(bytes, (CC_LONG)length, md);
113
114    NSData *data = [NSData dataWithBytes:md length:CC_SHA1_DIGEST_LENGTH];
115
116    if ([data respondsToSelector:@selector(base64EncodedStringWithOptions:)]) {
117        return [data base64EncodedStringWithOptions:0];
118    }
119
120    return [data base64Encoding];
121}
122
123@implementation NSData (SRWebSocket)
124
125- (NSString *)stringBySHA1ThenBase64Encoding;
126{
127    return newSHA1String(self.bytes, self.length);
128}
129
130@end
131
132
133@implementation NSString (SRWebSocket)
134
135- (NSString *)stringBySHA1ThenBase64Encoding;
136{
137    return newSHA1String(self.UTF8String, self.length);
138}
139
140@end
141
142NSString *const SRWebSocketErrorDomain = @"SRWebSocketErrorDomain";
143NSString *const SRHTTPResponseErrorKey = @"HTTPResponseStatusCode";
144
145// Returns number of bytes consumed. Returning 0 means you didn't match.
146// Sends bytes to callback handler;
147typedef size_t (^stream_scanner)(NSData *collected_data);
148
149typedef void (^data_callback)(SRWebSocket *webSocket,  NSData *data);
150
151@interface SRIOConsumer : NSObject {
152    stream_scanner _scanner;
153    data_callback _handler;
154    size_t _bytesNeeded;
155    BOOL _readToCurrentFrame;
156    BOOL _unmaskBytes;
157}
158@property (nonatomic, copy, readonly) stream_scanner consumer;
159@property (nonatomic, copy, readonly) data_callback handler;
160@property (nonatomic, assign) size_t bytesNeeded;
161@property (nonatomic, assign, readonly) BOOL readToCurrentFrame;
162@property (nonatomic, assign, readonly) BOOL unmaskBytes;
163
164@end
165
166// This class is not thread-safe, and is expected to always be run on the same queue.
167@interface SRIOConsumerPool : NSObject
168
169- (id)initWithBufferCapacity:(NSUInteger)poolSize;
170
171- (SRIOConsumer *)consumerWithScanner:(stream_scanner)scanner handler:(data_callback)handler bytesNeeded:(size_t)bytesNeeded readToCurrentFrame:(BOOL)readToCurrentFrame unmaskBytes:(BOOL)unmaskBytes;
172- (void)returnConsumer:(SRIOConsumer *)consumer;
173
174@end
175
176@interface SRWebSocket ()  <NSStreamDelegate>
177
178- (void)_writeData:(NSData *)data;
179- (void)_closeWithProtocolError:(NSString *)message;
180- (void)_failWithError:(NSError *)error;
181
182- (void)_disconnect;
183
184- (void)_readFrameNew;
185- (void)_readFrameContinue;
186
187- (void)_pumpScanner;
188
189- (void)_pumpWriting;
190
191- (void)_addConsumerWithScanner:(stream_scanner)consumer callback:(data_callback)callback;
192- (void)_addConsumerWithDataLength:(size_t)dataLength callback:(data_callback)callback readToCurrentFrame:(BOOL)readToCurrentFrame unmaskBytes:(BOOL)unmaskBytes;
193- (void)_addConsumerWithScanner:(stream_scanner)consumer callback:(data_callback)callback dataLength:(size_t)dataLength;
194- (void)_readUntilBytes:(const void *)bytes length:(size_t)length callback:(data_callback)dataHandler;
195- (void)_readUntilHeaderCompleteWithCallback:(data_callback)dataHandler;
196
197- (void)_sendFrameWithOpcode:(SROpCode)opcode data:(id)data;
198
199- (BOOL)_checkHandshake:(CFHTTPMessageRef)httpMessage;
200- (void)_SR_commonInit;
201
202- (void)_initializeStreams;
203- (void)_connect;
204
205@property (nonatomic) SRReadyState readyState;
206
207@property (nonatomic) NSOperationQueue *delegateOperationQueue;
208@property (nonatomic) dispatch_queue_t delegateDispatchQueue;
209
210@end
211
212
213@implementation SRWebSocket {
214    NSInteger _webSocketVersion;
215
216    NSOperationQueue *_delegateOperationQueue;
217    dispatch_queue_t _delegateDispatchQueue;
218
219    dispatch_queue_t _workQueue;
220    NSMutableArray *_consumers;
221
222    NSInputStream *_inputStream;
223    NSOutputStream *_outputStream;
224
225    NSMutableData *_readBuffer;
226    NSUInteger _readBufferOffset;
227
228    NSMutableData *_outputBuffer;
229    NSUInteger _outputBufferOffset;
230
231    uint8_t _currentFrameOpcode;
232    size_t _currentFrameCount;
233    size_t _readOpCount;
234    uint32_t _currentStringScanPosition;
235    NSMutableData *_currentFrameData;
236
237    NSString *_closeReason;
238
239    NSString *_secKey;
240
241    BOOL _pinnedCertFound;
242
243    uint8_t _currentReadMaskKey[4];
244    size_t _currentReadMaskOffset;
245
246    BOOL _consumerStopped;
247
248    BOOL _closeWhenFinishedWriting;
249    BOOL _failed;
250
251    BOOL _secure;
252    NSURLRequest *_urlRequest;
253
254    CFHTTPMessageRef _receivedHTTPHeaders;
255
256    BOOL _sentClose;
257    BOOL _didFail;
258    int _closeCode;
259
260    BOOL _isPumping;
261
262    NSMutableSet *_scheduledRunloops;
263
264    // We use this to retain ourselves.
265    __strong SRWebSocket *_selfRetain;
266
267    NSArray *_requestedProtocols;
268    SRIOConsumerPool *_consumerPool;
269}
270
271@synthesize delegate = _delegate;
272@synthesize url = _url;
273@synthesize readyState = _readyState;
274@synthesize protocol = _protocol;
275
276static __strong NSData *CRLFCRLF;
277
278+ (void)initialize;
279{
280    CRLFCRLF = [[NSData alloc] initWithBytes:"\r\n\r\n" length:4];
281}
282
283- (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols;
284{
285    self = [super init];
286    if (self) {
287        assert(request.URL);
288        _url = request.URL;
289        _urlRequest = request;
290
291        _requestedProtocols = [protocols copy];
292
293        [self _SR_commonInit];
294    }
295
296    return self;
297}
298
299- (id)initWithURLRequest:(NSURLRequest *)request;
300{
301    return [self initWithURLRequest:request protocols:nil];
302}
303
304- (id)initWithURL:(NSURL *)url;
305{
306    return [self initWithURL:url protocols:nil];
307}
308
309- (id)initWithURL:(NSURL *)url protocols:(NSArray *)protocols;
310{
311    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
312    return [self initWithURLRequest:request protocols:protocols];
313}
314
315- (void)_SR_commonInit;
316{
317
318    NSString *scheme = _url.scheme.lowercaseString;
319    assert([scheme isEqualToString:@"ws"] || [scheme isEqualToString:@"http"] || [scheme isEqualToString:@"wss"] || [scheme isEqualToString:@"https"]);
320
321    if ([scheme isEqualToString:@"wss"] || [scheme isEqualToString:@"https"]) {
322        _secure = YES;
323    }
324
325    _readyState = SR_CONNECTING;
326    _consumerStopped = YES;
327    _webSocketVersion = 13;
328
329    _workQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
330
331    // Going to set a specific on the queue so we can validate we're on the work queue
332    dispatch_queue_set_specific(_workQueue, (__bridge void *)self, maybe_bridge(_workQueue), NULL);
333
334    _delegateDispatchQueue = dispatch_get_main_queue();
335    sr_dispatch_retain(_delegateDispatchQueue);
336
337    _readBuffer = [[NSMutableData alloc] init];
338    _outputBuffer = [[NSMutableData alloc] init];
339
340    _currentFrameData = [[NSMutableData alloc] init];
341
342    _consumers = [[NSMutableArray alloc] init];
343
344    _consumerPool = [[SRIOConsumerPool alloc] init];
345
346    _scheduledRunloops = [[NSMutableSet alloc] init];
347
348    [self _initializeStreams];
349
350    // default handlers
351}
352
353- (void)assertOnWorkQueue;
354{
355    assert(dispatch_get_specific((__bridge void *)self) == maybe_bridge(_workQueue));
356}
357
358- (void)dealloc
359{
360    _inputStream.delegate = nil;
361    _outputStream.delegate = nil;
362
363    [_inputStream close];
364    [_outputStream close];
365
366    sr_dispatch_release(_workQueue);
367    _workQueue = NULL;
368
369    if (_receivedHTTPHeaders) {
370        CFRelease(_receivedHTTPHeaders);
371        _receivedHTTPHeaders = NULL;
372    }
373
374    if (_delegateDispatchQueue) {
375        sr_dispatch_release(_delegateDispatchQueue);
376        _delegateDispatchQueue = NULL;
377    }
378}
379
380#ifndef NDEBUG
381
382- (void)setReadyState:(SRReadyState)aReadyState;
383{
384    [self willChangeValueForKey:@"readyState"];
385    assert(aReadyState > _readyState);
386    _readyState = aReadyState;
387    [self didChangeValueForKey:@"readyState"];
388}
389
390#endif
391
392- (void)open;
393{
394    assert(_url);
395    NSAssert(_readyState == SR_CONNECTING, @"Cannot call -(void)open on SRWebSocket more than once");
396
397    _selfRetain = self;
398
399    [self _connect];
400}
401
402// Calls block on delegate queue
403- (void)_performDelegateBlock:(dispatch_block_t)block;
404{
405    if (_delegateOperationQueue) {
406        [_delegateOperationQueue addOperationWithBlock:block];
407    } else {
408        assert(_delegateDispatchQueue);
409        dispatch_async(_delegateDispatchQueue, block);
410    }
411}
412
413- (void)setDelegateDispatchQueue:(dispatch_queue_t)queue;
414{
415    if (queue) {
416        sr_dispatch_retain(queue);
417    }
418
419    if (_delegateDispatchQueue) {
420        sr_dispatch_release(_delegateDispatchQueue);
421    }
422
423    _delegateDispatchQueue = queue;
424}
425
426- (BOOL)_checkHandshake:(CFHTTPMessageRef)httpMessage;
427{
428    NSString *acceptHeader = CFBridgingRelease(CFHTTPMessageCopyHeaderFieldValue(httpMessage, CFSTR("Sec-WebSocket-Accept")));
429
430    if (acceptHeader == nil) {
431        return NO;
432    }
433
434    NSString *concattedString = [_secKey stringByAppendingString:SRWebSocketAppendToSecKeyString];
435    NSString *expectedAccept = [concattedString stringBySHA1ThenBase64Encoding];
436
437    return [acceptHeader isEqualToString:expectedAccept];
438}
439
440- (void)_HTTPHeadersDidFinish;
441{
442    NSInteger responseCode = CFHTTPMessageGetResponseStatusCode(_receivedHTTPHeaders);
443
444    if (responseCode >= 400) {
445        SRFastLog(@"Request failed with response code %d", responseCode);
446        [self _failWithError:[NSError errorWithDomain:SRWebSocketErrorDomain code:2132 userInfo:@{NSLocalizedDescriptionKey:[NSString stringWithFormat:@"received bad response code from server %ld", (long)responseCode], SRHTTPResponseErrorKey:@(responseCode)}]];
447        return;
448    }
449
450    if(![self _checkHandshake:_receivedHTTPHeaders]) {
451        [self _failWithError:[NSError errorWithDomain:SRWebSocketErrorDomain code:2133 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Invalid Sec-WebSocket-Accept response"] forKey:NSLocalizedDescriptionKey]]];
452        return;
453    }
454
455    NSString *negotiatedProtocol = CFBridgingRelease(CFHTTPMessageCopyHeaderFieldValue(_receivedHTTPHeaders, CFSTR("Sec-WebSocket-Protocol")));
456    if (negotiatedProtocol) {
457        // Make sure we requested the protocol
458        if ([_requestedProtocols indexOfObject:negotiatedProtocol] == NSNotFound) {
459            [self _failWithError:[NSError errorWithDomain:SRWebSocketErrorDomain code:2133 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Server specified Sec-WebSocket-Protocol that wasn't requested"] forKey:NSLocalizedDescriptionKey]]];
460            return;
461        }
462
463        _protocol = negotiatedProtocol;
464    }
465
466    self.readyState = SR_OPEN;
467
468    if (!_didFail) {
469        [self _readFrameNew];
470    }
471
472    [self _performDelegateBlock:^{
473        if ([self.delegate respondsToSelector:@selector(webSocketDidOpen:)]) {
474            [self.delegate webSocketDidOpen:self];
475        };
476    }];
477}
478
479
480- (void)_readHTTPHeader;
481{
482    if (_receivedHTTPHeaders == NULL) {
483        _receivedHTTPHeaders = CFHTTPMessageCreateEmpty(NULL, NO);
484    }
485
486    [self _readUntilHeaderCompleteWithCallback:^(SRWebSocket *self, NSData *data) {
487      CFHTTPMessageAppendBytes(self->_receivedHTTPHeaders, (const UInt8 *)data.bytes, data.length);
488
489      if (CFHTTPMessageIsHeaderComplete(self->_receivedHTTPHeaders)) {
490        SRFastLog(@"Finished reading headers %@",
491                  CFBridgingRelease(CFHTTPMessageCopyAllHeaderFields(self->_receivedHTTPHeaders)));
492        [self _HTTPHeadersDidFinish];
493      } else {
494        [self _readHTTPHeader];
495      }
496    }];
497}
498
499- (void)didConnect
500{
501    SRFastLog(@"Connected");
502    CFHTTPMessageRef request = CFHTTPMessageCreateRequest(NULL, CFSTR("GET"), (__bridge CFURLRef)_url, kCFHTTPVersion1_1);
503
504    // Set host first so it defaults
505    CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Host"), (__bridge CFStringRef)(_url.port ? [NSString stringWithFormat:@"%@:%@", _url.host, _url.port] : _url.host));
506
507    NSMutableData *keyBytes = [[NSMutableData alloc] initWithLength:16];
508    BOOL success = !SecRandomCopyBytes(kSecRandomDefault, keyBytes.length, keyBytes.mutableBytes);
509    assert(success);
510
511    if ([keyBytes respondsToSelector:@selector(base64EncodedStringWithOptions:)]) {
512        _secKey = [keyBytes base64EncodedStringWithOptions:0];
513    } else {
514        _secKey = [keyBytes base64Encoding];
515    }
516
517    assert([_secKey length] == 24);
518
519    CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Upgrade"), CFSTR("websocket"));
520    CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Connection"), CFSTR("Upgrade"));
521    CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Key"), (__bridge CFStringRef)_secKey);
522    CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Version"), (__bridge CFStringRef)[NSString stringWithFormat:@"%ld", (long)_webSocketVersion]);
523
524    CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Origin"), (__bridge CFStringRef)_url.SR_origin);
525
526    if (_requestedProtocols) {
527        CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Protocol"), (__bridge CFStringRef)[_requestedProtocols componentsJoinedByString:@", "]);
528    }
529
530    [_urlRequest.allHTTPHeaderFields enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
531        CFHTTPMessageSetHeaderFieldValue(request, (__bridge CFStringRef)key, (__bridge CFStringRef)obj);
532    }];
533
534    NSData *message = CFBridgingRelease(CFHTTPMessageCopySerializedMessage(request));
535
536    CFRelease(request);
537
538    [self _writeData:message];
539    [self _readHTTPHeader];
540}
541
542- (void)_initializeStreams;
543{
544    assert(_url.port.unsignedIntValue <= UINT32_MAX);
545    uint32_t port = _url.port.unsignedIntValue;
546    if (port == 0) {
547        if (!_secure) {
548            port = 80;
549        } else {
550            port = 443;
551        }
552    }
553    NSString *host = _url.host;
554
555    CFReadStreamRef readStream = NULL;
556    CFWriteStreamRef writeStream = NULL;
557
558    CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)host, port, &readStream, &writeStream);
559
560    _outputStream = CFBridgingRelease(writeStream);
561    _inputStream = CFBridgingRelease(readStream);
562
563
564    if (_secure) {
565        NSMutableDictionary *SSLOptions = [[NSMutableDictionary alloc] init];
566
567        [_outputStream setProperty:(__bridge id)kCFStreamSocketSecurityLevelNegotiatedSSL forKey:(__bridge id)kCFStreamPropertySocketSecurityLevel];
568
569        // If we're using pinned certs, don't validate the certificate chain
570        if ([_urlRequest SR_SSLPinnedCertificates].count) {
571            [SSLOptions setValue:[NSNumber numberWithBool:NO] forKey:(__bridge id)kCFStreamSSLValidatesCertificateChain];
572        }
573
574#ifdef DEBUG
575        [SSLOptions setValue:[NSNumber numberWithBool:NO] forKey:(__bridge id)kCFStreamSSLValidatesCertificateChain];
576        NSLog(@"SocketRocket: In debug mode.  Allowing connection to any root cert");
577#endif
578
579        [_outputStream setProperty:SSLOptions
580                            forKey:(__bridge id)kCFStreamPropertySSLSettings];
581    }
582
583    _inputStream.delegate = self;
584    _outputStream.delegate = self;
585}
586
587- (void)_connect;
588{
589    if (!_scheduledRunloops.count) {
590        [self scheduleInRunLoop:[NSRunLoop SR_networkRunLoop] forMode:NSDefaultRunLoopMode];
591    }
592
593
594    [_outputStream open];
595    [_inputStream open];
596}
597
598- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode;
599{
600    [_outputStream scheduleInRunLoop:aRunLoop forMode:mode];
601    [_inputStream scheduleInRunLoop:aRunLoop forMode:mode];
602
603    [_scheduledRunloops addObject:@[aRunLoop, mode]];
604}
605
606- (void)unscheduleFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode;
607{
608    [_outputStream removeFromRunLoop:aRunLoop forMode:mode];
609    [_inputStream removeFromRunLoop:aRunLoop forMode:mode];
610
611    [_scheduledRunloops removeObject:@[aRunLoop, mode]];
612}
613
614- (void)close;
615{
616    [self closeWithCode:SRStatusCodeNormal reason:nil];
617}
618
619- (void)closeWithCode:(NSInteger)code reason:(NSString *)reason;
620{
621    assert(code);
622    dispatch_async(_workQueue, ^{
623        if (self.readyState == SR_CLOSING || self.readyState == SR_CLOSED) {
624            return;
625        }
626
627        BOOL wasConnecting = self.readyState == SR_CONNECTING;
628
629        self.readyState = SR_CLOSING;
630
631        SRFastLog(@"Closing with code %d reason %@", code, reason);
632
633        if (wasConnecting) {
634            [self _disconnect];
635            return;
636        }
637
638        size_t maxMsgSize = [reason maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding];
639        NSMutableData *mutablePayload = [[NSMutableData alloc] initWithLength:sizeof(uint16_t) + maxMsgSize];
640        NSData *payload = mutablePayload;
641
642        ((uint16_t *)mutablePayload.mutableBytes)[0] = EndianU16_BtoN(code);
643
644        if (reason) {
645            NSRange remainingRange = {0};
646
647            NSUInteger usedLength = 0;
648
649            BOOL success = [reason getBytes:(char *)mutablePayload.mutableBytes + sizeof(uint16_t) maxLength:payload.length - sizeof(uint16_t) usedLength:&usedLength encoding:NSUTF8StringEncoding options:NSStringEncodingConversionExternalRepresentation range:NSMakeRange(0, reason.length) remainingRange:&remainingRange];
650
651            assert(success);
652            assert(remainingRange.length == 0);
653
654            if (usedLength != maxMsgSize) {
655                payload = [payload subdataWithRange:NSMakeRange(0, usedLength + sizeof(uint16_t))];
656            }
657        }
658
659
660        [self _sendFrameWithOpcode:SROpCodeConnectionClose data:payload];
661    });
662}
663
664- (void)_closeWithProtocolError:(NSString *)message;
665{
666    // Need to shunt this on the _callbackQueue first to see if they received any messages
667    [self _performDelegateBlock:^{
668        [self closeWithCode:SRStatusCodeProtocolError reason:message];
669        dispatch_async(self->_workQueue, ^{
670          [self _disconnect];
671        });
672    }];
673}
674
675- (void)_failWithError:(NSError *)error;
676{
677    dispatch_async(_workQueue, ^{
678        if (self.readyState != SR_CLOSED) {
679          self->_failed = YES;
680          [self _performDelegateBlock:^{
681            if ([self.delegate respondsToSelector:@selector(webSocket:didFailWithError:)]) {
682              [self.delegate webSocket:self didFailWithError:error];
683            }
684          }];
685
686          self.readyState = SR_CLOSED;
687          self->_selfRetain = nil;
688
689          SRFastLog(@"Failing with error %@", error.localizedDescription);
690
691          [self _disconnect];
692        }
693    });
694}
695
696- (void)_writeData:(NSData *)data;
697{
698    [self assertOnWorkQueue];
699
700    if (_closeWhenFinishedWriting) {
701            return;
702    }
703    [_outputBuffer appendData:data];
704    [self _pumpWriting];
705}
706
707- (void)send:(id)data;
708{
709    NSAssert(self.readyState != SR_CONNECTING, @"Invalid State: Cannot call send: until connection is open");
710    // TODO: maybe not copy this for performance
711    data = [data copy];
712    dispatch_async(_workQueue, ^{
713        if ([data isKindOfClass:[NSString class]]) {
714            [self _sendFrameWithOpcode:SROpCodeTextFrame data:[(NSString *)data dataUsingEncoding:NSUTF8StringEncoding]];
715        } else if ([data isKindOfClass:[NSData class]]) {
716            [self _sendFrameWithOpcode:SROpCodeBinaryFrame data:data];
717        } else if (data == nil) {
718            [self _sendFrameWithOpcode:SROpCodeTextFrame data:data];
719        } else {
720            assert(NO);
721        }
722    });
723}
724
725- (void)sendPing:(NSData *)data;
726{
727    NSAssert(self.readyState == SR_OPEN, @"Invalid State: Cannot call send: until connection is open");
728    // TODO: maybe not copy this for performance
729    data = [data copy] ?: [NSData data]; // It's okay for a ping to be empty
730    dispatch_async(_workQueue, ^{
731        [self _sendFrameWithOpcode:SROpCodePing data:data];
732    });
733}
734
735- (void)handlePing:(NSData *)pingData;
736{
737    // Need to pingpong this off _callbackQueue first to make sure messages happen in order
738    [self _performDelegateBlock:^{
739      dispatch_async(self->_workQueue, ^{
740        [self _sendFrameWithOpcode:SROpCodePong data:pingData];
741      });
742    }];
743}
744
745- (void)handlePong:(NSData *)pongData;
746{
747    SRFastLog(@"Received pong");
748    [self _performDelegateBlock:^{
749        if ([self.delegate respondsToSelector:@selector(webSocket:didReceivePong:)]) {
750            [self.delegate webSocket:self didReceivePong:pongData];
751        }
752    }];
753}
754
755- (void)_handleMessage:(id)message
756{
757    SRFastLog(@"Received message");
758    [self _performDelegateBlock:^{
759        [self.delegate webSocket:self didReceiveMessage:message];
760    }];
761}
762
763
764static inline BOOL closeCodeIsValid(int closeCode) {
765    if (closeCode < 1000) {
766        return NO;
767    }
768
769    if (closeCode >= 1000 && closeCode <= 1011) {
770        if (closeCode == 1004 ||
771            closeCode == 1005 ||
772            closeCode == 1006) {
773            return NO;
774        }
775        return YES;
776    }
777
778    if (closeCode >= 3000 && closeCode <= 3999) {
779        return YES;
780    }
781
782    if (closeCode >= 4000 && closeCode <= 4999) {
783        return YES;
784    }
785
786    return NO;
787}
788
789//  Note from RFC:
790//
791//  If there is a body, the first two
792//  bytes of the body MUST be a 2-byte unsigned integer (in network byte
793//  order) representing a status code with value /code/ defined in
794//  Section 7.4.  Following the 2-byte integer the body MAY contain UTF-8
795//  encoded data with value /reason/, the interpretation of which is not
796//  defined by this specification.
797
798- (void)handleCloseWithData:(NSData *)data;
799{
800    size_t dataSize = data.length;
801    __block uint16_t closeCode = 0;
802
803    SRFastLog(@"Received close frame");
804
805    if (dataSize == 1) {
806        // TODO handle error
807        [self _closeWithProtocolError:@"Payload for close must be larger than 2 bytes"];
808        return;
809    } else if (dataSize >= 2) {
810        [data getBytes:&closeCode length:sizeof(closeCode)];
811        _closeCode = EndianU16_BtoN(closeCode);
812        if (!closeCodeIsValid(_closeCode)) {
813            [self _closeWithProtocolError:[NSString stringWithFormat:@"Cannot have close code of %d", _closeCode]];
814            return;
815        }
816        if (dataSize > 2) {
817            _closeReason = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(2, dataSize - 2)] encoding:NSUTF8StringEncoding];
818            if (!_closeReason) {
819                [self _closeWithProtocolError:@"Close reason MUST be valid UTF-8"];
820                return;
821            }
822        }
823    } else {
824        _closeCode = SRStatusNoStatusReceived;
825    }
826
827    [self assertOnWorkQueue];
828
829    if (self.readyState == SR_OPEN) {
830        [self closeWithCode:1000 reason:nil];
831    }
832    dispatch_async(_workQueue, ^{
833        [self _disconnect];
834    });
835}
836
837- (void)_disconnect;
838{
839    [self assertOnWorkQueue];
840    SRFastLog(@"Trying to disconnect");
841    _closeWhenFinishedWriting = YES;
842    [self _pumpWriting];
843}
844
845- (void)_handleFrameWithData:(NSData *)frameData opCode:(NSInteger)opcode;
846{
847    // Check that the current data is valid UTF8
848
849    BOOL isControlFrame = (opcode == SROpCodePing || opcode == SROpCodePong || opcode == SROpCodeConnectionClose);
850    if (!isControlFrame) {
851        [self _readFrameNew];
852    } else {
853        dispatch_async(_workQueue, ^{
854            [self _readFrameContinue];
855        });
856    }
857
858    switch (opcode) {
859        case SROpCodeTextFrame: {
860            NSString *str = [[NSString alloc] initWithData:frameData encoding:NSUTF8StringEncoding];
861            if (str == nil && frameData) {
862                [self closeWithCode:SRStatusCodeInvalidUTF8 reason:@"Text frames must be valid UTF-8"];
863                dispatch_async(_workQueue, ^{
864                    [self _disconnect];
865                });
866
867                return;
868            }
869            [self _handleMessage:str];
870            break;
871        }
872        case SROpCodeBinaryFrame:
873            [self _handleMessage:[frameData copy]];
874            break;
875        case SROpCodeConnectionClose:
876            [self handleCloseWithData:frameData];
877            break;
878        case SROpCodePing:
879            [self handlePing:frameData];
880            break;
881        case SROpCodePong:
882            [self handlePong:frameData];
883            break;
884        default:
885            [self _closeWithProtocolError:[NSString stringWithFormat:@"Unknown opcode %ld", (long)opcode]];
886            // TODO: Handle invalid opcode
887            break;
888    }
889}
890
891- (void)_handleFrameHeader:(frame_header)frame_header curData:(NSData *)curData;
892{
893    assert(frame_header.opcode != 0);
894
895    if (self.readyState != SR_OPEN) {
896        return;
897    }
898
899
900    BOOL isControlFrame = (frame_header.opcode == SROpCodePing || frame_header.opcode == SROpCodePong || frame_header.opcode == SROpCodeConnectionClose);
901
902    if (isControlFrame && !frame_header.fin) {
903        [self _closeWithProtocolError:@"Fragmented control frames not allowed"];
904        return;
905    }
906
907    if (isControlFrame && frame_header.payload_length >= 126) {
908        [self _closeWithProtocolError:@"Control frames cannot have payloads larger than 126 bytes"];
909        return;
910    }
911
912    if (!isControlFrame) {
913        _currentFrameOpcode = frame_header.opcode;
914        _currentFrameCount += 1;
915    }
916
917    if (frame_header.payload_length == 0) {
918        if (isControlFrame) {
919            [self _handleFrameWithData:curData opCode:frame_header.opcode];
920        } else {
921            if (frame_header.fin) {
922                [self _handleFrameWithData:_currentFrameData opCode:frame_header.opcode];
923            } else {
924                // TODO add assert that opcode is not a control;
925                [self _readFrameContinue];
926            }
927        }
928    } else {
929        assert(frame_header.payload_length <= SIZE_T_MAX);
930        [self _addConsumerWithDataLength:(size_t)frame_header.payload_length callback:^(SRWebSocket *self, NSData *newData) {
931            if (isControlFrame) {
932                [self _handleFrameWithData:newData opCode:frame_header.opcode];
933            } else {
934                if (frame_header.fin) {
935                    [self _handleFrameWithData:self->_currentFrameData opCode:frame_header.opcode];
936                } else {
937                    // TODO add assert that opcode is not a control;
938                    [self _readFrameContinue];
939                }
940
941            }
942        } readToCurrentFrame:!isControlFrame unmaskBytes:frame_header.masked];
943    }
944}
945
946/* From RFC:
947
948 0                   1                   2                   3
949 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
950 +-+-+-+-+-------+-+-------------+-------------------------------+
951 |F|R|R|R| opcode|M| Payload len |    Extended payload length    |
952 |I|S|S|S|  (4)  |A|     (7)     |             (16/64)           |
953 |N|V|V|V|       |S|             |   (if payload len==126/127)   |
954 | |1|2|3|       |K|             |                               |
955 +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
956 |     Extended payload length continued, if payload len == 127  |
957 + - - - - - - - - - - - - - - - +-------------------------------+
958 |                               |Masking-key, if MASK set to 1  |
959 +-------------------------------+-------------------------------+
960 | Masking-key (continued)       |          Payload Data         |
961 +-------------------------------- - - - - - - - - - - - - - - - +
962 :                     Payload Data continued ...                :
963 + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
964 |                     Payload Data continued ...                |
965 +---------------------------------------------------------------+
966 */
967
968static const uint8_t SRFinMask          = 0x80;
969static const uint8_t SROpCodeMask       = 0x0F;
970static const uint8_t SRRsvMask          = 0x70;
971static const uint8_t SRMaskMask         = 0x80;
972static const uint8_t SRPayloadLenMask   = 0x7F;
973
974
975- (void)_readFrameContinue;
976{
977    assert((_currentFrameCount == 0 && _currentFrameOpcode == 0) || (_currentFrameCount > 0 && _currentFrameOpcode > 0));
978
979    [self _addConsumerWithDataLength:2 callback:^(SRWebSocket *self, NSData *data) {
980        __block frame_header header = {0};
981
982        const uint8_t *headerBuffer = data.bytes;
983        assert(data.length >= 2);
984
985        if (headerBuffer[0] & SRRsvMask) {
986            [self _closeWithProtocolError:@"Server used RSV bits"];
987            return;
988        }
989
990        uint8_t receivedOpcode = (SROpCodeMask & headerBuffer[0]);
991
992        BOOL isControlFrame = (receivedOpcode == SROpCodePing || receivedOpcode == SROpCodePong || receivedOpcode == SROpCodeConnectionClose);
993
994        if (!isControlFrame && receivedOpcode != 0 && self->_currentFrameCount > 0) {
995            [self _closeWithProtocolError:@"all data frames after the initial data frame must have opcode 0"];
996            return;
997        }
998
999        if (receivedOpcode == 0 && self->_currentFrameCount == 0) {
1000            [self _closeWithProtocolError:@"cannot continue a message"];
1001            return;
1002        }
1003
1004        header.opcode = receivedOpcode == 0 ? self->_currentFrameOpcode : receivedOpcode;
1005
1006        header.fin = !!(SRFinMask & headerBuffer[0]);
1007
1008
1009        header.masked = !!(SRMaskMask & headerBuffer[1]);
1010        header.payload_length = SRPayloadLenMask & headerBuffer[1];
1011
1012        headerBuffer = NULL;
1013
1014        if (header.masked) {
1015            [self _closeWithProtocolError:@"Client must receive unmasked data"];
1016        }
1017
1018        size_t extra_bytes_needed = header.masked ? sizeof(self->_currentReadMaskKey) : 0;
1019
1020        if (header.payload_length == 126) {
1021            extra_bytes_needed += sizeof(uint16_t);
1022        } else if (header.payload_length == 127) {
1023            extra_bytes_needed += sizeof(uint64_t);
1024        }
1025
1026        if (extra_bytes_needed == 0) {
1027            [self _handleFrameHeader:header curData:self->_currentFrameData];
1028        } else {
1029            [self _addConsumerWithDataLength:extra_bytes_needed callback:^(SRWebSocket *self, NSData *data) {
1030                size_t mapped_size = data.length;
1031                const void *mapped_buffer = data.bytes;
1032                size_t offset = 0;
1033
1034                if (header.payload_length == 126) {
1035                    assert(mapped_size >= sizeof(uint16_t));
1036                    uint16_t newLen = EndianU16_BtoN(*(uint16_t *)(mapped_buffer));
1037                    header.payload_length = newLen;
1038                    offset += sizeof(uint16_t);
1039                } else if (header.payload_length == 127) {
1040                    assert(mapped_size >= sizeof(uint64_t));
1041                    header.payload_length = EndianU64_BtoN(*(uint64_t *)(mapped_buffer));
1042                    offset += sizeof(uint64_t);
1043                } else {
1044                    assert(header.payload_length < 126 && header.payload_length >= 0);
1045                }
1046
1047
1048                if (header.masked) {
1049                  assert(mapped_size >= sizeof(self->_currentReadMaskOffset) + offset);
1050                  memcpy(self->_currentReadMaskKey,
1051                         ((uint8_t *)mapped_buffer) + offset,
1052                         sizeof(self->_currentReadMaskKey));
1053                }
1054
1055                [self _handleFrameHeader:header curData:self->_currentFrameData];
1056            } readToCurrentFrame:NO unmaskBytes:NO];
1057        }
1058    } readToCurrentFrame:NO unmaskBytes:NO];
1059}
1060
1061- (void)_readFrameNew;
1062{
1063  dispatch_async(_workQueue, ^{
1064    [self->_currentFrameData setLength:0];
1065
1066    self->_currentFrameOpcode = 0;
1067    self->_currentFrameCount = 0;
1068    self->_readOpCount = 0;
1069    self->_currentStringScanPosition = 0;
1070
1071    [self _readFrameContinue];
1072  });
1073}
1074
1075- (void)_pumpWriting;
1076{
1077    [self assertOnWorkQueue];
1078
1079    NSUInteger dataLength = _outputBuffer.length;
1080    if (dataLength - _outputBufferOffset > 0 && _outputStream.hasSpaceAvailable) {
1081        NSInteger bytesWritten = [_outputStream write:_outputBuffer.bytes + _outputBufferOffset maxLength:dataLength - _outputBufferOffset];
1082        if (bytesWritten == -1) {
1083            [self _failWithError:[NSError errorWithDomain:SRWebSocketErrorDomain code:2145 userInfo:[NSDictionary dictionaryWithObject:@"Error writing to stream" forKey:NSLocalizedDescriptionKey]]];
1084             return;
1085        }
1086
1087        _outputBufferOffset += bytesWritten;
1088
1089        if (_outputBufferOffset > 4096 && _outputBufferOffset > (_outputBuffer.length >> 1)) {
1090            _outputBuffer = [[NSMutableData alloc] initWithBytes:(char *)_outputBuffer.bytes + _outputBufferOffset length:_outputBuffer.length - _outputBufferOffset];
1091            _outputBufferOffset = 0;
1092        }
1093    }
1094
1095    if (_closeWhenFinishedWriting &&
1096        _outputBuffer.length - _outputBufferOffset == 0 &&
1097        (_inputStream.streamStatus != NSStreamStatusNotOpen &&
1098         _inputStream.streamStatus != NSStreamStatusClosed) &&
1099        !_sentClose) {
1100        _sentClose = YES;
1101
1102        [_outputStream close];
1103        [_inputStream close];
1104
1105
1106        for (NSArray *runLoop in [_scheduledRunloops copy]) {
1107            [self unscheduleFromRunLoop:[runLoop objectAtIndex:0] forMode:[runLoop objectAtIndex:1]];
1108        }
1109
1110        if (!_failed) {
1111            [self _performDelegateBlock:^{
1112                if ([self.delegate respondsToSelector:@selector(webSocket:didCloseWithCode:reason:wasClean:)]) {
1113                  [self.delegate webSocket:self
1114                          didCloseWithCode:self->_closeCode
1115                                    reason:self->_closeReason
1116                                  wasClean:YES];
1117                }
1118            }];
1119        }
1120
1121        _selfRetain = nil;
1122    }
1123}
1124
1125- (void)_addConsumerWithScanner:(stream_scanner)consumer callback:(data_callback)callback;
1126{
1127    [self assertOnWorkQueue];
1128    [self _addConsumerWithScanner:consumer callback:callback dataLength:0];
1129}
1130
1131- (void)_addConsumerWithDataLength:(size_t)dataLength callback:(data_callback)callback readToCurrentFrame:(BOOL)readToCurrentFrame unmaskBytes:(BOOL)unmaskBytes;
1132{
1133    [self assertOnWorkQueue];
1134    assert(dataLength);
1135
1136    [_consumers addObject:[_consumerPool consumerWithScanner:nil handler:callback bytesNeeded:dataLength readToCurrentFrame:readToCurrentFrame unmaskBytes:unmaskBytes]];
1137    [self _pumpScanner];
1138}
1139
1140- (void)_addConsumerWithScanner:(stream_scanner)consumer callback:(data_callback)callback dataLength:(size_t)dataLength;
1141{
1142    [self assertOnWorkQueue];
1143    [_consumers addObject:[_consumerPool consumerWithScanner:consumer handler:callback bytesNeeded:dataLength readToCurrentFrame:NO unmaskBytes:NO]];
1144    [self _pumpScanner];
1145}
1146
1147
1148static const char CRLFCRLFBytes[] = {'\r', '\n', '\r', '\n'};
1149
1150- (void)_readUntilHeaderCompleteWithCallback:(data_callback)dataHandler;
1151{
1152    [self _readUntilBytes:CRLFCRLFBytes length:sizeof(CRLFCRLFBytes) callback:dataHandler];
1153}
1154
1155- (void)_readUntilBytes:(const void *)bytes length:(size_t)length callback:(data_callback)dataHandler;
1156{
1157    // TODO optimize so this can continue from where we last searched
1158    stream_scanner consumer = ^size_t(NSData *data) {
1159        __block size_t found_size = 0;
1160        __block size_t match_count = 0;
1161
1162        size_t size = data.length;
1163        const unsigned char *buffer = data.bytes;
1164        for (size_t i = 0; i < size; i++ ) {
1165            if (((const unsigned char *)buffer)[i] == ((const unsigned char *)bytes)[match_count]) {
1166                match_count += 1;
1167                if (match_count == length) {
1168                    found_size = i + 1;
1169                    break;
1170                }
1171            } else {
1172                match_count = 0;
1173            }
1174        }
1175        return found_size;
1176    };
1177    [self _addConsumerWithScanner:consumer callback:dataHandler];
1178}
1179
1180
1181// Returns true if did work
1182- (BOOL)_innerPumpScanner {
1183
1184    BOOL didWork = NO;
1185
1186    if (self.readyState >= SR_CLOSING) {
1187        return didWork;
1188    }
1189
1190    if (!_consumers.count) {
1191        return didWork;
1192    }
1193
1194    size_t curSize = _readBuffer.length - _readBufferOffset;
1195    if (!curSize) {
1196        return didWork;
1197    }
1198
1199    SRIOConsumer *consumer = [_consumers objectAtIndex:0];
1200
1201    size_t bytesNeeded = consumer.bytesNeeded;
1202
1203    size_t foundSize = 0;
1204    if (consumer.consumer) {
1205        NSData *tempView = [NSData dataWithBytesNoCopy:(char *)_readBuffer.bytes + _readBufferOffset length:_readBuffer.length - _readBufferOffset freeWhenDone:NO];
1206        foundSize = consumer.consumer(tempView);
1207    } else {
1208        assert(consumer.bytesNeeded);
1209        if (curSize >= bytesNeeded) {
1210            foundSize = bytesNeeded;
1211        } else if (consumer.readToCurrentFrame) {
1212            foundSize = curSize;
1213        }
1214    }
1215
1216    NSData *slice = nil;
1217    if (consumer.readToCurrentFrame || foundSize) {
1218        NSRange sliceRange = NSMakeRange(_readBufferOffset, foundSize);
1219        slice = [_readBuffer subdataWithRange:sliceRange];
1220
1221        _readBufferOffset += foundSize;
1222
1223        if (_readBufferOffset > 4096 && _readBufferOffset > (_readBuffer.length >> 1)) {
1224            _readBuffer = [[NSMutableData alloc] initWithBytes:(char *)_readBuffer.bytes + _readBufferOffset length:_readBuffer.length - _readBufferOffset];            _readBufferOffset = 0;
1225        }
1226
1227        if (consumer.unmaskBytes) {
1228            NSMutableData *mutableSlice = [slice mutableCopy];
1229
1230            NSUInteger len = mutableSlice.length;
1231            uint8_t *bytes = mutableSlice.mutableBytes;
1232
1233            for (NSUInteger i = 0; i < len; i++) {
1234                bytes[i] = bytes[i] ^ _currentReadMaskKey[_currentReadMaskOffset % sizeof(_currentReadMaskKey)];
1235                _currentReadMaskOffset += 1;
1236            }
1237
1238            slice = mutableSlice;
1239        }
1240
1241        if (consumer.readToCurrentFrame) {
1242            [_currentFrameData appendData:slice];
1243
1244            _readOpCount += 1;
1245
1246            if (_currentFrameOpcode == SROpCodeTextFrame) {
1247                // Validate UTF8 stuff.
1248                size_t currentDataSize = _currentFrameData.length;
1249                if (_currentFrameOpcode == SROpCodeTextFrame && currentDataSize > 0) {
1250                    // TODO: Optimize the crap out of this.  Don't really have to copy all the data each time
1251
1252                    size_t scanSize = currentDataSize - _currentStringScanPosition;
1253
1254                    NSData *scan_data = [_currentFrameData subdataWithRange:NSMakeRange(_currentStringScanPosition, scanSize)];
1255                    int32_t valid_utf8_size = validate_dispatch_data_partial_string(scan_data);
1256
1257                    if (valid_utf8_size == -1) {
1258                        [self closeWithCode:SRStatusCodeInvalidUTF8 reason:@"Text frames must be valid UTF-8"];
1259                        dispatch_async(_workQueue, ^{
1260                            [self _disconnect];
1261                        });
1262                        return didWork;
1263                    } else {
1264                        _currentStringScanPosition += valid_utf8_size;
1265                    }
1266                }
1267
1268            }
1269
1270            consumer.bytesNeeded -= foundSize;
1271
1272            if (consumer.bytesNeeded == 0) {
1273                [_consumers removeObjectAtIndex:0];
1274                consumer.handler(self, nil);
1275                [_consumerPool returnConsumer:consumer];
1276                didWork = YES;
1277            }
1278        } else if (foundSize) {
1279            [_consumers removeObjectAtIndex:0];
1280            consumer.handler(self, slice);
1281            [_consumerPool returnConsumer:consumer];
1282            didWork = YES;
1283        }
1284    }
1285    return didWork;
1286}
1287
1288-(void)_pumpScanner;
1289{
1290    [self assertOnWorkQueue];
1291
1292    if (!_isPumping) {
1293        _isPumping = YES;
1294    } else {
1295        return;
1296    }
1297
1298    while ([self _innerPumpScanner]) {
1299
1300    }
1301
1302    _isPumping = NO;
1303}
1304
1305//#define NOMASK
1306
1307static const size_t SRFrameHeaderOverhead = 32;
1308
1309- (void)_sendFrameWithOpcode:(SROpCode)opcode data:(id)data;
1310{
1311    [self assertOnWorkQueue];
1312
1313    if (nil == data) {
1314        return;
1315    }
1316
1317    NSAssert([data isKindOfClass:[NSData class]] || [data isKindOfClass:[NSString class]], @"NSString or NSData");
1318
1319    size_t payloadLength = [data isKindOfClass:[NSString class]] ? [(NSString *)data lengthOfBytesUsingEncoding:NSUTF8StringEncoding] : [data length];
1320
1321    NSMutableData *frame = [[NSMutableData alloc] initWithLength:payloadLength + SRFrameHeaderOverhead];
1322    if (!frame) {
1323        [self closeWithCode:SRStatusCodeMessageTooBig reason:@"Message too big"];
1324        return;
1325    }
1326    uint8_t *frame_buffer = (uint8_t *)[frame mutableBytes];
1327
1328    // set fin
1329    frame_buffer[0] = SRFinMask | opcode;
1330
1331    BOOL useMask = YES;
1332#ifdef NOMASK
1333    useMask = NO;
1334#endif
1335
1336    if (useMask) {
1337    // set the mask and header
1338        frame_buffer[1] |= SRMaskMask;
1339    }
1340
1341    size_t frame_buffer_size = 2;
1342
1343    const uint8_t *unmasked_payload = NULL;
1344    if ([data isKindOfClass:[NSData class]]) {
1345        unmasked_payload = (uint8_t *)[data bytes];
1346    } else if ([data isKindOfClass:[NSString class]]) {
1347        unmasked_payload =  (const uint8_t *)[data UTF8String];
1348    } else {
1349        return;
1350    }
1351
1352    if (payloadLength < 126) {
1353        frame_buffer[1] |= payloadLength;
1354    } else if (payloadLength <= UINT16_MAX) {
1355        frame_buffer[1] |= 126;
1356        *((uint16_t *)(frame_buffer + frame_buffer_size)) = EndianU16_BtoN((uint16_t)payloadLength);
1357        frame_buffer_size += sizeof(uint16_t);
1358    } else {
1359        frame_buffer[1] |= 127;
1360        *((uint64_t *)(frame_buffer + frame_buffer_size)) = EndianU64_BtoN((uint64_t)payloadLength);
1361        frame_buffer_size += sizeof(uint64_t);
1362    }
1363
1364    if (!useMask) {
1365        for (size_t i = 0; i < payloadLength; i++) {
1366            frame_buffer[frame_buffer_size] = unmasked_payload[i];
1367            frame_buffer_size += 1;
1368        }
1369    } else {
1370        uint8_t *mask_key = frame_buffer + frame_buffer_size;
1371        BOOL success = !SecRandomCopyBytes(kSecRandomDefault, sizeof(uint32_t), (uint8_t *)mask_key);
1372        assert(success);
1373        frame_buffer_size += sizeof(uint32_t);
1374
1375        // TODO: could probably optimize this with SIMD
1376        for (size_t i = 0; i < payloadLength; i++) {
1377            frame_buffer[frame_buffer_size] = unmasked_payload[i] ^ mask_key[i % sizeof(uint32_t)];
1378            frame_buffer_size += 1;
1379        }
1380    }
1381
1382    assert(frame_buffer_size <= [frame length]);
1383    frame.length = frame_buffer_size;
1384
1385    [self _writeData:frame];
1386}
1387
1388- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode;
1389{
1390    if (_secure && !_pinnedCertFound && (eventCode == NSStreamEventHasBytesAvailable || eventCode == NSStreamEventHasSpaceAvailable)) {
1391
1392        NSArray *sslCerts = [_urlRequest SR_SSLPinnedCertificates];
1393        if (sslCerts) {
1394            SecTrustRef secTrust = (__bridge SecTrustRef)[aStream propertyForKey:(__bridge id)kCFStreamPropertySSLPeerTrust];
1395            if (secTrust) {
1396                NSInteger numCerts = SecTrustGetCertificateCount(secTrust);
1397                for (NSInteger i = 0; i < numCerts && !_pinnedCertFound; i++) {
1398                    SecCertificateRef cert = SecTrustGetCertificateAtIndex(secTrust, i);
1399                    NSData *certData = CFBridgingRelease(SecCertificateCopyData(cert));
1400
1401                    for (id ref in sslCerts) {
1402                        SecCertificateRef trustedCert = (__bridge SecCertificateRef)ref;
1403                        NSData *trustedCertData = CFBridgingRelease(SecCertificateCopyData(trustedCert));
1404
1405                        if ([trustedCertData isEqualToData:certData]) {
1406                            _pinnedCertFound = YES;
1407                            break;
1408                        }
1409                    }
1410                }
1411            }
1412
1413            if (!_pinnedCertFound) {
1414                dispatch_async(_workQueue, ^{
1415                    [self _failWithError:[NSError errorWithDomain:SRWebSocketErrorDomain code:23556 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Invalid server cert"] forKey:NSLocalizedDescriptionKey]]];
1416                });
1417                return;
1418            }
1419        }
1420    }
1421
1422    dispatch_async(_workQueue, ^{
1423        switch (eventCode) {
1424            case NSStreamEventOpenCompleted: {
1425                SRFastLog(@"NSStreamEventOpenCompleted %@", aStream);
1426                if (self.readyState >= SR_CLOSING) {
1427                    return;
1428                }
1429                assert(self->_readBuffer);
1430
1431                if (self.readyState == SR_CONNECTING && aStream == self->_inputStream) {
1432                  [self didConnect];
1433                }
1434                [self _pumpWriting];
1435                [self _pumpScanner];
1436                break;
1437            }
1438
1439            case NSStreamEventErrorOccurred: {
1440                SRFastLog(@"NSStreamEventErrorOccurred %@ %@", aStream, [[aStream streamError] copy]);
1441                /// TODO specify error better!
1442                [self _failWithError:aStream.streamError];
1443                self->_readBufferOffset = 0;
1444                [self->_readBuffer setLength:0];
1445                break;
1446
1447            }
1448
1449            case NSStreamEventEndEncountered: {
1450                [self _pumpScanner];
1451                SRFastLog(@"NSStreamEventEndEncountered %@", aStream);
1452                if (aStream.streamError) {
1453                    [self _failWithError:aStream.streamError];
1454                } else {
1455                    if (self.readyState != SR_CLOSED) {
1456                        self.readyState = SR_CLOSED;
1457                        self->_selfRetain = nil;
1458                    }
1459
1460                    if (!self->_sentClose && !self->_failed) {
1461                      self->_sentClose = YES;
1462                      // If we get closed in this state it's probably not clean because we should be
1463                      // sending this when we send messages
1464                      [self
1465                          _performDelegateBlock:^{
1466                            if ([self.delegate respondsToSelector:@selector(webSocket:didCloseWithCode:reason:wasClean:)]) {
1467                              [self.delegate webSocket:self
1468                                      didCloseWithCode:SRStatusCodeGoingAway
1469                                                reason:@"Stream end encountered"
1470                                              wasClean:NO];
1471                            }
1472                          }];
1473                    }
1474                }
1475
1476                break;
1477            }
1478
1479            case NSStreamEventHasBytesAvailable: {
1480                SRFastLog(@"NSStreamEventHasBytesAvailable %@", aStream);
1481                enum EnumType : int { bufferSize = 2048 };
1482                uint8_t buffer[bufferSize];
1483
1484                while (self->_inputStream.hasBytesAvailable) {
1485                  NSInteger bytes_read = [self->_inputStream read:buffer maxLength:bufferSize];
1486
1487                  if (bytes_read > 0) {
1488                    [self->_readBuffer appendBytes:buffer length:bytes_read];
1489                  } else if (bytes_read < 0) {
1490                    [self _failWithError:self->_inputStream.streamError];
1491                  }
1492
1493                  if (bytes_read != bufferSize) {
1494                    break;
1495                  }
1496                };
1497                [self _pumpScanner];
1498                break;
1499            }
1500
1501            case NSStreamEventHasSpaceAvailable: {
1502                SRFastLog(@"NSStreamEventHasSpaceAvailable %@", aStream);
1503                [self _pumpWriting];
1504                break;
1505            }
1506
1507            default:
1508                SRFastLog(@"(default)  %@", aStream);
1509                break;
1510        }
1511    });
1512}
1513
1514@end
1515
1516
1517@implementation SRIOConsumer
1518
1519@synthesize bytesNeeded = _bytesNeeded;
1520@synthesize consumer = _scanner;
1521@synthesize handler = _handler;
1522@synthesize readToCurrentFrame = _readToCurrentFrame;
1523@synthesize unmaskBytes = _unmaskBytes;
1524
1525- (void)setupWithScanner:(stream_scanner)scanner handler:(data_callback)handler bytesNeeded:(size_t)bytesNeeded readToCurrentFrame:(BOOL)readToCurrentFrame unmaskBytes:(BOOL)unmaskBytes;
1526{
1527    _scanner = [scanner copy];
1528    _handler = [handler copy];
1529    _bytesNeeded = bytesNeeded;
1530    _readToCurrentFrame = readToCurrentFrame;
1531    _unmaskBytes = unmaskBytes;
1532    assert(_scanner || _bytesNeeded);
1533}
1534
1535
1536@end
1537
1538
1539@implementation SRIOConsumerPool {
1540    NSUInteger _poolSize;
1541    NSMutableArray *_bufferedConsumers;
1542}
1543
1544- (id)initWithBufferCapacity:(NSUInteger)poolSize;
1545{
1546    self = [super init];
1547    if (self) {
1548        _poolSize = poolSize;
1549        _bufferedConsumers = [[NSMutableArray alloc] initWithCapacity:poolSize];
1550    }
1551    return self;
1552}
1553
1554- (id)init
1555{
1556    return [self initWithBufferCapacity:8];
1557}
1558
1559- (SRIOConsumer *)consumerWithScanner:(stream_scanner)scanner handler:(data_callback)handler bytesNeeded:(size_t)bytesNeeded readToCurrentFrame:(BOOL)readToCurrentFrame unmaskBytes:(BOOL)unmaskBytes;
1560{
1561    SRIOConsumer *consumer = nil;
1562    if (_bufferedConsumers.count) {
1563        consumer = [_bufferedConsumers lastObject];
1564        [_bufferedConsumers removeLastObject];
1565    } else {
1566        consumer = [[SRIOConsumer alloc] init];
1567    }
1568
1569    [consumer setupWithScanner:scanner handler:handler bytesNeeded:bytesNeeded readToCurrentFrame:readToCurrentFrame unmaskBytes:unmaskBytes];
1570
1571    return consumer;
1572}
1573
1574- (void)returnConsumer:(SRIOConsumer *)consumer;
1575{
1576    if (_bufferedConsumers.count < _poolSize) {
1577        [_bufferedConsumers addObject:consumer];
1578    }
1579}
1580
1581@end
1582
1583
1584@implementation  NSURLRequest (CertificateAdditions)
1585
1586- (NSArray *)SR_SSLPinnedCertificates;
1587{
1588    return [NSURLProtocol propertyForKey:@"SR_SSLPinnedCertificates" inRequest:self];
1589}
1590
1591@end
1592
1593@implementation  NSMutableURLRequest (CertificateAdditions)
1594
1595- (NSArray *)SR_SSLPinnedCertificates;
1596{
1597    return [NSURLProtocol propertyForKey:@"SR_SSLPinnedCertificates" inRequest:self];
1598}
1599
1600- (void)setSR_SSLPinnedCertificates:(NSArray *)SR_SSLPinnedCertificates;
1601{
1602    [NSURLProtocol setProperty:SR_SSLPinnedCertificates forKey:@"SR_SSLPinnedCertificates" inRequest:self];
1603}
1604
1605@end
1606
1607@implementation NSURL (SRWebSocket)
1608
1609- (NSString *)SR_origin;
1610{
1611    NSString *scheme = [self.scheme lowercaseString];
1612
1613    if ([scheme isEqualToString:@"wss"]) {
1614        scheme = @"https";
1615    } else if ([scheme isEqualToString:@"ws"]) {
1616        scheme = @"http";
1617    }
1618
1619    if (self.port) {
1620        return [NSString stringWithFormat:@"%@://%@:%@/", scheme, self.host, self.port];
1621    } else {
1622        return [NSString stringWithFormat:@"%@://%@/", scheme, self.host];
1623    }
1624}
1625
1626@end
1627
1628//#define SR_ENABLE_LOG
1629
1630static inline void SRFastLog(NSString *format, ...)  {
1631#ifdef SR_ENABLE_LOG
1632    __block va_list arg_list;
1633    va_start (arg_list, format);
1634
1635    NSString *formattedString = [[NSString alloc] initWithFormat:format arguments:arg_list];
1636
1637    va_end(arg_list);
1638
1639    NSLog(@"[SR] %@", formattedString);
1640#endif
1641}
1642
1643
1644#ifdef HAS_ICU
1645
1646static inline int32_t validate_dispatch_data_partial_string(NSData *data) {
1647    if ([data length] > INT32_MAX) {
1648        // INT32_MAX is the limit so long as this Framework is using 32 bit ints everywhere.
1649        return -1;
1650    }
1651
1652    int32_t size = (int32_t)[data length];
1653
1654    const void * contents = [data bytes];
1655    const uint8_t *str = (const uint8_t *)contents;
1656
1657    UChar32 codepoint = 1;
1658    int32_t offset = 0;
1659    int32_t lastOffset = 0;
1660    while(offset < size && codepoint > 0)  {
1661        lastOffset = offset;
1662        U8_NEXT(str, offset, size, codepoint);
1663    }
1664
1665    if (codepoint == -1) {
1666        // Check to see if the last byte is valid or whether it was just continuing
1667        if (!U8_IS_LEAD(str[lastOffset]) || U8_COUNT_TRAIL_BYTES(str[lastOffset]) + lastOffset < (int32_t)size) {
1668
1669            size = -1;
1670        } else {
1671            uint8_t leadByte = str[lastOffset];
1672            U8_MASK_LEAD_BYTE(leadByte, U8_COUNT_TRAIL_BYTES(leadByte));
1673
1674            for (int i = lastOffset + 1; i < offset; i++) {
1675                if (U8_IS_SINGLE(str[i]) || U8_IS_LEAD(str[i]) || !U8_IS_TRAIL(str[i])) {
1676                    size = -1;
1677                }
1678            }
1679
1680            if (size != -1) {
1681                size = lastOffset;
1682            }
1683        }
1684    }
1685
1686    if (size != -1 && ![[NSString alloc] initWithBytesNoCopy:(char *)[data bytes] length:size encoding:NSUTF8StringEncoding freeWhenDone:NO]) {
1687        size = -1;
1688    }
1689
1690    return size;
1691}
1692
1693#else
1694
1695// This is a hack, and probably not optimal
1696static inline int32_t validate_dispatch_data_partial_string(NSData *data) {
1697    static const int maxCodepointSize = 3;
1698
1699    for (int i = 0; i < maxCodepointSize; i++) {
1700        NSString *str = [[NSString alloc] initWithBytesNoCopy:(char *)data.bytes length:data.length - i encoding:NSUTF8StringEncoding freeWhenDone:NO];
1701        if (str) {
1702            return data.length - i;
1703        }
1704    }
1705
1706    return -1;
1707}
1708
1709#endif
1710
1711static _SRRunLoopThread *networkThread = nil;
1712static NSRunLoop *networkRunLoop = nil;
1713
1714@implementation NSRunLoop (SRWebSocket)
1715
1716+ (NSRunLoop *)SR_networkRunLoop {
1717    static dispatch_once_t onceToken;
1718    dispatch_once(&onceToken, ^{
1719        networkThread = [[_SRRunLoopThread alloc] init];
1720        networkThread.name = @"com.squareup.SocketRocket.NetworkThread";
1721        [networkThread start];
1722        networkRunLoop = networkThread.runLoop;
1723    });
1724
1725    return networkRunLoop;
1726}
1727
1728@end
1729
1730
1731@implementation _SRRunLoopThread {
1732    dispatch_group_t _waitGroup;
1733}
1734
1735@synthesize runLoop = _runLoop;
1736
1737- (void)dealloc
1738{
1739    sr_dispatch_release(_waitGroup);
1740}
1741
1742- (id)init
1743{
1744    self = [super init];
1745    if (self) {
1746        _waitGroup = dispatch_group_create();
1747        dispatch_group_enter(_waitGroup);
1748    }
1749    return self;
1750}
1751
1752- (void)main;
1753{
1754    @autoreleasepool {
1755        _runLoop = [NSRunLoop currentRunLoop];
1756        dispatch_group_leave(_waitGroup);
1757
1758        NSTimer *timer = [[NSTimer alloc] initWithFireDate:[NSDate distantFuture] interval:0.0 target:nil selector:nil userInfo:nil repeats:NO];
1759        [_runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
1760
1761        while ([_runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) {
1762
1763        }
1764        assert(NO);
1765    }
1766}
1767
1768- (NSRunLoop *)runLoop;
1769{
1770    dispatch_group_wait(_waitGroup, DISPATCH_TIME_FOREVER);
1771    return _runLoop;
1772}
1773
1774@end
1775