diff --git a/Libraries/Network/RCTNetworking.h b/Libraries/Network/RCTNetworking.h index 2068f32b803038..358ab52b6d1206 100644 --- a/Libraries/Network/RCTNetworking.h +++ b/Libraries/Network/RCTNetworking.h @@ -45,6 +45,8 @@ - (RCTNetworkTask *)networkTaskWithRequest:(NSURLRequest *)request completionBlock:(RCTURLRequestCompletionBlock)completionBlock; +- (RCTNetworkTask *)networkTaskWithRequest:(NSURLRequest *)request handler:(id)handler completionBlock:(RCTURLRequestCompletionBlock)completionBlock; + - (void)addRequestHandler:(id)handler; - (void)addResponseHandler:(id)handler; diff --git a/Libraries/Network/RCTNetworking.mm b/Libraries/Network/RCTNetworking.mm index 22ca45b0fa3ff4..11e569cebb3c69 100644 --- a/Libraries/Network/RCTNetworking.mm +++ b/Libraries/Network/RCTNetworking.mm @@ -17,6 +17,7 @@ #import #import +#import #import "RCTNetworkPlugins.h" @@ -154,6 +155,7 @@ @implementation RCTNetworking } @synthesize methodQueue = _methodQueue; +@synthesize moduleRegistry = _moduleRegistry; RCT_EXPORT_MODULE() @@ -188,6 +190,14 @@ - (void)invalidate _responseHandlers = nil; } +// TODO (T93136931) - Investigate why this is needed. This setter shouldn't be +// necessary, since moduleRegistry is a property on RCTEventEmitter (which this +// class inherits from). +- (void)setModuleRegistry:(RCTModuleRegistry *)moduleRegistry +{ + _moduleRegistry = moduleRegistry; +} + - (NSArray *)supportedEvents { return @[@"didCompleteNetworkResponse", @@ -393,9 +403,9 @@ - (RCTURLRequestCancellationBlock)processDataForHTTPQuery:(nullable NSDictionary } NSURLRequest *request = [RCTConvert NSURLRequest:query[@"uri"]]; if (request) { - __block RCTURLRequestCancellationBlock cancellationBlock = nil; - RCTNetworkTask *task = [self networkTaskWithRequest:request completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) { + id handler = [self.moduleRegistry moduleForName:"FileRequestHandler"]; + RCTNetworkTask *task = [self networkTaskWithRequest:request handler:handler completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) { dispatch_async(self->_methodQueue, ^{ cancellationBlock = callback(error, data ? @{@"body": data, @"contentType": RCTNullIfNil(response.MIMEType)} : nil); }); @@ -676,6 +686,19 @@ - (RCTNetworkTask *)networkTaskWithRequest:(NSURLRequest *)request completionBlo return task; } +- (RCTNetworkTask *)networkTaskWithRequest:(NSURLRequest *)request handler:(id)handler completionBlock:(RCTURLRequestCompletionBlock)completionBlock +{ + if (!handler) { + // specified handler is nil, fall back to generic method + return [self networkTaskWithRequest:request completionBlock:completionBlock]; + } + RCTNetworkTask *task = [[RCTNetworkTask alloc] initWithRequest:request + handler:handler + callbackQueue:_methodQueue]; + task.completionBlock = completionBlock; + return task; +} + #pragma mark - JS API RCT_EXPORT_METHOD(sendRequest:(JS::NativeNetworkingIOS::SpecSendRequestQuery &)query