On Jan 10, 2014, at 4:07 AM, Quinn The Eskimo! < email@hidden> wrote: Most of Jens's comments are spot on, but this is wrong, at least on modern systems. I tested this with a test app running in the iOS 7 simulator and "nc" running on my Mac. Huh. My hypothesis had been that this applied to all chunked responses, but maybe there are other factors involved too.
There’s a simple test case attached, which still fails for me today on OS X 10.9.1. It tries to get a changes-feed from a public CouchDB server and never gets any data back. Here’s what the server is sending:
HTTP/1.1 200 OK Transfer-Encoding: chunked Server: CouchDB/1.3.0 (Erlang OTP/R15B03) Date: Fri, 10 Jan 2014 17:56:53 GMT Content-Type: text/plain; charset=utf-8 Cache-Control: must-revalidate
6b {"seq":1,"id":"017384e1ea0b97d252344df2310001e7","changes":[{"rev":"1-4c6114c65e295552ab1019e2b046b10e"}]}
(At this point the server stops sending data but leaves the socket open; this is a continuous feed where it sends a chunk every time there’s a server-side event.)
We may actually be seeing the same behavior. I notice that in your example the delegate’s data callback wasn’t invoked until your fake server had sent about 512 bytes of response. In my test case the response is shorter than that. So it looks like there’s no reasonable timeout on the buffering, meaning that streamed responses of this nature don’t get delivered to clients in a timely manner. Even if the changes-feed were longer, chances are the last couple of events wouldn’t be sent because they’re still sitting in the buffer. This makes NSURLConnection useless for this purpose.
I’ve spent significant time working around this issue over the past 18 months, by the way. First diagnosing it, then writing a workaround in the form of a CFStream-based HTTP client with its own chunked-mode parser. I don’t say this to whine, just to point out that to me this has been a serious issue.
—Jens
PS: Also FYI, NSURLConnection isn’t the only HTTP client code with this problem. I’ve seen similar misbehavior in some HTTP proxies/gateways. |