Skip to content

Commit

Permalink
Merge pull request #313 from Simperium/develop
Browse files Browse the repository at this point in the history
Simperium Mk 0.6.6
  • Loading branch information
jleandroperez committed Jul 18, 2014
2 parents cdae6ee + 677ab5e commit c3ea937
Show file tree
Hide file tree
Showing 22 changed files with 222 additions and 548 deletions.
24 changes: 0 additions & 24 deletions External/SocketRocket/NSData+SRB64Additions.h

This file was deleted.

39 changes: 0 additions & 39 deletions External/SocketRocket/NSData+SRB64Additions.m

This file was deleted.

13 changes: 13 additions & 0 deletions External/SocketRocket/SRWebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ typedef enum {
SR_CLOSED = 3,
} SRReadyState;

typedef enum SRStatusCode : NSInteger {
SRStatusCodeNormal = 1000,
SRStatusCodeGoingAway = 1001,
SRStatusCodeProtocolError = 1002,
SRStatusCodeUnhandledType = 1003,
// 1004 reserved.
SRStatusNoStatusReceived = 1005,
// 1004-1006 reserved.
SRStatusCodeInvalidUTF8 = 1007,
SRStatusCodePolicyViolated = 1008,
SRStatusCodeMessageTooBig = 1009,
} SRStatusCode;

@class SRWebSocket;

extern NSString *const SRWebSocketErrorDomain;
Expand Down
75 changes: 23 additions & 52 deletions External/SocketRocket/SRWebSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
#import <CommonCrypto/CommonDigest.h>
#import <Security/SecRandom.h>

#import "base64.h"
#import "NSData+SRB64Additions.h"

#if OS_OBJECT_USE_OBJC_RETAIN_RELEASE
#define sr_dispatch_retain(x)
#define sr_dispatch_release(x)
Expand All @@ -48,7 +45,7 @@
#endif

#if !__has_feature(objc_arc)
#error SocketRocket muust be compiled with ARC enabled
#error SocketRocket must be compiled with ARC enabled
#endif


Expand All @@ -62,19 +59,6 @@
// B-F reserved.
} SROpCode;

typedef enum {
SRStatusCodeNormal = 1000,
SRStatusCodeGoingAway = 1001,
SRStatusCodeProtocolError = 1002,
SRStatusCodeUnhandledType = 1003,
// 1004 reserved.
SRStatusNoStatusReceived = 1005,
// 1004-1006 reserved.
SRStatusCodeInvalidUTF8 = 1007,
SRStatusCodePolicyViolated = 1008,
SRStatusCodeMessageTooBig = 1009,
} SRStatusCode;

typedef struct {
BOOL fin;
// BOOL rsv1;
Expand All @@ -88,9 +72,6 @@
static NSString *const SRWebSocketAppendToSecKeyString = @"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";

static inline int32_t validate_dispatch_data_partial_string(NSData *data);

// Simperium Fix: Commenting out unused method
//static inline dispatch_queue_t log_queue();
static inline void SRFastLog(NSString *format, ...);

@interface NSData (SRWebSocket)
Expand Down Expand Up @@ -125,20 +106,12 @@ @interface _SRRunLoopThread : NSThread

static NSString *newSHA1String(const char *bytes, size_t length) {
uint8_t md[CC_SHA1_DIGEST_LENGTH];


assert(length >= 0);
assert(length <= UINT32_MAX);
CC_SHA1(bytes, (CC_LONG)length, md);

size_t buffer_size = ((sizeof(md) * 3 + 2) / 2);

char *buffer = (char *)malloc(buffer_size);

int len = b64_ntop(md, CC_SHA1_DIGEST_LENGTH, buffer, buffer_size);
if (len == -1) {
free(buffer);
return nil;
} else{
return [[NSString alloc] initWithBytesNoCopy:buffer length:len encoding:NSASCIIStringEncoding freeWhenDone:YES];
}
return [[NSData dataWithBytes:md length:CC_SHA1_DIGEST_LENGTH] base64Encoding];
}

@implementation NSData (SRWebSocket)
Expand Down Expand Up @@ -527,7 +500,7 @@ - (void)didConnect

NSMutableData *keyBytes = [[NSMutableData alloc] initWithLength:16];
SecRandomCopyBytes(kSecRandomDefault, keyBytes.length, keyBytes.mutableBytes);
_secKey = [keyBytes SR_stringByBase64Encoding];
_secKey = keyBytes.base64Encoding;
assert([_secKey length] == 24);

CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Upgrade"), CFSTR("websocket"));
Expand Down Expand Up @@ -555,7 +528,8 @@ - (void)didConnect

- (void)_initializeStreams;
{
unsigned int port = _url.port.unsignedIntValue;
assert(_url.port.unsignedIntValue <= UINT32_MAX);
uint32_t port = _url.port.unsignedIntValue;
if (port == 0) {
if (!_secure) {
port = 80;
Expand All @@ -568,7 +542,7 @@ - (void)_initializeStreams;
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;

CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)host, (UInt32)port, &readStream, &writeStream);
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)host, port, &readStream, &writeStream);

_outputStream = CFBridgingRelease(writeStream);
_inputStream = CFBridgingRelease(readStream);
Expand Down Expand Up @@ -923,7 +897,8 @@ - (void)_handleFrameHeader:(frame_header)frame_header curData:(NSData *)curData;
}
}
} else {
[self _addConsumerWithDataLength:frame_header.payload_length callback:^(SRWebSocket *self, NSData *newData) {
assert(frame_header.payload_length <= SIZE_T_MAX);
[self _addConsumerWithDataLength:(size_t)frame_header.payload_length callback:^(SRWebSocket *self, NSData *newData) {
if (isControlFrame) {
[self _handleFrameWithData:newData opCode:frame_header.opcode];
} else {
Expand Down Expand Up @@ -1135,6 +1110,7 @@ - (void)_addConsumerWithScanner:(stream_scanner)consumer callback:(data_callback
[self _pumpScanner];
}


- (void)_scheduleCleanup
{
@synchronized(self) {
Expand Down Expand Up @@ -1162,6 +1138,7 @@ - (void)_cleanupSelfReference:(NSTimer *)timer
});
}


static const char CRLFCRLFBytes[] = {'\r', '\n', '\r', '\n'};

- (void)_readUntilHeaderCompleteWithCallback:(data_callback)dataHandler;
Expand Down Expand Up @@ -1480,13 +1457,13 @@ - (void)safeHandleEvent:(NSStreamEvent)eventCode stream:(NSStream *)aStream
self.readyState = SR_CLOSED;
[self _scheduleCleanup];
}

if (!_sentClose && !_failed) {
_sentClose = YES;
// If we get closed in this state it's probably not clean because we should be sending this when we send messages
[self _performDelegateBlock:^{
if ([self.delegate respondsToSelector:@selector(webSocket:didCloseWithCode:reason:wasClean:)]) {
[self.delegate webSocket:self didCloseWithCode:0 reason:@"Stream end encountered" wasClean:NO];
[self.delegate webSocket:self didCloseWithCode:SRStatusCodeGoingAway reason:@"Stream end encountered" wasClean:NO];
}
}];
}
Expand Down Expand Up @@ -1643,17 +1620,6 @@ - (NSString *)SR_origin;

@end

// Simperium Fix: Commenting out unused method
//static inline dispatch_queue_t log_queue() {
// static dispatch_queue_t queue = 0;
// static dispatch_once_t onceToken;
// dispatch_once(&onceToken, ^{
// queue = dispatch_queue_create("fast log queue", DISPATCH_QUEUE_SERIAL);
// });
//
// return queue;
//}

//#define SR_ENABLE_LOG

static inline void SRFastLog(NSString *format, ...) {
Expand All @@ -1673,9 +1639,14 @@ static inline void SRFastLog(NSString *format, ...) {
#ifdef HAS_ICU

static inline int32_t validate_dispatch_data_partial_string(NSData *data) {
if ([data length] > INT32_MAX) {
// INT32_MAX is the limit so long as this Framework is using 32 bit ints everywhere.
return -1;
}

int32_t size = (int32_t)[data length];

const void * contents = [data bytes];
long size = [data length];

const uint8_t *str = (const uint8_t *)contents;

UChar32 codepoint = 1;
Expand Down Expand Up @@ -1711,7 +1682,7 @@ static inline int32_t validate_dispatch_data_partial_string(NSData *data) {
size = -1;
}

return (int32_t)size;
return size;
}

#else
Expand Down

0 comments on commit c3ea937

Please sign in to comment.