curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder Daniel himself.

Getting CURLINFO_CONTENT_LENGTH_DOWNLOAD_T during a header call back will return -1.

From: Kenji Shukuwa via curl-library <curl-library_at_lists.haxx.se>
Date: Fri, 17 May 2024 17:25:52 +0900

 Until curl 8.6.0, it was possible to obtain Content-Length using CURLINFO_CONTENT_LENGTH_DOWNLOAD_T in the header callback, but after https://github.com/curl/curl/pull/13134 was committed, it now returns -1.

 According to the API documentation for curl_easy_getinfo(), "Use this function after a performed transfer if you want to get transfer related data.", so it may just have been possible to retrieve the data by chance up until now, but is the above behavior by design?


Sudo code:
```
static size_t header_callback(char *buffer, size_t size,
                              size_t nitems, void *userdata)
{
  CURL *curl = userdata;
  CURLcode res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &cl);
  if(!res) {
    printf("Download size: %" CURL_FORMAT_CURL_OFF_T "\n", cl);
  }

  return nitems * size;
}
 
int main(void)
{
  CURL *curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");

    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
    curl_easy_setopt(curl, CURLOPT_HEADERDATA, curl);

    curl_easy_perform(curl);
  }
}
```

-- 
Unsubscribe: https://lists.haxx.se/mailman/listinfo/curl-library
Etiquette:   https://curl.se/mail/etiquette.html
Received on 2024-05-17