Refactor replace_encoding_header.