Skip to content

Commit

Permalink
Encode the style URL in iOS (#2965)
Browse files Browse the repository at this point in the history
  • Loading branch information
qqz003 authored Oct 30, 2024
1 parent 75ed159 commit d140d5e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions platform/darwin/src/http_file_source.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
11 changes: 11 additions & 0 deletions platform/darwin/test/MLNResourceTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,15 @@ - (void)testOfflineQueryParameterIsAddedForOfflineResourceForChina {
[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

0 comments on commit d140d5e

Please sign in to comment.