Skip to content

Implement CachedURLResponse. #2245

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 3 commits into from
Jun 12, 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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ if(ENABLE_TESTING)
TestFoundation/TestAffineTransform.swift
TestFoundation/TestBundle.swift
TestFoundation/TestByteCountFormatter.swift
TestFoundation/TestCachedURLResponse.swift
TestFoundation/TestCalendar.swift
TestFoundation/TestCharacterSet.swift
TestFoundation/TestCodable.swift
Expand Down
5 changes: 4 additions & 1 deletion Foundation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
03B6F5841F15F339004F25AF /* TestURLProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03B6F5831F15F339004F25AF /* TestURLProtocol.swift */; };
1513A8432044893F00539722 /* FileManager_XDG.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1513A8422044893F00539722 /* FileManager_XDG.swift */; };
1520469B1D8AEABE00D02E36 /* HTTPServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1520469A1D8AEABE00D02E36 /* HTTPServer.swift */; };
152EF3942283457C001E1269 /* TestNSSortDescriptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 152EF3932283457B001E1269 /* TestNSSortDescriptor.swift */; };
1539391422A07007006DFF4F /* TestCachedURLResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1539391322A07007006DFF4F /* TestCachedURLResponse.swift */; };
153CC8352215E00200BFE8F3 /* ScannerAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 153CC8322214C3D100BFE8F3 /* ScannerAPI.swift */; };
153E951120111DC500F250BE /* CFKnownLocations.h in Headers */ = {isa = PBXBuildFile; fileRef = 153E950F20111DC500F250BE /* CFKnownLocations.h */; settings = {ATTRIBUTES = (Private, ); }; };
153E951220111DC500F250BE /* CFKnownLocations.c in Sources */ = {isa = PBXBuildFile; fileRef = 153E951020111DC500F250BE /* CFKnownLocations.c */; };
Expand Down Expand Up @@ -616,6 +616,7 @@
1513A8422044893F00539722 /* FileManager_XDG.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileManager_XDG.swift; sourceTree = "<group>"; };
1520469A1D8AEABE00D02E36 /* HTTPServer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTTPServer.swift; sourceTree = "<group>"; };
152EF3932283457B001E1269 /* TestNSSortDescriptor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSSortDescriptor.swift; sourceTree = "<group>"; };
1539391322A07007006DFF4F /* TestCachedURLResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestCachedURLResponse.swift; sourceTree = "<group>"; };
153CC8322214C3D100BFE8F3 /* ScannerAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScannerAPI.swift; sourceTree = "<group>"; };
153E950F20111DC500F250BE /* CFKnownLocations.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CFKnownLocations.h; sourceTree = "<group>"; };
153E951020111DC500F250BE /* CFKnownLocations.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = CFKnownLocations.c; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1711,6 +1712,7 @@
EA66F63C1BF1619600136161 /* TestNSArray.swift */,
294E3C1C1CC5E19300E4F44C /* TestNSAttributedString.swift */,
90E645DE1E4C89A400D0D47C /* TestNSCache.swift */,
1539391322A07007006DFF4F /* TestCachedURLResponse.swift */,
52829AD61C160D64003BC4EF /* TestCalendar.swift */,
15F10CDB218909BF00D88114 /* TestNSCalendar.swift */,
5BC1D8BC1BF3ADFE009D3973 /* TestCharacterSet.swift */,
Expand Down Expand Up @@ -2877,6 +2879,7 @@
7D8BD739225ED1480057CF37 /* TestMeasurement.swift in Sources */,
CD1C7F7D1E303B47008E331C /* TestNSError.swift in Sources */,
294E3C1D1CC5E19300E4F44C /* TestNSAttributedString.swift in Sources */,
1539391422A07007006DFF4F /* TestCachedURLResponse.swift in Sources */,
5B13B3431C582D4C00651CE2 /* TestScanner.swift in Sources */,
5B13B3401C582D4C00651CE2 /* TestNSRange.swift in Sources */,
5B13B3371C582D4C00651CE2 /* TestNotificationCenter.swift in Sources */,
Expand Down
68 changes: 47 additions & 21 deletions Foundation/URLCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,13 @@ extension URLCache {
It is used to maintain characteristics and attributes of a cached
object.
*/
open class CachedURLResponse : NSObject, NSSecureCoding, NSCopying {

public required init?(coder aDecoder: NSCoder) {
NSUnimplemented()
}

open func encode(with aCoder: NSCoder) {
NSUnimplemented()
}

static public var supportsSecureCoding: Bool {
return true
}

open class CachedURLResponse : NSObject, NSCopying {
open override func copy() -> Any {
return copy(with: nil)
}

open func copy(with zone: NSZone? = nil) -> Any {
NSUnimplemented()
return self
}

/*!
Expand All @@ -81,7 +68,12 @@ open class CachedURLResponse : NSObject, NSSecureCoding, NSCopying {
corresponding to the given response.
@result an initialized CachedURLResponse.
*/
public init(response: URLResponse, data: Data) { NSUnimplemented() }
public init(response: URLResponse, data: Data) {
self.response = response.copy() as! URLResponse
self.data = data
self.userInfo = nil
self.storagePolicy = .allowed
}

/*!
@method initWithResponse:data:userInfo:storagePolicy:
Expand All @@ -95,35 +87,69 @@ open class CachedURLResponse : NSObject, NSSecureCoding, NSCopying {
@param storagePolicy an URLCache.StoragePolicy constant.
@result an initialized CachedURLResponse.
*/
public init(response: URLResponse, data: Data, userInfo: [AnyHashable : Any]? = [:], storagePolicy: URLCache.StoragePolicy) { NSUnimplemented() }
public init(response: URLResponse, data: Data, userInfo: [AnyHashable : Any]? = nil, storagePolicy: URLCache.StoragePolicy) {
self.response = response.copy() as! URLResponse
self.data = data
self.userInfo = userInfo
self.storagePolicy = storagePolicy
}

/*!
@method response
@abstract Returns the response wrapped by this instance.
@result The response wrapped by this instance.
*/
/*@NSCopying*/ open var response: URLResponse { NSUnimplemented() }
/*@NSCopying*/ open private(set) var response: URLResponse

/*!
@method data
@abstract Returns the data of the receiver.
@result The data of the receiver.
*/
/*@NSCopying*/ open var data: Data { NSUnimplemented() }
/*@NSCopying*/ open private(set) var data: Data

/*!
@method userInfo
@abstract Returns the userInfo dictionary of the receiver.
@result The userInfo dictionary of the receiver.
*/
open var userInfo: [AnyHashable : Any]? { NSUnimplemented() }
open private(set) var userInfo: [AnyHashable : Any]?

/*!
@method storagePolicy
@abstract Returns the URLCache.StoragePolicy constant of the receiver.
@result The URLCache.StoragePolicy constant of the receiver.
*/
open var storagePolicy: URLCache.StoragePolicy { NSUnimplemented() }
open private(set) var storagePolicy: URLCache.StoragePolicy

open override func isEqual(_ value: Any?) -> Bool {
switch value {
case let other as CachedURLResponse:
return self.isEqual(to: other)
default:
return false
}
}

private func isEqual(to other: CachedURLResponse) -> Bool {
if self === other {
return true
}

// We cannot compare userInfo because of the values are Any, which
// doesn't conform to Equatable.
return self.response == other.response &&
self.data == other.data &&
self.storagePolicy == other.storagePolicy
}

open override var hash: Int {
var hasher = Hasher()
hasher.combine(response)
hasher.combine(data)
hasher.combine(storagePolicy)
return hasher.finalize()
}
}

open class URLCache : NSObject {
Expand Down
29 changes: 29 additions & 0 deletions Foundation/URLResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,35 @@ open class URLResponse : NSObject, NSSecureCoding, NSCopying {
///
/// This method always returns a valid filename.
open fileprivate(set) var suggestedFilename: String?

open override func isEqual(_ value: Any?) -> Bool {
switch value {
case let other as URLResponse:
return self.isEqual(to: other)
default:
return false
}
}

private func isEqual(to other: URLResponse) -> Bool {
if self === other {
return true
}

return self.url == other.url &&
self.expectedContentLength == other.expectedContentLength &&
self.mimeType == other.mimeType &&
self.textEncodingName == other.textEncodingName
}

open override var hash: Int {
var hasher = Hasher()
hasher.combine(url)
hasher.combine(expectedContentLength)
hasher.combine(mimeType)
hasher.combine(textEncodingName)
return hasher.finalize()
}
}

/// A Response to an HTTP URL load.
Expand Down
Loading