cURL / Mailing Lists / curl-library / Single Mail

curl-library

Connection resuability after CURLE_WRITE_ERROR

From: vlad <gvlad_at_t72.ru>
Date: Wed, 17 Jan 2007 09:38:35 +0900

Callback function (CURLOPT_WRITEFUNCTION) used in my application may
fail to receive data in some situations. In that case libcurl closes the
second socket and leaves the connection alive. That's ok. But libcurl
does not read any ftp response in that case. That's why when reusing
connection it reads _old_ ftp response and fails. In my case libcurl
sends PASV command but receives 450 result and responds about wrong
reply to PASV command.

Here is an example code that causes an error:

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

bool first=true;

int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
    return first ? 0 : size * nmemb;
}

int main(int argc, char **argv)
{
    CURLcode res;
    curl_global_init(CURL_GLOBAL_ALL);
    CURL* curl = curl_easy_init();
    if(curl)
    {
        curl_easy_setopt(curl,CURLOPT_URL,
"ftp://ftp.sunet.se/pub/www/utilities/curl/curl-7.9.2.tar.gz");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, 0);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);
        res = curl_easy_perform(curl);
        assert(0 != res);
        first = false;
        res = curl_easy_perform(curl);
        assert(0 == res);
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
    return 0;
}

One solution I found is to change one line in ftp.c (I'm using libcurl
7.16.0):
if(!ftp->no_transfer && !status) {
to
if(!ftp->no_transfer && (!status || status == CURLE_WRITE_ERROR)) {

So is there another more wise solution?
Received on 2007-01-17