Resume downloading from a certain position

254 Views Asked by At

I need to download the file after a pause. But I do not know how to implement it correctly using https://github.com/yhirose/cpp-httplib . But the problem is that when the download resumes, the server starts sending me the file first. Question: how do I tell the server the size of the piece that I have already downloaded, so that the server sends me only the necessary part? My code:

   std::string body;
   httplib::Client cli( url, port );
   cli.set_follow_location( true );
   
   int file_size = is_part_of_file ? GetFileSize( result_file_name.c_str() ) : 0; // it is downloaded part of the file
   int last_percent;
   auto res = cli.Get(
      file_path.c_str(), httplib::Headers(),
      [ & ]( const httplib::Response& response )
      {
         ( void )response;
         return *is_download;
      },
      [ & ]( const char* data, size_t data_length )
      {
         body.append( data, data_length );
         return *is_download;
      },
      [ & ]( uint64_t len, uint64_t total )
      {
         int percent = ( int )( len * 100 / total );
         if( last_percent != percent )
         {
            *p_percent = ( ( float )percent / 100 );
         }
         last_percent = percent;
         return *is_download;
      } );

   if( res )
   {
      std::ofstream out( result_file_name, std::ios::binary | std::ios::app );
      out << body;
      out.close();
   }
   else
   {
      if( is_part_of_file )
      {
         std::ofstream out( result_file_name, std::ios::binary | std::ios::app );
         out << body;
         out.close();
      }
      return false;
   }
0

There are 0 best solutions below