Skip to content

Commit 855f90f

Browse files
authored
Merge pull request #4962 from parkera/parkera/change_curl_feature_availability
Update the way we check for libcurl feature availability
2 parents 927e685 + ce23015 commit 855f90f

File tree

6 files changed

+72
-56
lines changed

6 files changed

+72
-56
lines changed

Sources/FoundationNetworking/URLSession/URLSessionConfiguration.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,9 @@ open class URLSessionConfiguration : NSObject, NSCopying {
216216
Note that these headers are added to the request only if not already present. */
217217
open var httpAdditionalHeaders: [AnyHashable : Any]? = nil
218218

219-
#if NS_CURL_MISSING_MAX_HOST_CONNECTIONS
220219
/* The maximum number of simultaneous persistent connections per host */
221-
@available(*, deprecated, message: "This platform doles not support selecting the maximum number of simultaneous persistent connections per host. This property is ignored.")
220+
/* On platforms with NS_CURL_MISSING_MAX_HOST_CONNECTIONS, this property is ignored. */
222221
open var httpMaximumConnectionsPerHost: Int
223-
#else
224-
/* The maximum number of simultaneous persistent connections per host */
225-
open var httpMaximumConnectionsPerHost: Int
226-
#endif
227222

228223
/* The cookie storage object to use, or nil to indicate that no cookies should be handled */
229224
open var httpCookieStorage: HTTPCookieStorage?

Sources/FoundationNetworking/URLSession/URLSessionTask.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,8 @@ open class URLSessionTask : NSObject, NSCopying {
3838
didSet { updateProgress() }
3939
}
4040

41-
#if NS_CURL_MISSING_XFERINFOFUNCTION
42-
@available(*, deprecated, message: "This platform doesn't fully support reporting the progress of a URLSessionTask. The progress instance returned will be functional, but may not have continuous updates as bytes are sent or received.")
41+
/* On platforms with NS_CURL_XFERINFOFUNCTION_SUPPORTED not set, the progress instance returned will be functional, but may not have continuous updates as bytes are sent or received. */
4342
open private(set) var progress = Progress(totalUnitCount: -1)
44-
#else
45-
open private(set) var progress = Progress(totalUnitCount: -1)
46-
#endif
4743

4844
func updateProgress() {
4945
self.workQueue.async {

Sources/FoundationNetworking/URLSession/libcurl/EasyHandle.swift

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -209,40 +209,39 @@ extension _EasyHandle {
209209
}
210210
#endif
211211

212-
#if !NS_CURL_MISSING_CURLINFO_CAINFO
213212
#if !os(Windows) && !os(macOS) && !os(iOS) && !os(watchOS) && !os(tvOS)
214-
// Check if there is a default path; if there is, it will already
215-
// be set, so leave things alone
216-
var p: UnsafeMutablePointer<Int8>? = nil
217-
218-
try! CFURLSession_easy_getinfo_charp(rawHandle, CFURLSessionInfoCAINFO, &p).asError()
219-
220-
if p != nil {
221-
return
222-
}
223-
224-
// Otherwise, search a list of known paths
225-
let paths = [
226-
"/etc/ssl/certs/ca-certificates.crt",
227-
"/etc/pki/tls/certs/ca-bundle.crt",
228-
"/usr/share/ssl/certs/ca-bundle.crt",
229-
"/usr/local/share/certs/ca-root-nss.crt",
230-
"/etc/ssl/cert.pem"
231-
]
232-
233-
for path in paths {
234-
var isDirectory: ObjCBool = false
235-
if FileManager.default.fileExists(atPath: path,
236-
isDirectory: &isDirectory)
237-
&& !isDirectory.boolValue {
238-
path.withCString { pathPtr in
239-
try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionCAINFO, UnsafeMutablePointer(mutating: pathPtr)).asError()
240-
}
241-
return
242-
}
213+
if NS_CURL_CURLINFO_CAINFO_SUPPORTED == 1 {
214+
// Check if there is a default path; if there is, it will already
215+
// be set, so leave things alone
216+
var p: UnsafeMutablePointer<Int8>? = nil
217+
218+
try! CFURLSession_easy_getinfo_charp(rawHandle, CFURLSessionInfoCAINFO, &p).asError()
219+
if p != nil {
220+
return
221+
}
222+
223+
// Otherwise, search a list of known paths
224+
let paths = [
225+
"/etc/ssl/certs/ca-certificates.crt",
226+
"/etc/pki/tls/certs/ca-bundle.crt",
227+
"/usr/share/ssl/certs/ca-bundle.crt",
228+
"/usr/local/share/certs/ca-root-nss.crt",
229+
"/etc/ssl/cert.pem"
230+
]
231+
232+
for path in paths {
233+
var isDirectory: ObjCBool = false
234+
if FileManager.default.fileExists(atPath: path,
235+
isDirectory: &isDirectory)
236+
&& !isDirectory.boolValue {
237+
path.withCString { pathPtr in
238+
try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionCAINFO, UnsafeMutablePointer(mutating: pathPtr)).asError()
239+
}
240+
return
241+
}
242+
}
243243
}
244244
#endif // !os(Windows) && !os(macOS) && !os(iOS) && !os(watchOS) && !os(tvOS)
245-
#endif // !NS_CURL_MISSING_CURLINFO_CAINFO
246245
}
247246

248247
/// Set allowed protocols
@@ -626,13 +625,13 @@ fileprivate extension _EasyHandle {
626625

627626
try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionPROGRESSDATA, UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())).asError()
628627

629-
#if !NS_CURL_MISSING_XFERINFOFUNCTION
630-
try! CFURLSession_easy_setopt_tc(rawHandle, CFURLSessionOptionXFERINFOFUNCTION, { (userdata: UnsafeMutableRawPointer?, dltotal :Int64, dlnow: Int64, ultotal: Int64, ulnow: Int64) -> Int32 in
631-
guard let handle = _EasyHandle.from(callbackUserData: userdata) else { return -1 }
632-
handle.updateProgressMeter(with: _Progress(totalBytesSent: ulnow, totalBytesExpectedToSend: ultotal, totalBytesReceived: dlnow, totalBytesExpectedToReceive: dltotal))
633-
return 0
634-
}).asError()
635-
#endif
628+
if NS_CURL_XFERINFOFUNCTION_SUPPORTED == 1 {
629+
try! CFURLSession_easy_setopt_tc(rawHandle, CFURLSessionOptionXFERINFOFUNCTION, { (userdata: UnsafeMutableRawPointer?, dltotal :Int64, dlnow: Int64, ultotal: Int64, ulnow: Int64) -> Int32 in
630+
guard let handle = _EasyHandle.from(callbackUserData: userdata) else { return -1 }
631+
handle.updateProgressMeter(with: _Progress(totalBytesSent: ulnow, totalBytesExpectedToSend: ultotal, totalBytesReceived: dlnow, totalBytesExpectedToReceive: dltotal))
632+
return 0
633+
}).asError()
634+
}
636635

637636
}
638637
/// This callback function gets called by libcurl when it receives body

Sources/FoundationNetworking/URLSession/libcurl/MultiHandle.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ extension URLSession {
6565

6666
extension URLSession._MultiHandle {
6767
func configure(with configuration: URLSession._Configuration) {
68-
#if !NS_CURL_MISSING_MAX_HOST_CONNECTIONS
69-
try! CFURLSession_multi_setopt_l(rawHandle, CFURLSessionMultiOptionMAX_HOST_CONNECTIONS, numericCast(configuration.httpMaximumConnectionsPerHost)).asError()
70-
#endif
68+
if NS_CURL_MAX_HOST_CONNECTIONS_SUPPORTED == 1 {
69+
try! CFURLSession_multi_setopt_l(rawHandle, CFURLSessionMultiOptionMAX_HOST_CONNECTIONS, numericCast(configuration.httpMaximumConnectionsPerHost)).asError()
70+
}
7171

7272
try! CFURLSession_multi_setopt_l(rawHandle, CFURLSessionMultiOptionPIPELINING, configuration.httpShouldUsePipelining ? 3 : 2).asError()
7373
//TODO: We may want to set

Sources/_CFURLSessionInterface/CFURLSessionInterface.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,10 @@ CFURLSessionOption const CFURLSessionOptionTCP_KEEPIDLE = { CURLOPT_TCP_KEEPIDLE
540540
CFURLSessionOption const CFURLSessionOptionTCP_KEEPINTVL = { CURLOPT_TCP_KEEPINTVL };
541541
CFURLSessionOption const CFURLSessionOptionSSL_OPTIONS = { CURLOPT_SSL_OPTIONS };
542542
CFURLSessionOption const CFURLSessionOptionMAIL_AUTH = { CURLOPT_MAIL_AUTH };
543-
#if !NS_CURL_MISSING_XFERINFOFUNCTION
543+
#if NS_CURL_XFERINFOFUNCTION_SUPPORTED
544544
CFURLSessionOption const CFURLSessionOptionXFERINFOFUNCTION = { CURLOPT_XFERINFOFUNCTION };
545+
#else
546+
CFURLSessionOption const CFURLSessionOptionXFERINFOFUNCTION = { 0 };
545547
#endif
546548

547549
CFURLSessionInfo const CFURLSessionInfoTEXT = { CURLINFO_TEXT };
@@ -586,8 +588,10 @@ CFURLSessionInfo const CFURLSessionInfoFTP_ENTRY_PATH = { CURLINFO_FTP_ENTRY_PAT
586588
CFURLSessionInfo const CFURLSessionInfoREDIRECT_URL = { CURLINFO_REDIRECT_URL };
587589
CFURLSessionInfo const CFURLSessionInfoPRIMARY_IP = { CURLINFO_PRIMARY_IP };
588590
CFURLSessionInfo const CFURLSessionInfoAPPCONNECT_TIME = { CURLINFO_APPCONNECT_TIME };
589-
#if !NS_CURL_MISSING_CURLINFO_CAINFO
591+
#if NS_CURL_CURLINFO_CAINFO_SUPPORTED
590592
CFURLSessionInfo const CFURLSessionInfoCAINFO = { CURLINFO_CAINFO };
593+
#else
594+
CFURLSessionInfo const CFURLSessionInfoCAINFO = { CURLINFO_NONE };
591595
#endif
592596
CFURLSessionInfo const CFURLSessionInfoCERTINFO = { CURLINFO_CERTINFO };
593597
CFURLSessionInfo const CFURLSessionInfoCONDITION_UNMET = { CURLINFO_CONDITION_UNMET };
@@ -607,11 +611,12 @@ CFURLSessionMultiOption const CFURLSessionMultiOptionPIPELINING = { CURLMOPT_PIP
607611
CFURLSessionMultiOption const CFURLSessionMultiOptionTIMERFUNCTION = { CURLMOPT_TIMERFUNCTION };
608612
CFURLSessionMultiOption const CFURLSessionMultiOptionTIMERDATA = { CURLMOPT_TIMERDATA };
609613
CFURLSessionMultiOption const CFURLSessionMultiOptionMAXCONNECTS = { CURLMOPT_MAXCONNECTS };
610-
#if !NS_CURL_MISSING_MAX_HOST_CONNECTIONS
614+
#if NS_CURL_MAX_HOST_CONNECTIONS_SUPPORTED
611615
CFURLSessionMultiOption const CFURLSessionMultiOptionMAX_HOST_CONNECTIONS = { CURLMOPT_MAX_HOST_CONNECTIONS };
616+
#else
617+
CFURLSessionMultiOption const CFURLSessionMultiOptionMAX_HOST_CONNECTIONS = { 0 };
612618
#endif
613619

614-
615620
CFURLSessionMultiCode const CFURLSessionMultiCodeCALL_MULTI_PERFORM = { CURLM_CALL_MULTI_PERFORM };
616621
CFURLSessionMultiCode const CFURLSessionMultiCodeOK = { CURLM_OK };
617622
CFURLSessionMultiCode const CFURLSessionMultiCodeBAD_HANDLE = { CURLM_BAD_HANDLE };

Sources/_CFURLSessionInterface/include/CFURLSessionInterface.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@
3434
#include <winsock2.h>
3535
#endif
3636

37+
// 7.84.0 or later
38+
#if LIBCURL_VERSION_MAJOR > 7 || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR > 84) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR == 84 && LIBCURL_VERSION_PATCH >= 0)
39+
#define NS_CURL_CURLINFO_CAINFO_SUPPORTED 1
40+
#else
41+
#define NS_CURL_CURLINFO_CAINFO_SUPPORTED 0
42+
#endif
43+
44+
// 7.30.0 or later
45+
#if LIBCURL_VERSION_MAJOR > 7 || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR > 30) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR == 30 && LIBCURL_VERSION_PATCH >= 0)
46+
#define NS_CURL_MAX_HOST_CONNECTIONS_SUPPORTED 1
47+
#else
48+
#define NS_CURL_MAX_HOST_CONNECTIONS_SUPPORTED 0
49+
#endif
50+
51+
// 7.32.0 or later
52+
#if LIBCURL_VERSION_MAJOR > 7 || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR > 32) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR == 32 && LIBCURL_VERSION_PATCH >= 0)
53+
#define NS_CURL_XFERINFOFUNCTION_SUPPORTED 1
54+
#else
55+
#define NS_CURL_XFERINFOFUNCTION_SUPPORTED 0
56+
#endif
57+
3758
CF_IMPLICIT_BRIDGING_ENABLED
3859
CF_EXTERN_C_BEGIN
3960

0 commit comments

Comments
 (0)