Skip to content

Commit fe662b3

Browse files
committed
Fixing potential blocking bug in GCDAsyncSocket on iOS when using TLS (I believe it only affects iOS 5+.) Special thanks to the XMPPFramework mailing list for helping to identify this problem and find a solution.
1 parent 6ba5f77 commit fe662b3

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

GCD/GCDAsyncSocket.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3859,7 +3859,7 @@ - (void)flushSSLBuffers
38593859

38603860
#if TARGET_OS_IPHONE
38613861

3862-
if (flags & kSecureSocketHasBytesAvailable)
3862+
if ((flags & kSecureSocketHasBytesAvailable) && CFReadStreamHasBytesAvailable(readStream))
38633863
{
38643864
LogVerbose(@"%@ - Flushing ssl buffers into partialReadBuffer...", THIS_METHOD);
38653865

@@ -4013,7 +4013,10 @@ - (void)doReadData
40134013
// Relegated to using CFStream... :( Boo! Give us SecureTransport Apple!
40144014

40154015
estimatedBytesAvailable = 0;
4016-
hasBytesAvailable = (flags & kSecureSocketHasBytesAvailable) ? YES : NO;
4016+
if ((flags & kSecureSocketHasBytesAvailable) && CFReadStreamHasBytesAvailable(readStream))
4017+
hasBytesAvailable = YES;
4018+
else
4019+
hasBytesAvailable = NO;
40174020
}
40184021
else
40194022
{

GCD/Xcode/SimpleHTTPClient/Mobile/SimpleHTTPClient.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
DC80CA2B13C3B43F00D29D06 /* Project object */ = {
178178
isa = PBXProject;
179179
attributes = {
180-
LastUpgradeCheck = 0420;
180+
LastUpgradeCheck = 0430;
181181
};
182182
buildConfigurationList = DC80CA2E13C3B43F00D29D06 /* Build configuration list for PBXProject "SimpleHTTPClient" */;
183183
compatibilityVersion = "Xcode 3.2";

GCD/Xcode/SimpleHTTPClient/Mobile/SimpleHTTPClient/SimpleHTTPClientAppDelegate.m

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,21 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
6767
// When the asynchronous sockets connects, it will invoke the socket:didConnectToHost:port: delegate method.
6868

6969
NSError *error = nil;
70+
NSString *host = @"deusty.com";
7071

7172
#if USE_SECURE_CONNECTION
7273
uint16_t port = 443; // HTTPS
7374
#else
7475
uint16_t port = 80; // HTTP
7576
#endif
7677

77-
if (![asyncSocket connectToHost:@"deusty.com" onPort:port error:&error])
78+
if (![asyncSocket connectToHost:host onPort:port error:&error])
7879
{
7980
DDLogError(@"Unable to connect to due to invalid configuration: %@", error);
8081
}
8182
else
8283
{
83-
DDLogVerbose(@"Connecting...");
84+
DDLogVerbose(@"Connecting to \"%@\" on port %hu...", host, port);
8485
}
8586

8687
#if USE_SECURE_CONNECTION
@@ -106,6 +107,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
106107
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]
107108
forKey:(NSString *)kCFStreamSSLValidatesCertificateChain];
108109

110+
DDLogVerbose(@"Requesting StartTLS with options:\n%@", options);
111+
109112
[asyncSocket startTLS:options];
110113

111114
#endif
@@ -134,6 +137,8 @@ - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UI
134137

135138
[asyncSocket writeData:requestData withTimeout:-1.0 tag:0];
136139

140+
DDLogVerbose(@"Sending HTTP Request:\n%@", requestStr);
141+
137142
// Side Note:
138143
//
139144
// The AsyncSocket family supports queued reads and writes.
@@ -204,7 +209,7 @@ - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)t
204209

205210
#else
206211

207-
DDLogInfo(@"Full httpResponse: %@", httpResponse);
212+
DDLogInfo(@"Full HTTP Response:\n%@", httpResponse);
208213

209214
#endif
210215

0 commit comments

Comments
 (0)