CDK-443: Fix refactor header field handling.

This refactors the header field handling to avoid multiple calls to
trim() and duplicate validation checks.