mologie / curl-asio

Seamlessly integrate libcurl with Boost.Asio
Other
53 stars 28 forks source link

Note (2018): This library is currently unmaintained and a rewrite for C++14/17, possibly as simple header-only library, is planned.

curl-asio

Here be dragons. Although this library generally works quite well, it is still being developed and has not been extensively tested. You will probably be fine, but don't look at me when things catch fire.

This library makes use of libcurl's multi interface in order to enable easy integration into Boost.Asio applications.

Installation

  1. If not already done, install cURL and its header files
  2. Clone this git repository. There are no tags or packages yet.
  3. Run CMake and point it to cURL
  4. make && make install

Example

#include <boost/asio.hpp>
#include <boost/make_shared.hpp>
#include <curl-asio.h>
#include <fstream>

int main(int argc, char* argv[])
{
    // this example program downloads argv[1] to argv[2] (arg validation omitted for readability)
    char* url = argv[1];
    char* file_name = argv[2];

    // start by creating an io_service object
    boost::asio::io_service io_service;

    // construct an instance of curl::easy
    curl::easy downloader(io_service);

    // set the object's properties
    downloader.set_url(url);
    downloader.set_sink(boost::make_shared<std::ofstream>(file_name, std::ios::binary));

    // download the file
    boost::system::error_code ec;
    downloader.perform(ec);

    // error handling
    if (!ec)
    {
        std::cerr << "Download succeeded" << std::endl;
    }
    else
    {
        std::cerr << "Download failed: " << ec.message() << std::endl;
    }

    return 0;
}

More examples, including one for curl-asio's asynchronous interface, can be found in the wiki and the examples directory.

Todo

License

Curl-asio is licensed under the same MIT/X derivate license used by libcurl.