Accept-Encoding: gzip on iOS

Benjohn picture Benjohn · Aug 15, 2014 · Viewed 12.6k times · Source

Q1 With the iOS App kit, can I expect that Accept-Encoding: gzip will be used automatically, or do I need to explicitly ask for it to be used by setting this in the URL request somewhere?

I'm currently using the call [NSData dataWithContentsOfURL: url], although I am likely to change over to specifically setting up an NSURLSession – is this likely to automatically request gzip encoding?

Q2 Also, if it is used and the server obliges with gzip-ed content, am I correct that the client side will automatically decode the compressed content transparently so that I don't need to?

Answer

Tommy picture Tommy · Aug 15, 2014

It's all automatic, though I'm not immediately able to find an authoritative reference. You can prove it empirically using a proxy.

The best I can find so far: the iOS 7.1 Release Notes mention in passing that...

A compatibility behavior has been added to address an issue where some web servers would send the wrong Content-Length value for “Content-Encoding: gzip” content. Previously, NSURLConnection and NSURLSession would send a “network connection was lost” / NSURLErrorNetworkConnectionLost (-1005) error in this situation.

The compatibility behavior applies only if the Content-Length value exactly matches the expanded gzip’d content. It won’t apply for “off by 1” or similar miscounting.

i.e. NSURLConnection and NSURLSession include code to handle gzip content, and definitely did so prior to 7.1 (as per the reference to "previously"). Anecdotally, they appear to have done so since time immemorial but in any case 7.0+ is about 98% of the app-downloading market now.