libcurl: How to Pause and Resume downloading file using curl_easy_pause()

Viewed 752

I need to implement an HTTP/HTTPS downloader in C++ / Linux (Ubuntu), using libcurl.

I have browse in Internet during many days, and I was not able to find neither one complete working example on how to use curl_easy_pause(), to "Pause" a working download, and the corresponding function to "Resume" the downloading process.

Here is the code I have found for the download function, in StackOverflow:

( Download is not resuming using Curl C API )

Here below, I am providing just a squeleton (the complete code is in the previous link).

The code is working and I am able to download.

But I cannot realize where I should put the curl_easy_pause() function, in such way that, if some condition is accomplished (example, if I press a "Pause / Resume" button), the download function acts accordingly: Pause or Resume, respectively.

Here is the squeleton of the code (and, below of the squeleton are my comments):

int do_dl(void) // downloader function
{
   // Variables definition / initialization here

    char url[] = {"http://file_to_download.mp4"};
    char filename[]={"given_name.mp4"};

   // Other Variables definition / initialization here

    curl_global_init(CURL_GLOBAL_DEFAULT);   
    curl = curl_easy_init();
    
    if (curl)
    {
        curl_easy_setopt(curl, CURLOPT_URL, url);

        /*callbacks*/
        
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, dl_write);
        curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, dl_progress);
        curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);

        // Other curl_easy_setop() configurations

        if(CURLE_OK != (curl_retval = curl_easy_perform(curl)))
        {
           printf("curl_retval:[%d]\n", curl_retval);
           switch(curl_retval)
           {
              //Transferred a partial file
              case CURLE_WRITE_ERROR: //can be due to a dropped connection
              break;

              //all defined in curl/curl.h

              default: //suggest quitting on unhandled error
              retval=0;
           };

           curl_easy_getinfo( // code here );

           switch(http_response)
           {
              // code here
           };
        } // end of if(CURLE_OK ....)
        else
        {
           printf("our work here is done ;)\n");
           retval=2;
        }
     } // end of if(curl)

     if (fp) fclose(fp);

     if (curl) curl_easy_cleanup(curl);

     return retval;
}

Comments:

  1. I suppose that my confussion appears because this is a piece of structure code, where is no loop, and AFTER the curl_easy_perform(curl) function is called, it seems it is executed until the file is successfully download or until a failure / error is found. I have realized that the callbacks are setup in the configuration stage (with curl_easy_setopt() ), and, somehow, the library calls those callbacks "continuously" during the download process.

  2. My current approach is that the "Pause" status might be reached calling curl_easy_pause() as another callback function (?), for instance, depending on the curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, dl_progress) function. But where and how?

  3. Similarly, the "Resume" status might be reached calling curl_easy_setopt(curl, CURLOPT_RESUME_FROM,st.st_size) function. But where and how?

I was not able to implement it successfully.

Anybody can help, please?

Thanks in advance!

0 Answers
Related