简体   繁体   English

是libcurl示例httpcustomheader.c越野车(显示错误的做法)还是我错过了什么?

[英]Is the libcurl example httpcustomheader.c buggy (showing bad practice) or am I missing something?

The libcurl examples contains an example for custom HTTP headers . libcurl示例包含一个自定义HTTP标头示例

The example makes use of curl_slist_append like this: 该示例使用curl_slist_append像这样:

struct curl_slist *chunk = NULL;

/* Remove a header curl would otherwise add by itself */ 
chunk = curl_slist_append(chunk, "Accept:");

/* Add a custom header */ 
chunk = curl_slist_append(chunk, "Another: yes");

/* Modify a header curl otherwise adds differently */ 
chunk = curl_slist_append(chunk, "Host: example.com");

/* Add a header with "blank" contents to the right of the colon. Note that
   we're then using a semicolon in the string we pass to curl! */ 
chunk = curl_slist_append(chunk, "X-silly-header;");

According to the documentation of curl_slist_append a null pointer will be returned if something goes wrong: 根据curl_slist_append文档,如果出现问题,将返回空指针:

RETURN VALUE 返回值

A null pointer is returned if anything went wrong, otherwise the new list pointer is returned. 如果发生任何错误,则返回空指针,否则返回新的列表指针。

Question: 题:

When for example the call 例如当电话

chunk = curl_slist_append(chunk, "Another: yes");

fails, won't the original list, that chunk previously pointed to, be lost? 失败,先前指定的那个原始列表不会丢失吗? And as a consequence: won't this leak memory? 结果:这不会泄漏内存吗? Or is there some magic that I am missing and that is not mentioned in the curl_slist_append documentation? 还是我缺少某些魔术,而curl_slist_append文档中没有提到?

To make matters worse: won't the next call to curl_slist_append possibly create a new list (unlikely as we are probably out of memory already, but possible)? 更糟的是:下一次对curl_slist_append的调用是否可能会创建一个新列表(不太可能,因为我们可能已经内存不足,但有可能)吗?

Your suspicions seem entirely correct. 您的怀疑似乎完全正确。 The source for curl_slist_append can be viewed here . curl_slist_append的来源可以在这里查看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM