Skip to content

Commit 31d1d41

Browse files
authored
Merge pull request #1103 from johnno1962b/master
2 parents 37a4485 + 5890353 commit 31d1d41

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

Foundation/NSURLSession/http/EasyHandle.swift

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ internal final class _EasyHandle {
5757
fileprivate var pauseState: _PauseState = []
5858
internal var fileLength: Int64 = 0
5959
internal var timeoutTimer: _TimeoutSource!
60+
#if os(Android)
61+
static fileprivate var _CAInfoFile: UnsafeMutablePointer<Int8>?
62+
#endif
6063

6164
init(delegate: _EasyHandleDelegate) {
6265
self.delegate = delegate
@@ -168,6 +171,20 @@ extension _EasyHandle {
168171
let protocols = (CFURLSessionProtocolHTTP | CFURLSessionProtocolHTTPS)
169172
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionPROTOCOLS, protocols).asError()
170173
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionREDIR_PROTOCOLS, protocols).asError()
174+
#if os(Android)
175+
// See https://curl.haxx.se/docs/sslcerts.html
176+
// For SSL to work you need "cacert.pem" to be accessable
177+
// at the path pointed to by the URLSessionCAInfo env var.
178+
// Downloadable here: https://curl.haxx.se/ca/cacert.pem
179+
if let caInfo = _EasyHandle._CAInfoFile {
180+
if String(cString: caInfo) == "UNSAFE_SSL_NOVERIFY" {
181+
try! CFURLSession_easy_setopt_int(rawHandle, CFURLSessionOptionSSL_VERIFYPEER, 0).asError()
182+
}
183+
else {
184+
try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionCAINFO, caInfo).asError()
185+
}
186+
}
187+
#endif
171188
//TODO: Added in libcurl 7.45.0
172189
//TODO: Set default protocol for schemeless URLs
173190
//CURLOPT_DEFAULT_PROTOCOL available only in libcurl 7.45.0
@@ -268,7 +285,7 @@ fileprivate func printLibcurlDebug(handle: CFURLSessionEasyHandle, type: CInt, d
268285

269286
fileprivate func printLibcurlDebug(type: CFURLSessionInfo, data: String, task: URLSessionTask) {
270287
// libcurl sends is data with trailing CRLF which inserts lots of newlines into our output.
271-
print("[\(task.taskIdentifier)] \(type.debugHeader) \(data.mapControlToPictures)")
288+
NSLog("[\(task.taskIdentifier)] \(type.debugHeader) \(data.mapControlToPictures)")
272289
}
273290

274291
fileprivate extension String {
@@ -615,6 +632,19 @@ extension _EasyHandle._CurlStringList {
615632
}
616633
}
617634

635+
#if os(Android)
636+
extension URLSession {
637+
638+
public static func setCAInfoFile(_ _CAInfoFile: String) {
639+
free(_EasyHandle._CAInfoFile)
640+
_CAInfoFile.withCString {
641+
_EasyHandle._CAInfoFile = strdup($0)
642+
}
643+
}
644+
645+
}
646+
#endif
647+
618648
extension CFURLSessionEasyCode : Equatable {
619649
public static func ==(lhs: CFURLSessionEasyCode, rhs: CFURLSessionEasyCode) -> Bool {
620650
return lhs.value == rhs.value

0 commit comments

Comments
 (0)