Skip to content

Commit

Permalink
Fix RKHTTPRequestHandler crash
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Aug 10, 2015
1 parent f309540 commit 5d20b90
Showing 1 changed file with 43 additions and 19 deletions.
62 changes: 43 additions & 19 deletions Libraries/Network/RCTDownloadTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,42 +61,66 @@ - (void)cancel
[self invalidate];
}

- (BOOL)validateRequestToken:(id)requestToken
{
if (![requestToken isEqual:_requestToken]) {
if (RCT_DEBUG) {
RCTAssert([requestToken isEqual:_requestToken],
@"Unrecognized request token: %@", requestToken);
}
if (_completionBlock) {
_completionBlock(_response, _data, [NSError errorWithDomain:RCTErrorDomain code:0
userInfo:@{NSLocalizedDescriptionKey: @"Unrecognized request token."}]);
[self invalidate];
}
return NO;
}
return YES;
}

- (void)URLRequest:(id)requestToken didSendDataWithProgress:(int64_t)bytesSent
{
RCTAssert([requestToken isEqual:_requestToken], @"Unrecognized request token: %@", requestToken);
if (_uploadProgressBlock) {
_uploadProgressBlock(bytesSent, _request.HTTPBody.length);
if ([self validateRequestToken:requestToken]) {
if (_uploadProgressBlock) {
_uploadProgressBlock(bytesSent, _request.HTTPBody.length);
}
}
}

- (void)URLRequest:(id)requestToken didReceiveResponse:(NSURLResponse *)response
{
RCTAssert([requestToken isEqual:_requestToken], @"Unrecognized request token: %@", requestToken);
_response = response;
if (_responseBlock) {
_responseBlock(response);
if ([self validateRequestToken:requestToken]) {
_response = response;
if (_responseBlock) {
_responseBlock(response);
}
}
}

- (void)URLRequest:(id)requestToken didReceiveData:(NSData *)data
{
RCTAssert([requestToken isEqual:_requestToken], @"Unrecognized request token: %@", requestToken);
if (!_data) {
_data = [[NSMutableData alloc] init];
}
[_data appendData:data];
if (_incrementalDataBlock) {
_incrementalDataBlock(data);
}
if (_downloadProgressBlock && _response.expectedContentLength > 0) {
_downloadProgressBlock(_data.length, _response.expectedContentLength);
if ([self validateRequestToken:requestToken]) {
if (!_data) {
_data = [[NSMutableData alloc] init];
}
[_data appendData:data];
if (_incrementalDataBlock) {
_incrementalDataBlock(data);
}
if (_downloadProgressBlock && _response.expectedContentLength > 0) {
_downloadProgressBlock(_data.length, _response.expectedContentLength);
}
}
}

- (void)URLRequest:(id)requestToken didCompleteWithError:(NSError *)error
{
_completionBlock(_response, _data, error);
[self invalidate];
if ([self validateRequestToken:requestToken]) {
if (_completionBlock) {
_completionBlock(_response, _data, error);
[self invalidate];
}
}
}

@end

0 comments on commit 5d20b90

Please sign in to comment.