Updated version
Content length should be set, even if the header isn't sent.
Removed unnecessary code.