Skip to content

[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

Merged
merged 1 commit into from
May 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions Foundation/NSHTTPCookieStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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!
Copy link
Member

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?

Copy link
Contributor Author

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.

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()
}

Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's a subfolder, shouldn't there be a / between the bundleName and fileName?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filename already has "/" prepended.

}

open var cookies: [HTTPCookie]? {
Expand Down
11 changes: 7 additions & 4 deletions TestFoundation/TestNSHTTPCookieStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there be an / after /.local/share?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"/" is appended to the bundleName during initialisation

var bundleName = "/" + bundlePath.components(separatedBy: "/").last!

}
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"
Expand Down
2 changes: 1 addition & 1 deletion TestFoundation/XDGTestHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let rawValue = getenv("XDG_DATA_HOME")
let xdg_data_home = String(utf8String: rawValue!)
storage.setCookie(simpleCookie)
let fm = FileManager.default
let destPath = xdg_data_home! + "/.cookies.shared"
let destPath = xdg_data_home! + "/xdgTestHelper/.cookies.shared"
var isDir = false
let exists = fm.fileExists(atPath: destPath, isDirectory: &isDir)
if (!exists) {
Expand Down