@@ -226,17 +226,22 @@ open class HTTPCookieStorage: NSObject {
226
226
in accordance with policy settings.
227
227
*/
228
228
open func setCookies( _ cookies: [ HTTPCookie ] , for url: URL ? , mainDocumentURL: URL ? ) {
229
+ //if the cookieAcceptPolicy is `never` we don't have anything to do
229
230
guard cookieAcceptPolicy != . never else { return }
230
- guard let theUrl = url else { return }
231
231
232
- var validCookies = [ HTTPCookie] ( )
233
- if mainDocumentURL != nil && cookieAcceptPolicy == . onlyFromMainDocumentDomain {
234
- //TODO: needs a careful study of the behaviour on Darwin
235
- NSUnimplemented ( )
236
- } else {
237
- validCookies = cookies. filter { theUrl. host != nil && theUrl. host!. hasSuffix ( $0. domain) }
238
- }
232
+ //if the urls don't have a host, we cannot do anything
233
+ guard let urlHost = url? . host else { return }
234
+ guard mainDocumentURL != nil , let mainDocumentHost = mainDocumentURL!. host else { return }
235
+
236
+ //if the mainDocumentURL is non-nil then the cookieAcceptPolicy must be `onlyFromMainDocumentDomain` only
237
+ guard mainDocumentURL != nil && cookieAcceptPolicy == . onlyFromMainDocumentDomain else { return }
238
+
239
+ //if the mainDocumentURL is non-nil, the url.host must be a suffix of manDocumentURL.host
240
+ //this is based on Darwin's behaviour
241
+ guard mainDocumentURL != nil && mainDocumentHost. hasSuffix ( urlHost) else { return }
239
242
243
+ //save only those cookies whose domain matches with the url.host
244
+ let validCookies = cookies. filter { urlHost == $0. domain }
240
245
for cookie in validCookies {
241
246
setCookie ( cookie)
242
247
}
0 commit comments