Parsing HTTP message without the body
Parsing HTTP message without the body
- Subject: Parsing HTTP message without the body
- From: Jens Alfke <email@hidden>
- Date: Tue, 16 Apr 2013 09:55:48 -0700
I’m using CFHTTPMessage to parse an incoming HTTP message (request or response) from a stream. (“Stream” in the abstract sense; it’s not a direct socket connection.) But I only want CF to read the headers; I want to read the request body myself.
So I create an empty message, then call CFHTTPMessageAppendBytes when data arrives, until CFHTTPMessageIsHeaderComplete returns true. Then I query the message to get the header info.
The problem is that the last batch of bytes I appended are likely to contain the beginning of the body as well as the end of the headers. I want to read the body myself, so I can process it as it arrives instead of having it buffered into a CFDataRef. But how can I get back the beginning of the body that I’ve already fed to CFHTTPMessageAppendBytes?
* I could call CFHTTPMessageCopyBody to get the body data read so far, and use that as the start of my body stream. But can I be sure that the data past the header goes directly into the message’s body property instead of being buffered somewhere? (Assume the message is not chunked or gzipped. It’s also probably lacking a Content-Length property, since the enclosing stream’s EOF already unambiguously indicates the end of the message.)
* Or I can just grep through the data I fed to AppendBytes and find the “\r\n\r\n” and start after that. But I’m not 100% certain that this is always a reliable way to find the end of the HTTP headers.
Which do you think is best?
—Jens
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden