curl-cpp
static c++17 wrapper for curl with -fno-exceptions support
curl_easy_get.cc

Why make Easy_ref_t RAII-less?

It would simplify design of Easy_ref_t, since it doesn't need to implement RAII-part logic.

It would also make it easier to use member function of Easy_ref_t in Multi_t, provided that libcurl callback provides CURL* and that option CURLOPT_PRIVATE which enables storing any object, isn't support until 7.10.3.

Easy_ref_t's member function cannot be called in multiple threads simultaneously.

PERSISTENT CONNECTIONS

Persistent connections means that libcurl can re-use the same connection for several transfers, if the conditions are right.

libcurl will always attempt to use persistent connections. It will by default, cache 5 connections.

Whenever you use Easy_ref_t::perform or Multi::perform/Multi::socket_action, libcurl will attempt to use an existing connection to do the transfer, and if none exists it'll open a new one that will be subject for re-use on a possible following call to these functions.

To allow libcurl to take full advantage of persistent connections, you should do as many of your file transfers as possible using the same handle.

If you use the easy interface, and the Easy_t get destroyed, all the possibly open connections held by libcurl will be closed and forgotten.

When you've created a multi handle and are using the multi interface, the connection pool is instead kept in the multi handle so closing and creating new easy handles to do transfers will not affect them. Instead all added easy handles can take advantage of the single shared pool.

It can also be archieved by using curl::Share_base or curl::Share and enable_sharing(Share_base::Options::connection_cache).

#include "../curl_easy.hpp"
#include "../curl_url.hpp"
#include <cassert>
#include "utility.hpp"
int main(int argc, char* argv[])
{
curl::curl_t curl{nullptr};
assert(curl.has_CURLU());
auto url = curl.create_Url();
assert(url.get());
auto url_ref = curl::Url_ref_t{url.get()};
assert_same(url_ref.set_url("http://en.cppreference.com/").get_return_value(),
curl::Url_ref_t::set_code::ok);
auto easy1 = curl.create_easy();
assert(easy1);
curl::Easy_ref_t easy_ref1{easy1.get()};
easy_ref1.set_url(url_ref);
easy_ref1.setup_establish_connection_only();
easy_ref1.perform();
easy_ref1.request_get();
easy_ref1.set_writeback(reinterpret_cast<decltype(easy_ref1)::writeback_t>(std::fwrite), stdout);
assert_same(easy_ref1.perform().get_return_value(), curl::Easy_ref_t::code::ok);
assert_same(easy_ref1.get_response_code(), 302L);
easy_ref1.set_url("http://en.cppreference.com/");
assert_same(easy_ref1.perform().get_return_value(), curl::Easy_ref_t::code::ok);
assert_same(easy_ref1.get_response_code(), 302L);
auto easy2 = curl.dup_easy(easy1);
assert(easy2);
curl::Easy_ref_t easy_ref2{easy2.get()};
//constexpr const char expected_response[] = "<!DOCTYPE HTML PUBLIC ";
//constexpr const auto expected_len = sizeof(expected_response) - 1;
//std::string response;
//response.reserve(expected_len);
//easy_ref2.set_read_writeback(std::pair{response, expected_len});
//assert_same(easy_ref2.perform().get_return_value(), curl::Easy_ref_t::code::ok);
//assert_same(easy_ref2.get_response_code(), 302L);
//assert_same(response, std::string_view{expected_response});
return 0;
}
curl::Url_ref_t
Definition: curl_url.hpp:24
curl::Easy_ref_t
Definition: curl_easy.hpp:53
curl::curl_t
Definition: curl.hpp:62
curl::Easy_ref_t::set_url
void set_url(const Url_ref_t &url) noexcept
Definition: curl_easy.cc:97