cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Is it safe to set curl options inside a callback function?

From: Milmar Tan <milmarqtan_at_gmail.com>
Date: Sat, 29 Aug 2009 09:23:02 +0800

I wasn't able to enable mail delivery in my account, so I'm reposting
my response here.

---
From: Daniel Stenberg <daniel_at_haxx.se>
Date: Fri, 28 Aug 2009 23:28:59 +0200 (CEST)
>> The nature of the program I'm making is that I need to be able to get the
>> content-length header to allocate space for an image, then write the image
>> to this allocated space in just a single curl_easy_perform call.
>How about those responses that don't have a content-length header? To make the
>solution solid, you can't rely on that header always being present. I would
>rather suggest you use a realloc concept like the getinmemory.c example that
>is totally independent from that header or not.
For now, I'm limiting my program to support only images with the
content-length header present. By the way, this is for a device with
limited memory and cpu capability.
>> So, my idea is this: Using a single curl_easy_perform call, I'll wait for
>> the content-length header in the CURLOPT_HEADERFUNCTION callback. Then when
>> this callback is called and I find the content-length header, I'll perform
>> the allocations, stop the retrieval of the headers, then start writing the
>> data retrieved.
>Why do it that complicated? You could instead let the header callback remain
>and just ignore the rest of the headers and you can have the write callback
>already set.
I can't set the write callback before the header callback gets called
because I haven't yet allocated the buffer that will hold the image at
that time. However, I did try to set the write callback earlier when I
was simulating this via PC and I noticed that the headers were also
being written to the buffer together with the image data. That's why
in the sample program above, after I get the content-length, I
immediately disable CURLOPT_HEADER so that when the write callback is
called only the image will be written.
>> Is it safe to call these curl_easy_setopts inside headerwrite?
>I'm not entirely sure, but I think it is.
Just want to ask, what makes you unsure? =)
My initial tests show the above hack works. But it's quite scary to
suddenly get an error with the hack in place during production of the
device.
Thanks,
Milmar
Received on 2009-08-29