@@ -57,6 +57,8 @@ internal final class _EasyHandle {
57
57
fileprivate var pauseState : _PauseState = [ ]
58
58
internal var timeoutTimer : _TimeoutSource !
59
59
internal lazy var errorBuffer = [ UInt8] ( repeating: 0 , count: Int ( CFURLSessionEasyErrorSize) )
60
+ internal var _config : URLSession . _Configuration ? = nil
61
+ internal var _url : URL ? = nil
60
62
61
63
init ( delegate: _EasyHandleDelegate ) {
62
64
self . delegate = delegate
@@ -154,10 +156,16 @@ extension _EasyHandle {
154
156
/// URL to use in the request
155
157
/// - SeeAlso: https://curl.haxx.se/libcurl/c/CURLOPT_URL.html
156
158
func set( url: URL ) {
159
+ _url = url
157
160
url. absoluteString. withCString {
158
161
try ! CFURLSession_easy_setopt_ptr ( rawHandle, CFURLSessionOptionURL, UnsafeMutablePointer ( mutating: $0) ) . asError ( )
159
162
}
160
163
}
164
+
165
+ func set( sessionConfig config: URLSession . _Configuration ) {
166
+ _config = config
167
+ }
168
+
161
169
/// Set allowed protocols
162
170
///
163
171
/// - Note: This has security implications. Not limiting this, someone could
@@ -512,8 +520,8 @@ fileprivate extension _EasyHandle {
512
520
///
513
521
/// - SeeAlso: <https://curl.haxx.se/libcurl/c/CURLOPT_HEADERFUNCTION.html>
514
522
func didReceive( headerData data: UnsafeMutablePointer < Int8 > , size: Int , nmemb: Int , contentLength: Double ) -> Int {
523
+ let buffer = Data ( bytes: data, count: size*nmemb)
515
524
let d : Int = {
516
- let buffer = Data ( bytes: data, count: size*nmemb)
517
525
switch delegate? . didReceive ( headerData: buffer, contentLength: Int64 ( contentLength) ) {
518
526
case . some( . proceed) : return size * nmemb
519
527
case . some( . abort) : return 0
@@ -525,8 +533,28 @@ fileprivate extension _EasyHandle {
525
533
return 0
526
534
}
527
535
} ( )
536
+ setCookies ( headerData: buffer)
528
537
return d
529
538
}
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
+
530
558
/// This callback function gets called by libcurl when it wants to send data
531
559
/// it to the network.
532
560
///
0 commit comments