RCTNetworkTask.m
4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* 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.
*/
#import <React/RCTLog.h>
#import <React/RCTNetworkTask.h>
#import <React/RCTUtils.h>
@implementation RCTNetworkTask
{
NSMutableData *_data;
id<RCTURLRequestHandler> _handler;
dispatch_queue_t _callbackQueue;
RCTNetworkTask *_selfReference;
}
- (instancetype)initWithRequest:(NSURLRequest *)request
handler:(id<RCTURLRequestHandler>)handler
callbackQueue:(dispatch_queue_t)callbackQueue
{
RCTAssertParam(request);
RCTAssertParam(handler);
RCTAssertParam(callbackQueue);
static NSUInteger requestID = 0;
if ((self = [super init])) {
_requestID = @(requestID++);
_request = request;
_handler = handler;
_callbackQueue = callbackQueue;
_status = RCTNetworkTaskPending;
dispatch_queue_set_specific(callbackQueue, (__bridge void *)self, (__bridge void *)self, NULL);
}
return self;
}
RCT_NOT_IMPLEMENTED(- (instancetype)init)
- (void)invalidate
{
_selfReference = nil;
_completionBlock = nil;
_downloadProgressBlock = nil;
_incrementalDataBlock = nil;
_responseBlock = nil;
_uploadProgressBlock = nil;
_requestToken = nil;
}
- (void)dispatchCallback:(dispatch_block_t)callback
{
if (dispatch_get_specific((__bridge void *)self) == (__bridge void *)self) {
callback();
} else {
dispatch_async(_callbackQueue, callback);
}
}
- (void)start
{
if (_status != RCTNetworkTaskPending) {
RCTLogError(@"RCTNetworkTask was already started or completed");
return;
}
if (_requestToken == nil) {
id token = [_handler sendRequest:_request withDelegate:self];
if ([self validateRequestToken:token]) {
_selfReference = self;
_status = RCTNetworkTaskInProgress;
}
}
}
- (void)cancel
{
if (_status == RCTNetworkTaskFinished) {
return;
}
_status = RCTNetworkTaskFinished;
id token = _requestToken;
if (token && [_handler respondsToSelector:@selector(cancelRequest:)]) {
[_handler cancelRequest:token];
}
[self invalidate];
}
- (BOOL)validateRequestToken:(id)requestToken
{
BOOL valid = YES;
if (_requestToken == nil) {
if (requestToken == nil) {
if (RCT_DEBUG) {
RCTLogError(@"Missing request token for request: %@", _request);
}
valid = NO;
}
_requestToken = requestToken;
} else if (![requestToken isEqual:_requestToken]) {
if (RCT_DEBUG) {
RCTLogError(@"Unrecognized request token: %@ expected: %@", requestToken, _requestToken);
}
valid = NO;
}
if (!valid) {
_status = RCTNetworkTaskFinished;
if (_completionBlock) {
RCTURLRequestCompletionBlock completionBlock = _completionBlock;
[self dispatchCallback:^{
completionBlock(self->_response, nil, RCTErrorWithMessage(@"Invalid request token."));
}];
}
[self invalidate];
}
return valid;
}
- (void)URLRequest:(id)requestToken didSendDataWithProgress:(int64_t)bytesSent
{
if (![self validateRequestToken:requestToken]) {
return;
}
if (_uploadProgressBlock) {
RCTURLRequestProgressBlock uploadProgressBlock = _uploadProgressBlock;
int64_t length = _request.HTTPBody.length;
[self dispatchCallback:^{
uploadProgressBlock(bytesSent, length);
}];
}
}
- (void)URLRequest:(id)requestToken didReceiveResponse:(NSURLResponse *)response
{
if (![self validateRequestToken:requestToken]) {
return;
}
_response = response;
if (_responseBlock) {
RCTURLRequestResponseBlock responseBlock = _responseBlock;
[self dispatchCallback:^{
responseBlock(response);
}];
}
}
- (void)URLRequest:(id)requestToken didReceiveData:(NSData *)data
{
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;
[self dispatchCallback:^{
incrementalDataBlock(data, length, total);
}];
}
if (_downloadProgressBlock && total > 0) {
RCTURLRequestProgressBlock downloadProgressBlock = _downloadProgressBlock;
[self dispatchCallback:^{
downloadProgressBlock(length, total);
}];
}
}
- (void)URLRequest:(id)requestToken didCompleteWithError:(NSError *)error
{
if (![self validateRequestToken:requestToken]) {
return;
}
_status = RCTNetworkTaskFinished;
if (_completionBlock) {
RCTURLRequestCompletionBlock completionBlock = _completionBlock;
[self dispatchCallback:^{
completionBlock(self->_response, self->_data, error);
}];
}
[self invalidate];
}
@end