Skip to content

Commit 284decb

Browse files
author
Olha Danylova
committed
The processAsyncAMFResponse method from HttpEngine class fixed to process response correctly when NSURLSession returns error
1 parent 3172e4c commit 284decb

File tree

7 files changed

+54
-41
lines changed

7 files changed

+54
-41
lines changed

SDK/backendlessAPI/Classes/weborb/Client/HttpEngine.m

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -137,35 +137,44 @@ -(NSData *)createRequest:(V3Message *)v3Msg {
137137
return [self createRequest:v3Msg headers:nil];
138138
}
139139

140-
-(void)processAsyncAMFResponse:(AsyncHttpResponse *)async {
141-
id <IResponder> responder = (async.responder) ? async.responder : _responder;
142-
int statusCode = (int)[async.responseUrl statusCode];
143-
if (statusCode != 200) {
144-
[DebLog log:@"HttpEngine -> sendRequest: (SYNC) response with *** INVALID statusCode = '%d'", statusCode];
145-
NSString *code = [NSString stringWithFormat:@"%d", statusCode];
146-
NSString *detail = [NSString stringWithFormat:@"HttpEngine: INVALID statusCode %d", statusCode];
147-
[responder errorHandler:[Fault fault:detail detail:detail faultCode:code]];
148-
return;
149-
}
150-
151-
NSString *contentType = [[async.responseUrl allHeaderFields] valueForKey:@"Content-Type"];
152-
if (!(contentType) || ![contentType isEqualToString:@"application/x-amf"]) {
153-
NSString *body = [[NSString alloc] initWithData:async.receivedData encoding:NSUTF8StringEncoding];
154-
[DebLog log:@"HttpEngine -> processAsyncAMFResponse: response with *** INVALID 'Content-Type' = '%@', body = '%@'", contentType, body];
155-
Fault *fault = [Fault fault:@"HttpEngine: INVALID response 'Content-Type'" detail:contentType faultCode:@"9000"];
156-
[responder errorHandler:fault];
157-
return;
158-
}
159-
FlashorbBinaryReader *reader = [[FlashorbBinaryReader alloc] initWithStream:(char *)[async.receivedData bytes] andSize:[async.receivedData length]];
160-
[DebLog log:ON_PRINT_RESPONSE text:@"HttpEngine -> processAsyncAMFResponse: (ASYNC RESPONSE)\n"];
161-
[reader print:ON_PRINT_RESPONSE];
162-
Request *responseObject = [RequestParser readMessage:reader];
163-
NSArray *responseData = [responseObject getRequestBodyData];
164-
id <ICacheableAdaptingType> type = [responseData objectAtIndex:0];
140+
-(void)processAsyncAMFResponse:(AsyncHttpResponse *)async processIfNeeded:(BOOL)processNeeded {
141+
if (processNeeded) {
142+
id <IResponder> responder = nil;
143+
if (async.responder) {
144+
responder = async.responder;
145+
}
146+
else {
147+
responder = _responder;
148+
}
149+
int statusCode = (int)[async.responseUrl statusCode];
150+
if (statusCode != 200) {
151+
[DebLog log:@"HttpEngine -> sendRequest: (SYNC) response with *** INVALID statusCode = '%d'", statusCode];
152+
NSString *code = [NSString stringWithFormat:@"%d", statusCode];
153+
NSString *detail = [NSString stringWithFormat:@"HttpEngine: INVALID statusCode %d", statusCode];
154+
[responder errorHandler:[Fault fault:detail detail:detail faultCode:code]];
155+
return;
156+
}
157+
158+
NSString *contentType = [[async.responseUrl allHeaderFields] valueForKey:@"Content-Type"];
159+
if (!(contentType) || ![contentType isEqualToString:@"application/x-amf"]) {
160+
NSString *body = [[NSString alloc] initWithData:async.receivedData encoding:NSUTF8StringEncoding];
161+
[DebLog log:@"HttpEngine -> processAsyncAMFResponse: response with *** INVALID 'Content-Type' = '%@', body = '%@'", contentType, body];
162+
Fault *fault = [Fault fault:@"HttpEngine: INVALID response 'Content-Type'" detail:contentType faultCode:@"9000"];
163+
[responder errorHandler:fault];
164+
return;
165+
}
166+
167+
FlashorbBinaryReader *reader = [[FlashorbBinaryReader alloc] initWithStream:(char *)[async.receivedData bytes] andSize:[async.receivedData length]];
168+
[DebLog log:ON_PRINT_RESPONSE text:@"HttpEngine -> processAsyncAMFResponse: (ASYNC RESPONSE)\n"];
169+
[reader print:ON_PRINT_RESPONSE];
170+
Request *responseObject = [RequestParser readMessage:reader];
171+
NSArray *responseData = [responseObject getRequestBodyData];
172+
id <ICacheableAdaptingType> type = [responseData objectAtIndex:0];
165173
#if _ReaderReferenceCache_IS_SINGLETON_
166-
[[ReaderReferenceCache cache] cleanCache];
174+
[[ReaderReferenceCache cache] cleanCache];
167175
#endif
168-
[responder responseHandler:type];
176+
[responder responseHandler:type];
177+
}
169178
}
170179

171180
-(void)pollingResponse:(id)result {
@@ -237,7 +246,6 @@ -(id)invoke:(NSString *)className method:(NSString *)methodName args:(NSArray *)
237246
}
238247

239248
-(id)sendRequest:(V3Message *)v3Msg {
240-
241249
NSHTTPURLResponse *responseUrl;
242250
NSError *error = nil;
243251
NSData *receivedData;
@@ -298,16 +306,10 @@ -(void)sendURLRequest:(NSURLRequest *)request responder:(id <IResponder>)respond
298306
AsyncHttpResponse *async = [[AsyncHttpResponse alloc] init];
299307
async.receivedData = [NSMutableData new];
300308
async.responder = responder;
301-
if (response) {
302-
NSHTTPURLResponse *responseUrl = (NSHTTPURLResponse *)response;
303-
[async.receivedData setLength:0];
304-
async.responseUrl = responseUrl;
305-
}
306-
if (data) {
307-
[DebLog logN:@"HttpEngine ->connection didReceiveData: length = %d", [data length]];
308-
[async.receivedData appendData:data];
309-
}
309+
BOOL processNeeded = YES;
310+
310311
if (error) {
312+
processNeeded = NO;
311313
[self setNetworkActivityIndicatorOn:NO];
312314
#if REPEAT_REQUEST_ON
313315
if ([self isNSURLErrorDomain:error] && async.request) {
@@ -319,7 +321,18 @@ -(void)sendURLRequest:(NSURLRequest *)request responder:(id <IResponder>)respond
319321
Fault *fault = (error) ? [Fault fault:[error domain] detail:[error localizedDescription] faultCode:[NSString stringWithFormat:@"%ld",(long)[error code]]] : UNKNOWN_FAULT;
320322
[async.responder errorHandler:fault];
321323
}
322-
[self processAsyncAMFResponse:async];
324+
else {
325+
if (response) {
326+
NSHTTPURLResponse *responseUrl = (NSHTTPURLResponse *)response;
327+
[async.receivedData setLength:0];
328+
async.responseUrl = responseUrl;
329+
}
330+
if (data) {
331+
[DebLog logN:@"HttpEngine ->connection didReceiveData: length = %d", [data length]];
332+
[async.receivedData appendData:data];
333+
}
334+
}
335+
[self processAsyncAMFResponse:async processIfNeeded:processNeeded];
323336
}] resume];
324337
}
325338

@@ -370,7 +383,7 @@ - (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NS
370383
blockResponse = subResponse;
371384
dispatch_group_leave(group);
372385
}] resume];
373-
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
386+
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
374387
*error = blockError;
375388
*response = blockResponse;
376389
} @catch (NSException *exception) {

0 commit comments

Comments
 (0)