In the HTTP header, line breaks are tokens to separate fields in the header.
But, if I wan't to send a line break literal in a custom field how should I escape it?
"http headers end with an empty line ... that is not always the case" Wrong. Headers ALWAYS end with an empty line.
Format of an HTTP RequestAn HTTP request contains a series of lines that each end with a carriage return and a line feed, represented as either <CR><LF> or \r\n .
The line terminator for message-header fields is the sequence CRLF. However, we recommend that applications, when parsing such headers, recognize a single LF as a line terminator and ignore the leading CR.
If you are designing your own custom extension field, you may use BASE64 or quoted-printable to escape(and unescape) the value.
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).
The idea is, that HTTP is ASCII
-only and newlines and such are not allowed. If both sender and receiver can interpret YOUR encoding then you can encode whatever you want, however you want. That's how DNS international names are handled with the Host
header (it's called PUNYCODE).
Short answer is: You don't, unless you control both sender and receiver.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With