Backport HTTP encoding / decoding optimizations which were introduced by...

... #2007.

The backport is partly done to keep backward compatibility