Is it possible to read the remote file as istream with libcurl?

I would like to use the libcurl library to open a remote date file and repeat it using istream. I looked at a good example in this thread , but it writes the remote file to a local file. Instead, I would like remote reads to be transferred to istream for subsequent programmatic manipulation. Is it possible? I would really appreciate help.

Best, Aaron

+3
source share
1 answer

Boost IO Stream may be a better solution than native STL. At the very least, it’s much easier to create a boost stream. From forcing your own documents:

#include <curl/curl.h>
#include <boost/iostreams/stream.hpp>

class CURLDevice
{
    private:
        CURL* handle;
    public:
        typedef char                            char_type;
        typedef boost::iostreams::source_tag    category;

        CURLDevice()
        {
            handle = curl_easy_init();
        }

        CURLDevice(const std::string &url)
        {
            handle = curl_easy_init();
            open( url );
        }

        ~CURLDevice()
        {
            curl_easy_cleanup(handle);
        }

        void open(const std::string &url)
        {
            curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
            curl_easy_setopt(handle, CURLOPT_CONNECT_ONLY, 1);
            curl_easy_perform(handle);
        }

        std::streamsize read(char* s, std::streamsize n)
        {
            size_t read;
            CURLcode ret = curl_easy_recv(handle, s, n, &read);
            if ( ret == CURLE_OK || ret == CURLE_AGAIN )
                return read;
            else
                return -1;
        }
};

typedef boost::iostreams::stream<CURLDevice> CURLStream;

int main(int argc, char **argv)
{
    curl_global_init(CURL_GLOBAL_ALL);

    {
        CURLStream stream("http://google.com");

        char buffer[256];
        int sz;

        do
        {
            sz = 256;
            stream.read( buffer, sz );
            sz = stream.gcount();
            std::cout.write( buffer, sz );
        }
        while( sz > 0 );
    }

    curl_global_cleanup();

    return 0;
}

: , segfault CURL, , , .

+2

Source: https://habr.com/ru/post/1749615/


All Articles