func setCookie()

in Sources/Instrumentation/WKWebView/Cookie/WebViewCookieManager.swift [101:139]


    func setCookie(params: [String: Any], callback: ((_ response: [String: Any]?) -> ())?) {
        guard let cookies = params["cookie"] as? String else {
            return
        }
        
        var properties = [HTTPCookiePropertyKey: Any]()
        let segments = cookies.components(separatedBy: ";")
        for (idx, sgmt) in segments.enumerated() {
            let trimmedSgmt = sgmt.trimmingCharacters(in: CharacterSet.whitespaces)
            let keyValues = trimmedSgmt.components(separatedBy: "=")

            if (keyValues.count == 2 && keyValues[0].count > 0){
                let key = keyValues[0].trimmingCharacters(in: CharacterSet.whitespaces)
                let value = keyValues[1].trimmingCharacters(in: CharacterSet.whitespaces)
                
                if (0 == idx) {
                    properties[HTTPCookiePropertyKey.name] = key
                    properties[HTTPCookiePropertyKey.value] = value
                } else if (key == "domain") {
                    properties[HTTPCookiePropertyKey.domain] = value
                } else if (key == "path") {
                    properties[HTTPCookiePropertyKey.path] = value
                } else if (key == "expires") {
                    properties[HTTPCookiePropertyKey.expires] = WebViewCookieManager.dateFormatter.date(from: value)
                }
            } else if (keyValues.count == 1 && keyValues[0].count > 0) {
                let key = keyValues[0].trimmingCharacters(in: CharacterSet.whitespaces)
                if (key == "Secure") {
                    properties[HTTPCookiePropertyKey.secure] = true
                }
            }
        }
        
        if (properties.count > 0) {
            if let cookieObject: HTTPCookie = HTTPCookie.init(properties: properties) {
                HTTPCookieStorage.shared.setCookie(cookieObject)
            }
        }
    }