Search code examples
objective-cmacoslibcurltokenizecfhttp

Tokenising stream of HTTP messages in Objective-C


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?


Solution

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