Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Completed Asana tasks:

* CL-2138 (details of fix is on Asana task page)
  • Loading branch information...
commit 73fae69913b637fe9726b22a8f56e0261c7e0847 1 parent 5e50396
@parfeon parfeon authored
View
25 objective-c/3.4/pubnub/libs/PubNub/Data/PNJSONSerialization.m
@@ -97,15 +97,22 @@ + (void)JSONObjectWithString:(NSString *)jsonString
}
+ (void)getCallbackMethodName:(NSString **)callbackMethodName fromJSONString:(NSString *)jsonString {
-
- // Checking whether there are parenthesis in JSON
- NSRange parenthesisRange = [jsonString rangeOfString:@"("];
- if (parenthesisRange.location != NSNotFound &&
- ([jsonString characterAtIndex:(parenthesisRange.location+parenthesisRange.length)] == '[' ||
- [jsonString characterAtIndex:(parenthesisRange.location+parenthesisRange.length)] == '{')) {
-
- NSScanner *scanner = [NSScanner scannerWithString:jsonString];
- [scanner scanUpToString:@"(" intoString:callbackMethodName];
+
+ if (jsonString) {
+
+ // Checking whether there are parenthesis in JSON
+ NSRange parenthesisRange = [jsonString rangeOfString:@"("];
+ if (parenthesisRange.location != NSNotFound &&
+ ([jsonString characterAtIndex:(parenthesisRange.location+parenthesisRange.length)] == '[' ||
+ [jsonString characterAtIndex:(parenthesisRange.location+parenthesisRange.length)] == '{')) {
+
+ NSScanner *scanner = [NSScanner scannerWithString:jsonString];
+ [scanner scanUpToString:@"(" intoString:callbackMethodName];
+ }
+ }
+ else {
+
+ PNLog(PNLogGeneralLevel, self, @"JSON string is empty");
}
}
View
7 objective-c/3.4/pubnub/libs/PubNub/Data/PNResponse.h
@@ -58,12 +58,17 @@
#pragma mark - Instance methods
/**
- * Intialize response instance with response
+ * Initialize response instance with response
* body content data, response size and status
* code (HTTP status code)
*/
- (id)initWithContent:(NSData *)content size:(NSUInteger)responseSize code:(NSUInteger)statusCode;
+/**
+ * Return whether request has been processed correctly or not
+ */
+- (BOOL)isCorrectResponse;
+
#pragma mark -
View
95 objective-c/3.4/pubnub/libs/PubNub/Data/PNResponse.m
@@ -13,6 +13,7 @@
//
#import "PNResponse.h"
+#import "PNRequestsImport.h"
#pragma mark Static
@@ -108,36 +109,82 @@ - (id)initWithContent:(NSData *)content size:(NSUInteger)responseSize code:(NSUI
NSString *decodedResponse = [self decodedResponse];
- [PNJSONSerialization JSONObjectWithString:decodedResponse
- completionBlock:^(id result, BOOL isJSONP, NSString *callbackMethodName){
-
- if (isJSONP) {
-
- NSArray *callbackMethodElements = [callbackMethodName componentsSeparatedByString:@"_"];
-
- if ([callbackMethodElements count] > 1) {
-
- self.callbackMethod = [callbackMethodElements objectAtIndex:kPNResponseCallbackMethodNameIndex];
- self.requestIdentifier = [callbackMethodElements objectAtIndex:kPNResponseRequestIdentifierIndex];
- }
- else {
-
- self.callbackMethod = callbackMethodName;
- }
-
- self.response = result;
- }
- }
- errorBlock:^(NSError *error) {
-
- PNLog(PNLogGeneralLevel, self, @"ERROR: %@", error);
- }];
+ if (decodedResponse) {
+
+ [PNJSONSerialization JSONObjectWithString:decodedResponse
+ completionBlock:^(id result, BOOL isJSONP, NSString *callbackMethodName){
+
+ if (isJSONP) {
+
+ NSArray *callbackMethodElements = [callbackMethodName componentsSeparatedByString:@"_"];
+
+ if ([callbackMethodElements count] > 1) {
+
+ self.callbackMethod = [callbackMethodElements objectAtIndex:kPNResponseCallbackMethodNameIndex];
+ self.requestIdentifier = [callbackMethodElements objectAtIndex:kPNResponseRequestIdentifierIndex];
+ }
+ else {
+
+ self.callbackMethod = callbackMethodName;
+ }
+
+ self.response = result;
+ }
+ }
+ errorBlock:^(NSError *error) {
+
+ PNLog(PNLogGeneralLevel, self, @"ERROR: %@", error);
+ }];
+ }
+ // Looks like message can't be decoded event from RAW response
+ // looks like malformed data arrived with characters which can't
+ // be encoded
+ else {
+
+ PNLog(PNLogGeneralLevel, self, @"FAILED TO DECODE DATA");
+
+ // Mark that request is failed to be processed correctly
+ self.statusCode = 404;
+ self.size = 0;
+
+ // Trying to extract callback method and request identifier
+ NSString *responseString = [[NSString alloc] initWithData:self.content encoding:NSUTF8StringEncoding];
+ if (responseString) {
+
+ NSRange openingBracketRange = [responseString rangeOfString:@"("];
+ if (openingBracketRange.location != NSNotFound) {
+
+ NSString *callbackMethod = [responseString substringToIndex:openingBracketRange.location];
+ NSArray *callbackMethodElements = [callbackMethod componentsSeparatedByString:@"_"];
+
+ if ([callbackMethodElements count] > 1) {
+
+ self.callbackMethod = [callbackMethodElements objectAtIndex:kPNResponseCallbackMethodNameIndex];
+ self.requestIdentifier = [callbackMethodElements objectAtIndex:kPNResponseRequestIdentifierIndex];
+ }
+ else {
+
+ self.callbackMethod = callbackMethod;
+ }
+ }
+ }
+ else {
+
+ // Assign 'subscription' callback method
+ self.callbackMethod = PNServiceResponseCallbacks.subscriptionCallback;
+ }
+ }
}
return self;
}
+- (BOOL)isCorrectResponse {
+
+ return (self.size > 0 && self.statusCode == 200);
+}
+
- (NSString *)decodedResponse {
NSString *encodedString = [[NSString alloc] initWithData:self.content encoding:NSUTF8StringEncoding];
View
6 objective-c/3.4/pubnub/libs/PubNub/Data/Parsers/PNResponseDeserialize.m
@@ -149,7 +149,7 @@ - (NSArray *)parseResponseData:(NSMutableData *)data {
// Try construct response instance
PNResponse *response = [self responseInRange:contentRange ofData:data];
if(response) {
-
+
[parsedData addObject:response];
}
else {
@@ -173,9 +173,9 @@ - (NSArray *)parseResponseData:(NSMutableData *)data {
// Try construct response instance
PNResponse *response = [self responseInRange:contentRange ofData:data];
if(response) {
-
+
[parsedData addObject:response];
-
+
// Update content search range
responseRange.location = responseRange.location + contentRange.length;
Please sign in to comment.
Something went wrong with that request. Please try again.