better gateway header handling

ref #1826