如何通过CURLOPT_HEADERFUNCTION提取标题信息?

时间:2019-01-29 07:51:13

标签: c++ visual-studio libcurl

我想通过在我的c ++程序中使用CURLOPT_HEADERFUNCTION提取标题信息。 您可以在github中检查我的项目。 https://github.com/LUCIF680/Erza-Download-Manager

How can I use CURLOPT_HEADERFUNCTION to read a single response header field?提供了有关如何获取这些标头信息的解决方案,但我想知道为什么我的代码无法正常工作,并提供了一个可能的解决方案示例。

//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函数中获取正确的文件大小,但在主函数中获取0个字节。

1 个答案:

答案 0 :(得分:1)

如您的github软件仓库中所示,oprations(运算符!?)是一个局部变量,将在readHeader函数的末尾释放。处理readHeader函数并为给定的Erza实例获取正确文件大小的方法是将其指针传递给userdata值。 Erza类可以改写为:

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 );
}