Send 'content-length' HTTP header

Also improved file writing resiliency