Skip to content

Commit cffa65a

Browse files
seabayleaparkera
authored andcommitted
Swift 3 API Parity: Use URLRequest rather than NSURLRequest (#600)
1 parent 6926ec5 commit cffa65a

File tree

10 files changed

+260
-52
lines changed

10 files changed

+260
-52
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@
323323
D3E8D6D11C367AB600295652 /* NSSpecialValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3E8D6D01C367AB600295652 /* NSSpecialValue.swift */; };
324324
D3E8D6D31C36982700295652 /* NSKeyedUnarchiver-EdgeInsetsTest.plist in Resources */ = {isa = PBXBuildFile; fileRef = D3E8D6D21C36982700295652 /* NSKeyedUnarchiver-EdgeInsetsTest.plist */; };
325325
D3E8D6D51C36AC0C00295652 /* NSKeyedUnarchiver-RectTest.plist in Resources */ = {isa = PBXBuildFile; fileRef = D3E8D6D41C36AC0C00295652 /* NSKeyedUnarchiver-RectTest.plist */; };
326+
D4FE895B1D703D1100DA7986 /* TestURLRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4FE895A1D703D1100DA7986 /* TestURLRequest.swift */; };
326327
D51239DF1CD9DA0800D433EE /* CFSocket.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D88E01BBC9B0300234F36 /* CFSocket.c */; };
327328
D512D17C1CD883F00032E6A5 /* TestNSFileHandle.swift in Sources */ = {isa = PBXBuildFile; fileRef = D512D17B1CD883F00032E6A5 /* TestNSFileHandle.swift */; };
328329
D5C40F331CDA1D460005690C /* TestNSOperationQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5C40F321CDA1D460005690C /* TestNSOperationQueue.swift */; };
@@ -756,6 +757,7 @@
756757
D3E8D6D01C367AB600295652 /* NSSpecialValue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSSpecialValue.swift; sourceTree = "<group>"; };
757758
D3E8D6D21C36982700295652 /* NSKeyedUnarchiver-EdgeInsetsTest.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "NSKeyedUnarchiver-EdgeInsetsTest.plist"; sourceTree = "<group>"; };
758759
D3E8D6D41C36AC0C00295652 /* NSKeyedUnarchiver-RectTest.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "NSKeyedUnarchiver-RectTest.plist"; sourceTree = "<group>"; };
760+
D4FE895A1D703D1100DA7986 /* TestURLRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestURLRequest.swift; sourceTree = "<group>"; };
759761
D512D17B1CD883F00032E6A5 /* TestNSFileHandle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSFileHandle.swift; sourceTree = "<group>"; };
760762
D5C40F321CDA1D460005690C /* TestNSOperationQueue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSOperationQueue.swift; sourceTree = "<group>"; };
761763
D834F9931C31C4060023812A /* TestNSOrderedSet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSOrderedSet.swift; sourceTree = "<group>"; };
@@ -1300,6 +1302,7 @@
13001302
EA66F65A1BF1976100136161 /* Tests */ = {
13011303
isa = PBXGroup;
13021304
children = (
1305+
D4FE895A1D703D1100DA7986 /* TestURLRequest.swift */,
13031306
C93559281C12C49F009FD6A9 /* TestNSAffineTransform.swift */,
13041307
EA66F63C1BF1619600136161 /* TestNSArray.swift */,
13051308
294E3C1C1CC5E19300E4F44C /* TestNSAttributedString.swift */,
@@ -2229,6 +2232,7 @@
22292232
5B13B3251C582D4700651CE2 /* main.swift in Sources */,
22302233
5B1FD9E31D6D17B80080E83C /* TestNSURLSession.swift in Sources */,
22312234
D512D17C1CD883F00032E6A5 /* TestNSFileHandle.swift in Sources */,
2235+
D4FE895B1D703D1100DA7986 /* TestURLRequest.swift in Sources */,
22322236
5B13B33A1C582D4C00651CE2 /* TestNSNumber.swift in Sources */,
22332237
5B13B3521C582D4C00651CE2 /* TestNSValue.swift in Sources */,
22342238
5B13B3311C582D4C00651CE2 /* TestNSIndexPath.swift in Sources */,

Foundation/NSURLRequest.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ open class NSMutableURLRequest : NSURLRequest {
300300
super.init(url: url, cachePolicy: cachePolicy, timeoutInterval: timeoutInterval)
301301
}
302302

303+
open override func copy(with zone: NSZone? = nil) -> Any {
304+
return mutableCopy(with: zone)
305+
}
306+
303307
/*@NSCopying */ open override var url: URL? {
304308
get { return super.url }
305309
//TODO: set { super.URL = newValue.map{ $0.copy() as! NSURL } }

Foundation/NSURLSession/Configuration.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal extension URLSession {
2424
let identifier: String?
2525

2626
/// default cache policy for requests
27-
let requestCachePolicy: NSURLRequest.CachePolicy
27+
let requestCachePolicy: URLRequest.CachePolicy
2828

2929
/// default timeout for requests. This will cause a timeout if no data is transmitted for the given timeout value, and is reset whenever data is transmitted.
3030
let timeoutIntervalForRequest: TimeInterval
@@ -33,7 +33,7 @@ internal extension URLSession {
3333
let timeoutIntervalForResource: TimeInterval
3434

3535
/// type of service for requests.
36-
let networkServiceType: NSURLRequest.NetworkServiceType
36+
let networkServiceType: URLRequest.NetworkServiceType
3737

3838
/// allow request to route over cellular.
3939
let allowsCellularAccess: Bool
@@ -100,21 +100,22 @@ internal extension URLSession._Configuration {
100100

101101
// Configure NSURLRequests
102102
internal extension URLSession._Configuration {
103-
func configure(request: NSMutableURLRequest) {
103+
func configure(request: URLRequest) {
104+
var request = request
104105
httpAdditionalHeaders?.forEach {
105106
guard request.value(forHTTPHeaderField: $0.0) == nil else { return }
106107
request.setValue($0.1, forHTTPHeaderField: $0.0)
107108
}
108109
}
109-
func setCookies(on request: NSMutableURLRequest) {
110+
func setCookies(on request: URLRequest) {
110111
if httpShouldSetCookies {
111112
//TODO: Ask the cookie storage what cookie to set.
112113
}
113114
}
114115
}
115116
// Cache Management
116117
private extension URLSession._Configuration {
117-
func cachedResponse(forRequest request: NSURLRequest) -> CachedURLResponse? {
118+
func cachedResponse(forRequest request: URLRequest) -> CachedURLResponse? {
118119
//TODO: Check the policy & consult the cache.
119120
// There's more detail on how this should work here:
120121
// <https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/index.html#//apple_ref/swift/enum/c:@E@URLRequestCachePolicy>

Foundation/NSURLSession/NSURLSession.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ open class URLSession : NSObject {
281281
*/
282282

283283
/* Creates a data task with the given request. The request may have a body stream. */
284-
open func dataTask(with request: NSURLRequest) -> URLSessionDataTask {
284+
open func dataTask(with request: URLRequest) -> URLSessionDataTask {
285285
return dataTask(with: _Request(request), behaviour: .callDelegate)
286286
}
287287

@@ -291,22 +291,22 @@ open class URLSession : NSObject {
291291
}
292292

293293
/* Creates an upload task with the given request. The body of the request will be created from the file referenced by fileURL */
294-
open func uploadTask(with request: NSURLRequest, fromFile fileURL: URL) -> URLSessionUploadTask {
294+
open func uploadTask(with request: URLRequest, fromFile fileURL: URL) -> URLSessionUploadTask {
295295
let r = URLSession._Request(request)
296296
return uploadTask(with: r, body: .file(fileURL), behaviour: .callDelegate)
297297
}
298298

299299
/* Creates an upload task with the given request. The body of the request is provided from the bodyData. */
300-
open func uploadTask(with request: NSURLRequest, fromData bodyData: Data) -> URLSessionUploadTask {
300+
open func uploadTask(with request: URLRequest, fromData bodyData: Data) -> URLSessionUploadTask {
301301
let r = URLSession._Request(request)
302302
return uploadTask(with: r, body: .data(createDispatchData(bodyData)), behaviour: .callDelegate)
303303
}
304304

305305
/* Creates an upload task with the given request. The previously set body stream of the request (if any) is ignored and the URLSession:task:needNewBodyStream: delegate will be called when the body payload is required. */
306-
open func uploadTask(withStreamedRequest request: NSURLRequest) -> URLSessionUploadTask { NSUnimplemented() }
306+
open func uploadTask(withStreamedRequest request: URLRequest) -> URLSessionUploadTask { NSUnimplemented() }
307307

308308
/* Creates a download task with the given request. */
309-
open func downloadTask(with request: NSURLRequest) -> URLSessionDownloadTask {
309+
open func downloadTask(with request: URLRequest) -> URLSessionDownloadTask {
310310
let r = URLSession._Request(request)
311311
return downloadTask(with: r, behavior: .callDelegate)
312312
}
@@ -328,10 +328,10 @@ open class URLSession : NSObject {
328328
// Helpers
329329
fileprivate extension URLSession {
330330
enum _Request {
331-
case request(NSURLRequest)
331+
case request(URLRequest)
332332
case url(URL)
333333
}
334-
func createConfiguredRequest(from request: URLSession._Request) -> NSURLRequest {
334+
func createConfiguredRequest(from request: URLSession._Request) -> URLRequest {
335335
let r = request.createMutableURLRequest()
336336
_configuration.configure(request: r)
337337
return r
@@ -341,15 +341,15 @@ extension URLSession._Request {
341341
init(_ url: URL) {
342342
self = .url(url)
343343
}
344-
init(_ request: NSURLRequest) {
344+
init(_ request: URLRequest) {
345345
self = .request(request)
346346
}
347347
}
348348
extension URLSession._Request {
349-
func createMutableURLRequest() -> NSMutableURLRequest {
349+
func createMutableURLRequest() -> URLRequest {
350350
switch self {
351-
case .url(let url): return NSMutableURLRequest(url: url)
352-
case .request(let r): return r.mutableCopy() as! NSMutableURLRequest
351+
case .url(let url): return URLRequest(url: url)
352+
case .request(let r): return r
353353
}
354354
}
355355
}
@@ -420,7 +420,7 @@ extension URLSession {
420420
* see <Foundation/NSURLError.h>. The delegate, if any, will still be
421421
* called for authentication challenges.
422422
*/
423-
open func dataTask(with request: NSURLRequest, completionHandler: @escaping (Data?, URLResponse?, NSError?) -> Void) -> URLSessionDataTask {
423+
open func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, NSError?) -> Void) -> URLSessionDataTask {
424424
return dataTask(with: _Request(request), behaviour: .dataCompletionHandler(completionHandler))
425425
}
426426

@@ -431,12 +431,12 @@ extension URLSession {
431431
/*
432432
* upload convenience method.
433433
*/
434-
open func uploadTask(with request: NSURLRequest, fromFile fileURL: URL, completionHandler: @escaping (Data?, URLResponse?, NSError?) -> Void) -> URLSessionUploadTask {
434+
open func uploadTask(with request: URLRequest, fromFile fileURL: URL, completionHandler: @escaping (Data?, URLResponse?, NSError?) -> Void) -> URLSessionUploadTask {
435435
let fileData = try! Data(contentsOf: fileURL)
436436
return uploadTask(with: request, fromData: fileData, completionHandler: completionHandler)
437437
}
438438

439-
open func uploadTask(with request: NSURLRequest, fromData bodyData: Data?, completionHandler: @escaping (Data?, URLResponse?, NSError?) -> Void) -> URLSessionUploadTask {
439+
open func uploadTask(with request: URLRequest, fromData bodyData: Data?, completionHandler: @escaping (Data?, URLResponse?, NSError?) -> Void) -> URLSessionUploadTask {
440440
return uploadTask(with: _Request(request), body: .data(createDispatchData(bodyData!)), behaviour: .dataCompletionHandler(completionHandler))
441441
}
442442

@@ -446,7 +446,7 @@ extension URLSession {
446446
* copied during the invocation of the completion routine. The file
447447
* will be removed automatically.
448448
*/
449-
open func downloadTask(with request: NSURLRequest, completionHandler: @escaping (URL?, URLResponse?, NSError?) -> Void) -> URLSessionDownloadTask {
449+
open func downloadTask(with request: URLRequest, completionHandler: @escaping (URL?, URLResponse?, NSError?) -> Void) -> URLSessionDownloadTask {
450450
return downloadTask(with: _Request(request), behavior: .downloadCompletionHandler(completionHandler))
451451
}
452452

Foundation/NSURLSession/NSURLSessionConfiguration.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
/// on behalf of a suspended application, within certain constraints.
3434
open class URLSessionConfiguration : NSObject, NSCopying {
3535
public override init() {
36-
self.requestCachePolicy = NSURLRequest.CachePolicy.useProtocolCachePolicy
36+
self.requestCachePolicy = URLRequest.CachePolicy.useProtocolCachePolicy
3737
self.timeoutIntervalForRequest = 60
3838
self.timeoutIntervalForResource = 604800
3939
self.networkServiceType = .default
@@ -51,10 +51,10 @@ open class URLSessionConfiguration : NSObject, NSCopying {
5151
}
5252

5353
private init(identifier: String?,
54-
requestCachePolicy: NSURLRequest.CachePolicy,
54+
requestCachePolicy: URLRequest.CachePolicy,
5555
timeoutIntervalForRequest: TimeInterval,
5656
timeoutIntervalForResource: TimeInterval,
57-
networkServiceType: NSURLRequest.NetworkServiceType,
57+
networkServiceType: URLRequest.NetworkServiceType,
5858
allowsCellularAccess: Bool,
5959
discretionary: Bool,
6060
connectionProxyDictionary: [AnyHashable:Any]?,
@@ -126,7 +126,7 @@ open class URLSessionConfiguration : NSObject, NSCopying {
126126
open var identifier: String?
127127

128128
/* default cache policy for requests */
129-
open var requestCachePolicy: NSURLRequest.CachePolicy
129+
open var requestCachePolicy: URLRequest.CachePolicy
130130

131131
/* default timeout for requests. This will cause a timeout if no data is transmitted for the given timeout value, and is reset whenever data is transmitted. */
132132
open var timeoutIntervalForRequest: TimeInterval
@@ -135,7 +135,7 @@ open class URLSessionConfiguration : NSObject, NSCopying {
135135
open var timeoutIntervalForResource: TimeInterval
136136

137137
/* type of service for requests. */
138-
open var networkServiceType: NSURLRequest.NetworkServiceType
138+
open var networkServiceType: URLRequest.NetworkServiceType
139139

140140
/* allow request to route over cellular. */
141141
open var allowsCellularAccess: Bool

Foundation/NSURLSession/NSURLSessionDelegate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public protocol URLSessionTaskDelegate : URLSessionDelegate {
9595
*
9696
* For tasks in background sessions, redirections will always be followed and this method will not be called.
9797
*/
98-
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: NSURLRequest, completionHandler: @escaping (NSURLRequest?) -> Void)
98+
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void)
9999

100100
/* The task has received a request specific authentication challenge.
101101
* If this delegate is not implemented, the session specific authentication challenge
@@ -122,7 +122,7 @@ public protocol URLSessionTaskDelegate : URLSessionDelegate {
122122
}
123123

124124
extension URLSessionTaskDelegate {
125-
public func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: NSURLRequest, completionHandler: @escaping (NSURLRequest?) -> Void) {
125+
public func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
126126
completionHandler(request)
127127
}
128128

0 commit comments

Comments
 (0)