@@ -201,6 +201,7 @@ open class URLCache : NSObject {
201
201
self . memoryCapacity = memoryCapacity
202
202
self . diskCapacity = diskCapacity
203
203
204
+ // As per the function of URLCache, `diskCapacity` of `0` is assumed as no-persistence.
204
205
if diskCapacity > 0 , let _path = path {
205
206
self . persistence = _CachePersistence ( path: _path)
206
207
}
@@ -322,17 +323,13 @@ fileprivate struct _CachePersistence {
322
323
}
323
324
324
325
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 )
330
327
}
331
328
332
329
func saveCachedResponse( _ response: CachedURLResponse , for request: URLRequest ) {
333
- let archivedData = NSKeyedArchiver . archivedData ( withRootObject: response)
334
330
do {
335
- if let fileIdentifier = request. cacheFileIdentifier {
331
+ if let archivedData = try ? NSKeyedArchiver . archivedData ( withRootObject: response, requiringSecureCoding: true ) ,
332
+ let fileIdentifier = request. cacheFileIdentifier {
336
333
let cacheFileURL = urlForFileIdentifier ( fileIdentifier)
337
334
try archivedData. write ( to: cacheFileURL)
338
335
}
@@ -348,7 +345,7 @@ fileprivate struct _CachePersistence {
348
345
349
346
let url = urlForFileIdentifier ( fileIdentifier)
350
347
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 {
352
349
return nil
353
350
}
354
351
@@ -367,23 +364,28 @@ extension URLRequest {
367
364
guard let scheme = self . url? . scheme, scheme == " http " || scheme == " https " ,
368
365
let method = httpMethod, !method. isEmpty,
369
366
let urlString = url? . absoluteString else {
370
- return nil
367
+ return nil
371
368
}
372
369
373
- var hashString = " \( scheme) _ \( method) _ \( urlString) "
370
+ var hashString = " \( abs ( method. hashValue) ) - \( abs ( urlString. hashValue) ) "
371
+
374
372
if let userAgent = self . allHTTPHeaderFields ? [ " User-Agent " ] , !userAgent. isEmpty {
375
- hashString. append ( contentsOf : " _ \( userAgent) " )
373
+ hashString. append ( " \( abs ( userAgent. hashValue ) ) " )
376
374
}
377
375
378
376
if let acceptLanguage = self . allHTTPHeaderFields ? [ " Accept-Language " ] , !acceptLanguage. isEmpty {
379
- hashString. append ( contentsOf : " _ \( acceptLanguage) " )
377
+ hashString. append ( " - \( abs ( acceptLanguage. hashValue ) ) " )
380
378
}
381
379
382
380
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) ) " )
384
386
}
385
387
386
- return String ( format : " %X " , hashString. hashValue )
388
+ return hashString
387
389
}
388
390
389
391
}
0 commit comments