http: fix chunk length calculation when using a custom encoding

Fix the calculation of http chunk length when writing a string with an encoding.

Added test.

Fixes issue #5777.