@@ -41,7 +41,7 @@ open class HTTPCookieStorage: NSObject {
41
41
private static let sharedSyncQ = DispatchQueue ( label: " org.swift.HTTPCookieStorage.sharedSyncQ " )
42
42
43
43
/* only modified in init */
44
- private var cookieFilePath : String !
44
+ private var cookieFilePath : String ?
45
45
46
46
/* synchronized on syncQ, please don't use _allCookies directly outside of init/deinit */
47
47
private var _allCookies : [ String : HTTPCookie ]
@@ -61,22 +61,27 @@ open class HTTPCookieStorage: NSObject {
61
61
}
62
62
private let syncQ = DispatchQueue ( label: " org.swift.HTTPCookieStorage.syncQ " )
63
63
64
- private init ( cookieStorageName: String ) {
64
+ private let isEphemeral : Bool
65
+
66
+ private init ( cookieStorageName: String , isEphemeral: Bool = false ) {
65
67
_allCookies = [ : ]
66
68
cookieAcceptPolicy = . always
69
+ self . isEphemeral = isEphemeral
67
70
super. init ( )
68
- let bundlePath = Bundle . main. bundlePath
69
- var bundleName = bundlePath. components ( separatedBy: " / " ) . last!
70
- if let range = bundleName. range ( of: " . " , options: . backwards, range: nil , locale: nil ) {
71
- bundleName = String ( bundleName [ ..< range. lowerBound] )
71
+ if !isEphemeral {
72
+ let bundlePath = Bundle . main. bundlePath
73
+ var bundleName = bundlePath. components ( separatedBy: " / " ) . last!
74
+ if let range = bundleName. range ( of: " . " , options: . backwards, range: nil , locale: nil ) {
75
+ bundleName = String ( bundleName [ ..< range. lowerBound] )
76
+ }
77
+ let cookieFolderPath = _CFXDGCreateDataHomePath ( ) . _swiftObject + " / " + bundleName
78
+ cookieFilePath = filePath ( path: cookieFolderPath, fileName: " /.cookies. " + cookieStorageName, bundleName: bundleName)
79
+ loadPersistedCookies ( )
72
80
}
73
- let cookieFolderPath = _CFXDGCreateDataHomePath ( ) . _swiftObject + " / " + bundleName
74
- cookieFilePath = filePath ( path: cookieFolderPath, fileName: " /.cookies. " + cookieStorageName, bundleName: bundleName)
75
- loadPersistedCookies ( )
76
81
}
77
82
78
83
private func loadPersistedCookies( ) {
79
- guard let cookiesData = try ? Data ( contentsOf: URL ( fileURLWithPath: cookieFilePath) ) else { return }
84
+ guard let cookieFilePath = self . cookieFilePath , let cookiesData = try ? Data ( contentsOf: URL ( fileURLWithPath: cookieFilePath) ) else { return }
80
85
guard let cookies = try ? PropertyListSerialization . propertyList ( from: cookiesData, format: nil ) else { return }
81
86
var cookies0 = cookies as? [ String : [ String : Any ] ] ?? [ : ]
82
87
self . syncQ. sync {
@@ -108,6 +113,12 @@ open class HTTPCookieStorage: NSObject {
108
113
return FileManager . default. currentDirectoryPath + " / " + bundleName + fileName
109
114
}
110
115
116
+ // `URLSessionConfiguration.ephemeral` needs an ephemeral cookie storage.
117
+ // Ephemeral cookie storage is an in-memory store and does not load from, and store to, a persistent store.
118
+ internal class func ephemeralStorage( ) -> HTTPCookieStorage {
119
+ return HTTPCookieStorage ( cookieStorageName: " Ephemeral " , isEphemeral: true )
120
+ }
121
+
111
122
open var cookies : [ HTTPCookie ] ? {
112
123
return Array ( self . syncQ. sync { self . allCookies. values } )
113
124
}
@@ -122,7 +133,7 @@ open class HTTPCookieStorage: NSObject {
122
133
open class var shared : HTTPCookieStorage {
123
134
return sharedStorage
124
135
}
125
-
136
+
126
137
/*!
127
138
@method sharedCookieStorageForGroupContainerIdentifier:
128
139
@abstract Get the cookie storage for the container associated with the specified application group identifier
@@ -175,7 +186,7 @@ open class HTTPCookieStorage: NSObject {
175
186
}
176
187
177
188
open override var description : String {
178
- return " <NSHTTPCookieStorage cookies count: \( cookies? . count ?? 0 ) > "
189
+ return " \( self . isEphemeral ? " Ephemeral " : " " ) <NSHTTPCookieStorage cookies count: \( cookies? . count ?? 0 ) > "
179
190
}
180
191
181
192
private func createCookie( _ properties: [ String : Any ] ) -> HTTPCookie ? {
@@ -192,6 +203,11 @@ open class HTTPCookieStorage: NSObject {
192
203
}
193
204
194
205
private func updatePersistentStore( ) {
206
+ // No persistence if this is an ephemeral storage
207
+ if self . isEphemeral { return }
208
+
209
+ guard let cookieFilePath = self . cookieFilePath else { return }
210
+
195
211
if #available( macOS 10 . 12 , iOS 10 . 0 , tvOS 10 . 0 , watchOS 3 . 0 , * ) {
196
212
dispatchPrecondition ( condition: DispatchPredicate . onQueue ( self . syncQ) )
197
213
}
0 commit comments