如何使用 libcurl 解析 HTTP 标头?

发布于 2024-10-09 17:26:23 字数 649 浏览 5 评论 0原文

我环顾四周,感到非常惊讶的是,似乎没有办法可以在 libcurl 中通用地解析标头(这似乎是当今 http 的规范 C 库)。

我发现的最接近的东西是一个邮件列表帖子,其中有人建议其他人搜索邮件列表档案。

libcurl 通过 setopt 提供的唯一工具是 CURLOPT_HEADERFUNCTION 它将一次向标头响应提供一行。

考虑到标头可以跨越多行,这似乎太原始了。理想情况下,这应该正确完成一次(最好由库本身完成),而不是让应用程序开发人员不断重新发明。

编辑

天真的事情不起作用的示例,请参阅以下要点,其中包含 libcurl 代码示例和无法解析的正确格式的 http 响应: https://gist.github.com/762954

I've been looking around and am quite surprised that there seems to be no means by which one can parse headers generically in libcurl (which seems to be the canonical C library for http these days).

The closest thing I've found was a mailing list post where someone suggested someone else search through the mailing list archives.

The only facility that is provided by libcurl via setopt is CURLOPT_HEADERFUNCTION which will feed the header responses a single line at a time.

This seems entirely too primitive considering headers can span multiple lines. Ideally this should be done once correctly (preferably by the library itself) and not left for the application developers to do continually reinvent.

Edit:

An example of the naïve thing not working, see the following gist with a libcurl code example and a properly formed http response that it can't parse: https://gist.github.com/762954

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由如风 2024-10-16 17:26:23

已经一年多了,所以我想我会以“手动”方式关闭它。或者:

如果你遇到 cURL 问题,我为你感到难过,

您有多行标头,必须解析每一行标头。

Been over a year, so I think I'll close this as "manually." Or:

If you're having cURL problems, I feel bad for you son,

You've got multi-line headers and must parse each one.

两仪 2024-10-16 17:26:23

libcurl 读取每个 HTTP 标头并将其作为单个完整行发送到 标头回调

HTTP 1.1 RFC 7230 系列中不允许使用“续”HTTP 标头行,而且在此之前它们实际上已经消失,但它们也会发送到回调。

标头 API

从 libcurl 7.84.0 开始,它提供了一个易于使用的 API 来访问先前传输中的所有和任何响应标头。请参阅 curl_easy_header 以访问特定的,或使用 curl_easy_nextheader 如果你想迭代它们。

标头 API 支持“连续”行,并且无论响应来自哪个 HTTP 版本,它的工作方式都是相同的。

libcurl reads each HTTP header and sends it as a single complete line to the header callback.

"Continued" HTTP header lines are not allowed in the HTTP 1.1 RFC 7230 family, and they were virtually extinct even before that but they are also sent to the callback.

Header API

Since libcurl 7.84.0, it provides an easy-to-use API to access all and any response headers from a previous transfer. See curl_easy_header for accessing a specific one, or use curl_easy_nextheader if you want to iterate over them.

The header API supports "continued" lines and it also works identically no matter which HTTP version the response comes over.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文