目标:
修改可在此处找到的HTTP/2 multiplexing
的libcurl
示例,以在有效负载响应到达某个缓冲区时将其保存到文件中,而不是像上述示例当前那样将其写入文件。然后,缓冲区中的有效负载将可用于打印、搜索字符串等操作。
预期产出:
程序应打印出每个收到的有效负载响应,以便在callback
函数检测到已传递时stdout
。
实际输出:
有时,程序在少量传输中按预期工作(请参阅下面main()
中的代码行int num_transfers = 3
)。如果传输次数增加到 8 或 10,有时程序无法正常运行,程序仍将输出打印到stdout
,但以默认格式,如果代码中不包含CURL_WRITEFUNCTION
/CURL_WRITEDATA
,libcurl
会这样做,可能表明callback
函数没有收到任何内容?同样在这种情况下,将打印不正确的响应数。
在main()
内的主do...while
循环中,我设置了chunk.memory
和chunk.size
在打印出来后等于0。如果不这样做,每次收到新的回复时,这些回复都会继续增长。但是,我不确定这是否是正确的方法。
当前尝试:
使用可在此处找到的libcurl
示例,我尝试模拟将输出写入回调函数的功能,如下所示(而不是将每个响应有效负载写入文件)。
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
/* somewhat unix-specific */
#include <sys/time.h>
#include <unistd.h>
/* curl stuff */
#include <curl/curl.h>
#include <curl/mprintf.h>
#ifndef CURLPIPE_MULTIPLEX
#define CURLPIPE_MULTIPLEX 0
#endif
struct CURLMsg *msg;
struct transfer {
CURL *easy;
unsigned int num;
FILE *out;
};
struct MemoryStruct {
char *memory;
size_t size;
};
struct MemoryStruct chunk;
#define NUM_HANDLES 1000
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;
char *ptr = (char*)realloc(mem->memory, mem->size + realsize + 1);
if(!ptr) {
/* out of memory! */
std::cout << "not enough memory (realloc returned NULL)" << std::endl;
return 0;
}
mem->memory = ptr;
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
static void setup(struct transfer *t, int num)
{
CURL *hnd;
hnd = t->easy = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(hnd, CURLOPT_WRITEDATA, (void *)&chunk);
/* set the same URL */
curl_easy_setopt(hnd, CURLOPT_URL, "https://someurl.xyz");
/* HTTP/2 please */
curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
/* we use a self-signed test server, skip verification during debugging */
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
#if (CURLPIPE_MULTIPLEX > 0)
/* wait for pipe connection to confirm */
curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
#endif
}
int main() {
struct transfer trans[NUM_HANDLES];
CURLM *multi_handle;
int i;
int still_running = 0; /* keep number of running handles */
int num_transfers = 3;
chunk.memory = (char*)malloc(1);
chunk.size = 0;
/* init a multi stack */
multi_handle = curl_multi_init();
for(i = 0; i < num_transfers; i++) {
setup(&trans[i], i);
/* add the individual transfer */
curl_multi_add_handle(multi_handle, trans[i].easy);
}
curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
// Main loop
do {
CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
if(still_running) {
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
}
if(mc) {
break;
}
// Get response
do {
int queued;
msg = curl_multi_info_read(multi_handle, &queued);
if ((msg) && (msg->msg == CURLMSG_DONE) && (msg->data.result == CURLE_OK)) {
// Print the response payload
std::cout << "size: " << chunk.size << std::endl;
std::cout << chunk.memory << std::endl;
chunk.memory = 0;
chunk.size = 0;
}
} while (msg);
} while (still_running);
for(i = 0; i < num_transfers; i++) {
curl_multi_remove_handle(multi_handle, trans[i].easy);
curl_easy_cleanup(trans[i].easy);
}
free(chunk.memory);
curl_multi_cleanup(multi_handle);
return 0;
}
总结问题:
问题 1.如何修改上述程序以将收到的有效负载响应正确异步保存到struct
或buffer
中,以便其可用于打印到stdout
或搜索strings
等功能?
首先,您应该将transfer
结构与访问输出的方式相关联:
struct transfer {
CURL *easy;
unsigned int num;
std::string contents;
};
并将CURLOPT_WRITEDATA
与指针相关联:
curl_easy_setopt(hnd, CURLOPT_WRITEDATA, (void *)t);
然后,WriteMemoryCallback
变成:
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {
transfer *t = (transfer *)userp;
size_t realsize = size * nmemb;
t->contents.append((const char *)contents, realsize);
return realsize;
}
之后,您可以在trans[i].contents
变量中找到内容。