gpt4 book ai didi

c - 如何在 libcurl 中使用 CURLINFO_PRIVATE 访问结构?

转载 作者:行者123 更新时间:2023-11-30 16:58:31 65 4
gpt4 key购买 nike

我已经为此绞尽脑汁好几个小时了,我试图从 CURLINFO_PRIVATE 访问我的结构,但 gcc 似乎没有将其识别为结构:

10-at-a-time.c: In function 'main':
10-at-a-time.c:148:48: error: request for member 'size' in something not a structure or union
printf("%lu bytes retrieved\n", (long)chunk->size);
^
10-at-a-time.c:152:15: error: request for member 'memory' in something not a structure or union
free(chunk->memory);

在 libcurl 文档中,它说它将返回一个字符指针:https://curl.haxx.se/libcurl/c/CURLINFO_PRIVATE.html

据我所知,我应该能够使用“->”语法取消引用该指针并获取结构实例。但这似乎不起作用。

这是我的代码,希望它很容易理解:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <curl/multi.h>

struct MemoryStruct {
char *memory;
size_t size;
};

static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;

mem->memory = realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}

memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;

return realsize;
}

static const char *urls[] = {
"http://www.microsoft.com",
"http://www.opensource.org",
"http://www.google.com",
"http://www.bbc.co.uk",
"http://www.newslink.org",
"http://www.un.org",
"http://www.news.com",
"http://www.cnn.com",
"http://www.wikipedia.org",
"http://www.dell.com",
"http://www.hp.com",
"http://www.cert.org",
"http://www.mit.edu",
"http://www.nist.gov",
"http://www.ebay.com",
"http://www.playstation.com",
"http://www.uefa.com",
"http://www.ieee.org",
"http://www.apple.com",
"http://www.symantec.com"
};

#define count sizeof(urls)/sizeof(char*)

static void init(CURLM *cm, int i)
{
CURL *eh = curl_easy_init();

struct MemoryStruct chunk;

chunk.memory = malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */

curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(eh, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(eh, CURLOPT_HEADER, 0L);
curl_easy_setopt(eh, CURLOPT_URL, urls[i]);
curl_easy_setopt(eh, CURLOPT_PRIVATE, (void *)&chunk);
curl_easy_setopt(eh, CURLOPT_VERBOSE, 0L);
curl_multi_add_handle(cm, eh);
}

int main(void)
{
int bots = 10;

if (count < bots) {
bots = count;
}

CURLM *cm;
CURLMsg *msg;
long L;
struct timeval timeout;
unsigned int current = 0;

int msg_queue = -1;
int handles = -1;
int maxfd = -1;

fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;

curl_global_init(CURL_GLOBAL_ALL);
cm = curl_multi_init();

curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)bots);

for (current = 0; current < bots; ++current) {
init(cm, current);
}

while (handles) {
curl_multi_perform(cm, &handles);

if (handles) {
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);

if (curl_multi_fdset(cm, &fdread, &fdwrite, &fdexcep, &maxfd)) {
fprintf(stderr, "E: curl_multi_fdset\n");
return EXIT_FAILURE;
}

if (curl_multi_timeout(cm, &L)) {
fprintf(stderr, "E: curl_multi_timeout\n");
return EXIT_FAILURE;
}

if (L == -1) {
L = 100;
}

if (maxfd == -1) {
sleep((unsigned int)L / 1000);
}
else {
timeout.tv_sec = L / 1000;
timeout.tv_usec = (L % 1000) * 1000;

if (0 > select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout)) {
fprintf(stderr, "E: select(%i,,,,%li): %i: %s\n", maxfd + 1, L, errno, strerror(errno));
return EXIT_FAILURE;
}
}
}

while ((msg = curl_multi_info_read(cm, &msg_queue))) {
if (msg->msg == CURLMSG_DONE) {
char *chunk;
CURL *e = msg->easy_handle;
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &chunk);
fprintf(stderr, "R: %d - %s\n", msg->data.result, curl_easy_strerror(msg->data.result));

printf("%lu bytes retrieved\n", (long)chunk->size);

curl_multi_remove_handle(cm, e);
curl_easy_cleanup(e);
free(chunk->memory);
}
else {
fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
}

if (current < count) {
init(cm, current++);
handles++;
}
}
}

curl_multi_cleanup(cm);
curl_global_cleanup();

return EXIT_SUCCESS;
}

顺便说一句,我是 C 语言新手。

最佳答案

设置私有(private)指针:

ptr = [something];
curl_easy_setopt(curl, CURLOPT_PRIVATE, ptr);

再次从设置它的句柄中提取指针:

curl_easy_getinfo(curl, CURLINFO_PRIVATE, &ptr);

就您的情况而言,当您再次提取指针时,您需要确保它是正确的结构指针,而不仅仅是 char *。也许喜欢:

struct MemoryStruct *chunk;
curl_easy_getinfo(curl, CURLINFO_PRIVATE, &chunk);

...这样您就可以访问该结构及其字段。

关于c - 如何在 libcurl 中使用 CURLINFO_PRIVATE 访问结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38780506/

65 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com