开发者

Libcurl: why curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, obj) passes my writefunction pointer, that is not equal to obj?

开发者 https://www.devze.com 2023-03-13 20:07 出处:网络
It\'s modified example, that ships with libcurl. I pass bodyfile to function write_data by setting CURLOPT_WRITEDATA. It really writes data to file, but pointer is not equal to pointer passed to setop

It's modified example, that ships with libcurl. I pass bodyfile to function write_data by setting CURLOPT_WRITEDATA. It really writes data to file, but pointer is not equal to pointer passed to setopt(curl_handle, CURLOPT_WRITEDATA, pointer). And I get SIGSEGV if I try to pass pointer to object, other than FILE. Why? How can I pass the same pointer?

Code:

#include &l开发者_开发技巧t;stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
FILE *bodyfile;
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
    assert(bodyfile == (FILE*) stream); //this assertion fails, but when i comment it, code works. Why?
    int written = fwrite(ptr, size, nmemb, (FILE *)stream);
    return written;
}

int main(void)
{
    CURL *curl_handle;
    static const char *headerfilename = "head.out";
    FILE *headerfile;
    static const char *bodyfilename = "body.out";

    curl_global_init(CURL_GLOBAL_ALL);

    /* init the curl session */
    curl_handle = curl_easy_init();

    /* set URL to get */
    curl_easy_setopt(curl_handle, CURLOPT_URL, "http://google.com");

    /* no progress meter please */
    curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);

    /* send all data to this function  */
    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

    /* open the files */
    headerfile = fopen(headerfilename,"w");
    if (headerfile == NULL) {
        curl_easy_cleanup(curl_handle);
        return -1;
    }
    bodyfile = fopen(bodyfilename,"w");
    if (bodyfile == NULL) {
        curl_easy_cleanup(curl_handle);
        return -1;
    }

    /* we want the headers to this file handle */
    curl_easy_setopt(curl_handle,   CURLOPT_WRITEHEADER, headerfile);

    /* we want the body to this file handle */
    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);

    /* get it! */
    curl_easy_perform(curl_handle);

    /* close the header file */
    fclose(headerfile);

    /* cleanup curl stuff */
    curl_easy_cleanup(curl_handle);

    return 0;
}


If you remove the assertion an insert:

printf("body: %p stream: %p\n", bodyfile, stream);

into write_data, and then add:

printf("head: %p body: %p\n", headerfile, bodyfile);

after opening both the header and the body file everything will become clear.

What happens is write_data is called a number of times with headerfile in stream, and then once with bodyfile. I'm guessing write_data is called once for every row in the header response.

The man page for curl_easy_setopt says the following about CURLOPT_HEADERFUNCTION:

If this option is not set, or if it is set to NULL, but CURLOPT_HEADERDATA (CURLOPT_WRITEHEADER) is set to anything but NULL, the function used to accept response data will be used instead. That is, it will be the function specified with CURLOPT_WRITEFUNCTION, or if it is not specified or NULL - the default, stream-writing function.

0

精彩评论

暂无评论...
验证码 换一张
取 消