封顶libcurl在X字符之后返回

Capping libcurl return after X characters

本文关键字:之后 返回 字符 libcurl 封顶      更新时间:2023-10-16

cURL检索的页面大于5000个字符似乎在我的应用程序中导致分割错误。

//Includes etc.
#include "mainheader.h"
using namespace std;
using namespace boost;
//Buffer is defined right after the includes
char buffer [5000];
//cURL result to buffer, which is declared above.
void function_pt(void *ptr, size_t size, size_t nmemb, void *stream)
{
    int n;
    n=sprintf(buffer,"%s", ptr, size, nmemb, stream);
}
//Function to check one URL
void checkurl(char* thisurl)
{
    //Start curl
    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();
    if(curl)
    {
        curl_easy_setopt(curl, CURLOPT_URL, thisurl);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, function_pt);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }
}

我不能100%确定这是否是导致分割错误的原因,有可能任何包含多个换行符的返回都会导致分割错误(尚未能够测试)。但我假设缓冲区会自动切断5000个字符后返回的数据,但我想这是天真的我。我怎样才能确定是这样呢?

你的短跑线错了。您为格式字符串传递了太多参数。此外,您还可以使用snprintf,它类似于sprintf,但能够获取最大长度以防止缓冲区溢出。