curlpp and the use of ContentLengthDownload

192 views
Skip to first unread message

degski

unread,
Sep 8, 2013, 9:30:05 AM9/8/13
to cur...@googlegroups.com
Hi All,

I'm trying to get the filesize of a file I would like to download, before doing so. I am able to download the file, that is not the issue. From what I understood from the docs and some googling is that the below should do the trick. The result though seems to be a random number, i.e. not -1 in some unsigned format, like "4.71219e+018".

void DownloadFile ( const std::string url, const fs::path file ) { // fs:: = boost::filesystem::
try {

curlpp::Cleanup cleaner;
curlpp::Easy request;

request.setOpt ( curlpp::options::Url ( url ) );
request.setOpt ( curlpp::options::FollowLocation ( true ) );
request.setOpt ( curlpp::options::Header ( true ) );
request.setOpt ( curlpp::options::NoBody ( true ) );

request.perform ( );

double s = curlpp::infos::ContentLengthDownload::get ( request );

std::cout << s << std::endl;
}

catch ( curlpp::LogicError & e ) {
    
std::cerr << e.what ( ) << std::endl;
}

catch ( curlpp::RuntimeError & e ) {
    
std::cerr << e.what ( ) << std::endl;
}
}

The header info returned is:

HTTP/1.1 200 OK
Date: Sun, 08 Sep 2013 13:12:34 GMT
Last-modified: Sun, 21 Jul 2013 10:36:54 GMT
Content-Length: 11044341
Content-Type: application/x-executable

The value of (in the above) s = 4.71219e+018

The url on which I am testing this is "http://www.swi-prolog.org/download/stable/bin/w64pl641.exe"

It would be much appreciated if someone could point out my error.

Obviously it's not hard to parse the header myself, but given that curlpp is so elegantly written, getting this to work the correct way should not be rocket-science.

Thank you in advance,


degski



degski

unread,
Sep 9, 2013, 4:53:07 AM9/9/13
to cur...@googlegroups.com
Hi All,

I've now tested libcurl.lib itself, just to check the problem is not in there. conclusion: it isn't, the problem is somehow in curlpp.

I've used the following c-code:

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.swi-prolog.org/download/stable/bin/w64pl641.exe");
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);

curl_easy_setopt(curl, CURLOPT_HEADER, 1);
curl_easy_setopt(curl, CURLOPT_NOBODY, 1);

    res = curl_easy_perform(curl);

double result;
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &result);
printf ( "%f\n", result );

    curl_easy_cleanup(curl);
  }
  return 0;
}

The above code gives the expected result and prints out the correct size of the file in question.

Which, as far as I can see is equivalent to this version in C++ with curlpp:

#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Infos.hpp>

int main ( ) {

curlpp::Cleanup cleaner;
curlpp::Easy request;

request.setOpt ( curlpp::options::Url ( "http://www.swi-prolog.org/download/stable/bin/w64pl641.exe" ) );
request.setOpt ( curlpp::options::FollowLocation ( true ) );
request.setOpt ( curlpp::options::Header ( true ) );
request.setOpt ( curlpp::options::NoBody ( true ) );

request.perform ( );

std::cout << curlpp::infos::ContentLengthDownload::get ( request );
}

This program though, does get the header (I've tested that), but the length printed is gibberish (4.71219e+018).

Any thoughts, please?

Cheers


degski

degski

unread,
Sep 9, 2013, 5:22:09 AM9/9/13
to cur...@googlegroups.com
Hi All,

Problem resolved, change code in file Info.cpp from line 49 to:

template<>
void
InfoTypeConverter<double>::get(curlpp::Easy & handle, 
CURLINFO info,
double & value)
{
  InfoGetter::get(handle, info, value);
}

And all goes to plan!

Cheers,


degski
Reply all
Reply to author
Forward
0 new messages