cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: How to retrieve webpage from curl_easy_perform

From: Josh Angstadt <josh_at_softgenetics.com>
Date: Tue, 03 Mar 2015 13:56:38 -0500

Thanks for your reply, I'm not sure I'm quite getting it still though, I
removed the line that sets WRITEFUNCTION so that it will default to
fwrite, and I created a FILE * object at a wider scope than the function
where this is called and set that as WRITEDATA, but now when it gets to
the curl_easy_perform call it just freezes up and crashes.

On 3/3/2015 12:23 PM, Patrick Monnerat wrote:
>
> Josh Angstadt wrote:
>
>> Specifically, I replaced the READFUNCTION and READDATA with
> WRITEFUNCTION and WRITEDATA respectively. At first I changed the
> callback function to this:
>
>> static size_t read_callback(void *ptr, size_t size, size_t nmemb,
> void *stream)
>> {
>> curl_off_t nread;
>> char *r = (char*)ptr;
>> size_t retcode = fwrite(r, size, nmemb, (FILE*)stream);
>> nread = (curl_off_t)retcode;
>> return retcode;
>> }
> To write to a file, you do not need this function: it is basically a
> null wrapper to the fwrite function (that is default, as previously
> noted). Just pass an opened FILE * to CURLOPT_WRITEDATA (not the result
> variable local to main).
>
>> When I ran that, it would get the curl_easy_perform call, then the
> program would freeze and crash.
>
> There can be many cause of this crash: what do you pass to
> CURLOPT_WRITEDATA? If it's not a FILE *, then the fwrite call will
> fail or even segfault (you receive in 'stream' what you've set with
> CURLOPT_WRITEDATA).
>
>> When I tried replacing the contents of the callback function with this
> test:
>
>> fprint("%s.*", size, (char*)stream);
>> return 0;
>> It gave me the error code 23 I mentioned before.
> It's normal: please read the documentation:
> http://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html
> <quote>
> Your callback should return the number of bytes actually taken care of.
> If that amount differs from the amount passed to your callback function,
> it'll signal an error condition to the library. This will cause the
> transfer to get aborted and the libcurl function used will return
> CURLE_WRITE_ERROR.
> </quote>
>
> To print a textual error message, use
> http://curl.haxx.se/libcurl/c/curl_easy_strerror.html: this is much
> clearer than "error code 23".
>
> -------------------------------------------------------------------
> List admin: http://cool.haxx.se/list/listinfo/curl-library
> Etiquette: http://curl.haxx.se/mail/etiquette.html

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2015-03-03