2015-03-23 20:28:42 +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-02-20 04:10:52 +00:00
|
|
|
|
2015-06-18 16:41:38 +00:00
|
|
|
#import "RCTNetworking.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import "RCTAssert.h"
|
2015-04-10 03:18:31 +00:00
|
|
|
#import "RCTConvert.h"
|
2015-07-23 10:55:12 +00:00
|
|
|
#import "RCTDownloadTask.h"
|
2015-06-09 19:25:33 +00:00
|
|
|
#import "RCTURLRequestHandler.h"
|
2015-06-05 22:23:30 +00:00
|
|
|
#import "RCTEventDispatcher.h"
|
2015-06-09 19:25:33 +00:00
|
|
|
#import "RCTHTTPRequestHandler.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTLog.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
typedef RCTURLRequestCancellationBlock (^RCTHTTPQueryResult)(NSError *error, NSDictionary *result);
|
2015-06-09 19:25:33 +00:00
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
@interface RCTNetworking ()
|
2015-06-09 19:25:33 +00:00
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
- (RCTURLRequestCancellationBlock)processDataForHTTPQuery:(NSDictionary *)data
|
|
|
|
callback:(RCTHTTPQueryResult)callback;
|
2015-06-09 19:25:33 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to convert FormData payloads into multipart/formdata requests.
|
|
|
|
*/
|
|
|
|
@interface RCTHTTPFormDataHelper : NSObject
|
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
@property (nonatomic, weak) RCTNetworking *networker;
|
2015-06-09 19:25:33 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTHTTPFormDataHelper
|
|
|
|
{
|
|
|
|
NSMutableArray *parts;
|
|
|
|
NSMutableData *multipartBody;
|
|
|
|
RCTHTTPQueryResult _callback;
|
|
|
|
NSString *boundary;
|
|
|
|
}
|
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
static NSString *RCTGenerateFormBoundary()
|
2015-06-09 19:25:33 +00:00
|
|
|
{
|
2015-07-23 10:55:12 +00:00
|
|
|
const size_t boundaryLength = 70;
|
|
|
|
const char *boundaryChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_./";
|
|
|
|
|
|
|
|
char *bytes = malloc(boundaryLength);
|
|
|
|
size_t charCount = strlen(boundaryChars);
|
|
|
|
for (int i = 0; i < boundaryLength; i++) {
|
|
|
|
bytes[i] = boundaryChars[arc4random_uniform((u_int32_t)charCount)];
|
|
|
|
}
|
|
|
|
return [[NSString alloc] initWithBytesNoCopy:bytes length:boundaryLength encoding:NSUTF8StringEncoding freeWhenDone:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (RCTURLRequestCancellationBlock)process:(NSArray *)formData
|
|
|
|
callback:(RCTHTTPQueryResult)callback
|
|
|
|
{
|
|
|
|
if (formData.count == 0) {
|
|
|
|
return callback(nil, nil);
|
2015-06-09 19:25:33 +00:00
|
|
|
}
|
2015-07-23 10:55:12 +00:00
|
|
|
|
2015-06-09 19:25:33 +00:00
|
|
|
parts = [formData mutableCopy];
|
|
|
|
_callback = callback;
|
|
|
|
multipartBody = [[NSMutableData alloc] init];
|
2015-07-23 10:55:12 +00:00
|
|
|
boundary = RCTGenerateFormBoundary();
|
2015-06-09 19:25:33 +00:00
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
return [_networker processDataForHTTPQuery:parts[0] callback:^(NSError *error, NSDictionary *result) {
|
|
|
|
return [self handleResult:result error:error];
|
2015-06-09 19:25:33 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
- (RCTURLRequestCancellationBlock)handleResult:(NSDictionary *)result
|
|
|
|
error:(NSError *)error
|
2015-06-09 19:25:33 +00:00
|
|
|
{
|
|
|
|
if (error) {
|
2015-07-23 10:55:12 +00:00
|
|
|
return _callback(error, nil);
|
2015-06-09 19:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start with boundary.
|
|
|
|
[multipartBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary]
|
|
|
|
dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
|
|
|
|
|
// Print headers.
|
2015-07-23 10:55:12 +00:00
|
|
|
NSMutableDictionary *headers = [parts[0][@"headers"] mutableCopy];
|
2015-06-09 19:25:33 +00:00
|
|
|
NSString *partContentType = result[@"contentType"];
|
|
|
|
if (partContentType != nil) {
|
|
|
|
[headers setObject:partContentType forKey:@"content-type"];
|
|
|
|
}
|
|
|
|
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *parameterKey, NSString *parameterValue, BOOL *stop) {
|
|
|
|
[multipartBody appendData:[[NSString stringWithFormat:@"%@: %@\r\n", parameterKey, parameterValue]
|
|
|
|
dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
|
}];
|
|
|
|
|
|
|
|
// Add the body.
|
|
|
|
[multipartBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
|
[multipartBody appendData:result[@"body"]];
|
|
|
|
[multipartBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
[parts removeObjectAtIndex:0];
|
|
|
|
if (parts.count) {
|
|
|
|
return [_networker processDataForHTTPQuery:parts[0] callback:^(NSError *err, NSDictionary *res) {
|
|
|
|
return [self handleResult:res error:err];
|
2015-06-09 19:25:33 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've processed the last item. Finish and return.
|
|
|
|
[multipartBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary]
|
|
|
|
dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
|
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=\"%@\"", boundary];
|
2015-07-23 10:55:12 +00:00
|
|
|
return _callback(nil, @{@"body": multipartBody, @"contentType": contentType});
|
2015-06-09 19:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bridge module that provides the JS interface to the network stack.
|
|
|
|
*/
|
2015-06-18 16:41:38 +00:00
|
|
|
@implementation RCTNetworking
|
2015-06-09 19:25:33 +00:00
|
|
|
{
|
2015-07-23 10:55:12 +00:00
|
|
|
NSMutableDictionary *_tasksByRequestID;
|
2015-06-09 19:25:33 +00:00
|
|
|
}
|
2015-06-05 22:23:30 +00:00
|
|
|
|
|
|
|
@synthesize bridge = _bridge;
|
2015-06-19 11:18:54 +00:00
|
|
|
@synthesize methodQueue = _methodQueue;
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-04-08 12:42:43 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-06-09 19:25:33 +00:00
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
2015-07-23 10:55:12 +00:00
|
|
|
_tasksByRequestID = [[NSMutableDictionary alloc] init];
|
2015-06-09 19:25:33 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
- (RCTURLRequestCancellationBlock)buildRequest:(NSDictionary *)query
|
|
|
|
completionBlock:(void (^)(NSURLRequest *request))block
|
2015-06-09 19:25:33 +00:00
|
|
|
{
|
|
|
|
NSURL *URL = [RCTConvert NSURL:query[@"url"]];
|
|
|
|
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
|
|
|
|
request.HTTPMethod = [[RCTConvert NSString:query[@"method"]] uppercaseString] ?: @"GET";
|
|
|
|
request.allHTTPHeaderFields = [RCTConvert NSDictionary:query[@"headers"]];
|
|
|
|
|
|
|
|
NSDictionary *data = [RCTConvert NSDictionary:query[@"data"]];
|
2015-07-23 10:55:12 +00:00
|
|
|
return [self processDataForHTTPQuery:data callback:^(NSError *error, NSDictionary *result) {
|
2015-06-09 19:25:33 +00:00
|
|
|
if (error) {
|
|
|
|
RCTLogError(@"Error processing request body: %@", error);
|
|
|
|
// Ideally we'd circle back to JS here and notify an error/abort on the request.
|
2015-07-23 10:55:12 +00:00
|
|
|
return (RCTURLRequestCancellationBlock)nil;
|
2015-06-09 19:25:33 +00:00
|
|
|
}
|
|
|
|
request.HTTPBody = result[@"body"];
|
|
|
|
NSString *contentType = result[@"contentType"];
|
|
|
|
if (contentType) {
|
2015-07-16 16:34:28 +00:00
|
|
|
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
|
2015-06-09 19:25:33 +00:00
|
|
|
}
|
2015-07-16 16:34:28 +00:00
|
|
|
|
|
|
|
// Gzip the request body
|
|
|
|
if ([request.allHTTPHeaderFields[@"Content-Encoding"] isEqualToString:@"gzip"]) {
|
|
|
|
request.HTTPBody = RCTGzipData(request.HTTPBody, -1 /* default */);
|
|
|
|
[request setValue:[@(request.HTTPBody.length) description] forHTTPHeaderField:@"Content-Length"];
|
|
|
|
}
|
|
|
|
|
2015-07-17 11:50:42 +00:00
|
|
|
block(request);
|
2015-07-23 10:55:12 +00:00
|
|
|
return (RCTURLRequestCancellationBlock)nil;
|
2015-06-09 19:25:33 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id<RCTURLRequestHandler>)handlerForRequest:(NSURLRequest *)request
|
|
|
|
{
|
|
|
|
NSMutableArray *handlers = [NSMutableArray array];
|
|
|
|
for (id<RCTBridgeModule> module in _bridge.modules.allValues) {
|
|
|
|
if ([module conformsToProtocol:@protocol(RCTURLRequestHandler)]) {
|
|
|
|
if ([(id<RCTURLRequestHandler>)module canHandleRequest:request]) {
|
|
|
|
[handlers addObject:module];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[handlers sortUsingComparator:^NSComparisonResult(id<RCTURLRequestHandler> a, id<RCTURLRequestHandler> b) {
|
|
|
|
float priorityA = [a respondsToSelector:@selector(handlerPriority)] ? [a handlerPriority] : 0;
|
|
|
|
float priorityB = [b respondsToSelector:@selector(handlerPriority)] ? [b handlerPriority] : 0;
|
|
|
|
if (priorityA < priorityB) {
|
|
|
|
return NSOrderedAscending;
|
|
|
|
} else if (priorityA > priorityB) {
|
|
|
|
return NSOrderedDescending;
|
|
|
|
} else {
|
|
|
|
RCTLogError(@"The RCTURLRequestHandlers %@ and %@ both reported that"
|
|
|
|
" they can handle the request %@, and have equal priority"
|
|
|
|
" (%g). This could result in non-deterministic behavior.",
|
|
|
|
a, b, request, priorityA);
|
|
|
|
|
|
|
|
return NSOrderedSame;
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
id<RCTURLRequestHandler> handler = [handlers lastObject];
|
|
|
|
if (!handler) {
|
2015-06-25 08:59:17 +00:00
|
|
|
RCTLogError(@"No suitable request handler found for %@", request.URL);
|
2015-06-09 19:25:33 +00:00
|
|
|
}
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-06-09 19:25:33 +00:00
|
|
|
* Process the 'data' part of an HTTP query.
|
|
|
|
*
|
|
|
|
* 'data' can be a JSON value of the following forms:
|
|
|
|
*
|
|
|
|
* - {"string": "..."}: a simple JS string that will be UTF-8 encoded and sent as the body
|
|
|
|
*
|
|
|
|
* - {"uri": "some-uri://..."}: reference to a system resource, e.g. an image in the asset library
|
|
|
|
*
|
|
|
|
* - {"formData": [...]}: list of data payloads that will be combined into a multipart/form-data request
|
|
|
|
*
|
|
|
|
* If successful, the callback be called with a result dictionary containing the following (optional) keys:
|
|
|
|
*
|
|
|
|
* - @"body" (NSData): the body of the request
|
|
|
|
*
|
|
|
|
* - @"contentType" (NSString): the content type header of the request
|
|
|
|
*
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2015-07-23 10:55:12 +00:00
|
|
|
- (RCTURLRequestCancellationBlock)processDataForHTTPQuery:(NSDictionary *)query callback:
|
|
|
|
(RCTURLRequestCancellationBlock (^)(NSError *error, NSDictionary *result))callback
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-06-09 19:25:33 +00:00
|
|
|
if (!query) {
|
2015-07-23 10:55:12 +00:00
|
|
|
return callback(nil, nil);
|
2015-06-09 19:25:33 +00:00
|
|
|
}
|
|
|
|
NSData *body = [RCTConvert NSData:query[@"string"]];
|
|
|
|
if (body) {
|
2015-07-23 10:55:12 +00:00
|
|
|
return callback(nil, @{@"body": body});
|
2015-06-09 19:25:33 +00:00
|
|
|
}
|
|
|
|
NSURLRequest *request = [RCTConvert NSURLRequest:query[@"uri"]];
|
|
|
|
if (request) {
|
2015-07-23 10:55:12 +00:00
|
|
|
|
|
|
|
__block RCTURLRequestCancellationBlock cancellationBlock = nil;
|
2015-07-27 20:46:59 +00:00
|
|
|
RCTDownloadTask *task = [self downloadTaskWithRequest:request completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) {
|
2015-07-23 10:55:12 +00:00
|
|
|
cancellationBlock = callback(error, data ? @{@"body": data, @"contentType": RCTNullIfNil(response.MIMEType)} : nil);
|
2015-06-09 19:25:33 +00:00
|
|
|
}];
|
2015-07-23 10:55:12 +00:00
|
|
|
|
|
|
|
__weak RCTDownloadTask *weakTask = task;
|
|
|
|
return ^{
|
|
|
|
[weakTask cancel];
|
|
|
|
if (cancellationBlock) {
|
|
|
|
cancellationBlock();
|
|
|
|
}
|
|
|
|
};
|
2015-06-09 19:25:33 +00:00
|
|
|
}
|
|
|
|
NSDictionaryArray *formData = [RCTConvert NSDictionaryArray:query[@"formData"]];
|
2015-07-23 10:55:12 +00:00
|
|
|
if (formData) {
|
2015-06-09 19:25:33 +00:00
|
|
|
RCTHTTPFormDataHelper *formDataHelper = [[RCTHTTPFormDataHelper alloc] init];
|
2015-07-23 10:55:12 +00:00
|
|
|
formDataHelper.networker = self;
|
|
|
|
return [formDataHelper process:formData callback:callback];
|
2015-05-26 20:29:41 +00:00
|
|
|
}
|
2015-06-09 19:25:33 +00:00
|
|
|
// Nothing in the data payload, at least nothing we could understand anyway.
|
|
|
|
// Ignore and treat it as if it were null.
|
2015-07-23 10:55:12 +00:00
|
|
|
return callback(nil, nil);
|
2015-06-09 19:25:33 +00:00
|
|
|
}
|
2015-06-05 22:23:30 +00:00
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
- (void)sendData:(NSData *)data forTask:(RCTDownloadTask *)task
|
2015-06-09 19:25:33 +00:00
|
|
|
{
|
|
|
|
if (data.length == 0) {
|
|
|
|
return;
|
2015-06-05 22:23:30 +00:00
|
|
|
}
|
2015-06-09 19:25:33 +00:00
|
|
|
|
|
|
|
// Get text encoding
|
2015-07-23 10:55:12 +00:00
|
|
|
NSURLResponse *response = task.response;
|
2015-06-09 19:25:33 +00:00
|
|
|
NSStringEncoding encoding = NSUTF8StringEncoding;
|
|
|
|
if (response.textEncodingName) {
|
|
|
|
CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName);
|
|
|
|
encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *responseText = [[NSString alloc] initWithData:data encoding:encoding];
|
|
|
|
if (!responseText && data.length) {
|
2015-07-08 16:33:49 +00:00
|
|
|
RCTLogWarn(@"Received data was invalid.");
|
2015-06-09 19:25:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
NSArray *responseJSON = @[task.requestID, responseText ?: @""];
|
2015-06-09 19:25:33 +00:00
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"didReceiveNetworkData"
|
|
|
|
body:responseJSON];
|
|
|
|
}
|
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
- (void)sendRequest:(NSURLRequest *)request
|
|
|
|
incrementalUpdates:(BOOL)incrementalUpdates
|
|
|
|
responseSender:(RCTResponseSenderBlock)responseSender
|
2015-06-09 19:25:33 +00:00
|
|
|
{
|
2015-07-23 10:55:12 +00:00
|
|
|
__block RCTDownloadTask *task;
|
|
|
|
|
2015-07-27 20:46:59 +00:00
|
|
|
RCTURLRequestProgressBlock uploadProgressBlock = ^(int64_t progress, int64_t total) {
|
2015-07-23 10:55:12 +00:00
|
|
|
dispatch_async(_methodQueue, ^{
|
2015-07-27 20:46:59 +00:00
|
|
|
NSArray *responseJSON = @[task.requestID, @((double)progress), @((double)total)];
|
2015-07-23 10:55:12 +00:00
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"didSendNetworkData" body:responseJSON];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
void (^responseBlock)(NSURLResponse *) = ^(NSURLResponse *response) {
|
|
|
|
dispatch_async(_methodQueue, ^{
|
|
|
|
NSHTTPURLResponse *httpResponse = nil;
|
|
|
|
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
|
|
|
|
// Might be a local file request
|
|
|
|
httpResponse = (NSHTTPURLResponse *)response;
|
|
|
|
}
|
|
|
|
NSArray *responseJSON = @[task.requestID,
|
|
|
|
@(httpResponse.statusCode ?: 200),
|
|
|
|
httpResponse.allHeaderFields ?: @{},
|
|
|
|
];
|
|
|
|
|
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"didReceiveNetworkResponse"
|
|
|
|
body:responseJSON];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
void (^incrementalDataBlock)(NSData *) = incrementalUpdates ? ^(NSData *data) {
|
|
|
|
dispatch_async(_methodQueue, ^{
|
|
|
|
[self sendData:data forTask:task];
|
|
|
|
});
|
|
|
|
} : nil;
|
|
|
|
|
|
|
|
RCTURLRequestCompletionBlock completionBlock =
|
|
|
|
^(NSURLResponse *response, NSData *data, NSError *error) {
|
|
|
|
dispatch_async(_methodQueue, ^{
|
|
|
|
if (!incrementalUpdates) {
|
|
|
|
[self sendData:data forTask:task];
|
|
|
|
}
|
|
|
|
NSArray *responseJSON = @[task.requestID,
|
|
|
|
RCTNullIfNil(error.localizedDescription),
|
|
|
|
];
|
2015-06-09 19:25:33 +00:00
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"didCompleteNetworkResponse"
|
|
|
|
body:responseJSON];
|
2015-06-09 19:25:33 +00:00
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
[_tasksByRequestID removeObjectForKey:task.requestID];
|
|
|
|
});
|
|
|
|
};
|
2015-06-09 19:25:33 +00:00
|
|
|
|
2015-07-27 20:46:59 +00:00
|
|
|
task = [self downloadTaskWithRequest:request completionBlock:completionBlock];
|
2015-07-23 10:55:12 +00:00
|
|
|
task.incrementalDataBlock = incrementalDataBlock;
|
|
|
|
task.responseBlock = responseBlock;
|
|
|
|
task.uploadProgressBlock = uploadProgressBlock;
|
2015-06-09 19:25:33 +00:00
|
|
|
|
2015-07-23 10:55:12 +00:00
|
|
|
if (task.requestID) {
|
|
|
|
_tasksByRequestID[task.requestID] = task;
|
|
|
|
responseSender(@[task.requestID]);
|
|
|
|
}
|
2015-06-09 19:25:33 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 20:46:59 +00:00
|
|
|
#pragma mark - Public API
|
|
|
|
|
|
|
|
- (RCTDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request
|
|
|
|
completionBlock:(RCTURLRequestCompletionBlock)completionBlock
|
|
|
|
{
|
|
|
|
id<RCTURLRequestHandler> handler = [self handlerForRequest:request];
|
|
|
|
if (!handler) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [[RCTDownloadTask alloc] initWithRequest:request
|
|
|
|
handler:handler
|
|
|
|
completionBlock:completionBlock];
|
|
|
|
}
|
|
|
|
|
2015-06-09 19:25:33 +00:00
|
|
|
#pragma mark - JS API
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(sendRequest:(NSDictionary *)query
|
|
|
|
responseSender:(RCTResponseSenderBlock)responseSender)
|
|
|
|
{
|
2015-07-23 10:55:12 +00:00
|
|
|
// TODO: buildRequest returns a cancellation block, but there's currently
|
|
|
|
// no way to invoke it, if, for example the request is cancelled while
|
|
|
|
// loading a large file to build the request body
|
2015-07-17 11:50:42 +00:00
|
|
|
[self buildRequest:query completionBlock:^(NSURLRequest *request) {
|
|
|
|
|
|
|
|
BOOL incrementalUpdates = [RCTConvert BOOL:query[@"incrementalUpdates"]];
|
2015-07-23 10:55:12 +00:00
|
|
|
[self sendRequest:request
|
|
|
|
incrementalUpdates:incrementalUpdates
|
2015-07-17 11:50:42 +00:00
|
|
|
responseSender:responseSender];
|
|
|
|
}];
|
2015-06-09 19:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(cancelRequest:(NSNumber *)requestID)
|
|
|
|
{
|
2015-07-23 10:55:12 +00:00
|
|
|
[_tasksByRequestID[requestID] cancel];
|
|
|
|
[_tasksByRequestID removeObjectForKey:requestID];
|
2015-06-05 22:23:30 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@end
|
2015-07-27 20:46:59 +00:00
|
|
|
|
|
|
|
@implementation RCTBridge (RCTNetworking)
|
|
|
|
|
|
|
|
- (RCTNetworking *)networking
|
|
|
|
{
|
|
|
|
return self.modules[RCTBridgeModuleNameForClass([RCTNetworking class])];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|