cURL / Mailing Lists / curl-library / Single Mail

curl-library

libcurl SFTP file upload

From: Mueller, Alexander <am_at_a-m-i.de>
Date: Fri, 4 Apr 2008 13:10:59 +0200

Hello,
 
after 2 days of experimenting with curl, hoping to be able to upload a file via SFTP, today I gave up and just wanted to post my experiences for others trying the same. Hopefully no one else uses 2 days time for this again and also fails.
 
First of all, simple FTP Uploading works. I use MS Windows XP and compile on MS Visual Studio 6. Used versions: curl 7.18.0, libssh2 V 0.18, OpenSSL 0.9.8g
 
In order to get SFTP going, you'll have to download OpenSSL package separately (this is not contained in libcurl, don't ask me why), compile, then download libssh2 package (also not contained).
 
When compiling libssh2, you'll have to do some tweaking. Go into win32 and open the .dsw file. Go into comp.c and search for
 
#ifdef LIBSSH2_HAVE_ZLIB
# include <zlib.h>
#endif

and put *before* this passage this line:
 
#undef LIBSSH2_HAVE_ZLIB
 
in order to remove zlib stuff (which is, of course, also not contained and yields compiler and linker errors if you don't include this line). Then in VS, go to "Project / Settings / Link" and remove zlib.lib, therefore include there "user32.lib advapi32.lib gdi32.lib"
 
Then, create a new .c file with this content, and include it to the project:
 
#include "stdio.h"
#include "windows.h"
 
#if _MSC_VER < 1300
// ist für VC 6.0 noch nötig, als workaround
long _ftol(double);
long _ftol2( double dblSource )
{
 return _ftol(dblSource);
}
long _aulldiv(unsigned long, unsigned long);
long _aulldvrm(unsigned long divisor, unsigned long dividend)
{
        return _aulldiv(divisor, dividend); //
}
#endif

After that, you'll be able to build the libssh2.lib and libssh2.dll
 
When you're done, tell libcurl that SSH shall be enabled by setting the additional compiler options "USE_LIBSSH2,HAVE_LIBSSH2,HAVE_LIBSSH2_H,LIBSSH2_WIN32"
 
Rebuild libcurl. If you don't do this, you'll get errors out of libcurl in the moment you use "sftp://" in the URL.
 
Comment this out: curl_easy_setopt(hCurl, CURLOPT_UPLOAD, 1) ;
otherwise you'll also get an error after connecting to the server

Next, you'll find out that connecting to an URL does not work with subfolders.
 
You can use
 
  curl_easy_setopt(hCurl, CURLOPT_URL, "sftp://mydomain.de/");

successfully, but this won't work (of cource the subfolder exists):
 
  curl_easy_setopt(hCurl, CURLOPT_URL, "sftp://mydomain.de/subfolder"); -> CURLE_REMOTE_FILE_NOT_FOUND
  curl_easy_setopt(hCurl, CURLOPT_URL, "sftp://mydomain.de/subfolder/"); -> CURLE_REMOTE_FILE_NOT_FOUND

Then, when trying to use the "cd" command in libcurl, libcurl goes into an endless loop, this seems to be the case with all SFTP commands that are not supported in libcurl. The same goes for "put".
 
I don't want to be inpolite or not thankful, I think libcurl does a good job, and it is free. But the documentation lists SFTP in the features list, and there is no hint that SFTP uploads aren't possible. There is also no code example for uploading per SFTP, so now I know why. But there should be somewhere a statment about this, and I hope that my post may help others in deciding if they want to try this or not
 
Alex
 
 
Received on 2008-04-04