[swift-corelibs-dev] NSURLSession & libcurl

Robert Stephen Thompson robert.thompson at willowtreeapps.com
Mon Mar 14 12:29:24 CDT 2016


Just a couple of tips based on my experience wrapping libxml2 for NSXMLDocument:
1. You’ll need to actually import and link libcurl with CoreFoundation instead of trying to make a libcurl module.modulemap and importing it directly. This is because if you do it that way, you’ll have to add libcurl as a dependency _throughout_ the Swift build process, and it would all be pretty disruptive (and difficult to do).
2. As a consequence of 1., you’ll need to wrap every function you call from libcurl in a new CoreFoundation API. See CFXMLInterface.h and CFXMLInterface.c for what I mean. Of course, this also gives you the opportunity to add nullability annotations, wrap things in CFString, CFError, CFArray, etc, so it’s not just busy-work for getting around the build system, heh. Just depends on how much C you want to write, you could just straight wrap the libcurl functions and do everything in Swift, but at the very least nullability annotations are a win here.

Thanks,
Robert Thompson
Software Engineer
WillowTree, Inc.®
willowtreeapps.com <http://willowtreeapps.com/>

> On Mar 14, 2016, at 11:50 AM, Daniel Eggert via swift-corelibs-dev <swift-corelibs-dev at swift.org> wrote:
> 
> 
>> On 14 Mar 2016, at 15:36, Philippe Hausler <phausler at apple.com> wrote:
>> 
>> I dont think we had an intention set forth for how that was to be implemented; honestly it was a bit of a “we dont know how this should be done for linux yet” when the initial API drafts were posted. That being said NSURLSession and friends are definitely pretty important areas in Foundation (hence why the placeholder implementations are there).
>> 
>> libcurl is readily available for linux (but we would need to alter our dependencies list for apt-get) and also is shipped for Darwin targets as well. so in spirit it might be a decent place to start.
>> 
>> There are a few places that might need some research on how to approach an implementation:
>> 
>> 1) Can curl’s APIs interface with run loops?
> 
> It interfaces very nicely with libdispatch -- is that an option?
> 
>> 2) Can curl’s API provide a reasonable implementation to back other APIs? NSURLCache, NSURLRequest, NSURLResponse, even older APIs like NSURLConnection?
> 
> As for NSURLRequest and NSURLResponse: These will need some 'glue' but should work nicely with libcurl's interface.
> 
> NSURLConnection is a bit of an oddball due it it's ties to NSRunLoop -- but it can certainly be made to work with libcurl.
> 
> NSURLCache: libcurl does not do any caching, and as a result the interaction with NSURLCache should be relatively straight forward.
> 
> libcurl's COOKIELIST should be able to interact with NSHTTPCookieStorage:
> https://curl.haxx.se/libcurl/c/CURLOPT_COOKIELIST.html
> 
>> 3) How would the background downloading part of NSURLSession be handled? Would it just elide that feature?
> 
> I would elide that for now. To support this, we'd need an independent daemon to do the downloading. That's quite a project on its own.
> 
>> 4) Can it handle the delegation protocols for all events? If not, can the events be synthesized?
> 
> It appears that this should work nicely. libcurl provides callbacks as headers are parsed.
> 
> One main pinpoint will be SSL / TLS support. That'll depend entirely on how libcurl is compiled and will not be able to pick up TLSMinimumSupportedProtocol / TLSMaximumSupportedProtocol. Also interaction with the NSURLAuthenticationChallenge and TLS remains a question. libcurl does provide a hook for this, but it's not trivial since the code will have to interact with the underlying TLS implementation.
> 
> 
> Any implementation will depend on NSOperation / NSOperationQueue -- and those will in turn to some extend depend on libdispatch being available.
> 
> And once libdispatch is available, it makes sense to use it for libcurl, too.
> 
> /Daniel
> 
> _______________________________________________
> swift-corelibs-dev mailing list
> swift-corelibs-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-corelibs-dev/attachments/20160314/ed962d1d/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <https://lists.swift.org/pipermail/swift-corelibs-dev/attachments/20160314/ed962d1d/attachment.sig>


More information about the swift-corelibs-dev mailing list