操作被应用程序回调中止-libcurl [英] operation was aborted by an application callback - libcurl
问题描述
我有以下libcurl程序.当我运行程序时,出现以下错误.
I have the following libcurl program. When I run the program I get the following error.
Operation was aborted by an application callback
Process finished with exit code 42
我完整的程序
#include <stdio.h>
#include <curl/curl.h>
#include <cstdint>
#include <malloc.h>
#include <cstring>
#define TIMETYPE double
#define TIMEOPT CURLINFO_TOTAL_TIME
#define STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES 6000
struct myprogress {
TIMETYPE lastruntime;
CURL *curl;
};
struct memoryStruct {
uint8_t* memory;
size_t size;
};
size_t handleData(void* contents, size_t size, size_t nmemb, void* stream) {
size_t realSize = size * nmemb;
struct memoryStruct* mem = static_cast<struct memoryStruct*>(stream);
mem->memory = static_cast<uint8_t*>(realloc(mem->memory, (mem->size + realSize + 1)));
if (mem->memory == nullptr) {
return 0;
}
memcpy(&(mem->memory[mem->size]), contents, realSize);
mem->size += realSize;
mem->memory[mem->size] = 0;
return realSize;
}
static int xferinfo(void *p,
curl_off_t dltotal, curl_off_t dlnow,
curl_off_t ultotal, curl_off_t ulnow)
{
struct myprogress *myp = (struct myprogress *)p;
CURL *curl = myp->curl;
TIMETYPE curtime = 0;
curl_easy_getinfo(curl, TIMEOPT, &curtime);
myp->lastruntime = curtime;
fprintf(stderr, "TOTAL TIME: %f \r\n", curtime);
fprintf(stderr," DOWN: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T
"\r\n", dlnow, dltotal);
if(dlnow > STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES)
return 1;
return 0;
}
int main(void)
{
CURL *curl;
CURLcode res = CURLE_OK;
struct myprogress prog;
struct memoryStruct m_chunk;
curl = curl_easy_init();
if(curl) {
prog.lastruntime = 0;
prog.curl = curl;
m_chunk.memory = static_cast<uint8_t*>(malloc(1));
if (nullptr == m_chunk.memory) {
return 1;
}
m_chunk.size = 0;
curl_easy_setopt(curl, CURLOPT_URL, "http://172.16.132.84:5000/download/file.out");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handleData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*) &m_chunk);
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo);
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &prog);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "%s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
return (int)res;
}
推荐答案
您的文件大于6000字节:
Your file is larger then 6000 bytes:
STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES 6000
在这种情况下,您在xferinfo函数中返回1,这将中止下载.
In this case you return 1 in your xferinfo function, which aborts the download.
文档
如果您的回调函数返回CURL_PROGRESSFUNC_CONTINUE,它将导致libcurl继续执行默认的进度函数.
If your callback function returns CURL_PROGRESSFUNC_CONTINUE it will cause libcurl to continue executing the default progress function.
从此回调返回任何其他非零值将导致libcurl中止传输并返回CURLE_ABORTED_BY_CALLBACK.
Returning any other non-zero value from this callback will cause libcurl to abort the transfer and return CURLE_ABORTED_BY_CALLBACK.
请参见 https://curl.haxx.se/libcurl/c/CURLOPT_XFERINFOFUNCTION.html
测试
如果我用您的代码下载了一个文件,文件为4431字节,则进入调试控制台:
If I download a file with your code with 4431 bytes I get in the debug console:
...
TOTAL TIME: 0.088631
DOWN: 4431 of 4431
Process finished with exit code 0
如果我将STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES更改为2000,那么我得到:
If I change STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES to 2000 then I get:
...
Operation was aborted by an application callback
Process finished with exit code 42
这篇关于操作被应用程序回调中止-libcurl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!