From 876798c5bb4813188954001294b28b5ab640b01a Mon Sep 17 00:00:00 2001 From: Alejandro Orozco Date: Tue, 2 Jan 2024 09:09:32 -0500 Subject: [PATCH] fix: Continuously read from buffer while no errors occur. (#11) --- CHANGELOG.md | 6 ++++ README.md | 2 +- SNASources/CellularSession.m | 30 ++++++++++++------- Sources/TwilioVerifySNAConfig.swift | 2 +- .../project.pbxproj | 4 +-- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 887275f..8680076 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.0.3 (26–12-2023) + +### Fixes + +- Continuously read from buffer while no errors occur. The buffer was closing itself after being put on hold, rather than waiting to read the entire package from the SSLRead. + # 0.0.2 (09–26-2022) ### Improvements diff --git a/README.md b/README.md index 4eb9bbc..fce35bf 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ During the current phase of this project, we only support SPM. We have plans to dependencies: [ .package( url: "https://github.com/twilio/twilio-verify-sna-ios.git", - .upToNextMajor(from: "0.0.2") + .upToNextMajor(from: "0.0.3") ) ] ``` diff --git a/SNASources/CellularSession.m b/SNASources/CellularSession.m index 9621094..46b4e79 100644 --- a/SNASources/CellularSession.m +++ b/SNASources/CellularSession.m @@ -192,7 +192,8 @@ - (CellularSessionResult * _Nonnull)performRequest:(NSURL * _Nonnull)url { const char* request = [requestString UTF8String]; char buffer[4096]; - + NSMutableData *responseData = [NSMutableData dataWithCapacity:0]; + // Step 4). Invoke the HTTP request using the instantiated socket if ([[url scheme] isEqualToString:@"http"]) { @@ -282,14 +283,21 @@ - (CellularSessionResult * _Nonnull)performRequest:(NSURL * _Nonnull)url { do { // SSLRead performs a typical application-level read operation. status = SSLRead(context, buffer, sizeof(buffer) - 1, &processed); - buffer[processed] = 0; - - // If the buffer was filled, then continue reading - if (processed == sizeof(buffer) - 1) { - status = errSSLWouldBlock; + + if (status == noErr && processed > 0) { + [responseData appendBytes:buffer length:processed]; // Append the received data to responseData + } else if (status == errSSLWouldBlock) { + // No more data available + SSLClose(context); + CFRelease(context); + status = noErr; + break; + } else { + // No data received + break; } - } while (status == errSSLWouldBlock); - + } while (status == noErr); + if (status && status != errSSLClosedGraceful) { SSLClose(context); CFRelease(context); @@ -301,9 +309,9 @@ - (CellularSessionResult * _Nonnull)performRequest:(NSURL * _Nonnull)url { } } - - NSString *response = [[NSString alloc] initWithBytes:buffer length:sizeof(buffer) encoding:NSASCIIStringEncoding]; - + + NSString *response = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding]; + // Step 5). Parse the HTTP response and check whether it contains a redirect HTTP code if ([response rangeOfString:@"HTTP/"].location == NSNotFound) { diff --git a/Sources/TwilioVerifySNAConfig.swift b/Sources/TwilioVerifySNAConfig.swift index c9a2974..1ee0ccb 100644 --- a/Sources/TwilioVerifySNAConfig.swift +++ b/Sources/TwilioVerifySNAConfig.swift @@ -20,5 +20,5 @@ import Foundation public struct TwilioVerifySNAConfig { - public static let version = "0.0.2" + public static let version = "0.0.3" } diff --git a/TwilioVerifySNADemo/TwilioVerifySNADemo.xcodeproj/project.pbxproj b/TwilioVerifySNADemo/TwilioVerifySNADemo.xcodeproj/project.pbxproj index 083cd4d..3a70676 100644 --- a/TwilioVerifySNADemo/TwilioVerifySNADemo.xcodeproj/project.pbxproj +++ b/TwilioVerifySNADemo/TwilioVerifySNADemo.xcodeproj/project.pbxproj @@ -402,7 +402,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 3; + CURRENT_PROJECT_VERSION = 4; DEVELOPMENT_TEAM = ""; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = Configuration/Info.plist; @@ -433,7 +433,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 3; + CURRENT_PROJECT_VERSION = 4; DEVELOPMENT_TEAM = ""; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = Configuration/Info.plist;