cURL / Mailing Lists / curl-library / Single Mail

curl-library

My application does not work properly for some people

From: Anker Works <ankerworks_at_gmail.com>
Date: Mon, 21 Jul 2014 17:18:48 +0200

I am developing an application to download files through html and I have
distributed it to some people to test.
This is the related code:

struct SDownloadFile
{
    const char *name;
    FILE *stream;
};

CDownloadHandler::CDownloadHandler() : m_curl(NULL), m_VerboseFile(NULL)
{
    curl_global_init(CURL_GLOBAL_DEFAULT);
    m_curl = curl_easy_init();
    if (m_curl == NULL)
        throw CDownloadException("Unable to initialize libcurl");

    CURLcode opt;

    opt = curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 1);
    if (opt != CURLE_OK)
        throw CDownloadException(opt);

    opt = curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, 15);
    if (opt != CURLE_OK)
        throw CDownloadException(opt);

    opt = curl_easy_setopt(m_curl, CURLOPT_TIMEOUT, 0);
    if (opt != CURLE_OK)
        throw CDownloadException(opt);
}

CDownloadHandler::~CDownloadHandler()
{
    curl_easy_cleanup(m_curl);
    curl_global_cleanup();
    if (m_VerboseFile)
        fclose(m_VerboseFile);
}

size_t CDownloadHandler::WriteDataToFile(void* ptr, size_t size, size_t
nmemb, void* stream)
{
    SDownloadFile* file = (SDownloadFile*)stream;
    if (file && !file->stream)
    {
        file->stream = FileOpen(file->name, "wb");
        if (!file->stream)
            throw CDownloadException("Unable to create file: " +
std::string(file->name));
    }

    return fwrite(ptr, size, nmemb, file->stream);
}

bool CDownloadHandler::DownloadFile(std::string source, std::string file)
{
    SDownloadFile f = { file.c_str(), NULL };
    CURLcode opt;

    opt = curl_easy_setopt(m_curl, CURLOPT_URL, source.c_str());
    if (opt != CURLE_OK)
        throw CDownloadException(opt);

    opt = curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION,
CDownloadHandler::WriteDataToFile);
    if (opt != CURLE_OK)
        throw CDownloadException(opt);

    opt = curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &f);
    if (opt != CURLE_OK)
        throw CDownloadException(opt);

    opt = curl_easy_perform(m_curl);
    if (f.stream)
        fclose(f.stream);

    if (opt != CURLE_OK)
        throw CDownloadException(opt);

    return true;
}

It works fine for everybody but there is a guy who sometimes get these
errors (from curl_easy_strerror output): "Transferred a partial file" and
"Failed to get data from the peer".
This person have a bad internet connection and I suppose that's the reason
why he has the error. However, when he tries to download something from
another application, he only has slow speed and everything works fine. So I
think the problem should be in my application.

The libcurl version I am using is 7.37.0, with windows 7 as OS.

Is there any way to make my application works for this guy too?

Thank you all.

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2014-07-21