-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[SR-4906] Separate cookieStorage by application #1009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,6 @@ open class HTTPCookieStorage: NSObject { | |
|
||
private static var sharedStorage: HTTPCookieStorage? | ||
private static var sharedCookieStorages: [String: HTTPCookieStorage] = [:] //for group storage containers | ||
|
||
private var cookieFilePath: String! | ||
private let workQueue: DispatchQueue = DispatchQueue(label: "HTTPCookieStorage.workqueue") | ||
var allCookies: [String: HTTPCookie] | ||
|
@@ -47,7 +46,13 @@ open class HTTPCookieStorage: NSObject { | |
allCookies = [:] | ||
cookieAcceptPolicy = .always | ||
super.init() | ||
cookieFilePath = filePath(path: _CFXDGCreateDataHomePath()._swiftObject, fileName: "/.cookies." + cookieStorageName) | ||
let bundlePath = Bundle.main.bundlePath | ||
var bundleName = bundlePath.components(separatedBy: "/").last! | ||
if let range = bundleName.range(of: ".", options: String.CompareOptions.backwards, range: nil, locale: nil) { | ||
bundleName = bundleName.substring(to: range.lowerBound) | ||
} | ||
let cookieFolderPath = _CFXDGCreateDataHomePath()._swiftObject + "/" + bundleName | ||
cookieFilePath = filePath(path: cookieFolderPath, fileName: "/.cookies." + cookieStorageName, bundleName: bundleName) | ||
loadPersistedCookies() | ||
} | ||
|
||
|
@@ -72,12 +77,13 @@ open class HTTPCookieStorage: NSObject { | |
} | ||
} | ||
|
||
private func filePath(path: String, fileName: String) -> String { | ||
private func filePath(path: String, fileName: String, bundleName: String) -> String { | ||
if directory(with: path) { | ||
return path + fileName | ||
} | ||
//if we were unable to create the desired directory, create the cookie file in the `pwd` | ||
return FileManager.default.currentDirectoryPath + fileName | ||
//if we were unable to create the desired directory, create the cookie file | ||
//in a subFolder (named after the bundle) of the `pwd` | ||
return FileManager.default.currentDirectoryPath + "/" + bundleName + fileName | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's a subfolder, shouldn't there be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. filename already has "/" prepended. |
||
} | ||
|
||
open var cookies: [HTTPCookie]? { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,18 +227,21 @@ class TestNSHTTPCookieStorage: XCTestCase { | |
storage.setCookie(testCookie) | ||
XCTAssertEqual(storage.cookies!.count, 3) | ||
var destPath: String | ||
let bundlePath = Bundle.main.bundlePath | ||
var bundleName = "/" + bundlePath.components(separatedBy: "/").last! | ||
if let range = bundleName.range(of: ".", options: String.CompareOptions.backwards, range: nil, locale: nil) { | ||
bundleName = bundleName.substring(to: range.lowerBound) | ||
} | ||
if let xdg_data_home = getenv("XDG_DATA_HOME") { | ||
destPath = String(utf8String: xdg_data_home)! + "/.cookies.shared" | ||
destPath = String(utf8String: xdg_data_home)! + bundleName + "/.cookies.shared" | ||
} else { | ||
destPath = NSHomeDirectory() + "/.local/share/.cookies.shared" | ||
destPath = NSHomeDirectory() + "/.local/share" + bundleName + "/.cookies.shared" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't there be an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "/" is appended to the bundleName during initialisation
|
||
} | ||
let fm = FileManager.default | ||
var isDir = false | ||
let exists = fm.fileExists(atPath: destPath, isDirectory: &isDir) | ||
XCTAssertTrue(exists) | ||
//Test by setting the environmental variable | ||
let bundle = Bundle.main | ||
let bundlePath = bundle.bundlePath | ||
let pathIndex = bundlePath.range(of: "/", options: .backwards)?.lowerBound | ||
let task = Process() | ||
task.launchPath = bundlePath.substring(to: pathIndex!) + "/xdgTestHelper/xdgTestHelper" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mamabusi Are we sure this will never have a nil?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pushkarnk Yes, 'bundlePath' is always a non-nil String that will ensure that the value returned by 'bundlePath.components(separatedBy: "/")' can be safely unwrapped.