Objective-C 中的 HTTP 消息流标记化
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,libcurl 无法为您拆分此内容。它仅分离通过网络接收的实际 HTTP 响应。
No, libcurl cannot split up this for you. It only separates actual HTTP responses that it receives over the network.