0

I'm using libcurl to upload files to a remote ftp server. I would like to make sure the server is ready to start receiving uploads before I actually start sending them (I can't upload a file for testing, have to know in advance if the ftp server is ready). Ideally I want to write a code which will login to the ftp server and change directory to the destination directory on the remote server. If this test passed I can assume the server is ready to receive files. I wrote the following code

curl_global_init(CURL_GLOBAL_ALL);
m_curl = curl_easy_init(); // Get a curl handle
if (!m_curl)
    return -1;
curl_easy_setopt(m_curl, CURLOPT_USERNAME, "username");
curl_easy_setopt(m_curl, CURLOPT_PASSWORD, "password");
curl_easy_setopt(m_curl, CURLOPT_UPLOAD, 1L);
struct curl_slist* headerList = NULL;
headerList = curl_slist_append(headerList, "CWD directory");
curl_easy_setopt(m_curl, CURLOPT_URL, "ftp://192.168.1.2/");
curl_easy_setopt(m_curl, CURLOPT_POSTQUOTE, headerList);
CURLcode res = curl_easy_perform(m_curl);
if(res == CURLE_OK)
    return 0;
else
    return -1;

username and password are correct and directory exists under ftp root. I look in the network capture and it seems like after successful login my client requests "PWD" which passes fine but then it asks for the SIZE of directory but it gets 550 error, could not get file size. I then tried without the headerList, just the login, again login passes fine and after PWD request I get 500 OOPS, vsf_sysutil_recv_peek no data. Any idea how can I test the server, in a similar manner?

e271p314
  • 3,841
  • 7
  • 36
  • 61

1 Answers1

1

First, don't use "CWD" in a QUOTE option - that's just not necessary. Provide the full path in the URL directly.

Then, your asking for an upload but you don't specify anything to upload. I suggest you instead "prepare" for the upload by doing a "dummy request" that for example lists the directory contents (which it does if you end the URL with a trailing slash) and...

then you keep the handle around and then set the options for the upload in a second request and it'll then re-use the connection and login so there won't be a second connection procedure.

Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222
  • I started with the full url and without the CWD, but I got the 500 OOPS, vsf_sysutil_recv_peek no data so I figured I was not using libcurl correctly. First of all I appreciate your answer, all I did was to add the trailing slash and that was it. Generally I don't expect a trailing slash in the url to be key for the code success or failure, most of the normalized urls come without trailing slash, perhaps consider supporting url without trailing slash. – e271p314 Nov 28 '13 at 22:06
  • libcurl supports URLs just fine without a trailing slash. But that's off-topic here anyway. – Daniel Stenberg Nov 28 '13 at 22:53