fix bug in handling gzipped images

quite a tricky bug with gzipped length
was unnoticed because there was also a bug in test code, duh..
did a small rewrite of iterator handling and content_length handling
to make it easier to hunt such bugs in the future