@@ -57,6 +57,8 @@ internal final class _EasyHandle {
5757 fileprivate var pauseState : _PauseState = [ ]
5858 internal var timeoutTimer : _TimeoutSource !
5959 internal lazy var errorBuffer = [ UInt8] ( repeating: 0 , count: Int ( CFURLSessionEasyErrorSize) )
60+ internal var _config : URLSession . _Configuration ? = nil
61+ internal var _url : URL ? = nil
6062
6163 init ( delegate: _EasyHandleDelegate ) {
6264 self . delegate = delegate
@@ -154,10 +156,16 @@ extension _EasyHandle {
154156 /// URL to use in the request
155157 /// - SeeAlso: https://curl.haxx.se/libcurl/c/CURLOPT_URL.html
156158 func set( url: URL ) {
159+ _url = url
157160 url. absoluteString. withCString {
158161 try ! CFURLSession_easy_setopt_ptr ( rawHandle, CFURLSessionOptionURL, UnsafeMutablePointer ( mutating: $0) ) . asError ( )
159162 }
160163 }
164+
165+ func set( sessionConfig config: URLSession . _Configuration ) {
166+ _config = config
167+ }
168+
161169 /// Set allowed protocols
162170 ///
163171 /// - Note: This has security implications. Not limiting this, someone could
@@ -512,8 +520,8 @@ fileprivate extension _EasyHandle {
512520 ///
513521 /// - SeeAlso: <https://curl.haxx.se/libcurl/c/CURLOPT_HEADERFUNCTION.html>
514522 func didReceive( headerData data: UnsafeMutablePointer < Int8 > , size: Int , nmemb: Int , contentLength: Double ) -> Int {
523+ let buffer = Data ( bytes: data, count: size*nmemb)
515524 let d : Int = {
516- let buffer = Data ( bytes: data, count: size*nmemb)
517525 switch delegate? . didReceive ( headerData: buffer, contentLength: Int64 ( contentLength) ) {
518526 case . some( . proceed) : return size * nmemb
519527 case . some( . abort) : return 0
@@ -525,8 +533,28 @@ fileprivate extension _EasyHandle {
525533 return 0
526534 }
527535 } ( )
536+ setCookies ( headerData: buffer)
528537 return d
529538 }
539+
540+ fileprivate func setCookies( headerData data: Data ) {
541+ guard let config = _config, config. httpCookieAcceptPolicy != HTTPCookie . AcceptPolicy. never else { return }
542+ guard let headerData = String ( data: data, encoding: String . Encoding. utf8) else { return }
543+ //Convert headerData from a string to a dictionary.
544+ //Ignore headers like 'HTTP/1.1 200 OK\r\n' which do not have a key value pair.
545+ let headerComponents = headerData. split { $0 == " : " }
546+ var headers : [ String : String ] = [ : ]
547+ //Trim the leading and trailing whitespaces (if any) before adding the header information to the dictionary.
548+ if headerComponents. count > 1 {
549+ headers [ String ( headerComponents [ 0 ] . trimmingCharacters ( in: . whitespacesAndNewlines) ) ] = headerComponents [ 1 ] . trimmingCharacters ( in: . whitespacesAndNewlines)
550+ }
551+ let cookies = HTTPCookie . cookies ( withResponseHeaderFields: headers, for: _url!)
552+ guard cookies. count > 0 else { return }
553+ if let cookieStorage = config. httpCookieStorage {
554+ cookieStorage. setCookies ( cookies, for: _url, mainDocumentURL: nil )
555+ }
556+ }
557+
530558 /// This callback function gets called by libcurl when it wants to send data
531559 /// it to the network.
532560 ///
0 commit comments