2015-07-23 10:55:12 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTLog.h>
|
|
|
|
#import <React/RCTNetworkTask.h>
|
|
|
|
#import <React/RCTUtils.h>
|
2015-07-23 10:55:12 +00:00
|
|
|
|
2015-10-19 16:04:54 +00:00
|
|
|
@implementation RCTNetworkTask
|
2015-07-23 10:55:12 +00:00
|
|
|
{
|
|
|
|
NSMutableData *_data;
|
|
|
|
id<RCTURLRequestHandler> _handler;
|
2016-08-17 17:34:15 +00:00
|
|
|
dispatch_queue_t _callbackQueue;
|
|
|
|
|
2015-10-19 16:04:54 +00:00
|
|
|
RCTNetworkTask *_selfReference;
|
2015-07-23 10:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithRequest:(NSURLRequest *)request
|
|
|
|
handler:(id<RCTURLRequestHandler>)handler
|
2016-08-17 17:34:15 +00:00
|
|
|
callbackQueue:(dispatch_queue_t)callbackQueue
|
2015-07-23 10:55:12 +00:00
|
|
|
{
|
|
|
|
RCTAssertParam(request);
|
|
|
|
RCTAssertParam(handler);
|
2016-08-17 17:34:15 +00:00
|
|
|
RCTAssertParam(callbackQueue);
|
2015-07-23 10:55:12 +00:00
|
|
|
|
|
|
|
static NSUInteger requestID = 0;
|
|
|
|
|
|
|
|
if ((self = [super init])) {
|
|
|
|
_requestID = @(requestID++);
|
|
|
|
_request = request;
|
|
|
|
_handler = handler;
|
2016-08-17 17:34:15 +00:00
|
|
|
_callbackQueue = callbackQueue;
|
2016-02-16 20:41:20 +00:00
|
|
|
_status = RCTNetworkTaskPending;
|
2016-08-17 17:34:16 +00:00
|
|
|
|
|
|
|
dispatch_queue_set_specific(callbackQueue, (__bridge void *)self, (__bridge void *)self, NULL);
|
2015-07-23 10:55:12 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-10-17 16:33:09 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
_selfReference = nil;
|
|
|
|
_completionBlock = nil;
|
|
|
|
_downloadProgressBlock = nil;
|
|
|
|
_incrementalDataBlock = nil;
|
|
|
|
_responseBlock = nil;
|
|
|
|
_uploadProgressBlock = nil;
|
2016-10-11 19:34:58 +00:00
|
|
|
_requestToken = nil;
|
2015-07-23 10:55:12 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 17:34:16 +00:00
|
|
|
- (void)dispatchCallback:(dispatch_block_t)callback
|
|
|
|
{
|
|
|
|
if (dispatch_get_specific((__bridge void *)self) == (__bridge void *)self) {
|
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
dispatch_async(_callbackQueue, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-17 16:33:09 +00:00
|
|
|
- (void)start
|
|
|
|
{
|
2016-10-11 19:34:58 +00:00
|
|
|
if (_status != RCTNetworkTaskPending) {
|
|
|
|
RCTLogError(@"RCTNetworkTask was already started or completed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-17 16:33:09 +00:00
|
|
|
if (_requestToken == nil) {
|
2016-08-17 17:34:15 +00:00
|
|
|
id token = [_handler sendRequest:_request withDelegate:self];
|
|
|
|
if ([self validateRequestToken:token]) {
|
2015-10-17 16:33:09 +00:00
|
|
|
_selfReference = self;
|
2016-02-16 20:41:20 +00:00
|
|
|
_status = RCTNetworkTaskInProgress;
|
2015-10-17 16:33:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-23 10:55:12 +00:00
|
|
|
|
|
|
|
- (void)cancel
|
|
|
|
{
|
2016-10-11 19:34:58 +00:00
|
|
|
if (_status == RCTNetworkTaskFinished) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-09 16:55:18 +00:00
|
|
|
_status = RCTNetworkTaskFinished;
|
2016-08-19 17:31:42 +00:00
|
|
|
id token = _requestToken;
|
|
|
|
if (token && [_handler respondsToSelector:@selector(cancelRequest:)]) {
|
|
|
|
[_handler cancelRequest:token];
|
2015-07-23 10:55:12 +00:00
|
|
|
}
|
|
|
|
[self invalidate];
|
|
|
|
}
|
|
|
|
|
2015-08-10 18:51:10 +00:00
|
|
|
- (BOOL)validateRequestToken:(id)requestToken
|
|
|
|
{
|
2016-06-09 16:55:18 +00:00
|
|
|
BOOL valid = YES;
|
2015-10-17 16:33:09 +00:00
|
|
|
if (_requestToken == nil) {
|
|
|
|
if (requestToken == nil) {
|
2016-06-09 16:55:18 +00:00
|
|
|
if (RCT_DEBUG) {
|
|
|
|
RCTLogError(@"Missing request token for request: %@", _request);
|
|
|
|
}
|
|
|
|
valid = NO;
|
2015-10-17 16:33:09 +00:00
|
|
|
}
|
|
|
|
_requestToken = requestToken;
|
2016-06-09 16:55:18 +00:00
|
|
|
} else if (![requestToken isEqual:_requestToken]) {
|
2015-08-10 18:51:10 +00:00
|
|
|
if (RCT_DEBUG) {
|
2015-10-13 15:12:54 +00:00
|
|
|
RCTLogError(@"Unrecognized request token: %@ expected: %@", requestToken, _requestToken);
|
2015-08-10 18:51:10 +00:00
|
|
|
}
|
2016-06-09 16:55:18 +00:00
|
|
|
valid = NO;
|
|
|
|
}
|
2016-08-17 17:34:15 +00:00
|
|
|
|
2016-06-09 16:55:18 +00:00
|
|
|
if (!valid) {
|
|
|
|
_status = RCTNetworkTaskFinished;
|
2015-08-10 18:51:10 +00:00
|
|
|
if (_completionBlock) {
|
2016-08-17 17:34:15 +00:00
|
|
|
RCTURLRequestCompletionBlock completionBlock = _completionBlock;
|
2016-08-17 17:34:16 +00:00
|
|
|
[self dispatchCallback:^{
|
2016-08-17 17:34:15 +00:00
|
|
|
completionBlock(self->_response, nil, RCTErrorWithMessage(@"Invalid request token."));
|
2016-08-17 17:34:16 +00:00
|
|
|
}];
|
2015-08-10 18:51:10 +00:00
|
|
|
}
|
2016-02-16 20:41:20 +00:00
|
|
|
[self invalidate];
|
2015-08-10 18:51:10 +00:00
|
|
|
}
|
2016-06-09 16:55:18 +00:00
|
|
|
return valid;
|
2015-08-10 18:51:10 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
- (void)URLRequest:(id)requestToken didSendDataWithProgress:(int64_t)bytesSent
|
|
|
|
{
|
2016-08-17 17:34:15 +00:00
|
|
|
if (![self validateRequestToken:requestToken]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_uploadProgressBlock) {
|
|
|
|
RCTURLRequestProgressBlock uploadProgressBlock = _uploadProgressBlock;
|
|
|
|
int64_t length = _request.HTTPBody.length;
|
2016-08-17 17:34:16 +00:00
|
|
|
[self dispatchCallback:^{
|
2016-08-17 17:34:15 +00:00
|
|
|
uploadProgressBlock(bytesSent, length);
|
2016-08-17 17:34:16 +00:00
|
|
|
}];
|
2015-07-23 10:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)URLRequest:(id)requestToken didReceiveResponse:(NSURLResponse *)response
|
|
|
|
{
|
2016-08-17 17:34:15 +00:00
|
|
|
if (![self validateRequestToken:requestToken]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_response = response;
|
|
|
|
if (_responseBlock) {
|
|
|
|
RCTURLRequestResponseBlock responseBlock = _responseBlock;
|
2016-08-17 17:34:16 +00:00
|
|
|
[self dispatchCallback:^{
|
2016-08-17 17:34:15 +00:00
|
|
|
responseBlock(response);
|
2016-08-17 17:34:16 +00:00
|
|
|
}];
|
2015-07-23 10:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)URLRequest:(id)requestToken didReceiveData:(NSData *)data
|
|
|
|
{
|
2016-08-17 17:34:15 +00:00
|
|
|
if (![self validateRequestToken:requestToken]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_data) {
|
|
|
|
_data = [NSMutableData new];
|
|
|
|
}
|
|
|
|
[_data appendData:data];
|
|
|
|
|
|
|
|
int64_t length = _data.length;
|
|
|
|
int64_t total = _response.expectedContentLength;
|
|
|
|
|
|
|
|
if (_incrementalDataBlock) {
|
|
|
|
RCTURLRequestIncrementalDataBlock incrementalDataBlock = _incrementalDataBlock;
|
2016-08-17 17:34:16 +00:00
|
|
|
[self dispatchCallback:^{
|
2016-08-17 17:34:15 +00:00
|
|
|
incrementalDataBlock(data, length, total);
|
2016-08-17 17:34:16 +00:00
|
|
|
}];
|
2016-08-17 17:34:15 +00:00
|
|
|
}
|
|
|
|
if (_downloadProgressBlock && total > 0) {
|
|
|
|
RCTURLRequestProgressBlock downloadProgressBlock = _downloadProgressBlock;
|
2016-08-17 17:34:16 +00:00
|
|
|
[self dispatchCallback:^{
|
2016-08-17 17:34:15 +00:00
|
|
|
downloadProgressBlock(length, total);
|
2016-08-17 17:34:16 +00:00
|
|
|
}];
|
2015-07-23 10:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)URLRequest:(id)requestToken didCompleteWithError:(NSError *)error
|
|
|
|
{
|
2016-08-17 17:34:15 +00:00
|
|
|
if (![self validateRequestToken:requestToken]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_status = RCTNetworkTaskFinished;
|
|
|
|
if (_completionBlock) {
|
|
|
|
RCTURLRequestCompletionBlock completionBlock = _completionBlock;
|
2016-08-17 17:34:16 +00:00
|
|
|
[self dispatchCallback:^{
|
2016-08-17 17:34:15 +00:00
|
|
|
completionBlock(self->_response, self->_data, error);
|
2016-08-17 17:34:16 +00:00
|
|
|
}];
|
2015-08-10 18:51:10 +00:00
|
|
|
}
|
2016-08-17 17:34:15 +00:00
|
|
|
[self invalidate];
|
2015-07-23 10:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|