libcurl 示例 httpcustomheader.c 有问题(显示不良做法)还是我遗漏了什么?
Is the libcurl example httpcustomheader.c buggy (showing bad practice) or am I missing something?
libcurl 示例包含 example for custom HTTP headers。
该示例使用 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;");
根据documentation of curl_slist_append,如果出现错误,将返回一个空指针:
RETURN VALUE
A null pointer is returned if anything went wrong, otherwise the new
list pointer is returned.
问题:
例如调用时
chunk = curl_slist_append(chunk, "Another: yes");
失败,原来的链表,也就是之前指向的chunk,会不会丢失?结果:这不会泄漏内存吗?
还是有一些我遗漏的魔法,curl_slist_append 文档中没有提到?
更糟糕的是:下一次调用 curl_slist_append 可能不会创建一个新列表(不太可能,因为我们可能已经内存不足,但有可能)?
您的怀疑似乎完全正确。 curl_slist_append
的来源可以查看 here。
libcurl 示例包含 example for custom HTTP headers。
该示例使用 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;");
根据documentation of curl_slist_append,如果出现错误,将返回一个空指针:
RETURN VALUE
A null pointer is returned if anything went wrong, otherwise the new list pointer is returned.
问题:
例如调用时
chunk = curl_slist_append(chunk, "Another: yes");
失败,原来的链表,也就是之前指向的chunk,会不会丢失?结果:这不会泄漏内存吗? 还是有一些我遗漏的魔法,curl_slist_append 文档中没有提到?
更糟糕的是:下一次调用 curl_slist_append 可能不会创建一个新列表(不太可能,因为我们可能已经内存不足,但有可能)?
您的怀疑似乎完全正确。 curl_slist_append
的来源可以查看 here。