fix bug where HTTP headers written after data has been sent