Objective-C 中的 HTTP 消息流标记化

发布于 2024-10-14 01:53:06 字数 528 浏览 3 评论 0原文

我有一个 NSData 对象,它由多个连接在一起的 HTTP 响应或请求组成。将请求/响应流标记为单个 CFHTTPMessageRef 对象的最有效方法是什么?

我当前的方法是一次读取一行数据,直到 CFHTTPMessageIsHeaderComplete 返回 YES,此时我会获取 Content-Length 的值code> header 来确定与此特定请求关联的正文的长度。

这种方法效果相当好,但在分块传输编码的情况下会失败。我现在可以添加额外的逻辑来处理分块传输,但我的解析逻辑将超出我的预期。同样,我目前只处理格式良好的消息——如果消息的格式不完美,它就会出错。

是否有(理想情况下)一组 Objective-C 类可以将数据流解析为离散的 HTTP 消息?这是 libcurl 可以执行的操作吗?

I have an NSData object that consists of several HTTP responses or requests concatenated together. What is the most effective way to tokenise this stream of requests/responses into individual CFHTTPMessageRef objects?

My current approach is to read the data one line at a time until CFHTTPMessageIsHeaderComplete returns YES, at which point I then grab the value of the Content-Length header to determine the length of the body associated with this particular request.

This approach works reasonably well, but fails in the case of chunked transfer encoding. I could now add additional logic to deal with chunked transfers, but my parsing logic will grow more than I would like. Similarly, I am only currently dealing with well-formed messages -- it will trip up should a message not be formatted perfectly.

Is there (ideally) a set of Objective-C classes that can parse a stream of data into discrete HTTP messages? Is this something that libcurl could perform?

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

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

发布评论

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

评论(1

小兔几 2024-10-21 01:53:06

不,libcurl 无法为您拆分此内容。它仅分离通过网络接收的实际 HTTP 响应。

No, libcurl cannot split up this for you. It only separates actual HTTP responses that it receives over the network.

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