From 9e03bceb0df9e4ee20f787e3c01897b53b515e30 Mon Sep 17 00:00:00 2001 From: Allen Schober Date: Fri, 21 Mar 2014 17:52:57 -0500 Subject: [PATCH 1/2] check HTTP status code of NSURLConnection responses during xhr polling transport just like when checking HTTP status code during handshake. Handles case when server restarts while connection with device is already established. --- SocketIOTransportXHR.m | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/SocketIOTransportXHR.m b/SocketIOTransportXHR.m index 34d7bb2..420b329 100644 --- a/SocketIOTransportXHR.m +++ b/SocketIOTransportXHR.m @@ -171,6 +171,27 @@ - (void) poll:(NSString *)data retryNumber:(int)retry - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { + // check for server status code (http://gigliwood.com/weblog/Cocoa/Q__When_is_an_conne.html) + if ([response respondsToSelector:@selector(statusCode)]) { + NSInteger statusCode = [((NSHTTPURLResponse *)response) statusCode]; + DEBUGLOG(@"didReceiveResponse() %i", statusCode); + + if (statusCode >= 400) { + // stop connecting; no more delegate messages + [connection cancel]; + + NSString *error = [NSString stringWithFormat:NSLocalizedString(@"Server returned status code %d", @""), statusCode]; + NSDictionary *errorInfo = [NSDictionary dictionaryWithObject:error forKey:NSLocalizedDescriptionKey]; + NSError *statusError = [NSError errorWithDomain:SocketIOError + code:statusCode + userInfo:errorInfo]; + + if ([delegate respondsToSelector:@selector(onError:)]) { + [delegate onError:statusError]; + } + } + } + [_data setLength:0]; } From c53d0694d3bf510aa1d604161a1e8e6b2c247bd9 Mon Sep 17 00:00:00 2001 From: Philipp Kyeck Date: Sun, 23 Mar 2014 11:52:50 +0100 Subject: [PATCH 2/2] clean code --- SocketIOTransportXHR.m | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/SocketIOTransportXHR.m b/SocketIOTransportXHR.m index 34d7bb2..4d3bfff 100644 --- a/SocketIOTransportXHR.m +++ b/SocketIOTransportXHR.m @@ -206,15 +206,13 @@ - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)er } } -// Sometimes Socket.IO "batches" up messages in one packet, -// so we have to split them. -// +// Sometimes Socket.IO "batches" up messages in one packet, so we have to split them. - (NSArray *)packetsFromPayload:(NSString *)payload { // "Batched" format is: // �[packet_0 length]�[packet_0]�[packet_1 length]�[packet_1]�[packet_n length]�[packet_n] - if([payload hasPrefix:@"\ufffd"]) { + if ([payload hasPrefix:@"\ufffd"]) { // Payload has multiple packets, split based on the '�' character // Skip the first character, then split NSArray *split = [[payload substringFromIndex:1] componentsSeparatedByString:@"\ufffd"]; @@ -224,14 +222,15 @@ - (NSArray *)packetsFromPayload:(NSString *)payload // Now all of the odd-numbered indices are the packets (1, 3, 5, etc.) [split enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - if(idx % 2 != 0) { + if (idx % 2 != 0) { [packets addObject:obj]; } }]; NSLog(@"Parsed a payload!"); return packets; - } else { + } + else { // Regular single-packet payload return @[payload]; }