Skip to content

Parity: URLSession.reset(…)/.flush(…) #2294

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 28, 2019
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
26 changes: 24 additions & 2 deletions Foundation/URLSession/URLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,31 @@ open class URLSession : NSObject {
}
}

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. */
/* empty all cookies, cache and credential stores, removes disk files, issues -flushWithCompletionHandler:. Invokes completionHandler() on the delegate queue. */
open func reset(completionHandler: @escaping () -> Void) {
let configuration = self.configuration

DispatchQueue.global(qos: .background).async {
configuration.urlCache?.removeAllCachedResponses()
if let storage = configuration.urlCredentialStorage {
for credentialEntry in storage.allCredentials {
for credential in credentialEntry.value {
storage.remove(credential.value, for: credentialEntry.key)
}
}
}

self.flush(completionHandler: completionHandler)
}
}

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. */
/* flush storage to disk and clear transient network caches. Invokes completionHandler() on the delegate queue. */
open func flush(completionHandler: @escaping () -> Void) {
// We create new CURL handles every request.
delegateQueue.addOperation {
completionHandler()
}
}

/* invokes completionHandler with outstanding data, upload and download tasks. */
open func getTasksWithCompletionHandler(completionHandler: @escaping ([URLSessionDataTask], [URLSessionUploadTask], [URLSessionDownloadTask]) -> Void) {
Expand Down
29 changes: 26 additions & 3 deletions Foundation/URLSession/URLSessionConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import Foundation
/// A background session can be used to perform networking operations
/// on behalf of a suspended application, within certain constraints.
open class URLSessionConfiguration : NSObject, NSCopying {
// -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.
@available(*, deprecated, message: "Use .default instead.", renamed: "URLSessionConfiguration.default")
public override init() {
self.requestCachePolicy = .useProtocolCachePolicy
self.timeoutIntervalForRequest = 60
Expand All @@ -55,6 +57,27 @@ open class URLSessionConfiguration : NSObject, NSCopying {
super.init()
}

internal convenience init(correctly: ()) {
self.init(identifier: nil,
requestCachePolicy: .useProtocolCachePolicy,
timeoutIntervalForRequest: 60,
timeoutIntervalForResource: 604800,
networkServiceType: .default,
allowsCellularAccess: true,
isDiscretionary: false,
connectionProxyDictionary: nil,
httpShouldUsePipelining: false,
httpShouldSetCookies: true,
httpCookieAcceptPolicy: .onlyFromMainDocumentDomain,
httpAdditionalHeaders: nil,
httpMaximumConnectionsPerHost: 6,
httpCookieStorage: .shared,
urlCredentialStorage: nil, // Should be .shared once implemented.
urlCache: nil,
shouldUseExtendedBackgroundIdleMode: false,
protocolClasses: [_HTTPURLProtocol.self, _FTPURLProtocol.self])
}

private init(identifier: String?,
requestCachePolicy: URLRequest.CachePolicy,
timeoutIntervalForRequest: TimeInterval,
Expand Down Expand Up @@ -121,15 +144,15 @@ open class URLSessionConfiguration : NSObject, NSCopying {
}

open class var `default`: URLSessionConfiguration {
return URLSessionConfiguration()
return URLSessionConfiguration(correctly: ())
}

open class var ephemeral: URLSessionConfiguration {
// Return a new ephemeral URLSessionConfiguration every time this property is invoked
// TODO: urlCache and urlCredentialStorage should also be ephemeral/in-memory
// URLCache and URLCredentialStorage are still unimplemented
let ephemeralConfiguration = URLSessionConfiguration()
ephemeralConfiguration.httpCookieStorage = HTTPCookieStorage.ephemeralStorage()
let ephemeralConfiguration = URLSessionConfiguration.default.copy() as! URLSessionConfiguration
ephemeralConfiguration.httpCookieStorage = .ephemeralStorage()
return ephemeralConfiguration
}

Expand Down