Reasoning behind CFReadStreamCreateForHTTPRequest in OSX/IOS

582 Views Asked by At

I have recently written an asynchronous HTTP client in c++ for iOS/OSX. I was confused as to why headers were not arriving in my CFHTTPMessageRef response object until I realised that there was an object lurking as a property of the stream which contains the headers (after handling the kCFStreamEventBytesAvailable event).

So now I copy the headers from the property into my response object during the notification of the stream ending, before calling my event handler.

(code available on request, there's quite a lot of it)

The Apple documentation is characteristically silent on the subject and I was wondering if anyone out there knows the reason for this design decision by apple. I am keen to know in case I have a fundamental misunderstanding somewhere?

EDIT: the documentation says this:

After you schedule the request on a run loop, you will eventually get a header complete callback. At this point, you can call CFReadStreamCopyProperty to get the message response from the read stream

However there seems to be no indication as to what the name or value of this event mask is.

EDIT: Having done some experimenting, I see that the stream creates its own response object some time after sending the kCFStreamEventOpenCompleted notification and before the first kCFStreamEventHasBytesAvailable notification.

In the kCFStreamEventHasBytesAvailable event handler I can do this:

auto response = (CFHTTPMessageRef)CFReadStreamCopyProperty(readStream,
                                          kCFStreamPropertyHTTPResponseHeader);
constexpr CFIndex bufferSize = 1024;
UInt8 buffer[bufferSize];
auto bytesRead = CFReadStreamRead(readStream, buffer, bufferSize);
if (bytesRead > 0) {
    CFHTTPMessageAppendBytes(response, buffer, bytesRead);
}
CFRelease(response);

and the stream's response message object is indeed updated with new body data. Now I am curious as to why this is not just done automatically by the stream?

0

There are 0 best solutions below