Fixed memory and file handle leaks and a content-type bug.

You can pull this if you want, it fixes a number of memory leaks and file handle leaks that I've observed.

It also has a commit in there to use the nginx config to figure out the content type of the extracted content.