cURL / Mailing Lists / curl-library / Single Mail

curl-library

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

From: Daniel Stenberg <daniel_at_haxx.se>
Date: Fri, 28 Aug 2009 23:28:59 +0200 (CEST)

On Fri, 28 Aug 2009, Milmar Tan wrote:

> 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.

> 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.

> Is it safe to call these curl_easy_setopts inside headerwrite?

I'm not entirely sure, but I think it is.

-- 
  / daniel.haxx.se
Received on 2009-08-28