|
7 | 7 | #import <Lynx/LynxViewBuilder.h> |
8 | 8 |
|
9 | 9 | static NSString *const OrbitResourceErrorDomain = @"org.modulefederation.lynx.resources"; |
| 10 | +static NSUInteger const OrbitResourcePathCacheLimit = 64; |
10 | 11 |
|
11 | 12 | @interface OrbitResourceFetcher () |
12 | 13 |
|
| 14 | +@property(nonatomic, strong) NSMutableDictionary<NSString *, NSString *> *resourcePathCache; |
| 15 | +@property(nonatomic, strong) NSMutableArray<NSString *> *resourcePathCacheOrder; |
| 16 | +@property(nonatomic, strong) NSURL *resourceCacheDirectory; |
| 17 | + |
13 | 18 | - (dispatch_block_t)loadDataForURLString:(NSString *)urlString |
14 | 19 | completion:(void (^)(NSData *_Nullable, |
15 | 20 | NSError *_Nullable))completion; |
16 | 21 | - (NSURL *_Nullable)localURLForString:(NSString *)urlString; |
| 22 | +- (BOOL)isAllowedLocalURL:(NSURL *)url; |
| 23 | +- (NSString *_Nullable)cachedResourcePathForURLString:(NSString *)urlString; |
| 24 | +- (NSString *_Nullable)storeResourceData:(NSData *)data |
| 25 | + forURLString:(NSString *)urlString |
| 26 | + error:(NSError **)error; |
17 | 27 | - (NSError *)errorWithMessage:(NSString *)message; |
18 | 28 |
|
19 | 29 | @end |
20 | 30 |
|
21 | 31 | @implementation OrbitResourceFetcher |
22 | 32 |
|
| 33 | +- (instancetype)init { |
| 34 | + self = [super init]; |
| 35 | + if (self) { |
| 36 | + _resourcePathCache = [NSMutableDictionary dictionary]; |
| 37 | + _resourcePathCacheOrder = [NSMutableArray array]; |
| 38 | + _resourceCacheDirectory = [[[NSURL fileURLWithPath:NSTemporaryDirectory() |
| 39 | + isDirectory:YES] |
| 40 | + URLByAppendingPathComponent:@"OrbitResources" |
| 41 | + isDirectory:YES] |
| 42 | + URLByAppendingPathComponent:NSUUID.UUID.UUIDString |
| 43 | + isDirectory:YES]; |
| 44 | + } |
| 45 | + return self; |
| 46 | +} |
| 47 | + |
| 48 | +- (void)dealloc { |
| 49 | + [[NSFileManager defaultManager] removeItemAtURL:self.resourceCacheDirectory error:nil]; |
| 50 | +} |
| 51 | + |
23 | 52 | - (void)configureBuilder:(LynxViewBuilder *)builder { |
24 | 53 | builder.enableGenericResourceFetcher = LynxBooleanOptionTrue; |
25 | 54 | builder.genericResourceFetcher = self; |
@@ -61,33 +90,73 @@ - (dispatch_block_t)fetchResourcePath:(LynxResourceRequest *)request |
61 | 90 | return ^{}; |
62 | 91 | } |
63 | 92 |
|
| 93 | + NSString *cachedPath = [self cachedResourcePathForURLString:request.url]; |
| 94 | + if (cachedPath) { |
| 95 | + callback(cachedPath, nil); |
| 96 | + return ^{}; |
| 97 | + } |
| 98 | + |
64 | 99 | return [self loadDataForURLString:request.url |
65 | 100 | completion:^(NSData *data, NSError *error) { |
66 | 101 | if (!data) { |
67 | 102 | callback(nil, error); |
68 | 103 | return; |
69 | 104 | } |
70 | 105 |
|
71 | | - NSURL *directory = [NSURL fileURLWithPath:NSTemporaryDirectory() |
72 | | - isDirectory:YES]; |
73 | | - directory = [directory URLByAppendingPathComponent:@"OrbitResources" |
74 | | - isDirectory:YES]; |
75 | 106 | NSError *writeError = nil; |
76 | | - [[NSFileManager defaultManager] createDirectoryAtURL:directory |
77 | | - withIntermediateDirectories:YES |
78 | | - attributes:nil |
79 | | - error:&writeError]; |
80 | | - NSURL *fileURL = [directory URLByAppendingPathComponent: |
81 | | - [NSUUID UUID].UUIDString]; |
82 | | - if (!writeError && [data writeToURL:fileURL options:NSDataWritingAtomic |
83 | | - error:&writeError]) { |
84 | | - callback(fileURL.path, nil); |
85 | | - } else { |
86 | | - callback(nil, writeError); |
87 | | - } |
| 107 | + NSString *path = [self storeResourceData:data |
| 108 | + forURLString:request.url |
| 109 | + error:&writeError]; |
| 110 | + callback(path, writeError); |
88 | 111 | }]; |
89 | 112 | } |
90 | 113 |
|
| 114 | +- (NSString *_Nullable)cachedResourcePathForURLString:(NSString *)urlString { |
| 115 | + @synchronized(self) { |
| 116 | + return self.resourcePathCache[urlString]; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +- (NSString *_Nullable)storeResourceData:(NSData *)data |
| 121 | + forURLString:(NSString *)urlString |
| 122 | + error:(NSError **)error { |
| 123 | + NSString *cachedPath = [self cachedResourcePathForURLString:urlString]; |
| 124 | + if (cachedPath) return cachedPath; |
| 125 | + |
| 126 | + [[NSFileManager defaultManager] createDirectoryAtURL:self.resourceCacheDirectory |
| 127 | + withIntermediateDirectories:YES |
| 128 | + attributes:nil |
| 129 | + error:error]; |
| 130 | + if (*error) return nil; |
| 131 | + |
| 132 | + NSString *extension = [NSURL URLWithString:urlString].pathExtension; |
| 133 | + NSString *filename = NSUUID.UUID.UUIDString; |
| 134 | + if (extension.length > 0) { |
| 135 | + filename = [filename stringByAppendingPathExtension:extension]; |
| 136 | + } |
| 137 | + NSURL *fileURL = [self.resourceCacheDirectory URLByAppendingPathComponent:filename]; |
| 138 | + if (![data writeToURL:fileURL options:NSDataWritingAtomic error:error]) return nil; |
| 139 | + |
| 140 | + @synchronized(self) { |
| 141 | + NSString *existingPath = self.resourcePathCache[urlString]; |
| 142 | + if (existingPath) { |
| 143 | + [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil]; |
| 144 | + return existingPath; |
| 145 | + } |
| 146 | + |
| 147 | + self.resourcePathCache[urlString] = fileURL.path; |
| 148 | + [self.resourcePathCacheOrder addObject:urlString]; |
| 149 | + if (self.resourcePathCacheOrder.count > OrbitResourcePathCacheLimit) { |
| 150 | + NSString *expiredURL = self.resourcePathCacheOrder.firstObject; |
| 151 | + NSString *expiredPath = self.resourcePathCache[expiredURL]; |
| 152 | + [self.resourcePathCacheOrder removeObjectAtIndex:0]; |
| 153 | + [self.resourcePathCache removeObjectForKey:expiredURL]; |
| 154 | + [[NSFileManager defaultManager] removeItemAtPath:expiredPath error:nil]; |
| 155 | + } |
| 156 | + } |
| 157 | + return fileURL.path; |
| 158 | +} |
| 159 | + |
91 | 160 | - (dispatch_block_t)loadDataForURLString:(NSString *)urlString |
92 | 161 | completion:(void (^)(NSData *_Nullable, |
93 | 162 | NSError *_Nullable))completion { |
@@ -139,23 +208,47 @@ - (dispatch_block_t)loadDataForURLString:(NSString *)urlString |
139 | 208 | - (NSURL *_Nullable)localURLForString:(NSString *)urlString { |
140 | 209 | NSURL *url = [NSURL URLWithString:urlString]; |
141 | 210 | if ([url.scheme isEqualToString:@"file"]) { |
142 | | - return url; |
| 211 | + return [self isAllowedLocalURL:url] ? url : nil; |
143 | 212 | } |
144 | 213 | if ([url.scheme isEqualToString:@"http"] || |
145 | 214 | [url.scheme isEqualToString:@"https"]) { |
146 | 215 | return nil; |
147 | 216 | } |
148 | 217 | if ([urlString isAbsolutePath]) { |
149 | | - return [NSURL fileURLWithPath:urlString]; |
| 218 | + NSURL *fileURL = [NSURL fileURLWithPath:urlString]; |
| 219 | + return [self isAllowedLocalURL:fileURL] ? fileURL : nil; |
150 | 220 | } |
151 | 221 |
|
152 | | - NSString *filename = urlString.lastPathComponent; |
| 222 | + NSString *relativePath = url.path.length > 0 ? url.path : urlString; |
| 223 | + relativePath = relativePath.stringByStandardizingPath; |
| 224 | + if (relativePath.length == 0 || [relativePath isEqualToString:@"."] || |
| 225 | + [relativePath isEqualToString:@".."] || |
| 226 | + [relativePath hasPrefix:@"../"]) { |
| 227 | + return nil; |
| 228 | + } |
| 229 | + NSString *filename = relativePath.lastPathComponent; |
| 230 | + NSString *subdirectory = relativePath.stringByDeletingLastPathComponent; |
153 | 231 | NSString *extension = filename.pathExtension; |
154 | 232 | NSString *name = extension.length > 0 |
155 | 233 | ? [filename stringByDeletingPathExtension] |
156 | 234 | : filename; |
157 | 235 | return [[NSBundle mainBundle] URLForResource:name |
158 | | - withExtension:extension.length > 0 ? extension : nil]; |
| 236 | + withExtension:extension.length > 0 ? extension : nil |
| 237 | + subdirectory:[subdirectory isEqualToString:@"."] |
| 238 | + ? nil |
| 239 | + : subdirectory]; |
| 240 | +} |
| 241 | + |
| 242 | +- (BOOL)isAllowedLocalURL:(NSURL *)url { |
| 243 | + NSString *path = url.URLByStandardizingPath.URLByResolvingSymlinksInPath.path; |
| 244 | + for (NSURL *directory in @[[NSBundle mainBundle].bundleURL, |
| 245 | + self.resourceCacheDirectory]) { |
| 246 | + NSString *directoryPath = |
| 247 | + directory.URLByStandardizingPath.URLByResolvingSymlinksInPath.path; |
| 248 | + NSString *prefix = [directoryPath stringByAppendingString:@"/"]; |
| 249 | + if ([path hasPrefix:prefix]) return YES; |
| 250 | + } |
| 251 | + return NO; |
159 | 252 | } |
160 | 253 |
|
161 | 254 | - (NSError *)errorWithMessage:(NSString *)message { |
|
0 commit comments