NSURLConnection gzip magic

Sun, 28. Jun 2009

Categories: en development Tags: Cocoa compression gzip htaccess iPhone NSURLConnection Objective C wget

For quite some time I ranted about not being able to use compressed network communcation out-of-the-box on the iPhone.

Despite being undocumented (or I just overlooked the hint), NSURLConnection does gzip decompression transparently!

That’s how to use it:

  1. ensure the webserver sends gzipped content, use e.g. wget to verify:

    $ wget --header='Accept-Encoding: gzip' \
    --server-response http://example.com/demo.xmlz

    It should (in case of a gzipped xml document) look like

    HTTP/1.1 200 OK
    ...
    Content-Type: text/xml
    Content-Encoding: gzip
    ...
  2. If your webserver doesn’t support transparent compression, you can still upload gzipped content and tell the server to send the correct response headers by setting up a .htaccess file:

...
AddType text/xml .xml .xmlz
AddEncoding gzip .gz .xmlz

# inspired by
# http://betterexplained.com/articles/
#  how-to-optimize-your-site-with-gzip-compression/
# compress all text & html:
AddOutputFilterByType DEFLATE text/html text/plain text/xml
...
  1. to verify from within your app, log the response header in the NSURLConnection callbacks:
- (void)connection:(NSURLConnection *)connection
  didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"didReceiveResponse %@: %@", [response URL],
      [(NSHTTPURLResponse*)response allHeaderFields]);
    buffer = [[NSMutableData dataWithCapacity:1024*1024] retain];
}

- (void)connection:(NSURLConnection *)connection
  didReceiveData:(NSData *)dat
{
    [buffer appendData:dat];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connectionDidFinishLoading %d bytes", [buffer length]);
    [buffer release];
}

It should look like

2009-06-28 14:31:09.722 DemoApp[3981:20b] \
  didReceiveResponse http://example.com/demo.xmlz: {
...
"Content-Type" = "text/xml";
"Content-Encoding" = gzip;
"Content-Length" = 123042;
}
...
2009-06-28 14:31:11.619 DemoApp[3981:20b] \
  connectionDidFinishLoading 602979 bytes

As you can see we received way more bytes than went over the wire.

P.S.: It seems not to be necessary setting the request header yourself:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
    cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
    timeoutInterval:60.0];
// set explicitly:
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];