Reputation: 913
Here's the setup:
I have an input stream created with CFReadStreamCreateForStreamedHTTPRequest
. It has the kCFStreamPropertyHTTPAttemptPersistentConnection
property set (I'm pooling connections). The server I'm connecting to is returning data with chunked encoding.
Everything works well in our normal case. My problem is that the server I'm talking to drops my connection if there are any problems. For the life of me I can't figure out how to detect the dropped connection.
In my stream's event callback I just see kCFStreamEventEndEncountered
but no error is ever reported even though in Wireshark I can clearly see that the connections are getting FIN long before the end-of-stream chunked encoding marker is sent.
It seems like I should be getting a kCFURLErrorNetworkConnectionLost
error event when the server drops the connection but I just get end-encountered.
Any suggestions greatly appreciated.
Upvotes: 1
Views: 1223
Reputation: 913
It took some digging but it turns out that this is, in fact, a problem with CFNetwork. I went back to the 10.4.10 Darwin sources and found this comment in CFHTTPFilter.c
// Premature end of stream. However, some servers send simply CRLF for their last chunk instead of 0CRLF; be tolerant of this.
So it seems that the built-in HTTP chunk decoder in iOS and OS X will consider a dropped connection to be orderly (and not report an error) if it successfully read the CRLF after the chunked data block and there are no more bytes outstanding in the buffer.
Upvotes: 3