Skip to content

Commit

Permalink
fix for data url
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Jul 13, 2016
1 parent a594d9b commit 8a406e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion EVURLCache.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 11 additions & 6 deletions EVURLCache/Pod/EVURLCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:") {
Expand Down Expand Up @@ -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)"
Expand All @@ -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
}
}

Expand Down

0 comments on commit 8a406e3

Please sign in to comment.