samedi 21 mai 2022

Curl Upload Stuck

I'm using curl-7.65.0 (Static) with Microsoft Visual Studio 2013. I have the following method to upload files to the FTP server.

bool uploadFile(string sourcePath, string url, string & error)
{
    FILE * file;
    file = fopen(sourcePath.c_str(), "rb");
    if (!file)
    {
        error = "Can not open source file";
        return false;
    }

    struct stat fileInformation;
    if (fstat(fileno(file), &fileInformation) != 0)
    {
        error = "Can not get source file information";
        return false;
    }

    CURL * curl;
    auto curlResultCode = CURLE_FAILED_INIT;
    curl = curl_easy_init();
    if (curl)
    {
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
        curl_easy_setopt(curl, CURLOPT_READDATA, file);
        curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, curl_off_t(fileInformation.st_size));
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, CURLFTP_CREATE_DIR_RETRY);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3600L);
        curlResultCode = curl_easy_perform(curl);
    }

    curl_easy_cleanup(curl);
    fclose(file);
    error = curl_easy_strerror(curlResultCode);
    return curlResultCode != CURLE_OK ? false : true;
}

When i call the uploadFile with following parameters:

sourcePath: c:/file.zip

url: ftp://user:password@127.0.0.1:21/files/2022/05/20/myfile.zip

I see the directories are created in the FTP server but the file is not uploading and eventually, I get the CURLE_OPERATION_TIMEDOUT error.

Here is the log from the FTP server (XLight FTP Server)

05/21/2022 17:26:29 (not login 127.0.0.1<--127.0.0.1:21) "220 Xlight FTP Server 3.5 ready..."
05/21/2022 17:26:29 (not login 127.0.0.1-->127.0.0.1:21) "USER user"
05/21/2022 17:26:29 (not login 127.0.0.1<--127.0.0.1:21) "331 Password required for user"
05/21/2022 17:26:30 (not login 127.0.0.1-->127.0.0.1:21) "PASS *****"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "230 Login OK"
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "PWD"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "257 "/""
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "CWD files"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "550 Can't change directory to "/files"."
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "MKD files"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "257 "/files/" directory created."
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "CWD files"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "250 Directory successfully changed"
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "CWD 2022"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "550 Can't change directory to "/files/2022"."
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "MKD 2022"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "257 "/files/2022/" directory created."
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "CWD 2022"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "250 Directory successfully changed"
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "CWD 05"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "550 Can't change directory to "/files/2022/05"."
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "MKD 05"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "257 "/files/2022/05/" directory created."
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "CWD 05"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "250 Directory successfully changed"
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "CWD 20"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "550 Can't change directory to "/files/2022/05/20"."
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "MKD 20"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "257 "/files/2022/05/20/" directory created."
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "CWD 20"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "250 Directory successfully changed"
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "EPSV"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "229 Entering Passive Mode (|||53237|)"
05/21/2022 17:26:33 (user 127.0.0.1-->127.0.0.1:21) "TYPE I"
05/21/2022 17:26:33 (user 127.0.0.1<--127.0.0.1:21) "200 Type set to I."

Can you tell me what's wrong here?

Update 1: I also added the read callback function but still same result.

size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
    FILE *readhere = (FILE *)userdata;
    curl_off_t nread;

    /* copy as much data as possible into the 'ptr' buffer, but no more than
    'size' * 'nmemb' bytes! */
    size_t retcode = fread(ptr, size, nmemb, readhere);

    nread = (curl_off_t)retcode;

    fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
        " bytes from file\n", nread);
    return retcode;
}

curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
curl_easy_setopt(curl, CURLOPT_READDATA, (void *)file);

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire