Skip to content

Commit 3ab5b52

Browse files
authored
Merge pull request swiftlang#2294 from millenomi/urlsession-reset-flush
Parity: URLSession.reset(…)/.flush(…)
2 parents d7af652 + 5e5be77 commit 3ab5b52

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

Diff for: Foundation/URLSession/URLSession.swift

+24-2
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,31 @@ open class URLSession : NSObject {
346346
}
347347
}
348348

349-
open func reset(completionHandler: @escaping () -> Void) { NSUnimplemented() } /* empty all cookies, cache and credential stores, removes disk files, issues -flushWithCompletionHandler:. Invokes completionHandler() on the delegate queue if not nil. */
349+
/* empty all cookies, cache and credential stores, removes disk files, issues -flushWithCompletionHandler:. Invokes completionHandler() on the delegate queue. */
350+
open func reset(completionHandler: @escaping () -> Void) {
351+
let configuration = self.configuration
352+
353+
DispatchQueue.global(qos: .background).async {
354+
configuration.urlCache?.removeAllCachedResponses()
355+
if let storage = configuration.urlCredentialStorage {
356+
for credentialEntry in storage.allCredentials {
357+
for credential in credentialEntry.value {
358+
storage.remove(credential.value, for: credentialEntry.key)
359+
}
360+
}
361+
}
362+
363+
self.flush(completionHandler: completionHandler)
364+
}
365+
}
350366

351-
open func flush(completionHandler: @escaping () -> Void) { NSUnimplemented() }/* flush storage to disk and clear transient network caches. Invokes completionHandler() on the delegate queue if not nil. */
367+
/* flush storage to disk and clear transient network caches. Invokes completionHandler() on the delegate queue. */
368+
open func flush(completionHandler: @escaping () -> Void) {
369+
// We create new CURL handles every request.
370+
delegateQueue.addOperation {
371+
completionHandler()
372+
}
373+
}
352374

353375
/* invokes completionHandler with outstanding data, upload and download tasks. */
354376
open func getTasksWithCompletionHandler(completionHandler: @escaping ([URLSessionDataTask], [URLSessionUploadTask], [URLSessionDownloadTask]) -> Void) {

Diff for: Foundation/URLSession/URLSessionConfiguration.swift

+26-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import Foundation
3636
/// A background session can be used to perform networking operations
3737
/// on behalf of a suspended application, within certain constraints.
3838
open class URLSessionConfiguration : NSObject, NSCopying {
39+
// -init is silently incorrect in URLSessionCofiguration on the desktop. Ensure code that relied on swift-corelibs-foundation's init() being functional is redirected to the appropriate cross-platform class property.
40+
@available(*, deprecated, message: "Use .default instead.", renamed: "URLSessionConfiguration.default")
3941
public override init() {
4042
self.requestCachePolicy = .useProtocolCachePolicy
4143
self.timeoutIntervalForRequest = 60
@@ -55,6 +57,27 @@ open class URLSessionConfiguration : NSObject, NSCopying {
5557
super.init()
5658
}
5759

60+
internal convenience init(correctly: ()) {
61+
self.init(identifier: nil,
62+
requestCachePolicy: .useProtocolCachePolicy,
63+
timeoutIntervalForRequest: 60,
64+
timeoutIntervalForResource: 604800,
65+
networkServiceType: .default,
66+
allowsCellularAccess: true,
67+
isDiscretionary: false,
68+
connectionProxyDictionary: nil,
69+
httpShouldUsePipelining: false,
70+
httpShouldSetCookies: true,
71+
httpCookieAcceptPolicy: .onlyFromMainDocumentDomain,
72+
httpAdditionalHeaders: nil,
73+
httpMaximumConnectionsPerHost: 6,
74+
httpCookieStorage: .shared,
75+
urlCredentialStorage: nil, // Should be .shared once implemented.
76+
urlCache: nil,
77+
shouldUseExtendedBackgroundIdleMode: false,
78+
protocolClasses: [_HTTPURLProtocol.self, _FTPURLProtocol.self])
79+
}
80+
5881
private init(identifier: String?,
5982
requestCachePolicy: URLRequest.CachePolicy,
6083
timeoutIntervalForRequest: TimeInterval,
@@ -121,15 +144,15 @@ open class URLSessionConfiguration : NSObject, NSCopying {
121144
}
122145

123146
open class var `default`: URLSessionConfiguration {
124-
return URLSessionConfiguration()
147+
return URLSessionConfiguration(correctly: ())
125148
}
126149

127150
open class var ephemeral: URLSessionConfiguration {
128151
// Return a new ephemeral URLSessionConfiguration every time this property is invoked
129152
// TODO: urlCache and urlCredentialStorage should also be ephemeral/in-memory
130153
// URLCache and URLCredentialStorage are still unimplemented
131-
let ephemeralConfiguration = URLSessionConfiguration()
132-
ephemeralConfiguration.httpCookieStorage = HTTPCookieStorage.ephemeralStorage()
154+
let ephemeralConfiguration = URLSessionConfiguration.default.copy() as! URLSessionConfiguration
155+
ephemeralConfiguration.httpCookieStorage = .ephemeralStorage()
133156
return ephemeralConfiguration
134157
}
135158

0 commit comments

Comments
 (0)