How to escape a line break literal in the HTTP header?
The actual answer to this question is that there is no standard for encoding line breaks.
You can use any Binary-to-text encoding such as URL-Encoding or Base64, but obviously that's only going to work if both sender and receiver implement the same method.
RFC 2616 did allow to 'fold' (i.e. wrap) header values over multiple lines, but the line breaks were treated as a single space character and not part of the parsed field value.
However, that specification has been obsoleted by RFC 7230 which forbids folding:
Historically, HTTP header field values could be extended over multiple lines by preceding each extra line with at least one space or horizontal tab (obs-fold).
This specification deprecates such line folding except within themessage/http
media type (Section 8.3.1).
A sender MUST NOT generate a message that includes line folding
A standard for line breaks in HTTP Header field values is not – and never was – established.
According to RFC2616 4.2 Message Headers:
Header fields can be extended over multiple lines by preceding each extra line with at least one SP or HT.
where SP means a space character (0x20) and HT means a horizontal tab character (0x09).
If you are designing your own custom extension field, you may use BASE64 or quoted-printable to escape(and unescape) the value.