diff --git a/framework/ios/base/bridge/HippyBridge+BundleLoad.mm b/framework/ios/base/bridge/HippyBridge+BundleLoad.mm index 3d797e6dc18..c320121b7e2 100644 --- a/framework/ios/base/bridge/HippyBridge+BundleLoad.mm +++ b/framework/ios/base/bridge/HippyBridge+BundleLoad.mm @@ -216,16 +216,14 @@ - (void)fetchBundleWithURL:(NSURL *)bundleURL completion:(void (^)(NSData *sourc HippyAssertParam(completion); // Fetch the bundle // Call the completion handler with the fetched data or error - [self loadContentsAsynchronouslyFromUrl:bundleURL.absoluteString - method:@"get" - params:nil - body:nil - queue:nil - progress:nil - completionHandler:^(NSData * _Nullable data, - NSDictionary * _Nullable userInfo, - NSURLResponse * _Nullable response, - NSError * _Nullable error) { + [self loadContentsAsyncFromUrl:bundleURL.absoluteString + params:nil + queue:nil + progress:nil + completionHandler:^(NSData * _Nullable data, + NSDictionary * _Nullable userInfo, + NSURLResponse * _Nullable response, + NSError * _Nullable error) { completion(data, error); }]; } diff --git a/framework/ios/module/imageloader/HippyImageLoaderModule.mm b/framework/ios/module/imageloader/HippyImageLoaderModule.mm index 37926663ad8..4fbee2c178b 100644 --- a/framework/ios/module/imageloader/HippyImageLoaderModule.mm +++ b/framework/ios/module/imageloader/HippyImageLoaderModule.mm @@ -59,13 +59,11 @@ @implementation HippyImageLoaderModule } HIPPY_EXPORT_METHOD(getSize:(NSString *)urlString resolver:(HippyPromiseResolveBlock)resolve rejecter:(HippyPromiseRejectBlock)reject) { - [self.bridge loadContentsAsynchronouslyFromUrl:urlString - method:@"Get" - params:nil - body:nil - queue:nil - progress:nil - completionHandler:^(NSData *data, NSDictionary *userInfo, NSURLResponse *response, NSError *error) { + [self.bridge loadContentsAsyncFromUrl:urlString + params:nil + queue:nil + progress:nil + completionHandler:^(NSData *data, NSDictionary *userInfo, NSURLResponse *response, NSError *error) { if (!error) { id imageProvider = [self imageProviderForData:data]; if (!imageProvider) { diff --git a/framework/ios/module/network/HippyNetWork.mm b/framework/ios/module/network/HippyNetWork.mm index 8c1ddc6430c..5174460302a 100644 --- a/framework/ios/module/network/HippyNetWork.mm +++ b/framework/ios/module/network/HippyNetWork.mm @@ -95,23 +95,29 @@ @implementation HippyNetWork } }]; - NSData *data = nil; - if (body) { - data = [body dataUsingEncoding:NSUTF8StringEncoding]; - } - // Record request start time CFTimeInterval startTime = CACurrentMediaTime(); + + // Construct url request + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:HippyURLWithString(url, nil)]; + if (method) { + [request setHTTPMethod:method]; + } + if (vfsParams) { + for (NSString *key in vfsParams) { + [request setValue:vfsParams[key] forHTTPHeaderField:key]; + } + } + if (body) { + [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]]; + } // Send Request - [self.bridge loadContentsAsynchronouslyFromUrl:url - method:method ?: @"GET" - params:vfsParams - body:data - queue:nil - progress:nil - completionHandler:^(NSData *data, NSDictionary *userInfo, - NSURLResponse *response, NSError *error) { + [self.bridge loadContentsAsyncWithRequest:request + queue:nil + progress:nil + completionHandler:^(NSData *data, NSDictionary *userInfo, + NSURLResponse *response, NSError *error) { NSStringEncoding encoding = GetStringEncodingFromURLResponse(response); NSString *dataStr = [[NSString alloc] initWithData:data encoding:encoding]; NSUInteger statusCode = 0; diff --git a/modules/vfs/ios/HippyBridge+VFSLoader.h b/modules/vfs/ios/HippyBridge+VFSLoader.h index 3fb132e9b5c..508db3ee8b1 100644 --- a/modules/vfs/ios/HippyBridge+VFSLoader.h +++ b/modules/vfs/ios/HippyBridge+VFSLoader.h @@ -25,15 +25,34 @@ NS_ASSUME_NONNULL_BEGIN +/// Category of HippyBridge responsible for loading data @interface HippyBridge (VFSLoader) -- (void)loadContentsAsynchronouslyFromUrl:(NSString *)urlString - method:(NSString *_Nullable)method - params:(NSDictionary *_Nullable)httpHeaders - body:(NSData *_Nullable)body - queue:(NSOperationQueue *_Nullable)queue - progress:(VFSHandlerProgressBlock _Nullable)progress - completionHandler:(VFSHandlerCompletionBlock)completionHandler; +/// Load data from url (GET) +/// - Parameters: +/// - urlString: request url +/// - httpHeaders: http headers, optional +/// - queue: operation queue, optional +/// - progress: progress callback, optional +/// - completionHandler: completion callback +- (void)loadContentsAsyncFromUrl:(NSString *)urlString + params:(nullable NSDictionary *)httpHeaders + queue:(nullable NSOperationQueue *)queue + progress:(nullable VFSHandlerProgressBlock)progress + completionHandler:(VFSHandlerCompletionBlock)completionHandler; + + +/// Load data using given request +/// - Parameters: +/// - request: URLRequest +/// - queue: operation queue, optional +/// - progress: progress callback, optional +/// - completionHandler: completion callback +- (void)loadContentsAsyncWithRequest:(NSURLRequest *)request + queue:(nullable NSOperationQueue *)queue + progress:(nullable VFSHandlerProgressBlock)progress + completionHandler:(VFSHandlerCompletionBlock)completionHandler; + @end NS_ASSUME_NONNULL_END diff --git a/modules/vfs/ios/HippyBridge+VFSLoader.mm b/modules/vfs/ios/HippyBridge+VFSLoader.mm index 4c2de791b7b..a3f13d55d5d 100644 --- a/modules/vfs/ios/HippyBridge+VFSLoader.mm +++ b/modules/vfs/ios/HippyBridge+VFSLoader.mm @@ -29,13 +29,11 @@ @implementation HippyBridge (VFSLoader) -- (void)loadContentsAsynchronouslyFromUrl:(NSString *)urlString - method:(NSString *_Nullable)method - params:(NSDictionary *)httpHeaders - body:(NSData *)body - queue:(NSOperationQueue *_Nullable)queue - progress:(VFSHandlerProgressBlock)progress - completionHandler:(VFSHandlerCompletionBlock)completionHandler { +- (void)loadContentsAsyncFromUrl:(NSString *)urlString + params:(NSDictionary *)httpHeaders + queue:(NSOperationQueue *)queue + progress:(VFSHandlerProgressBlock)progress + completionHandler:(VFSHandlerCompletionBlock)completionHandler { if (!urlString || !completionHandler) { return; } @@ -43,17 +41,24 @@ - (void)loadContentsAsynchronouslyFromUrl:(NSString *)urlString if (loader) { NSURL *url = HippyURLWithString(urlString, nil); NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; - if (method) { - [request setHTTPMethod:method]; - } if (httpHeaders) { for (NSString *key in httpHeaders) { [request setValue:httpHeaders[key] forHTTPHeaderField:key]; } } - if (body) { - [request setHTTPBody:body]; - } + loader->RequestUntrustedContent(request, nil, queue, progress, completionHandler); + } +} + +- (void)loadContentsAsyncWithRequest:(NSURLRequest *)request + queue:(NSOperationQueue *_Nullable)queue + progress:(VFSHandlerProgressBlock)progress + completionHandler:(VFSHandlerCompletionBlock)completionHandler { + if (!request || !completionHandler) { + return; + } + std::shared_ptr loader = [self vfsUriLoader].lock(); + if (loader) { loader->RequestUntrustedContent(request, nil, queue, progress, completionHandler); } }