Aminos Aminos - 3 months ago 15
C++ Question

Connecting to an FTP server through an HTTP proxy with libcURL fails

I am using this code to setup the HTTP proxy address before performing an FTP action with libCurl, the FTP server I am trying to connect is located in the internet (S&P's server)

curl_easy_setopt(m_pCURLSession, CURLOPT_PROXY, m_strProxy); //m_strProxy is a CString object having the overloading of the cast operator (const char*) so no problems here !
curl_easy_setopt(m_pCURLSession, CURLOPT_HTTPPROXYTUNNEL, 1);


The same code works without a problem for HTTP operations !

My HTTP proxy server (created with FreeProxy) works great with WinSCP and sometimes with FileZilla.

The issue is the following, if the HTTP proxy is located in another machine I got this cURL error :


Error=55 Failed sending data to the peer


When I put the HTTP proxy in the same machine I got this error :


(Error=56 | Failure when receiving data from the peer)!


When I delete this instruction :

curl_easy_setopt(m_pCURLSession, CURLOPT_HTTPPROXYTUNNEL, 1);


I got a strange response when I request FTP remote directorty listing, I receive a useless an HTML response.... whereas I am expecting a list of file/folders names separated by
"\r\n"
like when I don't use proxy.

Answer

The solution is simple but not obvious : first of all upgrade to the latest libcURL version because there is a known bug related to FTP tunneling through an HTTP proxy in the older version then use this option :

curl_easy_setopt(m_pCURLSession, CURLOPT_FTP_USE_EPSV, 0);

Happy coding :) Please upvote if this answer helped you because it wasn't easy for me to find the solution...