如何通过CURLOPT_HEADERFUNCTION提取header信息?

How to extract header information via CURLOPT_HEADERFUNCTION?

我想在我的 c++ 程序中使用 CURLOPT_HEADERFUNCTION 提取 header 信息。

提供了有关如何获取这些 header 信息的解决方案,但我想知道为什么我的代码无法正常工作以及可能的示例解决方案。

//readHeader function which returns the specific header information

size_t readHeader(char* header, size_t size, size_t nitems, void *userdata) {
Erza oprations; //class which contains string function like startsWith etc
if (oprations.startsWith(header, "Content-Length:")) {
    std::string header_in_string = oprations.replaceAll(header, "Content-Length:", "");
    long size = atol(header_in_string.c_str());
    file_size = size; // file_size is global variable
    std::cout << size; // here it is showing correct file size
}
else if (oprations.startsWith(header, "Content-Type:")) {
 // do something 
}else
 // do something
return size * nitems;
}
// part of main function
curl = curl_easy_init();
    if (curl) {
        fp = fopen(path, "wb");
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_CAINFO, "./ca-bundle.crt");
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, readHeader);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);

fclose(fp);
std::cout << file_size; // showing value 0

在 readHeader 函数中获取正确的文件大小但在 main 函数中获取 0 字节。

如您的 github depot 所示,oprations (operations !?) 是局部变量,将在 readHeader 函数结束时释放。处理 readHeader 函数并为给定 Erza 实例获取正确文件大小的一种方法是将其指针传递给 userdata 值。 Erza class 可以重写为:

class Erza : public Endeavour {

    //... your class body 

public: 
    bool download (const char *url,const char* path){
            curl = curl_easy_init();
            if (curl) {
                fp = fopen(path, "wb");
                curl_easy_setopt(curl, CURLOPT_URL, url);
                curl_easy_setopt(curl, CURLOPT_CAINFO, "./ca-bundle.crt");
                curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);

                curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, readHeader);
                curl_easy_setopt(curl, CURLOPT_HEADERDATA, this ); //<-- set this pointer to userdata value used in the callback.

                curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
                res = curl_easy_perform(curl);

                curl_easy_cleanup(curl);
                fclose(fp);
                return false;
            }else
                return true;

        }

    size_t analyseHeader( char* header, size_t size, size_t nitems ){

        if (startsWith(header, "Content-Length:")) {
            std::string header_in_string = replaceAll(header, "Content-Length:", "");
            long size = atol(header_in_string.c_str());
            file_size = size; // file_size is a member variable 
            std::cout << size; // here it is showing correct file size
        }
        else if (startsWith(header, "Content-Type:")) {
         // do something 
        }else
         // do something
        return size * nitems;
    }   

}//Eof class Erza

size_t readHeader(char* header, size_t size, size_t nitems, void *userdata) {

    //get the called context (Erza instance pointer set in userdata)

    Erza * oprations = (Erza *)userdata; 
    return oprations->analyseHeader( header, size, nitems );
}