Skip to content

Commit c7c82cd

Browse files
author
Pushkar Kulkarni
committed
implement setCookies with mainDocumentURL
1 parent 87bdd3d commit c7c82cd

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

Foundation/NSHTTPCookieStorage.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,22 @@ open class HTTPCookieStorage: NSObject {
226226
in accordance with policy settings.
227227
*/
228228
open func setCookies(_ cookies: [HTTPCookie], for url: URL?, mainDocumentURL: URL?) {
229+
//if the cookieAcceptPolicy is `never` we don't have anything to do
229230
guard cookieAcceptPolicy != .never else { return }
230-
guard let theUrl = url else { return }
231231

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 }
239242

243+
//save only those cookies whose domain matches with the url.host
244+
let validCookies = cookies.filter { urlHost == $0.domain }
240245
for cookie in validCookies {
241246
setCookie(cookie)
242247
}

0 commit comments

Comments
 (0)