From 8a406e38292e67ef116191261d968131a5db3f5c Mon Sep 17 00:00:00 2001 From: Edwin Vermeer Date: Wed, 13 Jul 2016 20:18:57 +0200 Subject: [PATCH] fix for data url --- EVURLCache.podspec | 2 +- EVURLCache/Pod/EVURLCache.swift | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/EVURLCache.podspec b/EVURLCache.podspec index 61555ed..5d67383 100644 --- a/EVURLCache.podspec +++ b/EVURLCache.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| # s.name = "EVURLCache" -s.version = "2.8.0" +s.version = "2.9.0" s.summary = "NSURLCache subclass for handeling all web requests that use NSURLRequest" s.description = "This is a NSURLCache subclass for handeling all web requests that use NSURLRequest. (This includes UIWebView)" s.homepage = "https://github.com/evermeer/EVURLCache" diff --git a/EVURLCache/Pod/EVURLCache.swift b/EVURLCache/Pod/EVURLCache.swift index f529448..4050f27 100644 --- a/EVURLCache/Pod/EVURLCache.swift +++ b/EVURLCache/Pod/EVURLCache.swift @@ -80,11 +80,10 @@ public class EVURLCache: NSURLCache { return nil } - // Is the file in the cache? If not, is the file in the PreCache? - var storagePath: String = EVURLCache.storagePathForRequest(request, rootPath: EVURLCache._cacheDirectory) + let storagePath = EVURLCache.storagePathForRequest(request, rootPath: EVURLCache._cacheDirectory) ?? "" if !NSFileManager.defaultManager().fileExistsAtPath(storagePath) { EVURLCache.debugLog("PRECACHE not found \(storagePath)") - storagePath = EVURLCache.storagePathForRequest(request, rootPath: EVURLCache._preCacheDirectory) + let storagePath = EVURLCache.storagePathForRequest(request, rootPath: EVURLCache._preCacheDirectory) ?? "" if !NSFileManager.defaultManager().fileExistsAtPath(storagePath) { EVURLCache.debugLog("CACHE not found \(storagePath)") return nil @@ -133,7 +132,7 @@ public class EVURLCache: NSURLCache { // check if caching is allowed if request.cachePolicy == NSURLRequestCachePolicy.ReloadIgnoringCacheData { // If the file is in the PreCache folder, then we do want to save a copy in case we are without internet connection - let storagePath: String = EVURLCache.storagePathForRequest(request, rootPath: EVURLCache._preCacheDirectory) + let storagePath = EVURLCache.storagePathForRequest(request, rootPath: EVURLCache._preCacheDirectory) ?? "" if !NSFileManager.defaultManager().fileExistsAtPath(storagePath) { EVURLCache.debugLog("CACHE not storing file, it's not allowed by the cachePolicy : \(request.URL)") return @@ -142,7 +141,7 @@ public class EVURLCache: NSURLCache { } // create storrage folder - let storagePath: String = EVURLCache.storagePathForRequest(request, rootPath: EVURLCache._cacheDirectory) + let storagePath: String = EVURLCache.storagePathForRequest(request, rootPath: EVURLCache._cacheDirectory) ?? "" if var storageDirectory: String = NSURL(fileURLWithPath: "\(storagePath)").URLByDeletingLastPathComponent?.absoluteString.stringByRemovingPercentEncoding { do { if storageDirectory.hasPrefix("file:") { @@ -207,10 +206,15 @@ public class EVURLCache: NSURLCache { } // build up the complete storrage path for a request plus root folder. - public static func storagePathForRequest(request: NSURLRequest, rootPath: String) -> String { + public static func storagePathForRequest(request: NSURLRequest, rootPath: String) -> String? { var localUrl: String! let host: String = request.URL?.host ?? "default" + let urlString = request.URL?.absoluteString ?? "" + if urlString.hasPrefix("data:") { + return nil + } + // The filename could be forced by the remote server. This could be used to force multiple url's to the same cache file if let cacheKey = request.valueForHTTPHeaderField(URLCACHE_CACHE_KEY) { localUrl = "\(host)/\(cacheKey)" @@ -219,6 +223,7 @@ public class EVURLCache: NSURLCache { localUrl = "\(host)\(path)" } else { NSLog("WARNING: Unable to get the path from the request: \(request)") + return nil } }