Skip to content

Commit 21282c5

Browse files
committed
Updates to NSHTTPCookie
• Fixed setting version from NSHTTPCookieVersion, was NSHTTPCookieSecure • Fixed string format when setting expires date • Fixed creating expiresDate from timeInterval. Docs state must be integer. • Fixed setting discard to match documentation • Replaced internal struct with private properties, serve the same purpose • Created internal properties dictionary that is returned by properties get. Closer to Cocoa compatibility
1 parent db4b395 commit 21282c5

File tree

1 file changed

+88
-90
lines changed

1 file changed

+88
-90
lines changed

Foundation/NSHTTPCookie.swift

Lines changed: 88 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,20 @@ public let NSHTTPCookiePort: String = "Port"
9595
attributes of a cookie.
9696
*/
9797
public class NSHTTPCookie : NSObject {
98-
private struct Cookie {
99-
let comment: String?
100-
let commentURL: NSURL?
101-
let domain: String
102-
let expiresDate: NSDate?
103-
let HTTPOnly: Bool
104-
let secure: Bool
105-
let sessionOnly: Bool
106-
let name: String
107-
let path: String
108-
let portList: [NSNumber]?
109-
let properties: [String: Any]?
110-
let value: String
111-
let version: Int
112-
}
113-
private let cookieRepresentation: Cookie
114-
98+
let _comment: String?
99+
let _commentURL: NSURL?
100+
let _domain: String
101+
let _expiresDate: NSDate?
102+
let _HTTPOnly: Bool
103+
let _secure: Bool
104+
let _sessionOnly: Bool
105+
let _name: String
106+
let _path: String
107+
let _portList: [NSNumber]?
108+
let _value: String
109+
let _version: Int
110+
var _properties: [String : Any]
111+
115112
/*!
116113
@method initWithProperties:
117114
@abstract Initialize a NSHTTPCookie object with a dictionary of
@@ -245,7 +242,7 @@ public class NSHTTPCookie : NSObject {
245242
else {
246243
return nil
247244
}
248-
245+
249246
let canonicalDomain: String
250247
if let domain = properties[NSHTTPCookieDomain] as? String {
251248
canonicalDomain = domain
@@ -257,99 +254,100 @@ public class NSHTTPCookie : NSObject {
257254
} else {
258255
return nil
259256
}
260-
261-
let secure: Bool
257+
258+
_path = path
259+
_name = name
260+
_value = value
261+
_domain = canonicalDomain
262+
262263
if let
263264
secureString = properties[NSHTTPCookieSecure] as? String
264265
where secureString.characters.count > 0
265266
{
266-
secure = true
267+
_secure = true
267268
} else {
268-
secure = false
269+
_secure = false
269270
}
270271

271272
let version: Int
272273
if let
273-
versionString = properties[NSHTTPCookieSecure] as? String
274+
versionString = properties[NSHTTPCookieVersion] as? String
274275
where versionString == "1"
275276
{
276277
version = 1
277278
} else {
278279
version = 0
279280
}
280-
281-
let portList: [NSNumber]?
282-
if let portString = properties[NSHTTPCookiePort] as? String {
283-
portList = portString.characters
281+
_version = version
282+
283+
if let portString = properties[NSHTTPCookiePort] as? String
284+
where _version == 1 {
285+
_portList = portString.characters
284286
.split(",")
285287
.flatMap { Int(String($0)) }
286288
.map { NSNumber(integer: $0) }
287289
} else {
288-
portList = nil
290+
_portList = nil
289291
}
290292

291293
// TODO: factor into a utility function
292-
let expiresDate: NSDate?
293294
if version == 0 {
294295
let expiresProperty = properties[NSHTTPCookieExpires]
295296
if let date = expiresProperty as? NSDate {
296-
// If the dictionary value is already an NSDate,
297-
// nothing left to do
298-
expiresDate = date
297+
_expiresDate = date
299298
} else if let dateString = expiresProperty as? String {
300-
// If the dictionary value is a string, parse it
301299
let formatter = NSDateFormatter()
302-
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm"
303-
300+
formatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss O" // per RFC 6265 '<rfc1123-date, defined in [RFC2616], Section 3.3.1>'
304301
let timeZone = NSTimeZone(abbreviation: "GMT")
305302
formatter.timeZone = timeZone
306-
307-
expiresDate = formatter.dateFromString(dateString)
303+
_expiresDate = formatter.dateFromString(dateString)
308304
} else {
309-
expiresDate = nil
310-
}
311-
} else if version == 1 {
312-
if let
313-
maximumAge = properties[NSHTTPCookieMaximumAge] as? String,
314-
secondsFromNow = Double(maximumAge)
315-
{
316-
expiresDate = NSDate(timeIntervalSinceNow: secondsFromNow)
317-
} else {
318-
expiresDate = nil
305+
_expiresDate = nil
319306
}
307+
} else if let
308+
maximumAge = properties[NSHTTPCookieMaximumAge] as? String,
309+
secondsFromNow = Int(maximumAge)
310+
where _version == 1 {
311+
_expiresDate = NSDate(timeIntervalSinceNow: Double(secondsFromNow))
320312
} else {
321-
expiresDate = nil
313+
_expiresDate = nil
322314
}
323-
324-
var discard = false
315+
316+
325317
if let discardString = properties[NSHTTPCookieDiscard] as? String {
326-
discard = discardString == "TRUE"
327-
} else if let
328-
_ = properties[NSHTTPCookieMaximumAge] as? String
329-
where version >= 1
330-
{
331-
discard = false
318+
_sessionOnly = discardString == "TRUE"
319+
} else {
320+
_sessionOnly = properties[NSHTTPCookieMaximumAge] == nil && version >= 1
332321
}
333-
334-
// TODO: commentURL can be a string or NSURL
335-
336-
self.cookieRepresentation = Cookie(
337-
comment: version == 1 ?
338-
properties[NSHTTPCookieComment] as? String : nil,
339-
commentURL: version == 1 ?
340-
properties[NSHTTPCookieCommentURL] as? NSURL : nil,
341-
domain: canonicalDomain,
342-
expiresDate: expiresDate,
343-
HTTPOnly: secure,
344-
secure: secure,
345-
sessionOnly: discard,
346-
name: name,
347-
path: path,
348-
portList: version == 1 ? portList : nil,
349-
properties: properties,
350-
value: value,
351-
version: version
352-
)
322+
if version == 0 {
323+
_comment = nil
324+
_commentURL = nil
325+
} else {
326+
_comment = properties[NSHTTPCookieComment] as? String
327+
if let commentURL = properties[NSHTTPCookieCommentURL] as? NSURL {
328+
_commentURL = commentURL
329+
} else if let commentURL = properties[NSHTTPCookieCommentURL] as? String {
330+
_commentURL = NSURL(string: commentURL)
331+
} else {
332+
_commentURL = nil
333+
}
334+
}
335+
_HTTPOnly = false
336+
_properties = [NSHTTPCookieComment : properties[NSHTTPCookieComment],
337+
NSHTTPCookieCommentURL : properties[NSHTTPCookieCommentURL],
338+
"Created" : NSDate().timeIntervalSinceReferenceDate, // Cocoa Compatibility
339+
NSHTTPCookieDiscard : _sessionOnly,
340+
NSHTTPCookieDomain : _domain,
341+
NSHTTPCookieExpires : _expiresDate,
342+
NSHTTPCookieMaximumAge : properties[NSHTTPCookieMaximumAge],
343+
NSHTTPCookieName : _name,
344+
NSHTTPCookieOriginURL : properties[NSHTTPCookieOriginURL],
345+
NSHTTPCookiePath : _path,
346+
NSHTTPCookiePort : _portList,
347+
NSHTTPCookieSecure : _secure,
348+
NSHTTPCookieValue : _value,
349+
NSHTTPCookieVersion : _version
350+
]
353351
}
354352

355353
/*!
@@ -376,7 +374,7 @@ public class NSHTTPCookie : NSObject {
376374
*/
377375
public class func requestHeaderFieldsWithCookies(cookies: [NSHTTPCookie]) -> [String : String] {
378376
var cookieString = cookies.reduce("") { (sum, next) -> String in
379-
return sum + "\(next.cookieRepresentation.name)=\(next.cookieRepresentation.value); "
377+
return sum + "\(next._name)=\(next._value); "
380378
}
381379
//Remove the final trailing semicolon and whitespace
382380
if ( cookieString.length > 0 ) {
@@ -412,7 +410,7 @@ public class NSHTTPCookie : NSObject {
412410
/// - Experiment: This is a draft API currently under consideration for official import into Foundation as a suitable alternative
413411
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
414412
public var properties: [String : Any]? {
415-
return self.cookieRepresentation.properties
413+
return _properties
416414
}
417415

418416
/*!
@@ -423,7 +421,7 @@ public class NSHTTPCookie : NSObject {
423421
@result the version of the receiver.
424422
*/
425423
public var version: Int {
426-
return self.cookieRepresentation.version
424+
return _version
427425
}
428426

429427
/*!
@@ -432,7 +430,7 @@ public class NSHTTPCookie : NSObject {
432430
@result the name of the receiver.
433431
*/
434432
public var name: String {
435-
return self.cookieRepresentation.name
433+
return _name
436434
}
437435

438436
/*!
@@ -441,7 +439,7 @@ public class NSHTTPCookie : NSObject {
441439
@result the value of the receiver.
442440
*/
443441
public var value: String {
444-
return self.cookieRepresentation.value
442+
return _value
445443
}
446444

447445
/*!
@@ -454,7 +452,7 @@ public class NSHTTPCookie : NSObject {
454452
@result The expires date of the receiver.
455453
*/
456454
/*@NSCopying*/ public var expiresDate: NSDate? {
457-
return self.cookieRepresentation.expiresDate
455+
return _expiresDate
458456
}
459457

460458
/*!
@@ -465,7 +463,7 @@ public class NSHTTPCookie : NSObject {
465463
be discarded at the end of the session.
466464
*/
467465
public var sessionOnly: Bool {
468-
return self.cookieRepresentation.sessionOnly
466+
return _sessionOnly
469467
}
470468

471469
/*!
@@ -478,7 +476,7 @@ public class NSHTTPCookie : NSObject {
478476
@result The domain of the receiver.
479477
*/
480478
public var domain: String {
481-
return self.cookieRepresentation.domain
479+
return _domain
482480
}
483481

484482
/*!
@@ -490,7 +488,7 @@ public class NSHTTPCookie : NSObject {
490488
@result The path of the receiver.
491489
*/
492490
public var path: String {
493-
return self.cookieRepresentation.path
491+
return _path
494492
}
495493

496494
/*!
@@ -505,7 +503,7 @@ public class NSHTTPCookie : NSObject {
505503
NO otherwise.
506504
*/
507505
public var secure: Bool {
508-
return self.cookieRepresentation.secure
506+
return _secure
509507
}
510508

511509
/*!
@@ -521,7 +519,7 @@ public class NSHTTPCookie : NSObject {
521519
NO otherwise.
522520
*/
523521
public var HTTPOnly: Bool {
524-
return self.cookieRepresentation.HTTPOnly
522+
return _HTTPOnly
525523
}
526524

527525
/*!
@@ -534,7 +532,7 @@ public class NSHTTPCookie : NSObject {
534532
comment.
535533
*/
536534
public var comment: String? {
537-
return self.cookieRepresentation.comment
535+
return _comment
538536
}
539537

540538
/*!
@@ -547,7 +545,7 @@ public class NSHTTPCookie : NSObject {
547545
has no comment URL.
548546
*/
549547
/*@NSCopying*/ public var commentURL: NSURL? {
550-
return self.cookieRepresentation.commentURL
548+
return _commentURL
551549
}
552550

553551
/*!
@@ -562,7 +560,7 @@ public class NSHTTPCookie : NSObject {
562560
port.
563561
*/
564562
public var portList: [NSNumber]? {
565-
return self.cookieRepresentation.portList
563+
return _portList
566564
}
567565
}
568566

0 commit comments

Comments
 (0)