curl / Mailing Lists / curl-library / Single Mail

curl-library

Re: Couple cuestions about CURLOPT_WRITEFUNCTION callback

From: Patrick Monnerat <patrick_at_monnerat.net>
Date: Mon, 13 Feb 2017 15:27:59 +0100

On 02/13/2017 02:47 PM, David Requena wrote:
>
> When I call fwrite I know what the written items are, what their size
> is, and how many of them there are. When in a callback I know how many
> items I'm receiving and their sizes, but not what they are, unless
> specified in the documentation.
>
You probably know (in your mind and your program) what is the size of
each item and how many you are EXPECTING, but not how many are PROVIDED
by this call to your callback: Libcurl itself is generic and thus knows
the later, but cannot know the formers.
As you have noted, this prototype signature allows having fwrite as a
direct callback: this is so only for convenience and, by now, libcurl
always sets size to 1 in calls, since it cannot not know your data
structure.
And you DO have nmemb bytes of received data in the buffer, although not
all expected data may be present at once.
As a consequence, if your data structure item size is > 1, it is highly
probable the last received item within the call is incomplete...
To understand how the callback has to be coded, you might consider you
are implementing a procedure similar to fwrite, that is, agnostic of
transmitted data structure.

> Now, thanks to your response (and Patrik's), I understand this has no
> significance at all in this instance. I gather it's just more
> convenient to call fwrite and friends from my callback function with
> the given data.
If you only write received data to a file, there is no need to write a
callback consisting of a wrapper to fwrite: fwrite is the default, so
you just have to set the CURLOPT_WRITEDATA custom pointer to the target
FILE *.
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2017-02-13