fix: don't append strings naive, set headers only if really required

Naive string appending is extremely slow.
Using a list and  ''.join() at the end is way faster.