curl-library
RE: libcurl and libssh2
Date: Tue, 20 Oct 2009 15:29:50 +0800
> -----Original Message-----
> From: curl-library-bounces_at_cool.haxx.se
> [mailto:curl-library-bounces_at_cool.haxx.se] On Behalf Of Michael Wood
> Sent: Friday, October 09, 2009 4:58 PM
> To: libcurl development
> Subject: Re: libcurl and libssh2
>
> The curl command can generate sample code for any options you specify.
>
> Try this:
> curl -u username sftp://shell.example.com/etc/issue --libcurl
> ssh2-download-example.c curl -T uploadfile -u user:passwd
> ftp://ftp.upload.com/myfile --libcurl ftp-upload-example.c
>
> and then examine the ssh2-{download,upload}-example.c files
> that are generated.
I have generated the sample code with sftp upload:
===========================================
qxu@durian(pts/4):~/opensrc/curl-7.19.6/src[111]$ ./curl -v -u qxu:fair123 -T CMakeLists.txt sftp://13.198.98.190/~/scan/test.txt --libcurl sftp_upload_test.c
===========================================
The upload is successful, and the sample code is generated, most of which can be understood.
However, the generated code doesn't specify the local filename (or filepointer) to be uploaded. I understand that this can be set by CURLOPT_READDATA in real coding. Still, I am not sure whether a callback function is necessary with the option CURLOPT_READFUNCTION. Or, is it optional?
By the way, the prototype of the callback looks pretty similar to C function fread():
===========================================
/* from http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTREADFUNCTION */
CURLOPT_READFUNCTION
Function pointer that should match the following prototype: size_t function( void *ptr, size_t size, size_t nmemb, void *stream); This function gets called by libcurl as soon as it needs to read data in order to send it to the peer. The data area pointed at by the pointer ptr may be filled with at most size multiplied with nmemb number of bytes. Your function must return the actual number of bytes that you stored in that memory area. Returning 0 will signal end-of-file to the library and cause it to stop the current transfer.
If you stop the current transfer by returning 0 "pre-maturely" (i.e before the server expected it, like when you've said you will upload N bytes and you upload less than N bytes), you may experience that the server "hangs" waiting for the rest of the data that won't come.
The read callback may return CURL_READFUNC_ABORT to stop the current operation immediately, resulting in a CURLE_ABORTED_BY_CALLBACK error code from the transfer (Added in 7.12.1)
From 7.18.0, the function can return CURL_READFUNC_PAUSE which then will cause reading from this connection to become paused. See curl_easy_pause(3) for further details.
If you set the callback pointer to NULL, or don't set it at all, the default internal read function will be used. It is simply doing an fread() on the FILE * stream set with CURLOPT_READDATA.
/* from K&R, 2nd Edition, Page 247 */
size_t fread(void *ptr, size_t size, size_t nobj, FILE *stream)
fread reads from stream into the array ptr at most nobj objects of size size.
fread returns the number of objects read; this may be less than the number requested. feof and ferror must be used to determine status.
===========================================
Can I assume that if the callback is not defined in the code, the system function fread() is used by default?
Thanks,
Xu Qiang
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
- text/plain attachment: sftp_upload_test.c