0

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;
   }
  • https://github.com/yhirose/cpp-httplib#range – 273K Apr 20 '22 at 15:23
  • 5
    Always study the protocol you want to use, know it before attempting to use it in any form. Any decent HTTP documentation (and of course the specification itself) should have listed header files like the `Range` header. From that it shouldn't have been to hard to find the cpp-netlib functions needed to use that header. – Some programmer dude Apr 20 '22 at 15:25

0 Answers0