Skip to content

Commit a6a4e79

Browse files
authored
Merge pull request #1009 from mamabusi/appSpecificSharedCookies
2 parents 46b4e84 + a660ad4 commit a6a4e79

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

Foundation/NSHTTPCookieStorage.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ open class HTTPCookieStorage: NSObject {
3838

3939
private static var sharedStorage: HTTPCookieStorage?
4040
private static var sharedCookieStorages: [String: HTTPCookieStorage] = [:] //for group storage containers
41-
4241
private var cookieFilePath: String!
4342
private let workQueue: DispatchQueue = DispatchQueue(label: "HTTPCookieStorage.workqueue")
4443
var allCookies: [String: HTTPCookie]
@@ -47,7 +46,13 @@ open class HTTPCookieStorage: NSObject {
4746
allCookies = [:]
4847
cookieAcceptPolicy = .always
4948
super.init()
50-
cookieFilePath = filePath(path: _CFXDGCreateDataHomePath()._swiftObject, fileName: "/.cookies." + cookieStorageName)
49+
let bundlePath = Bundle.main.bundlePath
50+
var bundleName = bundlePath.components(separatedBy: "/").last!
51+
if let range = bundleName.range(of: ".", options: String.CompareOptions.backwards, range: nil, locale: nil) {
52+
bundleName = bundleName.substring(to: range.lowerBound)
53+
}
54+
let cookieFolderPath = _CFXDGCreateDataHomePath()._swiftObject + "/" + bundleName
55+
cookieFilePath = filePath(path: cookieFolderPath, fileName: "/.cookies." + cookieStorageName, bundleName: bundleName)
5156
loadPersistedCookies()
5257
}
5358

@@ -72,12 +77,13 @@ open class HTTPCookieStorage: NSObject {
7277
}
7378
}
7479

75-
private func filePath(path: String, fileName: String) -> String {
80+
private func filePath(path: String, fileName: String, bundleName: String) -> String {
7681
if directory(with: path) {
7782
return path + fileName
7883
}
79-
//if we were unable to create the desired directory, create the cookie file in the `pwd`
80-
return FileManager.default.currentDirectoryPath + fileName
84+
//if we were unable to create the desired directory, create the cookie file
85+
//in a subFolder (named after the bundle) of the `pwd`
86+
return FileManager.default.currentDirectoryPath + "/" + bundleName + fileName
8187
}
8288

8389
open var cookies: [HTTPCookie]? {

TestFoundation/TestNSHTTPCookieStorage.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,18 +227,21 @@ class TestNSHTTPCookieStorage: XCTestCase {
227227
storage.setCookie(testCookie)
228228
XCTAssertEqual(storage.cookies!.count, 3)
229229
var destPath: String
230+
let bundlePath = Bundle.main.bundlePath
231+
var bundleName = "/" + bundlePath.components(separatedBy: "/").last!
232+
if let range = bundleName.range(of: ".", options: String.CompareOptions.backwards, range: nil, locale: nil) {
233+
bundleName = bundleName.substring(to: range.lowerBound)
234+
}
230235
if let xdg_data_home = getenv("XDG_DATA_HOME") {
231-
destPath = String(utf8String: xdg_data_home)! + "/.cookies.shared"
236+
destPath = String(utf8String: xdg_data_home)! + bundleName + "/.cookies.shared"
232237
} else {
233-
destPath = NSHomeDirectory() + "/.local/share/.cookies.shared"
238+
destPath = NSHomeDirectory() + "/.local/share" + bundleName + "/.cookies.shared"
234239
}
235240
let fm = FileManager.default
236241
var isDir = false
237242
let exists = fm.fileExists(atPath: destPath, isDirectory: &isDir)
238243
XCTAssertTrue(exists)
239244
//Test by setting the environmental variable
240-
let bundle = Bundle.main
241-
let bundlePath = bundle.bundlePath
242245
let pathIndex = bundlePath.range(of: "/", options: .backwards)?.lowerBound
243246
let task = Process()
244247
task.launchPath = bundlePath.substring(to: pathIndex!) + "/xdgTestHelper/xdgTestHelper"

TestFoundation/XDGTestHelper.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let rawValue = getenv("XDG_DATA_HOME")
1111
let xdg_data_home = String(utf8String: rawValue!)
1212
storage.setCookie(simpleCookie)
1313
let fm = FileManager.default
14-
let destPath = xdg_data_home! + "/.cookies.shared"
14+
let destPath = xdg_data_home! + "/xdgTestHelper/.cookies.shared"
1515
var isDir = false
1616
let exists = fm.fileExists(atPath: destPath, isDirectory: &isDir)
1717
if (!exists) {

0 commit comments

Comments
 (0)