Skip to content

Commit 0c2f49d

Browse files
authored
Merge pull request #790 from ikesyo/nsurlrequest-utilize-restricted-setter
2 parents 8234239 + eb4bcbc commit 0c2f49d

File tree

2 files changed

+45
-99
lines changed

2 files changed

+45
-99
lines changed

Foundation/NSURLRequest.swift

Lines changed: 43 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
134134

135135
public init(url: URL, cachePolicy: NSURLRequest.CachePolicy, timeoutInterval: TimeInterval) {
136136
self.url = url
137-
_cachePolicy = cachePolicy
138-
_timeoutInterval = timeoutInterval
137+
self.cachePolicy = cachePolicy
138+
self.timeoutInterval = timeoutInterval
139139
}
140140

141141
private func setValues(from source: NSURLRequest) {
142-
self._allHTTPHeaderFields = source.allHTTPHeaderFields
142+
self.allHTTPHeaderFields = source.allHTTPHeaderFields
143143
self.url = source.url
144144
self.mainDocumentURL = source.mainDocumentURL
145145
self.httpMethod = source.httpMethod
@@ -165,7 +165,7 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
165165
}
166166

167167
if let encodedHeaders = aDecoder.decodeObject(forKey: "NS._allHTTPHeaderFields") as? NSDictionary {
168-
self._allHTTPHeaderFields = encodedHeaders.reduce([String : String]()) { result, item in
168+
self.allHTTPHeaderFields = encodedHeaders.reduce([String : String]()) { result, item in
169169
var result = result
170170
if let key = item.key as? NSString,
171171
let value = item.value as? NSString {
@@ -184,10 +184,10 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
184184
}
185185

186186
let encodedCachePolicy = aDecoder.decodeObject(forKey: "NS._cachePolicy") as! NSNumber
187-
self._cachePolicy = CachePolicy(rawValue: encodedCachePolicy.uintValue)!
187+
self.cachePolicy = CachePolicy(rawValue: encodedCachePolicy.uintValue)!
188188

189189
let encodedTimeout = aDecoder.decodeObject(forKey: "NS._timeoutInterval") as! NSNumber
190-
self._timeoutInterval = encodedTimeout.doubleValue
190+
self.timeoutInterval = encodedTimeout.doubleValue
191191

192192
let encodedHttpBody: Data? = aDecoder.withDecodedUnsafeBufferPointer(forKey: "NS.httpBody") {
193193
guard let buffer = $0 else { return nil }
@@ -199,16 +199,16 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
199199
}
200200

201201
let encodedNetworkServiceType = aDecoder.decodeObject(forKey: "NS._networkServiceType") as! NSNumber
202-
self._networkServiceType = NetworkServiceType(rawValue: encodedNetworkServiceType.uintValue)!
202+
self.networkServiceType = NetworkServiceType(rawValue: encodedNetworkServiceType.uintValue)!
203203

204204
let encodedCellularAccess = aDecoder.decodeObject(forKey: "NS._allowsCellularAccess") as! NSNumber
205-
self._allowsCellularAccess = encodedCellularAccess.boolValue
205+
self.allowsCellularAccess = encodedCellularAccess.boolValue
206206

207207
let encodedHandleCookies = aDecoder.decodeObject(forKey: "NS._httpShouldHandleCookies") as! NSNumber
208-
self._httpShouldHandleCookies = encodedHandleCookies.boolValue
208+
self.httpShouldHandleCookies = encodedHandleCookies.boolValue
209209

210210
let encodedUsePipelining = aDecoder.decodeObject(forKey: "NS._httpShouldUsePipelining") as! NSNumber
211-
self._httpShouldUsePipelining = encodedUsePipelining.boolValue
211+
self.httpShouldUsePipelining = encodedUsePipelining.boolValue
212212
}
213213

214214
open func encode(with aCoder: NSCoder) {
@@ -217,20 +217,20 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
217217
}
218218

219219
aCoder.encode(self.url?._bridgeToObjectiveC(), forKey: "NS.url")
220-
aCoder.encode(self._allHTTPHeaderFields?._bridgeToObjectiveC(), forKey: "NS._allHTTPHeaderFields")
220+
aCoder.encode(self.allHTTPHeaderFields?._bridgeToObjectiveC(), forKey: "NS._allHTTPHeaderFields")
221221
aCoder.encode(self.mainDocumentURL?._bridgeToObjectiveC(), forKey: "NS.mainDocumentURL")
222222
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")
223+
aCoder.encode(self.cachePolicy.rawValue._bridgeToObjectiveC(), forKey: "NS._cachePolicy")
224+
aCoder.encode(self.timeoutInterval._bridgeToObjectiveC(), forKey: "NS._timeoutInterval")
225225
if let httpBody = self.httpBody?._bridgeToObjectiveC() {
226226
let bytePtr = httpBody.bytes.bindMemory(to: UInt8.self, capacity: httpBody.length)
227227
aCoder.encodeBytes(bytePtr, length: httpBody.length, forKey: "NS.httpBody")
228228
}
229229
//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")
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")
234234
}
235235

236236
open override func isEqual(_ object: Any?) -> Bool {
@@ -245,10 +245,10 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
245245
|| (other.url == self.url
246246
&& other.mainDocumentURL == self.mainDocumentURL
247247
&& other.httpMethod == self.httpMethod
248-
&& other._cachePolicy == self._cachePolicy
248+
&& other.cachePolicy == self.cachePolicy
249249
&& other.httpBodyStream == self.httpBodyStream
250-
&& other._allowsCellularAccess == self._allowsCellularAccess
251-
&& other._httpShouldHandleCookies == self._httpShouldHandleCookies)
250+
&& other.allowsCellularAccess == self.allowsCellularAccess
251+
&& other.httpShouldHandleCookies == self.httpShouldHandleCookies)
252252
}
253253
return false
254254
}
@@ -265,27 +265,16 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
265265
/// document" policy. There may also be other future uses.
266266
/*@NSCopying*/ open fileprivate(set) var mainDocumentURL: URL?
267267

268-
internal var _cachePolicy: CachePolicy = .useProtocolCachePolicy
269-
open var cachePolicy: CachePolicy {
270-
return _cachePolicy
271-
}
268+
open internal(set) var cachePolicy: CachePolicy = .useProtocolCachePolicy
272269

273-
internal var _timeoutInterval: TimeInterval = 60.0
274-
open var timeoutInterval: TimeInterval {
275-
return _timeoutInterval
276-
}
270+
open internal(set) var timeoutInterval: TimeInterval = 60.0
277271

278272
/// Returns the HTTP request method of the receiver.
279273
open fileprivate(set) var httpMethod: String? = "GET"
280274

281275
/// A dictionary containing all the HTTP header fields
282276
/// of the receiver.
283-
internal var _allHTTPHeaderFields: [String : String]? = nil
284-
open var allHTTPHeaderFields: [String : String]? {
285-
get {
286-
return _allHTTPHeaderFields
287-
}
288-
}
277+
open internal(set) var allHTTPHeaderFields: [String : String]? = nil
289278

290279
/// Returns the value which corresponds to the given header field.
291280
///
@@ -330,25 +319,13 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
330319
return nil
331320
}
332321

333-
internal var _networkServiceType: NetworkServiceType = .`default`
334-
open var networkServiceType: NetworkServiceType {
335-
return _networkServiceType
336-
}
322+
open internal(set) var networkServiceType: NetworkServiceType = .default
337323

338-
internal var _allowsCellularAccess: Bool = true
339-
open var allowsCellularAccess: Bool {
340-
return _allowsCellularAccess
341-
}
324+
open internal(set) var allowsCellularAccess: Bool = true
342325

343-
internal var _httpShouldHandleCookies: Bool = true
344-
open var httpShouldHandleCookies: Bool {
345-
return _httpShouldHandleCookies
346-
}
326+
open internal(set) var httpShouldHandleCookies: Bool = true
347327

348-
internal var _httpShouldUsePipelining: Bool = true
349-
open var httpShouldUsePipelining: Bool {
350-
return _httpShouldUsePipelining
351-
}
328+
open internal(set) var httpShouldUsePipelining: Bool = true
352329
}
353330

354331
/// An `NSMutableURLRequest` object represents a mutable URL load
@@ -422,30 +399,18 @@ open class NSMutableURLRequest : NSURLRequest {
422399
}
423400

424401
open override var cachePolicy: CachePolicy {
425-
get {
426-
return _cachePolicy
427-
}
428-
set {
429-
_cachePolicy = newValue
430-
}
402+
get { return super.cachePolicy }
403+
set { super.cachePolicy = newValue }
431404
}
432405

433406
open override var timeoutInterval: TimeInterval {
434-
get {
435-
return _timeoutInterval
436-
}
437-
set {
438-
_timeoutInterval = newValue
439-
}
407+
get { return super.timeoutInterval }
408+
set { super.timeoutInterval = newValue }
440409
}
441410

442411
open override var allHTTPHeaderFields: [String : String]? {
443-
get {
444-
return _allHTTPHeaderFields
445-
}
446-
set {
447-
_allHTTPHeaderFields = newValue
448-
}
412+
get { return super.allHTTPHeaderFields }
413+
set { super.allHTTPHeaderFields = newValue }
449414
}
450415

451416
/// Sets the value of the given HTTP header field.
@@ -462,7 +427,7 @@ open class NSMutableURLRequest : NSURLRequest {
462427
f.removeValue(forKey: old.0)
463428
}
464429
f[field] = value
465-
_allHTTPHeaderFields = f
430+
allHTTPHeaderFields = f
466431
}
467432

468433
/// Adds an HTTP header field in the current header dictionary.
@@ -483,7 +448,7 @@ open class NSMutableURLRequest : NSURLRequest {
483448
} else {
484449
f[field] = value
485450
}
486-
_allHTTPHeaderFields = f
451+
allHTTPHeaderFields = f
487452
}
488453

489454
open override var httpBody: Data? {
@@ -529,39 +494,23 @@ open class NSMutableURLRequest : NSURLRequest {
529494
}
530495

531496
open override var networkServiceType: NetworkServiceType {
532-
get {
533-
return _networkServiceType
534-
}
535-
set {
536-
_networkServiceType = newValue
537-
}
497+
get { return super.networkServiceType }
498+
set { super.networkServiceType = newValue }
538499
}
539500

540501
open override var allowsCellularAccess: Bool {
541-
get {
542-
return _allowsCellularAccess
543-
}
544-
set {
545-
_allowsCellularAccess = newValue
546-
}
502+
get { return super.allowsCellularAccess }
503+
set { super.allowsCellularAccess = newValue }
547504
}
548505

549506
open override var httpShouldHandleCookies: Bool {
550-
get {
551-
return _httpShouldHandleCookies
552-
}
553-
set {
554-
_httpShouldHandleCookies = newValue
555-
}
507+
get { return super.httpShouldHandleCookies }
508+
set { super.httpShouldHandleCookies = newValue }
556509
}
557510

558511
open override var httpShouldUsePipelining: Bool {
559-
get {
560-
return _httpShouldUsePipelining
561-
}
562-
set {
563-
_httpShouldUsePipelining = newValue
564-
}
512+
get { return super.httpShouldUsePipelining }
513+
set { super.httpShouldUsePipelining = newValue }
565514
}
566515
}
567516

Foundation/NSURLSession/NSURLSessionTask.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,7 @@ open class URLSessionTask : NSObject, NSCopying {
211211
* The error, if any, delivered via -URLSession:task:didCompleteWithError:
212212
* This property will be nil in the event that no error occured.
213213
*/
214-
fileprivate var _error: NSError?
215-
/*@NSCopying*/ open var error: NSError? {
216-
return self._error
217-
}
214+
/*@NSCopying*/ open fileprivate(set) var error: NSError?
218215

219216
/// Suspend the task.
220217
///
@@ -868,7 +865,7 @@ extension URLSessionTask {
868865
}
869866
}
870867
func completeTask(withError error: NSError) {
871-
self._error = error
868+
self.error = error
872869

873870
guard case .transferFailed = internalState else {
874871
fatalError("Trying to complete the task, but its transfer isn't complete / failed.")

0 commit comments

Comments
 (0)