curl-library
Does not returns in network connection error
Date: Sat, 24 Jan 2009 22:07:19 +0530
Hi,
I have made an client for uploading file using curl lib.
If in middle of transfer, network goes down, retCode =
curl_easy_perform(curlhandle);
Does not retuns. I am totally stuck.
I don't know what to do.
Code snippet:
Function that uploads.
CURLcode CFTPUpload::UploadRestFile()
{
char *buf = (char *)malloc(BUFFERSIZE);
int size_read = 0;
CURLcode retCode = CURLE_GOT_NOTHING;
if (pFile != NULL)
{
if(!fseek(pFile, m_uploadedLength, SEEK_SET))
{
buf = (char *)malloc(BUFFERSIZE);
size_read = (int)fread(buf, 1, BUFFERSIZE, pFile);
if(size_read > 0)
{
free(m_buffer.data);
m_buffer.data = (char *)malloc(size_read);
memcpy(m_buffer.data, buf, size_read);
m_buffer.offset = 0;
m_buffer.size = size_read;
curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curlhandle, CURLOPT_FTPAPPEND,
TRUE);
curl_easy_setopt(curlhandle, CURLOPT_RESUME_FROM,
m_uploadedLength);
}
retCode = curl_easy_perform(curlhandle);
if(retCode == CURLE_PARTIAL_FILE || retCode == CURLE_OK)
m_uploadedLength += size_read;
}
}
return retCode;
}
CURLcode CFTPUpload::Upload()
{
CURLcode retCode = CURLE_GOT_NOTHING;
if(m_serverName.IsEmpty() || m_fileName.IsEmpty() ||
m_username.IsEmpty() || m_passwd.IsEmpty())
{
OutputDebugString(_T("Either Servernmae, username, filename or
password is not set"));
return retCode;
}
errno_t e = fopen_s(&pFile, m_fileName, "rb");
if(e == 0 && pFile != NULL)
{
if(!fseek(pFile, 0, SEEK_END))
{
m_fileSize = ftell(pFile);
if(m_fileSize > 0)
{
curl_global_init(CURL_GLOBAL_ALL);
curlhandle = curl_easy_init();
if(curlhandle)
{
retCode = SetSettings(curlhandle);
if(retCode == CURLE_OK)
{
if(!fseek(pFile, 0, SEEK_SET))
{
retCode = CURLE_GOT_NOTHING;
char *buf = (char
*)malloc(BUFFERSIZE);
int size_read = (int)fread(buf, 1,
BUFFERSIZE, pFile);
if(size_read > 0)
{
m_buffer.data = (char
*)malloc(size_read);
memcpy(m_buffer.data, buf,
size_read);
m_buffer.offset = 0;
m_buffer.size = size_read;
retCode =
curl_easy_perform(curlhandle);
if(retCode ==
CURLE_PARTIAL_FILE || retCode == CURLE_OK)
m_uploadedLength =
size_read;
}
free(buf);
}
}
}
}
}
}
return retCode;
}
void CFTPUpload::CleanUp()
{
curl_easy_cleanup(curlhandle);
curl_global_cleanup();
if(pFile)
fclose(pFile);
}
CURLcode CFTPUpload::SetSettings(CURL *curlhandle)
{
CURLcode retCode = CURLE_GOT_NOTHING;
struct curl_slist *headerlist = NULL;
int index = m_fileName.ReverseFind(_T('\\'));
int strLen = m_fileName.GetLength() - 1;
CStringA fileName(m_fileName.Right(strLen - index));
CStringA tempfileName = fileName + ".temp";
CStringA RNTO = "RNTO ";
RNTO.Append(fileName);
CStringA RNFR = "RNFR ";
RNFR.Append(tempfileName);
/* build a list of commands to pass to libcurl */
headerlist = curl_slist_append(headerlist, RNFR);
headerlist = curl_slist_append(headerlist, RNTO);
/* pass in that last of FTP commands to run after the transfer */
retCode = curl_easy_setopt(curlhandle, CURLOPT_POSTQUOTE, headerlist);
retCode = curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
retCode = curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION,
ReadFunction);
retCode = curl_easy_setopt(curlhandle, CURLOPT_READDATA, &m_buffer);
retCode = curl_easy_setopt( curlhandle, CURLOPT_SEEKFUNCTION,
SeekFunction);
retCode = curl_easy_setopt( curlhandle, CURLOPT_SEEKDATA, NULL);
curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-"); /* disable passive
mode */
curl_easy_setopt(curlhandle,CURLOPT_URL, SetServerURL(tempfileName));
curl_easy_setopt(curlhandle,CURLOPT_USERPWD,
SetUnamePasswd(m_username, m_passwd));
if(!m_proxyName.IsEmpty())
{
retCode = curl_easy_setopt(curlhandle, CURLOPT_PROXY,
m_proxyName);
}
if(!m_proxyUsername.IsEmpty())
{
retCode = curl_easy_setopt(curlhandle,CURLOPT_PROXYUSERPWD,
SetUnamePasswd(m_proxyUsername, m_proxyPasswd));
}
retCode = curl_easy_setopt(curlhandle, CURLOPT_INFILESIZE, 0);
retCode = curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
return retCode;
}
This is the way I call it..
CURLcode retCode;
bool retry = false;
int tries = 3;
do
{
long uploadedFileSize = 0;
long fileSize = 0, percent = 0;
CString strPercent;
retCode = ftpUpload.Upload();
if(retCode == CURLE_PARTIAL_FILE)
{
do
{
if(retCode == CURLE_OK)
{
//do something
}
else if(retCode == CURLE_PARTIAL_FILE)
{
//do something
}
else
{
tries--;
retry = true;
if(tries == 0)
{
retry = false;
break;
}
}
retCode = ftpUpload.UploadRestFile();
}while (retCode != CURLE_OK);
}
else if(retCode == CURLE_OK)
{
}
else
{
tries--;
retry = true;
if(tries == 0)
{
retry = false;
break;
}
}
}while(retry);
please help!!!!
Thanks,
ranu
Received on 2009-01-24