@@ -156,11 +156,101 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
156
156
}
157
157
158
158
public required init ? ( coder aDecoder: NSCoder ) {
159
- NSUnimplemented ( )
159
+ guard aDecoder. allowsKeyedCoding else {
160
+ preconditionFailure ( " Unkeyed coding is unsupported. " )
161
+ }
162
+
163
+ if let encodedURL = aDecoder. decodeObject ( forKey: " NS.url " ) as? NSURL {
164
+ self . url = encodedURL. _swiftObject
165
+ }
166
+
167
+ if let encodedHeaders = aDecoder. decodeObject ( forKey: " NS._allHTTPHeaderFields " ) as? NSDictionary {
168
+ self . _allHTTPHeaderFields = encodedHeaders. reduce ( [ String : String] ( ) ) { result, item in
169
+ var result = result
170
+ if let key = item. key as? NSString ,
171
+ let value = item. value as? NSString {
172
+ result [ key. _swiftObject] = value. _swiftObject
173
+ }
174
+ return result
175
+ }
176
+ }
177
+
178
+ if let encodedDocumentURL = aDecoder. decodeObject ( forKey: " NS.mainDocumentURL " ) as? NSURL {
179
+ self . mainDocumentURL = encodedDocumentURL. _swiftObject
180
+ }
181
+
182
+ if let encodedMethod = aDecoder. decodeObject ( forKey: " NS.httpMethod " ) as? NSString {
183
+ self . httpMethod = encodedMethod. _swiftObject
184
+ }
185
+
186
+ let encodedCachePolicy = aDecoder. decodeObject ( forKey: " NS._cachePolicy " ) as! NSNumber
187
+ self . _cachePolicy = CachePolicy ( rawValue: encodedCachePolicy. uintValue) !
188
+
189
+ let encodedTimeout = aDecoder. decodeObject ( forKey: " NS._timeoutInterval " ) as! NSNumber
190
+ self . _timeoutInterval = encodedTimeout. doubleValue
191
+
192
+ let encodedHttpBody : Data ? = aDecoder. withDecodedUnsafeBufferPointer ( forKey: " NS.httpBody " ) {
193
+ guard let buffer = $0 else { return nil }
194
+ return Data ( buffer: buffer)
195
+ }
196
+
197
+ if let encodedHttpBody = encodedHttpBody {
198
+ self . _body = . data( encodedHttpBody)
199
+ }
200
+
201
+ let encodedNetworkServiceType = aDecoder. decodeObject ( forKey: " NS._networkServiceType " ) as! NSNumber
202
+ self . _networkServiceType = NetworkServiceType ( rawValue: encodedNetworkServiceType. uintValue) !
203
+
204
+ let encodedCellularAccess = aDecoder. decodeObject ( forKey: " NS._allowsCellularAccess " ) as! NSNumber
205
+ self . _allowsCellularAccess = encodedCellularAccess. boolValue
206
+
207
+ let encodedHandleCookies = aDecoder. decodeObject ( forKey: " NS._httpShouldHandleCookies " ) as! NSNumber
208
+ self . _httpShouldHandleCookies = encodedHandleCookies. boolValue
209
+
210
+ let encodedUsePipelining = aDecoder. decodeObject ( forKey: " NS._httpShouldUsePipelining " ) as! NSNumber
211
+ self . _httpShouldUsePipelining = encodedUsePipelining. boolValue
160
212
}
161
213
162
214
open func encode( with aCoder: NSCoder ) {
163
- NSUnimplemented ( )
215
+ guard aCoder. allowsKeyedCoding else {
216
+ preconditionFailure ( " Unkeyed coding is unsupported. " )
217
+ }
218
+
219
+ aCoder. encode ( self . url? . _bridgeToObjectiveC ( ) , forKey: " NS.url " )
220
+ aCoder. encode ( self . _allHTTPHeaderFields? . _bridgeToObjectiveC ( ) , forKey: " NS._allHTTPHeaderFields " )
221
+ aCoder. encode ( self . mainDocumentURL? . _bridgeToObjectiveC ( ) , forKey: " NS.mainDocumentURL " )
222
+ aCoder. encode ( self . httpMethod? . _bridgeToObjectiveC ( ) , forKey: " NS.httpMethod " )
223
+ aCoder. encode ( self . _cachePolicy. rawValue. _bridgeToObjectiveC ( ) , forKey: " NS._cachePolicy " )
224
+ aCoder. encode ( self . _timeoutInterval. _bridgeToObjectiveC ( ) , forKey: " NS._timeoutInterval " )
225
+ if let httpBody = self . httpBody? . _bridgeToObjectiveC ( ) {
226
+ let bytePtr = httpBody. bytes. bindMemory ( to: UInt8 . self, capacity: httpBody. length)
227
+ aCoder. encodeBytes ( bytePtr, length: httpBody. length, forKey: " NS.httpBody " )
228
+ }
229
+ //On macOS input stream is not encoded.
230
+ aCoder. encode ( self . _networkServiceType. rawValue. _bridgeToObjectiveC ( ) , forKey: " NS._networkServiceType " )
231
+ aCoder. encode ( self . _allowsCellularAccess. _bridgeToObjectiveC ( ) , forKey: " NS._allowsCellularAccess " )
232
+ aCoder. encode ( self . _httpShouldHandleCookies. _bridgeToObjectiveC ( ) , forKey: " NS._httpShouldHandleCookies " )
233
+ aCoder. encode ( self . _httpShouldUsePipelining. _bridgeToObjectiveC ( ) , forKey: " NS._httpShouldUsePipelining " )
234
+ }
235
+
236
+ open override func isEqual( _ object: Any ? ) -> Bool {
237
+ //On macOS this fields do not determine the result:
238
+ //allHTTPHeaderFields
239
+ //timeoutInterval
240
+ //httBody
241
+ //networkServiceType
242
+ //httpShouldUsePipelining
243
+ if let other = object as? NSURLRequest {
244
+ return other === self
245
+ || ( other. url == self . url
246
+ && other. mainDocumentURL == self . mainDocumentURL
247
+ && other. httpMethod == self . httpMethod
248
+ && other. _cachePolicy == self . _cachePolicy
249
+ && other. httpBodyStream == self . httpBodyStream
250
+ && other. _allowsCellularAccess == self . _allowsCellularAccess
251
+ && other. _httpShouldHandleCookies == self . _httpShouldHandleCookies)
252
+ }
253
+ return false
164
254
}
165
255
166
256
/// Indicates that NSURLRequest implements the NSSecureCoding protocol.
@@ -289,7 +379,7 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
289
379
/// example.
290
380
open class NSMutableURLRequest : NSURLRequest {
291
381
public required init ? ( coder aDecoder: NSCoder ) {
292
- NSUnimplemented ( )
382
+ super . init ( coder : aDecoder )
293
383
}
294
384
295
385
public convenience init ( url: URL ) {
0 commit comments