Skip to content

Commit 37ffb95

Browse files
authored
Merge pull request #2245 from drodriguez/cached-url-response
Implement CachedURLResponse.
2 parents 9e50b40 + 3dd622b commit 37ffb95

File tree

7 files changed

+381
-22
lines changed

7 files changed

+381
-22
lines changed

Diff for: CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ if(ENABLE_TESTING)
444444
TestFoundation/TestAffineTransform.swift
445445
TestFoundation/TestBundle.swift
446446
TestFoundation/TestByteCountFormatter.swift
447+
TestFoundation/TestCachedURLResponse.swift
447448
TestFoundation/TestCalendar.swift
448449
TestFoundation/TestCharacterSet.swift
449450
TestFoundation/TestCodable.swift

Diff for: Foundation.xcodeproj/project.pbxproj

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
03B6F5841F15F339004F25AF /* TestURLProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03B6F5831F15F339004F25AF /* TestURLProtocol.swift */; };
1212
1513A8432044893F00539722 /* FileManager_XDG.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1513A8422044893F00539722 /* FileManager_XDG.swift */; };
1313
1520469B1D8AEABE00D02E36 /* HTTPServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1520469A1D8AEABE00D02E36 /* HTTPServer.swift */; };
14-
152EF3942283457C001E1269 /* TestNSSortDescriptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 152EF3932283457B001E1269 /* TestNSSortDescriptor.swift */; };
14+
1539391422A07007006DFF4F /* TestCachedURLResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1539391322A07007006DFF4F /* TestCachedURLResponse.swift */; };
1515
153CC8352215E00200BFE8F3 /* ScannerAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 153CC8322214C3D100BFE8F3 /* ScannerAPI.swift */; };
1616
153E951120111DC500F250BE /* CFKnownLocations.h in Headers */ = {isa = PBXBuildFile; fileRef = 153E950F20111DC500F250BE /* CFKnownLocations.h */; settings = {ATTRIBUTES = (Private, ); }; };
1717
153E951220111DC500F250BE /* CFKnownLocations.c in Sources */ = {isa = PBXBuildFile; fileRef = 153E951020111DC500F250BE /* CFKnownLocations.c */; };
@@ -616,6 +616,7 @@
616616
1513A8422044893F00539722 /* FileManager_XDG.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileManager_XDG.swift; sourceTree = "<group>"; };
617617
1520469A1D8AEABE00D02E36 /* HTTPServer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTTPServer.swift; sourceTree = "<group>"; };
618618
152EF3932283457B001E1269 /* TestNSSortDescriptor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSSortDescriptor.swift; sourceTree = "<group>"; };
619+
1539391322A07007006DFF4F /* TestCachedURLResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestCachedURLResponse.swift; sourceTree = "<group>"; };
619620
153CC8322214C3D100BFE8F3 /* ScannerAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScannerAPI.swift; sourceTree = "<group>"; };
620621
153E950F20111DC500F250BE /* CFKnownLocations.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CFKnownLocations.h; sourceTree = "<group>"; };
621622
153E951020111DC500F250BE /* CFKnownLocations.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = CFKnownLocations.c; sourceTree = "<group>"; };
@@ -1711,6 +1712,7 @@
17111712
EA66F63C1BF1619600136161 /* TestNSArray.swift */,
17121713
294E3C1C1CC5E19300E4F44C /* TestNSAttributedString.swift */,
17131714
90E645DE1E4C89A400D0D47C /* TestNSCache.swift */,
1715+
1539391322A07007006DFF4F /* TestCachedURLResponse.swift */,
17141716
52829AD61C160D64003BC4EF /* TestCalendar.swift */,
17151717
15F10CDB218909BF00D88114 /* TestNSCalendar.swift */,
17161718
5BC1D8BC1BF3ADFE009D3973 /* TestCharacterSet.swift */,
@@ -2877,6 +2879,7 @@
28772879
7D8BD739225ED1480057CF37 /* TestMeasurement.swift in Sources */,
28782880
CD1C7F7D1E303B47008E331C /* TestNSError.swift in Sources */,
28792881
294E3C1D1CC5E19300E4F44C /* TestNSAttributedString.swift in Sources */,
2882+
1539391422A07007006DFF4F /* TestCachedURLResponse.swift in Sources */,
28802883
5B13B3431C582D4C00651CE2 /* TestScanner.swift in Sources */,
28812884
5B13B3401C582D4C00651CE2 /* TestNSRange.swift in Sources */,
28822885
5B13B3371C582D4C00651CE2 /* TestNotificationCenter.swift in Sources */,

Diff for: Foundation/URLCache.swift

+47-21
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,13 @@ extension URLCache {
4747
It is used to maintain characteristics and attributes of a cached
4848
object.
4949
*/
50-
open class CachedURLResponse : NSObject, NSSecureCoding, NSCopying {
51-
52-
public required init?(coder aDecoder: NSCoder) {
53-
NSUnimplemented()
54-
}
55-
56-
open func encode(with aCoder: NSCoder) {
57-
NSUnimplemented()
58-
}
59-
60-
static public var supportsSecureCoding: Bool {
61-
return true
62-
}
63-
50+
open class CachedURLResponse : NSObject, NSCopying {
6451
open override func copy() -> Any {
6552
return copy(with: nil)
6653
}
6754

6855
open func copy(with zone: NSZone? = nil) -> Any {
69-
NSUnimplemented()
56+
return self
7057
}
7158

7259
/*!
@@ -81,7 +68,12 @@ open class CachedURLResponse : NSObject, NSSecureCoding, NSCopying {
8168
corresponding to the given response.
8269
@result an initialized CachedURLResponse.
8370
*/
84-
public init(response: URLResponse, data: Data) { NSUnimplemented() }
71+
public init(response: URLResponse, data: Data) {
72+
self.response = response.copy() as! URLResponse
73+
self.data = data
74+
self.userInfo = nil
75+
self.storagePolicy = .allowed
76+
}
8577

8678
/*!
8779
@method initWithResponse:data:userInfo:storagePolicy:
@@ -95,35 +87,69 @@ open class CachedURLResponse : NSObject, NSSecureCoding, NSCopying {
9587
@param storagePolicy an URLCache.StoragePolicy constant.
9688
@result an initialized CachedURLResponse.
9789
*/
98-
public init(response: URLResponse, data: Data, userInfo: [AnyHashable : Any]? = [:], storagePolicy: URLCache.StoragePolicy) { NSUnimplemented() }
90+
public init(response: URLResponse, data: Data, userInfo: [AnyHashable : Any]? = nil, storagePolicy: URLCache.StoragePolicy) {
91+
self.response = response.copy() as! URLResponse
92+
self.data = data
93+
self.userInfo = userInfo
94+
self.storagePolicy = storagePolicy
95+
}
9996

10097
/*!
10198
@method response
10299
@abstract Returns the response wrapped by this instance.
103100
@result The response wrapped by this instance.
104101
*/
105-
/*@NSCopying*/ open var response: URLResponse { NSUnimplemented() }
102+
/*@NSCopying*/ open private(set) var response: URLResponse
106103

107104
/*!
108105
@method data
109106
@abstract Returns the data of the receiver.
110107
@result The data of the receiver.
111108
*/
112-
/*@NSCopying*/ open var data: Data { NSUnimplemented() }
109+
/*@NSCopying*/ open private(set) var data: Data
113110

114111
/*!
115112
@method userInfo
116113
@abstract Returns the userInfo dictionary of the receiver.
117114
@result The userInfo dictionary of the receiver.
118115
*/
119-
open var userInfo: [AnyHashable : Any]? { NSUnimplemented() }
116+
open private(set) var userInfo: [AnyHashable : Any]?
120117

121118
/*!
122119
@method storagePolicy
123120
@abstract Returns the URLCache.StoragePolicy constant of the receiver.
124121
@result The URLCache.StoragePolicy constant of the receiver.
125122
*/
126-
open var storagePolicy: URLCache.StoragePolicy { NSUnimplemented() }
123+
open private(set) var storagePolicy: URLCache.StoragePolicy
124+
125+
open override func isEqual(_ value: Any?) -> Bool {
126+
switch value {
127+
case let other as CachedURLResponse:
128+
return self.isEqual(to: other)
129+
default:
130+
return false
131+
}
132+
}
133+
134+
private func isEqual(to other: CachedURLResponse) -> Bool {
135+
if self === other {
136+
return true
137+
}
138+
139+
// We cannot compare userInfo because of the values are Any, which
140+
// doesn't conform to Equatable.
141+
return self.response == other.response &&
142+
self.data == other.data &&
143+
self.storagePolicy == other.storagePolicy
144+
}
145+
146+
open override var hash: Int {
147+
var hasher = Hasher()
148+
hasher.combine(response)
149+
hasher.combine(data)
150+
hasher.combine(storagePolicy)
151+
return hasher.finalize()
152+
}
127153
}
128154

129155
open class URLCache : NSObject {

Diff for: Foundation/URLResponse.swift

+29
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,35 @@ open class URLResponse : NSObject, NSSecureCoding, NSCopying {
138138
///
139139
/// This method always returns a valid filename.
140140
open fileprivate(set) var suggestedFilename: String?
141+
142+
open override func isEqual(_ value: Any?) -> Bool {
143+
switch value {
144+
case let other as URLResponse:
145+
return self.isEqual(to: other)
146+
default:
147+
return false
148+
}
149+
}
150+
151+
private func isEqual(to other: URLResponse) -> Bool {
152+
if self === other {
153+
return true
154+
}
155+
156+
return self.url == other.url &&
157+
self.expectedContentLength == other.expectedContentLength &&
158+
self.mimeType == other.mimeType &&
159+
self.textEncodingName == other.textEncodingName
160+
}
161+
162+
open override var hash: Int {
163+
var hasher = Hasher()
164+
hasher.combine(url)
165+
hasher.combine(expectedContentLength)
166+
hasher.combine(mimeType)
167+
hasher.combine(textEncodingName)
168+
return hasher.finalize()
169+
}
141170
}
142171

143172
/// A Response to an HTTP URL load.

0 commit comments

Comments
 (0)