@@ -266,54 +266,55 @@ open class HTTPCookie : NSObject {
266266 }
267267 _version = version
268268
269- if let portString = properties [ . port] as? String , _version == 1 {
270- _portList = portString. split ( separator: " , " )
269+ if let portString = properties [ . port] as? String {
270+ let portList = portString. split ( separator: " , " )
271271 . compactMap { Int ( String ( $0) ) }
272272 . map { NSNumber ( value: $0) }
273+ if version == 1 {
274+ _portList = portList
275+ } else {
276+ // Version 0 only stores a single port number
277+ _portList = portList. count > 0 ? [ portList [ 0 ] ] : nil
278+ }
273279 } else {
274280 _portList = nil
275281 }
276282
277- // TODO: factor into a utility function
278- if version == 0 {
283+ var expDate : Date ? = nil
284+ // Maximum-Age is prefered over expires-Date but only version 1 cookies use Maximum-Age
285+ if let maximumAge = properties [ . maximumAge] as? String ,
286+ let secondsFromNow = Int ( maximumAge) {
287+ if version == 1 {
288+ expDate = Date ( timeIntervalSinceNow: Double ( secondsFromNow) )
289+ }
290+ } else {
279291 let expiresProperty = properties [ . expires]
280292 if let date = expiresProperty as? Date {
281- _expiresDate = date
293+ expDate = date
282294 } else if let dateString = expiresProperty as? String {
283295 let formatter = DateFormatter ( )
284296 formatter. locale = Locale ( identifier: " en_US_POSIX " )
285297 formatter. dateFormat = " EEE, dd MMM yyyy HH:mm:ss O " // per RFC 6265 '<rfc1123-date, defined in [RFC2616], Section 3.3.1>'
286298 let timeZone = TimeZone ( abbreviation: " GMT " )
287299 formatter. timeZone = timeZone
288- _expiresDate = formatter. date ( from: dateString)
289- } else {
290- _expiresDate = nil
300+ expDate = formatter. date ( from: dateString)
291301 }
292- } else if
293- let maximumAge = properties [ . maximumAge] as? String ,
294- let secondsFromNow = Int ( maximumAge) , _version == 1 {
295- _expiresDate = Date ( timeIntervalSinceNow: Double ( secondsFromNow) )
296- } else {
297- _expiresDate = nil
298302 }
303+ _expiresDate = expDate
299304
300305 if let discardString = properties [ . discard] as? String {
301306 _sessionOnly = discardString == " TRUE "
302307 } else {
303308 _sessionOnly = properties [ . maximumAge] == nil && version >= 1
304309 }
305- if version == 0 {
306- _comment = nil
307- _commentURL = nil
310+
311+ _comment = properties [ . comment] as? String
312+ if let commentURL = properties [ . commentURL] as? URL {
313+ _commentURL = commentURL
314+ } else if let commentURL = properties [ . commentURL] as? String {
315+ _commentURL = URL ( string: commentURL)
308316 } else {
309- _comment = properties [ . comment] as? String
310- if let commentURL = properties [ . commentURL] as? URL {
311- _commentURL = commentURL
312- } else if let commentURL = properties [ . commentURL] as? String {
313- _commentURL = URL ( string: commentURL)
314- } else {
315- _commentURL = nil
316- }
317+ _commentURL = nil
317318 }
318319 _HTTPOnly = false
319320
@@ -363,7 +364,11 @@ open class HTTPCookie : NSObject {
363364 cookieString. removeLast ( )
364365 cookieString. removeLast ( )
365366 }
366- return [ " Cookie " : cookieString]
367+ if cookieString == " " {
368+ return [ : ]
369+ } else {
370+ return [ " Cookie " : cookieString]
371+ }
367372 }
368373
369374 /// Return an array of cookies parsed from the specified response header fields and URL.
@@ -418,9 +423,9 @@ open class HTTPCookie : NSObject {
418423 properties [ canonicalize ( name) ] = value
419424 }
420425
421- //if domain wasn't provided use the URL
426+ // If domain wasn't provided, extract it from the URL
422427 if properties [ . domain] == nil {
423- properties [ . domain] = url. absoluteString
428+ properties [ . domain] = url. host
424429 }
425430
426431 //the default Path is "/"
0 commit comments