Encode the style URL in iOS (#2965)

This commit is contained in:
qqz003
2024-10-30 19:34:23 +08:00
committed by GitHub
parent 75ed1596bd
commit d140d5e165
2 changed files with 14 additions and 2 deletions

View File

@@ -226,8 +226,9 @@ BOOL isValidMapboxEndpoint(NSURL *url) {
MLN_APPLE_EXPORT
NSURL *resourceURL(const Resource& resource) {
NSURL *url = [NSURL URLWithString:@(resource.url.c_str())];
NSString *encodedUrlString = [@(resource.url.c_str()) stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];
NSURL *url = [NSURL URLWithString:encodedUrlString];
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
if (isValidMapboxEndpoint(url)) {
NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];

View File

@@ -86,4 +86,15 @@ namespace mbgl {
[self internalTestOfflineQueryParameterIsAddedForOfflineResource:testURL];
}
- (void)testResourceURL {
using namespace mbgl;
// Test URL with characters requiring encoding
Resource resource(Resource::Kind::Unknown, "https://example.com/test?param1=a|b&param2=c|d");
NSURL *url = resourceURL(resource);
XCTAssertNotNil(url);
XCTAssertEqualObjects(url.absoluteString, @"https://example.com/test?param1=a%7Cb&param2=c%7Cd");
}
@end