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.
|
|
|
|
*/
|
|
|
|
|
2015-10-19 16:04:54 +00:00
|
|
|
#import "RCTNetworkTask.h"
|
2015-07-23 10:55:12 +00:00
|
|
|
|
2015-10-13 15:12:54 +00:00
|
|
|
#import "RCTLog.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;
|
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
|
|
|
|
completionBlock:(RCTURLRequestCompletionBlock)completionBlock
|
|
|
|
{
|
|
|
|
RCTAssertParam(request);
|
|
|
|
RCTAssertParam(handler);
|
|
|
|
RCTAssertParam(completionBlock);
|
|
|
|
|
|
|
|
static NSUInteger requestID = 0;
|
|
|
|
|
|
|
|
if ((self = [super init])) {
|
|
|
|
_requestID = @(requestID++);
|
|
|
|
_request = request;
|
|
|
|
_handler = handler;
|
|
|
|
_completionBlock = completionBlock;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-17 16:33:09 +00:00
|
|
|
- (void)start
|
|
|
|
{
|
|
|
|
if (_requestToken == nil) {
|
|
|
|
if ([self validateRequestToken:[_handler sendRequest:_request
|
|
|
|
withDelegate:self]]) {
|
|
|
|
_selfReference = self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-23 10:55:12 +00:00
|
|
|
|
|
|
|
- (void)cancel
|
|
|
|
{
|
2015-11-17 17:53:47 +00:00
|
|
|
__strong id strongToken = _requestToken;
|
|
|
|
if (strongToken && [_handler respondsToSelector:@selector(cancelRequest:)]) {
|
|
|
|
[_handler cancelRequest:strongToken];
|
2015-07-23 10:55:12 +00:00
|
|
|
}
|
|
|
|
[self invalidate];
|
|
|
|
}
|
|
|
|
|
2015-08-10 18:51:10 +00:00
|
|
|
- (BOOL)validateRequestToken:(id)requestToken
|
|
|
|
{
|
2015-10-17 16:33:09 +00:00
|
|
|
if (_requestToken == nil) {
|
|
|
|
if (requestToken == nil) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
_requestToken = requestToken;
|
|
|
|
}
|
2015-08-10 18:51:10 +00:00
|
|
|
if (![requestToken isEqual:_requestToken]) {
|
|
|
|
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
|
|
|
}
|
|
|
|
if (_completionBlock) {
|
|
|
|
_completionBlock(_response, _data, [NSError errorWithDomain:RCTErrorDomain code:0
|
|
|
|
userInfo:@{NSLocalizedDescriptionKey: @"Unrecognized request token."}]);
|
|
|
|
[self invalidate];
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
- (void)URLRequest:(id)requestToken didSendDataWithProgress:(int64_t)bytesSent
|
|
|
|
{
|
2015-08-10 18:51:10 +00:00
|
|
|
if ([self validateRequestToken:requestToken]) {
|
|
|
|
if (_uploadProgressBlock) {
|
|
|
|
_uploadProgressBlock(bytesSent, _request.HTTPBody.length);
|
|
|
|
}
|
2015-07-23 10:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)URLRequest:(id)requestToken didReceiveResponse:(NSURLResponse *)response
|
|
|
|
{
|
2015-08-10 18:51:10 +00:00
|
|
|
if ([self validateRequestToken:requestToken]) {
|
|
|
|
_response = response;
|
|
|
|
if (_responseBlock) {
|
|
|
|
_responseBlock(response);
|
|
|
|
}
|
2015-07-23 10:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)URLRequest:(id)requestToken didReceiveData:(NSData *)data
|
|
|
|
{
|
2015-08-10 18:51:10 +00:00
|
|
|
if ([self validateRequestToken:requestToken]) {
|
|
|
|
if (!_data) {
|
2015-08-17 14:35:34 +00:00
|
|
|
_data = [NSMutableData new];
|
2015-08-10 18:51:10 +00:00
|
|
|
}
|
|
|
|
[_data appendData:data];
|
|
|
|
if (_incrementalDataBlock) {
|
|
|
|
_incrementalDataBlock(data);
|
|
|
|
}
|
|
|
|
if (_downloadProgressBlock && _response.expectedContentLength > 0) {
|
|
|
|
_downloadProgressBlock(_data.length, _response.expectedContentLength);
|
|
|
|
}
|
2015-07-23 10:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)URLRequest:(id)requestToken didCompleteWithError:(NSError *)error
|
|
|
|
{
|
2015-08-10 18:51:10 +00:00
|
|
|
if ([self validateRequestToken:requestToken]) {
|
|
|
|
if (_completionBlock) {
|
|
|
|
_completionBlock(_response, _data, error);
|
|
|
|
[self invalidate];
|
|
|
|
}
|
|
|
|
}
|
2015-07-23 10:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|