Skip to content

Commit 9a9402b

Browse files
authored
Merge pull request #2770 from drexin/wip-older-curl-backport
[5.2] Support Linux variants with older libcurl
2 parents 420413f + 9daac3d commit 9a9402b

9 files changed

+55
-49
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ if(CMAKE_VERSION VERSION_LESS 3.16.0)
2525
endif()
2626

2727
option(BUILD_SHARED_LIBS "build shared libraries" ON)
28+
option(NS_CURL_ASSUME_FEATURES_MISSING "Assume that optional libcurl features are missing rather than test the library's version, for build debugging" NO)
2829

2930
find_package(CURL CONFIG)
3031
if(CURL_FOUND)

CoreFoundation/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,16 @@ if(CMAKE_SYSTEM_NAME STREQUAL Android)
391391
log)
392392
endif()
393393

394+
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
395+
if((NS_CURL_ASSUME_FEATURES_MISSING) OR (CURL_VERSION_STRING VERSION_LESS "7.32.0"))
396+
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:NS_CURL_MISSING_XFERINFOFUNCTION>)
397+
endif()
398+
399+
if((NS_CURL_ASSUME_FEATURES_MISSING) OR (CURL_VERSION_STRING VERSION_LESS "7.30.0"))
400+
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:NS_CURL_MISSING_MAX_HOST_CONNECTIONS>)
401+
endif()
402+
endif()
403+
394404
add_framework(CFURLSessionInterface
395405
${FRAMEWORK_LIBRARY_TYPE}
396406
FRAMEWORK_DIRECTORY

CoreFoundation/URL.subproj/CFURLSessionInterface.c

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ CFURLSessionEasyCode const CFURLSessionEasyCodeFTP_ACCEPT_TIMEOUT = { CURLE_FTP_
157157
CFURLSessionEasyCode const CFURLSessionEasyCodeFTP_WEIRD_PASV_REPLY = { CURLE_FTP_WEIRD_PASV_REPLY };
158158
CFURLSessionEasyCode const CFURLSessionEasyCodeFTP_WEIRD_227_FORMAT = { CURLE_FTP_WEIRD_227_FORMAT };
159159
CFURLSessionEasyCode const CFURLSessionEasyCodeFTP_CANT_GET_HOST = { CURLE_FTP_CANT_GET_HOST };
160-
//CFURLSessionEasyCode const CFURLSessionEasyCodeHTTP2 = { CURLE_HTTP2 };
161160
CFURLSessionEasyCode const CFURLSessionEasyCodeFTP_COULDNT_SET_TYPE = { CURLE_FTP_COULDNT_SET_TYPE };
162161
CFURLSessionEasyCode const CFURLSessionEasyCodePARTIAL_FILE = { CURLE_PARTIAL_FILE };
163162
CFURLSessionEasyCode const CFURLSessionEasyCodeFTP_COULDNT_RETR_FILE = { CURLE_FTP_COULDNT_RETR_FILE };
@@ -185,7 +184,6 @@ CFURLSessionEasyCode const CFURLSessionEasyCodeBAD_DOWNLOAD_RESUME = { CURLE_BAD
185184
CFURLSessionEasyCode const CFURLSessionEasyCodeFILE_COULDNT_READ_FILE = { CURLE_FILE_COULDNT_READ_FILE };
186185
CFURLSessionEasyCode const CFURLSessionEasyCodeLDAP_CANNOT_BIND = { CURLE_LDAP_CANNOT_BIND };
187186
CFURLSessionEasyCode const CFURLSessionEasyCodeLDAP_SEARCH_FAILED = { CURLE_LDAP_SEARCH_FAILED };
188-
//CFURLSessionEasyCode const CFURLSessionEasyCodeOBSOLETE40 = { CURLE_OBSOLETE40 };
189187
CFURLSessionEasyCode const CFURLSessionEasyCodeFUNCTION_NOT_FOUND = { CURLE_FUNCTION_NOT_FOUND };
190188
CFURLSessionEasyCode const CFURLSessionEasyCodeABORTED_BY_CALLBACK = { CURLE_ABORTED_BY_CALLBACK };
191189
CFURLSessionEasyCode const CFURLSessionEasyCodeBAD_FUNCTION_ARGUMENT = { CURLE_BAD_FUNCTION_ARGUMENT };
@@ -234,10 +232,6 @@ CFURLSessionEasyCode const CFURLSessionEasyCodeRTSP_CSEQ_ERROR = { CURLE_RTSP_CS
234232
CFURLSessionEasyCode const CFURLSessionEasyCodeRTSP_SESSION_ERROR = { CURLE_RTSP_SESSION_ERROR };
235233
CFURLSessionEasyCode const CFURLSessionEasyCodeFTP_BAD_FILE_LIST = { CURLE_FTP_BAD_FILE_LIST };
236234
CFURLSessionEasyCode const CFURLSessionEasyCodeCHUNK_FAILED = { CURLE_CHUNK_FAILED };
237-
CFURLSessionEasyCode const CFURLSessionEasyCodeNO_CONNECTION_AVAILABLE = { CURLE_NO_CONNECTION_AVAILABLE };
238-
//CFURLSessionEasyCode const CFURLSessionEasyCodeSSL_PINNEDPUBKEYNOTMATCH = { CURLE_SSL_PINNEDPUBKEYNOTMATCH };
239-
//CFURLSessionEasyCode const CFURLSessionEasyCodeSSL_INVALIDCERTSTATUS = { CURLE_SSL_INVALIDCERTSTATUS };
240-
241235

242236
CFURLSessionProtocol const CFURLSessionProtocolHTTP = CURLPROTO_HTTP;
243237
CFURLSessionProtocol const CFURLSessionProtocolHTTPS = CURLPROTO_HTTPS;
@@ -265,8 +259,6 @@ CFURLSessionProtocol const CFURLSessionProtocolRTMPTE = CURLPROTO_RTMPTE;
265259
CFURLSessionProtocol const CFURLSessionProtocolRTMPS = CURLPROTO_RTMPS;
266260
CFURLSessionProtocol const CFURLSessionProtocolRTMPTS = CURLPROTO_RTMPTS;
267261
CFURLSessionProtocol const CFURLSessionProtocolGOPHER = CURLPROTO_GOPHER;
268-
//CFURLSessionProtocol const CFURLSessionProtocolSMB = CURLPROTO_SMB;
269-
//CFURLSessionProtocol const CFURLSessionProtocolSMBS = CURLPROTO_SMBS;
270262
CFURLSessionProtocol const CFURLSessionProtocolALL = CURLPROTO_ALL;
271263

272264

@@ -308,7 +300,6 @@ CFURLSessionOption const CFURLSessionOptionTIMEVALUE = { CURLOPT_TIMEVALUE };
308300
CFURLSessionOption const CFURLSessionOptionCUSTOMREQUEST = { CURLOPT_CUSTOMREQUEST };
309301
CFURLSessionOption const CFURLSessionOptionSTDERR = { CURLOPT_STDERR };
310302
CFURLSessionOption const CFURLSessionOptionPOSTQUOTE = { CURLOPT_POSTQUOTE };
311-
/*CFURLSessionOption const CFURLSessionOptionOBSOLETE40 = { CURLOPT_OBSOLETE40 };*/
312303
CFURLSessionOption const CFURLSessionOptionVERBOSE = { CURLOPT_VERBOSE };
313304
CFURLSessionOption const CFURLSessionOptionHEADER = { CURLOPT_HEADER };
314305
CFURLSessionOption const CFURLSessionOptionNOPROGRESS = { CURLOPT_NOPROGRESS };
@@ -336,7 +327,6 @@ CFURLSessionOption const CFURLSessionOptionMAXREDIRS = { CURLOPT_MAXREDIRS };
336327
CFURLSessionOption const CFURLSessionOptionFILETIME = { CURLOPT_FILETIME };
337328
CFURLSessionOption const CFURLSessionOptionTELNETOPTIONS = { CURLOPT_TELNETOPTIONS };
338329
CFURLSessionOption const CFURLSessionOptionMAXCONNECTS = { CURLOPT_MAXCONNECTS };
339-
//CFURLSessionOption const CFURLSessionOptionOBSOLETE72 = { CURLOPT_OBSOLETE72 };
340330
CFURLSessionOption const CFURLSessionOptionFRESH_CONNECT = { CURLOPT_FRESH_CONNECT };
341331
CFURLSessionOption const CFURLSessionOptionFORBID_REUSE = { CURLOPT_FORBID_REUSE };
342332
CFURLSessionOption const CFURLSessionOptionRANDOM_FILE = { CURLOPT_RANDOM_FILE };
@@ -472,30 +462,9 @@ CFURLSessionOption const CFURLSessionOptionTCP_KEEPIDLE = { CURLOPT_TCP_KEEPIDLE
472462
CFURLSessionOption const CFURLSessionOptionTCP_KEEPINTVL = { CURLOPT_TCP_KEEPINTVL };
473463
CFURLSessionOption const CFURLSessionOptionSSL_OPTIONS = { CURLOPT_SSL_OPTIONS };
474464
CFURLSessionOption const CFURLSessionOptionMAIL_AUTH = { CURLOPT_MAIL_AUTH };
475-
CFURLSessionOption const CFURLSessionOptionSASL_IR = { CURLOPT_SASL_IR };
465+
#if !NS_CURL_MISSING_XFERINFOFUNCTION
476466
CFURLSessionOption const CFURLSessionOptionXFERINFOFUNCTION = { CURLOPT_XFERINFOFUNCTION };
477-
CFURLSessionOption const CFURLSessionOptionXFERINFODATA = { CURLOPT_XFERINFODATA };
478-
CFURLSessionOption const CFURLSessionOptionXOAUTH2_BEARER = { CURLOPT_XOAUTH2_BEARER };
479-
CFURLSessionOption const CFURLSessionOptionDNS_INTERFACE = { CURLOPT_DNS_INTERFACE };
480-
CFURLSessionOption const CFURLSessionOptionDNS_LOCAL_IP4 = { CURLOPT_DNS_LOCAL_IP4 };
481-
CFURLSessionOption const CFURLSessionOptionDNS_LOCAL_IP6 = { CURLOPT_DNS_LOCAL_IP6 };
482-
CFURLSessionOption const CFURLSessionOptionLOGIN_OPTIONS = { CURLOPT_LOGIN_OPTIONS };
483-
484-
//Options unavailable on Ubuntu 14.04
485-
/*CFURLSessionOption const CFURLSessionOptionSSL_ENABLE_NPN = { CURLOPT_SSL_ENABLE_NPN };
486-
CFURLSessionOption const CFURLSessionOptionSSL_ENABLE_ALPN = { CURLOPT_SSL_ENABLE_ALPN };
487-
CFURLSessionOption const CFURLSessionOptionEXPECT_100_TIMEOUT_MS = { CURLOPT_EXPECT_100_TIMEOUT_MS };
488-
CFURLSessionOption const CFURLSessionOptionPROXYHEADER = { CURLOPT_PROXYHEADER };
489-
CFURLSessionOption const CFURLSessionOptionHEADEROPT = { CURLOPT_HEADEROPT };
490-
CFURLSessionOption const CFURLSessionOptionPINNEDPUBLICKEY = { CURLOPT_PINNEDPUBLICKEY };
491-
CFURLSessionOption const CFURLSessionOptionUNIX_SOCKET_PATH = { CURLOPT_UNIX_SOCKET_PATH };
492-
CFURLSessionOption const CFURLSessionOptionSSL_VERIFYSTATUS = { CURLOPT_SSL_VERIFYSTATUS };
493-
CFURLSessionOption const CFURLSessionOptionSSL_FALSESTART = { CURLOPT_SSL_FALSESTART };
494-
CFURLSessionOption const CFURLSessionOptionPATH_AS_IS = { CURLOPT_PATH_AS_IS };
495-
CFURLSessionOption const CFURLSessionOptionPROXY_SERVICE_NAME = { CURLOPT_PROXY_SERVICE_NAME };
496-
CFURLSessionOption const CFURLSessionOptionSERVICE_NAME = { CURLOPT_SERVICE_NAME };
497-
CFURLSessionOption const CFURLSessionOptionPIPEWAIT = { CURLOPT_PIPEWAIT };*/
498-
467+
#endif
499468

500469
CFURLSessionInfo const CFURLSessionInfoTEXT = { CURLINFO_TEXT };
501470
CFURLSessionInfo const CFURLSessionInfoHEADER_IN = { CURLINFO_HEADER_IN };
@@ -548,7 +517,6 @@ CFURLSessionInfo const CFURLSessionInfoRTSP_CSEQ_RECV = { CURLINFO_RTSP_CSEQ_REC
548517
CFURLSessionInfo const CFURLSessionInfoPRIMARY_PORT = { CURLINFO_PRIMARY_PORT };
549518
CFURLSessionInfo const CFURLSessionInfoLOCAL_IP = { CURLINFO_LOCAL_IP };
550519
CFURLSessionInfo const CFURLSessionInfoLOCAL_PORT = { CURLINFO_LOCAL_PORT };
551-
CFURLSessionInfo const CFURLSessionInfoTLS_SESSION = { CURLINFO_TLS_SESSION };
552520
CFURLSessionInfo const CFURLSessionInfoLASTONE = { CURLINFO_LASTONE };
553521

554522

@@ -558,14 +526,9 @@ CFURLSessionMultiOption const CFURLSessionMultiOptionPIPELINING = { CURLMOPT_PIP
558526
CFURLSessionMultiOption const CFURLSessionMultiOptionTIMERFUNCTION = { CURLMOPT_TIMERFUNCTION };
559527
CFURLSessionMultiOption const CFURLSessionMultiOptionTIMERDATA = { CURLMOPT_TIMERDATA };
560528
CFURLSessionMultiOption const CFURLSessionMultiOptionMAXCONNECTS = { CURLMOPT_MAXCONNECTS };
529+
#if !NS_CURL_MISSING_MAX_HOST_CONNECTIONS
561530
CFURLSessionMultiOption const CFURLSessionMultiOptionMAX_HOST_CONNECTIONS = { CURLMOPT_MAX_HOST_CONNECTIONS };
562-
CFURLSessionMultiOption const CFURLSessionMultiOptionMAX_PIPELINE_LENGTH = { CURLMOPT_MAX_PIPELINE_LENGTH };
563-
CFURLSessionMultiOption const CFURLSessionMultiOptionCONTENT_LENGTH_PENALTY_SIZE = { CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE };
564-
CFURLSessionMultiOption const CFURLSessionMultiOptionCHUNK_LENGTH_PENALTY_SIZE = { CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE };
565-
CFURLSessionMultiOption const CFURLSessionMultiOptionPIPELINING_SITE_BL = { CURLMOPT_PIPELINING_SITE_BL };
566-
CFURLSessionMultiOption const CFURLSessionMultiOptionPIPELINING_SERVER_BL = { CURLMOPT_PIPELINING_SERVER_BL };
567-
CFURLSessionMultiOption const CFURLSessionMultiOptionMAX_TOTAL_CONNECTIONS = { CURLMOPT_MAX_TOTAL_CONNECTIONS };
568-
531+
#endif
569532

570533
CFURLSessionMultiCode const CFURLSessionMultiCodeCALL_MULTI_PERFORM = { CURLM_CALL_MULTI_PERFORM };
571534
CFURLSessionMultiCode const CFURLSessionMultiCodeOK = { CURLM_OK };
@@ -575,7 +538,6 @@ CFURLSessionMultiCode const CFURLSessionMultiCodeOUT_OF_MEMORY = { CURLM_OUT_OF_
575538
CFURLSessionMultiCode const CFURLSessionMultiCodeINTERNAL_ERROR = { CURLM_INTERNAL_ERROR };
576539
CFURLSessionMultiCode const CFURLSessionMultiCodeBAD_SOCKET = { CURLM_BAD_SOCKET };
577540
CFURLSessionMultiCode const CFURLSessionMultiCodeUNKNOWN_OPTION = { CURLM_UNKNOWN_OPTION };
578-
CFURLSessionMultiCode const CFURLSessionMultiCodeADDED_ALREADY = { CURLM_ADDED_ALREADY };
579541
CFURLSessionMultiCode const CFURLSessionMultiCodeLAST = { CURLM_LAST };
580542

581543

Foundation/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,22 @@ set_target_properties(Foundation PROPERTIES
178178
Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift
179179
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/swift)
180180

181+
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
182+
find_package(CURL CONFIG)
183+
if(NOT CURL_FOUND)
184+
find_package(CURL REQUIRED)
185+
endif()
186+
endif()
187+
188+
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
189+
if((NS_CURL_ASSUME_FEATURES_MISSING) OR (CURL_VERSION_STRING VERSION_LESS "7.32.0"))
190+
add_compile_definitions(NS_CURL_MISSING_XFERINFOFUNCTION)
191+
endif()
192+
193+
if((NS_CURL_ASSUME_FEATURES_MISSING) OR (CURL_VERSION_STRING VERSION_LESS "7.30.0"))
194+
add_compile_definitions(NS_CURL_MISSING_MAX_HOST_CONNECTIONS)
195+
endif()
196+
endif()
181197

182198
add_library(FoundationNetworking
183199
Boxing.swift

Foundation/URLSession/NativeProtocol.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,11 @@ internal class _NativeProtocol: URLProtocol, _EasyHandleDelegate {
301301
}
302302
}
303303

304-
func updateProgressMeter(with propgress: _EasyHandle._Progress) {
305-
//TODO: Update progress. Note that a single URLSessionTask might
306-
// perform multiple transfers. The values in `progress` are only for
307-
// the current transfer.
304+
func updateProgressMeter(with progress: _EasyHandle._Progress) {
305+
guard let progressReporter = self.task?.progress else { return }
306+
307+
progressReporter.totalUnitCount = progress.totalBytesExpectedToReceive + progress.totalBytesExpectedToSend
308+
progressReporter.completedUnitCount = progress.totalBytesReceived + progress.totalBytesSent
308309
}
309310

310311
/// The data drain.

Foundation/URLSession/URLSessionConfiguration.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,15 @@ open class URLSessionConfiguration : NSObject, NSCopying {
215215
/* Specifies additional headers which will be set on outgoing requests.
216216
Note that these headers are added to the request only if not already present. */
217217
open var httpAdditionalHeaders: [AnyHashable : Any]? = nil
218-
218+
219+
#if NS_CURL_MISSING_MAX_HOST_CONNECTIONS
220+
/* The maximum number of simultaneous persistent connections per host */
221+
@available(*, deprecated, message: "This platform does not support selecting the maximum number of simultaneous persistent connections per host. This property is ignored.")
222+
open var httpMaximumConnectionsPerHost: Int
223+
#else
219224
/* The maximum number of simultaneous persistent connections per host */
220225
open var httpMaximumConnectionsPerHost: Int
226+
#endif
221227

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

Foundation/URLSession/URLSessionTask.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ open class URLSessionTask : NSObject, NSCopying {
3737
open var countOfBytesClientExpectsToSend: Int64 = NSURLSessionTransferSizeUnknown {
3838
didSet { updateProgress() }
3939
}
40-
40+
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.")
43+
open private(set) var progress = Progress(totalUnitCount: -1)
44+
#else
4145
open private(set) var progress = Progress(totalUnitCount: -1)
46+
#endif
4247

4348
func updateProgress() {
4449
self.workQueue.async {

Foundation/URLSession/libcurl/EasyHandle.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,14 @@ fileprivate extension _EasyHandle {
498498
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionNOPROGRESS, 0).asError()
499499

500500
try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionPROGRESSDATA, UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())).asError()
501-
501+
502+
#if !NS_CURL_MISSING_XFERINFOFUNCTION
502503
try! CFURLSession_easy_setopt_tc(rawHandle, CFURLSessionOptionXFERINFOFUNCTION, { (userdata: UnsafeMutableRawPointer?, dltotal :Int64, dlnow: Int64, ultotal: Int64, ulnow: Int64) -> Int32 in
503504
guard let handle = _EasyHandle.from(callbackUserData: userdata) else { return -1 }
504505
handle.updateProgressMeter(with: _Progress(totalBytesSent: ulnow, totalBytesExpectedToSend: ultotal, totalBytesReceived: dlnow, totalBytesExpectedToReceive: dltotal))
505506
return 0
506507
}).asError()
508+
#endif
507509

508510
}
509511
/// This callback function gets called by libcurl when it receives body

Foundation/URLSession/libcurl/MultiHandle.swift

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

6666
extension URLSession._MultiHandle {
6767
func configure(with configuration: URLSession._Configuration) {
68+
#if !NS_CURL_MISSING_XFERINFOFUNCTION
6869
try! CFURLSession_multi_setopt_l(rawHandle, CFURLSessionMultiOptionMAX_HOST_CONNECTIONS, numericCast(configuration.httpMaximumConnectionsPerHost)).asError()
70+
#endif
71+
6972
try! CFURLSession_multi_setopt_l(rawHandle, CFURLSessionMultiOptionPIPELINING, configuration.httpShouldUsePipelining ? 3 : 2).asError()
7073
//TODO: We may want to set
7174
// CFURLSessionMultiOptionMAXCONNECTS

0 commit comments

Comments
 (0)