Factorize code
Fixes bad content-length. This header must be set BEFORE we write the content.