Skip to content

Commit 65323ad

Browse files
committed
Merge PR 182
2 parents 9311cdd + 21282c5 commit 65323ad

File tree

1 file changed

+88
-89
lines changed

1 file changed

+88
-89
lines changed

Foundation/NSHTTPCookie.swift

Lines changed: 88 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,20 @@ public let NSHTTPCookiePort: String = "Port"
5454
/// the various cookie attributes. It has accessors to get the various
5555
/// attributes of a cookie.
5656
public class NSHTTPCookie : NSObject {
57-
private struct Cookie {
58-
let comment: String?
59-
let commentURL: NSURL?
60-
let domain: String
61-
let expiresDate: NSDate?
62-
let HTTPOnly: Bool
63-
let secure: Bool
64-
let sessionOnly: Bool
65-
let name: String
66-
let path: String
67-
let portList: [NSNumber]?
68-
let properties: [String: Any]?
69-
let value: String
70-
let version: Int
71-
}
72-
private let cookieRepresentation: Cookie
57+
58+
let _comment: String?
59+
let _commentURL: NSURL?
60+
let _domain: String
61+
let _expiresDate: NSDate?
62+
let _HTTPOnly: Bool
63+
let _secure: Bool
64+
let _sessionOnly: Bool
65+
let _name: String
66+
let _path: String
67+
let _portList: [NSNumber]?
68+
let _value: String
69+
let _version: Int
70+
var _properties: [String : Any]
7371

7472
/// Initialize a NSHTTPCookie object with a dictionary of parameters
7573
///
@@ -204,7 +202,7 @@ public class NSHTTPCookie : NSObject {
204202
else {
205203
return nil
206204
}
207-
205+
208206
let canonicalDomain: String
209207
if let domain = properties[NSHTTPCookieDomain] as? String {
210208
canonicalDomain = domain
@@ -216,99 +214,100 @@ public class NSHTTPCookie : NSObject {
216214
} else {
217215
return nil
218216
}
219-
220-
let secure: Bool
217+
218+
_path = path
219+
_name = name
220+
_value = value
221+
_domain = canonicalDomain
222+
221223
if let
222224
secureString = properties[NSHTTPCookieSecure] as? String
223225
where secureString.characters.count > 0
224226
{
225-
secure = true
227+
_secure = true
226228
} else {
227-
secure = false
229+
_secure = false
228230
}
229231

230232
let version: Int
231233
if let
232-
versionString = properties[NSHTTPCookieSecure] as? String
234+
versionString = properties[NSHTTPCookieVersion] as? String
233235
where versionString == "1"
234236
{
235237
version = 1
236238
} else {
237239
version = 0
238240
}
239-
240-
let portList: [NSNumber]?
241-
if let portString = properties[NSHTTPCookiePort] as? String {
242-
portList = portString.characters
241+
_version = version
242+
243+
if let portString = properties[NSHTTPCookiePort] as? String
244+
where _version == 1 {
245+
_portList = portString.characters
243246
.split(separator: ",")
244247
.flatMap { Int(String($0)) }
245248
.map { NSNumber(integer: $0) }
246249
} else {
247-
portList = nil
250+
_portList = nil
248251
}
249252

250253
// TODO: factor into a utility function
251-
let expiresDate: NSDate?
252254
if version == 0 {
253255
let expiresProperty = properties[NSHTTPCookieExpires]
254256
if let date = expiresProperty as? NSDate {
255-
// If the dictionary value is already an NSDate,
256-
// nothing left to do
257-
expiresDate = date
257+
_expiresDate = date
258258
} else if let dateString = expiresProperty as? String {
259-
// If the dictionary value is a string, parse it
260259
let formatter = NSDateFormatter()
261-
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm"
262-
260+
formatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss O" // per RFC 6265 '<rfc1123-date, defined in [RFC2616], Section 3.3.1>'
263261
let timeZone = NSTimeZone(abbreviation: "GMT")
264262
formatter.timeZone = timeZone
265-
266-
expiresDate = formatter.dateFromString(dateString)
263+
_expiresDate = formatter.dateFromString(dateString)
267264
} else {
268-
expiresDate = nil
269-
}
270-
} else if version == 1 {
271-
if let
272-
maximumAge = properties[NSHTTPCookieMaximumAge] as? String,
273-
secondsFromNow = Double(maximumAge)
274-
{
275-
expiresDate = NSDate(timeIntervalSinceNow: secondsFromNow)
276-
} else {
277-
expiresDate = nil
265+
_expiresDate = nil
278266
}
267+
} else if let
268+
maximumAge = properties[NSHTTPCookieMaximumAge] as? String,
269+
secondsFromNow = Int(maximumAge)
270+
where _version == 1 {
271+
_expiresDate = NSDate(timeIntervalSinceNow: Double(secondsFromNow))
279272
} else {
280-
expiresDate = nil
273+
_expiresDate = nil
281274
}
282-
283-
var discard = false
275+
276+
284277
if let discardString = properties[NSHTTPCookieDiscard] as? String {
285-
discard = discardString == "TRUE"
286-
} else if let
287-
_ = properties[NSHTTPCookieMaximumAge] as? String
288-
where version >= 1
289-
{
290-
discard = false
278+
_sessionOnly = discardString == "TRUE"
279+
} else {
280+
_sessionOnly = properties[NSHTTPCookieMaximumAge] == nil && version >= 1
291281
}
292-
293-
// TODO: commentURL can be a string or NSURL
294-
295-
self.cookieRepresentation = Cookie(
296-
comment: version == 1 ?
297-
properties[NSHTTPCookieComment] as? String : nil,
298-
commentURL: version == 1 ?
299-
properties[NSHTTPCookieCommentURL] as? NSURL : nil,
300-
domain: canonicalDomain,
301-
expiresDate: expiresDate,
302-
HTTPOnly: secure,
303-
secure: secure,
304-
sessionOnly: discard,
305-
name: name,
306-
path: path,
307-
portList: version == 1 ? portList : nil,
308-
properties: properties,
309-
value: value,
310-
version: version
311-
)
282+
if version == 0 {
283+
_comment = nil
284+
_commentURL = nil
285+
} else {
286+
_comment = properties[NSHTTPCookieComment] as? String
287+
if let commentURL = properties[NSHTTPCookieCommentURL] as? NSURL {
288+
_commentURL = commentURL
289+
} else if let commentURL = properties[NSHTTPCookieCommentURL] as? String {
290+
_commentURL = NSURL(string: commentURL)
291+
} else {
292+
_commentURL = nil
293+
}
294+
}
295+
_HTTPOnly = false
296+
_properties = [NSHTTPCookieComment : properties[NSHTTPCookieComment],
297+
NSHTTPCookieCommentURL : properties[NSHTTPCookieCommentURL],
298+
"Created" : NSDate().timeIntervalSinceReferenceDate, // Cocoa Compatibility
299+
NSHTTPCookieDiscard : _sessionOnly,
300+
NSHTTPCookieDomain : _domain,
301+
NSHTTPCookieExpires : _expiresDate,
302+
NSHTTPCookieMaximumAge : properties[NSHTTPCookieMaximumAge],
303+
NSHTTPCookieName : _name,
304+
NSHTTPCookieOriginURL : properties[NSHTTPCookieOriginURL],
305+
NSHTTPCookiePath : _path,
306+
NSHTTPCookiePort : _portList,
307+
NSHTTPCookieSecure : _secure,
308+
NSHTTPCookieValue : _value,
309+
NSHTTPCookieVersion : _version
310+
]
312311
}
313312

314313
/// Return a dictionary of header fields that can be used to add the
@@ -319,7 +318,7 @@ public class NSHTTPCookie : NSObject {
319318
/// are the corresponding header field values.
320319
public class func requestHeaderFields(with cookies: [NSHTTPCookie]) -> [String : String] {
321320
var cookieString = cookies.reduce("") { (sum, next) -> String in
322-
return sum + "\(next.cookieRepresentation.name)=\(next.cookieRepresentation.value); "
321+
return sum + "\(next._name)=\(next._value); "
323322
}
324323
//Remove the final trailing semicolon and whitespace
325324
if ( cookieString.length > 0 ) {
@@ -351,25 +350,25 @@ public class NSHTTPCookie : NSObject {
351350
/// - Experiment: This is a draft API currently under consideration for official import into Foundation as a suitable alternative
352351
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
353352
public var properties: [String : Any]? {
354-
return self.cookieRepresentation.properties
353+
return _properties
355354
}
356355

357356
/// The version of the receiver.
358357
///
359358
/// Version 0 maps to "old-style" Netscape cookies.
360359
/// Version 1 maps to RFC2965 cookies. There may be future versions.
361360
public var version: Int {
362-
return self.cookieRepresentation.version
361+
return _version
363362
}
364363

365364
/// The name of the receiver.
366365
public var name: String {
367-
return self.cookieRepresentation.name
366+
return _name
368367
}
369368

370369
/// The value of the receiver.
371370
public var value: String {
372-
return self.cookieRepresentation.value
371+
return _value
373372
}
374373

375374
/// Returns The expires date of the receiver.
@@ -378,7 +377,7 @@ public class NSHTTPCookie : NSObject {
378377
/// deleted. The result will be nil if there is no specific expires
379378
/// date. This will be the case only for *session-only* cookies.
380379
/*@NSCopying*/ public var expiresDate: NSDate? {
381-
return self.cookieRepresentation.expiresDate
380+
return _expiresDate
382381
}
383382

384383
/// Whether the receiver is session-only.
@@ -387,7 +386,7 @@ public class NSHTTPCookie : NSObject {
387386
/// session (regardless of expiration date), `false` if receiver need not
388387
/// be discarded at the end of the session.
389388
public var isSessionOnly: Bool {
390-
return self.cookieRepresentation.sessionOnly
389+
return _sessionOnly
391390
}
392391

393392
/// The domain of the receiver.
@@ -397,7 +396,7 @@ public class NSHTTPCookie : NSObject {
397396
/// should be sent to subdomains as well, assuming certain other
398397
/// restrictions are valid. See RFC 2965 for more detail.
399398
public var domain: String {
400-
return self.cookieRepresentation.domain
399+
return _domain
401400
}
402401

403402
/// The path of the receiver.
@@ -406,7 +405,7 @@ public class NSHTTPCookie : NSObject {
406405
/// domain for which this cookie should be sent. The cookie will also
407406
/// be sent for children of that path, so `"/"` is the most general.
408407
public var path: String {
409-
return self.cookieRepresentation.path
408+
return _path
410409
}
411410

412411
/// Whether the receiver should be sent only over secure channels
@@ -416,7 +415,7 @@ public class NSHTTPCookie : NSObject {
416415
/// trusted servers (i.e. via SSL or TLS), and should not be delievered to any
417416
/// javascript applications to prevent cross-site scripting vulnerabilities.
418417
public var isSecure: Bool {
419-
return self.cookieRepresentation.secure
418+
return _secure
420419
}
421420

422421
/// Whether the receiver should only be sent to HTTP servers per RFC 2965
@@ -427,7 +426,7 @@ public class NSHTTPCookie : NSObject {
427426
/// Specifically these cookies should not be delivered to any javascript
428427
/// applications to prevent cross-site scripting vulnerabilities.
429428
public var isHTTPOnly: Bool {
430-
return self.cookieRepresentation.HTTPOnly
429+
return _HTTPOnly
431430
}
432431

433432
/// The comment of the receiver.
@@ -436,7 +435,7 @@ public class NSHTTPCookie : NSObject {
436435
/// presentation to the user explaining the contents and purpose of this
437436
/// cookie. It may be nil.
438437
public var comment: String? {
439-
return self.cookieRepresentation.comment
438+
return _comment
440439
}
441440

442441
/// The comment URL of the receiver.
@@ -445,7 +444,7 @@ public class NSHTTPCookie : NSObject {
445444
/// presentation to the user as a link for further information about
446445
/// this cookie. It may be nil.
447446
/*@NSCopying*/ public var commentURL: NSURL? {
448-
return self.cookieRepresentation.commentURL
447+
return _commentURL
449448
}
450449

451450
/// The list ports to which the receiver should be sent.
@@ -457,6 +456,6 @@ public class NSHTTPCookie : NSObject {
457456
/// The array may be nil, in which case this cookie can be sent to any
458457
/// port.
459458
public var portList: [NSNumber]? {
460-
return self.cookieRepresentation.portList
459+
return _portList
461460
}
462461
}

0 commit comments

Comments
 (0)