Skip to content

Commit b5dafd2

Browse files
committed
Secure archive and unarchive used for cache url response and cacheFileIdentifier hash mechanism updated.
1 parent ec38806 commit b5dafd2

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

Foundation/URLCache.swift

+16-14
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ open class URLCache : NSObject {
201201
self.memoryCapacity = memoryCapacity
202202
self.diskCapacity = diskCapacity
203203

204+
// As per the function of URLCache, `diskCapacity` of `0` is assumed as no-persistence.
204205
if diskCapacity > 0, let _path = path {
205206
self.persistence = _CachePersistence(path: _path)
206207
}
@@ -322,17 +323,13 @@ fileprivate struct _CachePersistence {
322323
}
323324

324325
func setupCacheDirectory() {
325-
do {
326-
try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true)
327-
} catch {
328-
fatalError("Unable to create directories for URLCache: \(error.localizedDescription)")
329-
}
326+
try? FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true)
330327
}
331328

332329
func saveCachedResponse(_ response: CachedURLResponse, for request: URLRequest) {
333-
let archivedData = NSKeyedArchiver.archivedData(withRootObject: response)
334330
do {
335-
if let fileIdentifier = request.cacheFileIdentifier {
331+
if let archivedData = try? NSKeyedArchiver.archivedData(withRootObject: response, requiringSecureCoding: true),
332+
let fileIdentifier = request.cacheFileIdentifier {
336333
let cacheFileURL = urlForFileIdentifier(fileIdentifier)
337334
try archivedData.write(to: cacheFileURL)
338335
}
@@ -348,7 +345,7 @@ fileprivate struct _CachePersistence {
348345

349346
let url = urlForFileIdentifier(fileIdentifier)
350347
guard let data = try? Data(contentsOf: url),
351-
let response = NSKeyedUnarchiver.unarchiveObject(with: data) as? CachedURLResponse else {
348+
let response = try? NSKeyedUnarchiver.unarchivedObject(ofClasses:[CachedURLResponse.self], from: data) as? CachedURLResponse else {
352349
return nil
353350
}
354351

@@ -367,23 +364,28 @@ extension URLRequest {
367364
guard let scheme = self.url?.scheme, scheme == "http" || scheme == "https",
368365
let method = httpMethod, !method.isEmpty,
369366
let urlString = url?.absoluteString else {
370-
return nil
367+
return nil
371368
}
372369

373-
var hashString = "\(scheme)_\(method)_\(urlString)"
370+
var hashString = "\(abs(method.hashValue))-\(abs(urlString.hashValue))"
371+
374372
if let userAgent = self.allHTTPHeaderFields?["User-Agent"], !userAgent.isEmpty {
375-
hashString.append(contentsOf: "_\(userAgent)")
373+
hashString.append("\(abs(userAgent.hashValue))")
376374
}
377375

378376
if let acceptLanguage = self.allHTTPHeaderFields?["Accept-Language"], !acceptLanguage.isEmpty {
379-
hashString.append(contentsOf: "_\(acceptLanguage)")
377+
hashString.append("-\(abs(acceptLanguage.hashValue))")
380378
}
381379

382380
if let range = self.allHTTPHeaderFields?["Range"], !range.isEmpty {
383-
hashString.append(contentsOf: "_\(range)")
381+
hashString.append("-\(abs(range.hashValue))")
382+
}
383+
384+
if let data = self.httpBody, !data.isEmpty {
385+
hashString.append("-\(abs(data.hashValue))")
384386
}
385387

386-
return String(format: "%X", hashString.hashValue)
388+
return hashString
387389
}
388390

389391
}

0 commit comments

Comments
 (0)