Skip to content

[3.1]Cherry picking Separate cookieStorage by application #1019

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 30, 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!
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
}

open var cookies: [HTTPCookie]? {
Expand Down
13 changes: 8 additions & 5 deletions TestFoundation/TestNSHTTPCookieStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,19 +227,22 @@ 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"
}
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
var pathIndex = bundlePath.range(of: "/", options: .backwards)?.lowerBound
let pathIndex = bundlePath.range(of: "/", options: .backwards)?.lowerBound
let task = Process()
task.launchPath = bundlePath.substring(to: pathIndex!) + "/xdgTestHelper/xdgTestHelper"
var environment = ProcessInfo.processInfo.environment
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