From ffc90c7f92e63e1a53ed107833e3deed492ab435 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Tue, 28 Jul 2020 11:57:15 -0700 Subject: [PATCH] Remove requestToken being nil check from [RCTNetworkTask validateRequestToken] Summary: Changelog: Fix [TypeError: Network request failed] on file upload # Problem In https://github.com/facebook/react-native/commit/31980094107ed37f8de70972dbcc319cc9a26339 I made method `loadImageForURL` blocking and nil returning since it was no longer cancellable. However inside `[RCTNetworkTask validateRequestToken]` was a logic counting on request token being non nil. This diff removes this check and adds `nullable` to `[RCTImageURLLoader loadImageForURL]` making it explicit. Reviewed By: PeteTheHeat Differential Revision: D22767174 fbshipit-source-id: 04d5562e381912233b9c14e8156cbf145288f063 --- Libraries/Image/RCTImageLoader.mm | 44 ++++++++++----------- Libraries/Image/RCTImageLoaderProtocol.h | 26 ++++++------ Libraries/Image/RCTImageURLLoader.h | 2 +- Libraries/Image/RCTLocalAssetImageLoader.mm | 16 ++++---- Libraries/Network/RCTNetworkTask.mm | 6 --- 5 files changed, 44 insertions(+), 50 deletions(-) diff --git a/Libraries/Image/RCTImageLoader.mm b/Libraries/Image/RCTImageLoader.mm index 6da126972aa7df..e356fb2d79cccd 100644 --- a/Libraries/Image/RCTImageLoader.mm +++ b/Libraries/Image/RCTImageLoader.mm @@ -314,17 +314,17 @@ - (void)setImageCache:(id)cache #pragma mark - RCTImageLoaderProtocol 2/3 -- (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest - callback:(RCTImageLoaderCompletionBlock)callback +- (nullable RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest + callback:(RCTImageLoaderCompletionBlock)callback { return [self loadImageWithURLRequest:imageURLRequest priority:RCTImageLoaderPriorityImmediate callback:callback]; } -- (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest - priority:(RCTImageLoaderPriority)priority - callback:(RCTImageLoaderCompletionBlock)callback { +- (nullable RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest + priority:(RCTImageLoaderPriority)priority + callback:(RCTImageLoaderCompletionBlock)callback { return [self loadImageWithURLRequest:imageURLRequest size:CGSizeZero scale:1 @@ -336,14 +336,14 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image completionBlock:callback]; } -- (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest - size:(CGSize)size - scale:(CGFloat)scale - clipped:(BOOL)clipped - resizeMode:(RCTResizeMode)resizeMode - progressBlock:(RCTImageLoaderProgressBlock)progressBlock - partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadBlock - completionBlock:(RCTImageLoaderCompletionBlock)completionBlock +- (nullable RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest + size:(CGSize)size + scale:(CGFloat)scale + clipped:(BOOL)clipped + resizeMode:(RCTResizeMode)resizeMode + progressBlock:(RCTImageLoaderProgressBlock)progressBlock + partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadBlock + completionBlock:(RCTImageLoaderCompletionBlock)completionBlock { return [self loadImageWithURLRequest:imageURLRequest size:size @@ -356,15 +356,15 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image completionBlock:completionBlock]; } -- (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest - size:(CGSize)size - scale:(CGFloat)scale - clipped:(BOOL)clipped - resizeMode:(RCTResizeMode)resizeMode - priority:(RCTImageLoaderPriority)priority - progressBlock:(RCTImageLoaderProgressBlock)progressBlock - partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadBlock - completionBlock:(RCTImageLoaderCompletionBlock)completionBlock +- (nullable RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest + size:(CGSize)size + scale:(CGFloat)scale + clipped:(BOOL)clipped + resizeMode:(RCTResizeMode)resizeMode + priority:(RCTImageLoaderPriority)priority + progressBlock:(RCTImageLoaderProgressBlock)progressBlock + partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadBlock + completionBlock:(RCTImageLoaderCompletionBlock)completionBlock { RCTImageURLLoaderRequest *request = [self loadImageWithURLRequest:imageURLRequest size:size diff --git a/Libraries/Image/RCTImageLoaderProtocol.h b/Libraries/Image/RCTImageLoaderProtocol.h index f14c196ebb296a..366fb7b57ceb6a 100644 --- a/Libraries/Image/RCTImageLoaderProtocol.h +++ b/Libraries/Image/RCTImageLoaderProtocol.h @@ -68,14 +68,14 @@ typedef NS_ENUM(NSUInteger, RCTImageLoaderPriority) { * Loads the specified image at the highest available resolution. * Can be called from any thread, will call back on an unspecified thread. */ -- (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest - callback:(RCTImageLoaderCompletionBlock)callback; +- (nullable RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest + callback:(RCTImageLoaderCompletionBlock)callback; /** * As above, but includes download `priority`. */ -- (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest - priority:(RCTImageLoaderPriority)priority - callback:(RCTImageLoaderCompletionBlock)callback; +- (nullable RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest + priority:(RCTImageLoaderPriority)priority + callback:(RCTImageLoaderCompletionBlock)callback; /** * As above, but includes target `size`, `scale` and `resizeMode`, which are used to @@ -86,14 +86,14 @@ typedef NS_ENUM(NSUInteger, RCTImageLoaderPriority) { * It is meant to be called repeatedly while loading the image as higher quality versions are decoded, * for instance with progressive JPEGs. */ -- (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest - size:(CGSize)size - scale:(CGFloat)scale - clipped:(BOOL)clipped - resizeMode:(RCTResizeMode)resizeMode - progressBlock:(RCTImageLoaderProgressBlock)progressBlock - partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadBlock - completionBlock:(RCTImageLoaderCompletionBlock)completionBlock; +- (nullable RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest + size:(CGSize)size + scale:(CGFloat)scale + clipped:(BOOL)clipped + resizeMode:(RCTResizeMode)resizeMode + progressBlock:(RCTImageLoaderProgressBlock)progressBlock + partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadBlock + completionBlock:(RCTImageLoaderCompletionBlock)completionBlock; /** * Finds an appropriate image decoder and passes the target `size`, `scale` and diff --git a/Libraries/Image/RCTImageURLLoader.h b/Libraries/Image/RCTImageURLLoader.h index a1f2144e47155c..d950b46f745f70 100644 --- a/Libraries/Image/RCTImageURLLoader.h +++ b/Libraries/Image/RCTImageURLLoader.h @@ -35,7 +35,7 @@ typedef dispatch_block_t RCTImageLoaderCancellationBlock; * has finished. The method should also return a cancellation block, if * applicable. */ -- (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL +- (nullable RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL size:(CGSize)size scale:(CGFloat)scale resizeMode:(RCTResizeMode)resizeMode diff --git a/Libraries/Image/RCTLocalAssetImageLoader.mm b/Libraries/Image/RCTLocalAssetImageLoader.mm index 8669ad3615d82c..0387af805e97d9 100644 --- a/Libraries/Image/RCTLocalAssetImageLoader.mm +++ b/Libraries/Image/RCTLocalAssetImageLoader.mm @@ -41,13 +41,13 @@ - (BOOL)shouldCacheLoadedImages return NO; } - - (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL - size:(CGSize)size - scale:(CGFloat)scale - resizeMode:(RCTResizeMode)resizeMode - progressHandler:(RCTImageLoaderProgressBlock)progressHandler - partialLoadHandler:(RCTImageLoaderPartialLoadBlock)partialLoadHandler - completionHandler:(RCTImageLoaderCompletionBlock)completionHandler + - (nullable RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL + size:(CGSize)size + scale:(CGFloat)scale + resizeMode:(RCTResizeMode)resizeMode + progressHandler:(RCTImageLoaderProgressBlock)progressHandler + partialLoadHandler:(RCTImageLoaderPartialLoadBlock)partialLoadHandler + completionHandler:(RCTImageLoaderCompletionBlock)completionHandler { UIImage *image = RCTImageFromLocalAssetURL(imageURL); if (image) { @@ -60,7 +60,7 @@ - (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL RCTLogWarn(@"%@", message); completionHandler(RCTErrorWithMessage(message), nil); } - + return nil; } diff --git a/Libraries/Network/RCTNetworkTask.mm b/Libraries/Network/RCTNetworkTask.mm index 1cc625298e32f1..5b07e2cb686501 100644 --- a/Libraries/Network/RCTNetworkTask.mm +++ b/Libraries/Network/RCTNetworkTask.mm @@ -99,12 +99,6 @@ - (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) {