2015-07-23 03:55:12 -07: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 09:04:54 -07:00
|
|
|
#import "RCTNetworkTask.h"
|
2015-07-23 03:55:12 -07:00
|
|
|
|
2015-10-13 08:12:54 -07:00
|
|
|
#import "RCTLog.h"
|
2015-07-23 03:55:12 -07:00
|
|
|
|
2015-10-19 09:04:54 -07:00
|
|
|
@implementation RCTNetworkTask
|
2015-07-23 03:55:12 -07:00
|
|
|
{
|
|
|
|
NSMutableData *_data;
|
|
|
|
id<RCTURLRequestHandler> _handler;
|
2015-10-19 09:04:54 -07:00
|
|
|
RCTNetworkTask *_selfReference;
|
2015-07-23 03:55:12 -07: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;
|
2016-02-16 12:41:20 -08:00
|
|
|
_status = RCTNetworkTaskPending;
|
2015-07-23 03:55:12 -07:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-10-17 09:33:09 -07:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
|
|
|
|
2015-07-23 03:55:12 -07:00
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
_selfReference = nil;
|
|
|
|
_completionBlock = nil;
|
|
|
|
_downloadProgressBlock = nil;
|
|
|
|
_incrementalDataBlock = nil;
|
|
|
|
_responseBlock = nil;
|
|
|
|
_uploadProgressBlock = nil;
|
|
|
|
}
|
|
|
|
|
2015-10-17 09:33:09 -07:00
|
|
|
- (void)start
|
|
|
|
{
|
|
|
|
if (_requestToken == nil) {
|
|
|
|
if ([self validateRequestToken:[_handler sendRequest:_request
|
|
|
|
withDelegate:self]]) {
|
|
|
|
_selfReference = self;
|
2016-02-16 12:41:20 -08:00
|
|
|
_status = RCTNetworkTaskInProgress;
|
2015-10-17 09:33:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-23 03:55:12 -07:00
|
|
|
|
|
|
|
- (void)cancel
|
|
|
|
{
|
2016-06-09 09:55:18 -07:00
|
|
|
_status = RCTNetworkTaskFinished;
|
2015-11-17 09:53:47 -08:00
|
|
|
__strong id strongToken = _requestToken;
|
|
|
|
if (strongToken && [_handler respondsToSelector:@selector(cancelRequest:)]) {
|
|
|
|
[_handler cancelRequest:strongToken];
|
2015-07-23 03:55:12 -07:00
|
|
|
}
|
|
|
|
[self invalidate];
|
|
|
|
}
|
|
|
|
|
2015-08-10 17:51:10 -01:00
|
|
|
- (BOOL)validateRequestToken:(id)requestToken
|
|
|
|
{
|
2016-06-09 09:55:18 -07:00
|
|
|
BOOL valid = YES;
|
2015-10-17 09:33:09 -07:00
|
|
|
if (_requestToken == nil) {
|
|
|
|
if (requestToken == nil) {
|
2016-06-09 09:55:18 -07:00
|
|
|
if (RCT_DEBUG) {
|
|
|
|
RCTLogError(@"Missing request token for request: %@", _request);
|
|
|
|
}
|
|
|
|
valid = NO;
|
2015-10-17 09:33:09 -07:00
|
|
|
}
|
|
|
|
_requestToken = requestToken;
|
2016-06-09 09:55:18 -07:00
|
|
|
} else if (![requestToken isEqual:_requestToken]) {
|
2015-08-10 17:51:10 -01:00
|
|
|
if (RCT_DEBUG) {
|
2015-10-13 08:12:54 -07:00
|
|
|
RCTLogError(@"Unrecognized request token: %@ expected: %@", requestToken, _requestToken);
|
2015-08-10 17:51:10 -01:00
|
|
|
}
|
2016-06-09 09:55:18 -07:00
|
|
|
valid = NO;
|
|
|
|
}
|
|
|
|
if (!valid) {
|
|
|
|
_status = RCTNetworkTaskFinished;
|
2015-08-10 17:51:10 -01:00
|
|
|
if (_completionBlock) {
|
2016-06-09 09:55:18 -07:00
|
|
|
_completionBlock(_response, nil, [NSError errorWithDomain:RCTErrorDomain code:0
|
|
|
|
userInfo:@{NSLocalizedDescriptionKey: @"Invalid request token."}]);
|
2015-08-10 17:51:10 -01:00
|
|
|
}
|
2016-02-16 12:41:20 -08:00
|
|
|
[self invalidate];
|
2015-08-10 17:51:10 -01:00
|
|
|
}
|
2016-06-09 09:55:18 -07:00
|
|
|
return valid;
|
2015-08-10 17:51:10 -01:00
|
|
|
}
|
|
|
|
|
2015-07-23 03:55:12 -07:00
|
|
|
- (void)URLRequest:(id)requestToken didSendDataWithProgress:(int64_t)bytesSent
|
|
|
|
{
|
2015-08-10 17:51:10 -01:00
|
|
|
if ([self validateRequestToken:requestToken]) {
|
|
|
|
if (_uploadProgressBlock) {
|
|
|
|
_uploadProgressBlock(bytesSent, _request.HTTPBody.length);
|
|
|
|
}
|
2015-07-23 03:55:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)URLRequest:(id)requestToken didReceiveResponse:(NSURLResponse *)response
|
|
|
|
{
|
2015-08-10 17:51:10 -01:00
|
|
|
if ([self validateRequestToken:requestToken]) {
|
|
|
|
_response = response;
|
|
|
|
if (_responseBlock) {
|
|
|
|
_responseBlock(response);
|
|
|
|
}
|
2015-07-23 03:55:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)URLRequest:(id)requestToken didReceiveData:(NSData *)data
|
|
|
|
{
|
2015-08-10 17:51:10 -01:00
|
|
|
if ([self validateRequestToken:requestToken]) {
|
|
|
|
if (!_data) {
|
2015-08-17 07:35:34 -07:00
|
|
|
_data = [NSMutableData new];
|
2015-08-10 17:51:10 -01:00
|
|
|
}
|
|
|
|
[_data appendData:data];
|
|
|
|
if (_incrementalDataBlock) {
|
Add responseType as a concept to RCTNetworking, send binary data as base64
Summary:
In preparation for Blob support (wherein binary XHR and WebSocket responses can be retained as native data blobs on the native side and JS receives a web-like opaque Blob object), this change makes RCTNetworking aware of the responseType that JS requests. A `xhr.responseType` of `''` or `'text'` translates to a native response type of `'text'`. A `xhr.responseType` of `arraybuffer` translates to a native response type of `base64`, as we currently lack an API to transmit TypedArrays directly to JS. This is analogous to how the WebSocket module already works, and it's a lot more versatile and much less brittle than converting a JS *string* back to a TypedArray, which is what's currently going on.
Now that we don't always send text down to JS, JS consumers might still want to get progress updates about a binary download. This is what the `'progress'` event is designed for, so this change also implements that. This change also follows the XHR spec with regards to `xhr.response` and `xhr.responseText`:
- if the response type is `'text'`, `xhr.responseText` can be peeked at by the JS consumer. It will be updated periodically as the download progresses, so long as there's either an `onreadystatechange` or `onprogress` handler on the XHR.
- if the response type is not `'text'`, `xhr.responseText` can't be accessed and `xhr.response` remains `null` until the response is fully received. `'progress'` events containing response details (total bytes, downloaded so far) are dispatched if there's an `onprogress` handler.
Once Blobs are landed, `xhr.responseType` of `'blob'` will correspond to the same native response type, which will cause RCTNetworking to only send a blob ID down to JS, which can then create a `Blob` object from that for consumers.
Closes https://github.com/facebook/react-native/pull/8324
Reviewed By: javache
Differential Revision: D3508822
Pulled By: davidaurelio
fbshipit-source-id: 441b2d4d40265b6036559c3ccb9fa962999fa5df
2016-07-13 04:53:54 -07:00
|
|
|
_incrementalDataBlock(data, _data.length, _response.expectedContentLength);
|
2015-08-10 17:51:10 -01:00
|
|
|
}
|
|
|
|
if (_downloadProgressBlock && _response.expectedContentLength > 0) {
|
|
|
|
_downloadProgressBlock(_data.length, _response.expectedContentLength);
|
|
|
|
}
|
2015-07-23 03:55:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)URLRequest:(id)requestToken didCompleteWithError:(NSError *)error
|
|
|
|
{
|
2015-08-10 17:51:10 -01:00
|
|
|
if ([self validateRequestToken:requestToken]) {
|
2016-06-09 09:55:18 -07:00
|
|
|
_status = RCTNetworkTaskFinished;
|
2015-08-10 17:51:10 -01:00
|
|
|
if (_completionBlock) {
|
|
|
|
_completionBlock(_response, _data, error);
|
|
|
|
}
|
2016-02-16 12:41:20 -08:00
|
|
|
[self invalidate];
|
2015-08-10 17:51:10 -01:00
|
|
|
}
|
2015-07-23 03:55:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|