Skip to content
This repository was archived by the owner on Oct 30, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions Libraries/Network/RCTHTTPRequestHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,21 @@ - (NSString *)stringToHex:(NSString *)string
-(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {

NSString *host = challenge.protectionSpace.host;

// Read the clientSSL info from MMKV
__block NSDictionary *clientSSL;
SecureStorage *secureStorage = [[SecureStorage alloc] init];

// https://github.com/ammarahm-ed/react-native-mmkv-storage/blob/master/src/loader.js#L31
[secureStorage getSecureKey:[self stringToHex:@"com.MMKV.default"] callback:^(NSArray *response) {
// Error happened
if ([response objectAtIndex:0] != [NSNull null]) {
return;
}
NSString *key = [response objectAtIndex:1];
NSData *cryptKey = [key dataUsingEncoding:NSUTF8StringEncoding];
MMKV *mmkv = [MMKV mmkvWithID:@"default" cryptKey:cryptKey mode:MMKVMultiProcess];
NSString *key = [secureStorage getSecureKey:[self stringToHex:@"com.MMKV.default"]];

if (key == NULL) {
return;
}

clientSSL = [mmkv getObjectOfClass:[NSDictionary class] forKey:host];
}];
NSData *cryptKey = [key dataUsingEncoding:NSUTF8StringEncoding];
MMKV *mmkv = [MMKV mmkvWithID:@"default" cryptKey:cryptKey mode:MMKVMultiProcess];
clientSSL = [mmkv getObjectOfClass:[NSDictionary class] forKey:host];

NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];

Expand Down
63 changes: 30 additions & 33 deletions Libraries/WebSocket/RCTSRWebSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ @implementation RCTSRWebSocket
int _closeCode;

BOOL _isPumping;

BOOL _cleanupScheduled;

NSMutableSet<NSArray *> *_scheduledRunloops;
Expand Down Expand Up @@ -328,7 +328,7 @@ - (void)dealloc

[_inputStream close];
[_outputStream close];

if (_receivedHTTPHeaders) {
CFRelease(_receivedHTTPHeaders);
_receivedHTTPHeaders = NULL;
Expand Down Expand Up @@ -482,7 +482,7 @@ - (void)didConnect
}

- (void)setClientSSL:(NSString *)path password:(NSString *)password options:(NSMutableDictionary *)options;
{
{
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSData *pkcs12data = [[NSData alloc] initWithContentsOfFile:path];
Expand Down Expand Up @@ -555,17 +555,15 @@ - (void)_initializeStreams
SecureStorage *secureStorage = [[SecureStorage alloc] init];

// https://github.com/ammarahm-ed/react-native-mmkv-storage/blob/master/src/loader.js#L31
[secureStorage getSecureKey:[self stringToHex:@"com.MMKV.default"] callback:^(NSArray *response) {
// Error happened
if ([response objectAtIndex:0] != [NSNull null]) {
return;
}
NSString *key = [response objectAtIndex:1];
NSData *cryptKey = [key dataUsingEncoding:NSUTF8StringEncoding];
MMKV *mmkv = [MMKV mmkvWithID:@"default" cryptKey:cryptKey mode:MMKVMultiProcess];
NSString *key = [secureStorage getSecureKey:[self stringToHex:@"com.MMKV.default"]];

if (key == NULL) {
return;
}

clientSSL = [mmkv getObjectOfClass:[NSDictionary class] forKey:host];
}];
NSData *cryptKey = [key dataUsingEncoding:NSUTF8StringEncoding];
MMKV *mmkv = [MMKV mmkvWithID:@"default" cryptKey:cryptKey mode:MMKVMultiProcess];
clientSSL = [mmkv getObjectOfClass:[NSDictionary class] forKey:host];

if (clientSSL != (id)[NSNull null]) {
NSString *path = [clientSSL objectForKey:@"path"];
Expand All @@ -574,7 +572,6 @@ - (void)_initializeStreams
[self setClientSSL:path password:password options:SSLOptions];
}


[_outputStream setProperty:SSLOptions
forKey:(__bridge id)kCFStreamPropertySSLSettings];
}
Expand Down Expand Up @@ -682,7 +679,7 @@ - (void)_failWithError:(NSError *)error
}];

self.readyState = RCTSR_CLOSED;

RCTSRLog(@"Failing with error %@", error.localizedDescription);

[self _disconnect];
Expand Down Expand Up @@ -1386,7 +1383,7 @@ - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
}
}
}

// _workQueue cannot be NULL
if (!_workQueue) {
return;
Expand All @@ -1410,25 +1407,25 @@ - (void)safeHandleEvent:(NSStreamEvent)eventCode stream:(NSStream *)aStream
return;
}
assert(self->_readBuffer);

if (self.readyState == RCTSR_CONNECTING && aStream == self->_inputStream) {
[self didConnect];
}
[self _pumpWriting];
[self _pumpScanner];
break;
}

case NSStreamEventErrorOccurred: {
RCTSRLog(@"NSStreamEventErrorOccurred %@ %@", aStream, [aStream.streamError copy]);
// TODO: specify error better!
[self _failWithError:aStream.streamError];
self->_readBufferOffset = 0;
self->_readBuffer.length = 0;
break;

}

case NSStreamEventEndEncountered: {
[self _pumpScanner];
RCTSRLog(@"NSStreamEventEndEncountered %@", aStream);
Expand All @@ -1440,7 +1437,7 @@ - (void)safeHandleEvent:(NSStreamEvent)eventCode stream:(NSStream *)aStream
self.readyState = RCTSR_CLOSED;
[self _scheduleCleanup];
}

if (!self->_sentClose && !self->_failed) {
self->_sentClose = YES;
// If we get closed in this state it's probably not clean because we should be sending this when we send messages
Expand All @@ -1452,38 +1449,38 @@ - (void)safeHandleEvent:(NSStreamEvent)eventCode stream:(NSStream *)aStream
}
});
}

break;
}

case NSStreamEventHasBytesAvailable: {
RCTSRLog(@"NSStreamEventHasBytesAvailable %@", aStream);
const int bufferSize = 2048;
uint8_t buffer[bufferSize];

while (self->_inputStream.hasBytesAvailable) {
NSInteger bytes_read = [self->_inputStream read:buffer maxLength:bufferSize];

if (bytes_read > 0) {
[self->_readBuffer appendBytes:buffer length:bytes_read];
} else if (bytes_read < 0) {
[self _failWithError:self->_inputStream.streamError];
}

if (bytes_read != bufferSize) {
break;
}
};
[self _pumpScanner];
break;
}

case NSStreamEventHasSpaceAvailable: {
RCTSRLog(@"NSStreamEventHasSpaceAvailable %@", aStream);
[self _pumpWriting];
break;
}

default:
RCTSRLog(@"(default) %@", aStream);
break;
Expand All @@ -1495,9 +1492,9 @@ - (void)_scheduleCleanup
if (_cleanupScheduled) {
return;
}

_cleanupScheduled = YES;

// Cleanup NSStream's delegate in the same RunLoop used by the streams themselves:
// This way we'll prevent race conditions between handleEvent and SRWebsocket's dealloc
NSTimer *timer = [NSTimer timerWithTimeInterval:(0.0f) target:self selector:@selector(_cleanupSelfReference:) userInfo:nil repeats:NO];
Expand All @@ -1509,16 +1506,16 @@ - (void)_cleanupSelfReference:(NSTimer *)timer
// Remove the streams, right now, from the networkRunLoop
[_inputStream close];
[_outputStream close];

// Unschedule from RunLoop
for (NSArray *runLoop in [_scheduledRunloops copy]) {
[self unscheduleFromRunLoop:runLoop[0] forMode:runLoop[1]];
}

// Nuke NSStream's delegate
_inputStream.delegate = nil;
_outputStream.delegate = nil;

// Cleanup selfRetain in the same GCD queue as usual
dispatch_async(_workQueue, ^{
self->_selfRetain = nil;
Expand Down