Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streaming/Chunked HTTP and NSURLSession Hanging

I have this piece of code that I have been trying to port. The code works 100% fine on windows using a WinHTTP implementation. On IOS 7 simulator, I am using NSURLSession. For regular HTTPS get/post seems to work fine.

Things start breaking down when I use a "streaming" HTTP. In this case, the content length is unknown, because the data is streaming in continuously.

I have a blocking synchronous below call that will wait until the current request completes. When I use the first command the synchronous loop will exit after the delegate is hit. However if I replace with the commented second line the synchronous loop hangs.

        [m_pDelegate.session invalidateAndCancel];
//      [m_pDelegate.session finishTasksAndInvalidate];
blockUntilOperationsComplete();

Eventually it will exit, and I do get my data callbacks. I believe the callbacks finally trigger MINUTES later because small keep-alive messages (16 bytes long) eventually overflow the buffer and trigger a delegate call. Is there a way to reduce the buffering threshold?

like image 781
user805547 Avatar asked May 09 '26 16:05

user805547


1 Answers

After wasting 2 days on this I'll leave this for the next soul that comes by. There is no way to reduce this buffer through existing NSURL* classes. It turns out that current implementation (on iOS7, and it seems that it's like that since forever) for chunked encoding buffers incoming data by waiting for 512 bytes of chunk encoded payload to gather, and only after that callbacks will occur - important part follows - if Content-Type is "text/html". After that all following traffic triggered callbacks will happen in real time.

However if server changes Content-Type header to "application/json" it will not be buffered and your callbacks will fire as soon as something is actually received.

like image 184
maksa Avatar answered May 11 '26 06:05

maksa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!