Skip to content

Commit

Permalink
refactor(ios): increase robustness of HippyUrl process
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed May 14, 2024
1 parent 0d3d6e0 commit a18ea5a
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 14 deletions.
1 change: 1 addition & 0 deletions driver/js/src/napi/jsc/jsc_ctx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,7 @@ std::shared_ptr<CtxValue> JSCCtx::RunScript(const string_view& data,

if (exception) {
SetException(std::make_shared<JSCCtxValue>(context_, exception));
FOOTSTONE_LOG(ERROR) << GetExceptionMessage(exception_);
return nullptr;
}

Expand Down
4 changes: 2 additions & 2 deletions framework/ios/base/bridge/HippyBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);
///
/// Note: 多个bridge使用相同的共享engineKey时,只有全部bridge实例销毁时engine资源才将释放,因此,请注意合理使用,避免出现意外的内存泄漏。
/// 传空时默认不共享,SDK内部默认分配一随机key。
- (instancetype)initWithDelegate:(id<HippyBridgeDelegate>)delegate
- (instancetype)initWithDelegate:(nullable id<HippyBridgeDelegate>)delegate
moduleProvider:(nullable HippyBridgeModuleProviderBlock)block
launchOptions:(nullable NSDictionary *)launchOptions
executorKey:(nullable NSString *)executorKey;
Expand All @@ -112,7 +112,7 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);
///
/// Note: 多个bridge使用相同的共享engineKey时,只有全部bridge实例销毁时engine资源才将释放,因此,请注意合理使用,避免出现意外的内存泄漏。
/// 传空时默认不共享,SDK内部默认分配一随机key。
- (instancetype)initWithDelegate:(id<HippyBridgeDelegate>)delegate
- (instancetype)initWithDelegate:(nullable id<HippyBridgeDelegate>)delegate
bundleURL:(nullable NSURL *)bundleURL
moduleProvider:(nullable HippyBridgeModuleProviderBlock)block
launchOptions:(nullable NSDictionary *)launchOptions
Expand Down
1 change: 1 addition & 0 deletions hippy.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ Pod::Spec.new do |s|
'GCC_ENABLE_CPP_EXCEPTIONS' => false,
'GCC_ENABLE_CPP_RTTI' => false,
}
iosvfs.dependency 'hippy/Base'
iosvfs.dependency 'hippy/VFS'
iosvfs.dependency 'hippy/Footstone'
iosvfs.dependency 'hippy/FootstoneUtils'
Expand Down
10 changes: 10 additions & 0 deletions modules/ios/base/HippyDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ method NS_UNAVAILABLE {
_Pragma("clang diagnostic pop")


#pragma mark - Clang Warnings

// warning list ref:https://clang.llvm.org/docs/DiagnosticsReference.html
#define HIPPY_CLANG_WARN_CONCAT(warning_name) HIPPY_STR_EXPAND(clang diagnostic ignored warning_name)
#define HIPPY_IGNORE_WARNING_BEGIN(warningName) _Pragma("clang diagnostic push") _Pragma(HIPPY_CLANG_WARN_CONCAT(#warningName))
#define HIPPY_IGNORE_WARNING_END _Pragma("clang diagnostic pop")


#pragma mark -

#define HIPPY_VERSION_3_0 300


Expand Down
10 changes: 1 addition & 9 deletions modules/ios/base/HippyUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -438,15 +438,7 @@ static void HPGetRGBAColorComponents(CGColorRef color, CGFloat rgba[4]) {
if (nil == uriData) {
return nil;
}
CFURLRef urlRef = NULL;
if ([URLString hasPrefix:@"http"] ||
[URLString hasPrefix:@"data:"] ||
[URLString hasPrefix:@"file:"]) {
urlRef = CFURLCreateWithBytes(NULL, [uriData bytes], [uriData length], kCFStringEncodingUTF8, (__bridge CFURLRef)baseURL);
}
else {
urlRef = CFURLCreateWithFileSystemPath(NULL, (__bridge CFStringRef)URLString, kCFURLPOSIXPathStyle, NO);
}
CFURLRef urlRef = CFURLCreateWithBytes(NULL, [uriData bytes], [uriData length], kCFStringEncodingUTF8, (__bridge CFURLRef)baseURL);
NSURL *source_url = CFBridgingRelease(urlRef);
return source_url;
}
Expand Down
7 changes: 4 additions & 3 deletions modules/vfs/ios/VFSUriLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
#import "TypeConverter.h"
#import "VFSUriLoader.h"
#import "VFSUriHandler.h"

#import "HippyAssert.h"
#include <functional>
#include <unordered_map>

#include "footstone/string_view_utils.h"

NSString *const VFSErrorDomain = @"VFSErrorDomain";
Expand Down Expand Up @@ -91,7 +90,9 @@
NSOperationQueue *operationQueue,
VFSHandlerProgressBlock progress,
VFSHandlerCompletionBlock completion) {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSURL *url = HippyURLWithString(urlString, nil);
HippyAssert(url, @"Invalid URL! %@", urlString);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
RequestUntrustedContent(request, extraInfo, operationQueue, progress, completion);
}

Expand Down
90 changes: 90 additions & 0 deletions tests/ios/HippyUtilsTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*!
* iOS SDK
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <XCTest/XCTest.h>
#import <hippy/HippyUtils.h>

@interface HippyUtilsTest : XCTestCase

@end

@implementation HippyUtilsTest

- (void)setUp {
// Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

- (void)testSDKVersionExists {
NSString *ver = [HippyUtils sdkVersion];
XCTAssertNotNil(ver);
}

- (void)testHippyURLWithString {
HIPPY_IGNORE_WARNING_BEGIN(-Wnonnull)
XCTAssertNil(HippyURLWithString(nil, nil));
HIPPY_IGNORE_WARNING_END
XCTAssert([[HippyURLWithString(@"", nil) absoluteString] length] == 0);

NSArray *testPaths = @[
@"http://hippyjs.org",
@"https://hippyjs.org",
@"file:///testAbsulotePath/subPath",
@"hpfile://./testHippyRelativePath/subPath",
@"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAA",
// Some exceptions, such as Spaces or newlines
@"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAA\n\n ",
];
for (NSString *path in testPaths) {
NSURL *url = HippyURLWithString(path, nil);
XCTAssertNotNil(url);
NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
XCTAssertNotNil(components.scheme);
XCTAssertNotNil(components.path);
}
testPaths = @[
@"测试中文",
];
for (NSString *path in testPaths) {
NSURL *url = HippyURLWithString(path, nil);
XCTAssertNotNil(url);
NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
XCTAssertNil(components.scheme);
XCTAssertNotNil(components.path);
}

NSString *baseUrl = @"https://hippyjs.org/#/";
testPaths = @[
@"hello/hippy",
];
for (NSString *path in testPaths) {
NSURL *url = HippyURLWithString(path, baseUrl);
XCTAssert([url.absoluteString isEqualToString:@"https://hippyjs.org/hello/hippy"]);
}

}


@end

0 comments on commit a18ea5a

Please sign in to comment.