title: libcurl: How to Pause and Resume downloading file using curl_easy_pause()
body: <p>I need to implement an HTTP/HTTPS downloader in C++ / Linux (Ubuntu), using <code>libcurl</code>.</p>
  <p>I have browse in Internet during many days, and I was not able to find neither <strong>one complete working example on how to use <code>curl_easy_pause()</code>, to &quot;Pause&quot; a working download, and the corresponding function to &quot;Resume&quot; the downloading process</strong>.</p>
  <p>Here is the code I have found for the download function, in StackOverflow:</p>
  <p>( <a href="https://stackoverflow.com/questions/10448874/download-is-not-resuming-using-curl-c-api?rq=1">Download is not resuming using Curl C API</a> )</p>
  <p>Here below, I am providing just a squeleton (the complete code is in the previous link).</p>
  <p>The code is working and I am able to download.</p>
  <p>But I cannot realize where I should put the <code>curl_easy_pause()</code> function, in such way that, if some condition is accomplished (example, if I press a &quot;Pause / Resume&quot; button), the download function acts accordingly: Pause or Resume, respectively.</p>
  <p>Here is the squeleton of the code (and, below of the squeleton are my comments):</p>
  <pre><code>int do_dl(void) // downloader function
  {
     // Variables definition / initialization here
  
      char url[] = {&quot;http://file_to_download.mp4&quot;};
      char filename[]={&quot;given_name.mp4&quot;};
  
     // 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(&quot;curl_retval:[%d]\n&quot;, 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(&quot;our work here is done ;)\n&quot;);
             retval=2;
          }
       } // end of if(curl)
  
       if (fp) fclose(fp);
  
       if (curl) curl_easy_cleanup(curl);
  
       return retval;
  }
  </code></pre>
  <p>Comments:</p>
  <ol>
  <li><p>I suppose that my confussion appears because this is a piece of structure code, where is no loop, and AFTER the <code>curl_easy_perform(curl)</code> 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 <code>curl_easy_setopt()</code> ), and, somehow, the library calls those callbacks &quot;continuously&quot; during the download process.</p>
  </li>
  <li><p>My current approach is that the &quot;Pause&quot; status might be reached calling <code>curl_easy_pause()</code> as another callback function (?), for instance, depending on the <code>curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, dl_progress)</code> function. But where and how?</p>
  </li>
  <li><p>Similarly, the &quot;Resume&quot; status might be reached calling  <code>curl_easy_setopt(curl, CURLOPT_RESUME_FROM,st.st_size)</code>
  function. But where and how?</p>
  </li>
  </ol>
  <p>I was not able to implement it successfully.</p>
  <p>Anybody can help, please?</p>
  <p>Thanks in advance!</p>