cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Availability of header info prior to writefunction callback

From: Dan Fandrich <dan_at_coneharvesters.com>
Date: Fri, 29 Jul 2011 09:19:18 -0700

On Fri, Jul 29, 2011 at 12:54:37PM +0000, Britton, David L. (ELS-DAY) wrote:
> I would like to be able to use the Content-Length response header to
> preallocate a large amount of memory for use by the write function. Through
> some trickery, I was able to get access to the active easy handle in the
> callback function and tried to call
>
> curl_easy_getinfo(_handleCurl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &
> _contentLength) from within the WRITEFUNCTION callback itself.
>
> It ?works? but it looks like the info in the handle isn?t set prior to
> completion of the download, which is too late.

The length to be downloaded can't always be known until the download
finishes, so this is likely by design. You can always capture the
Content-Length header (if it exists) with a header callback function and
parse it when/if it comes in. You can also get the same information from
the progress callback. I believe that in recent versions of libcurl, this
callback will be called once before the start of the transfer.

> Hoping I?m missing something trivial as allocating large amounts of memory 16k
> at a time is slowing things down significantly.

Why allocate only 16 KB at once? Why not allocate 1 MB each time? Or 10 MB?
Or 2× the amount already downloaded? Note that some allocators do something
like the latter already internally to reduce overhead.

>>> Dan
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2011-07-29