Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape a line break literal in the HTTP header?

Tags:

http-headers

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?

like image 676
Daniel Silveira Avatar asked Feb 06 '09 17:02

Daniel Silveira


People also ask

How do I end HTTP header?

"http headers end with an empty line ... that is not always the case" Wrong. Headers ALWAYS end with an empty line.

Does HTTP use carriage return?

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 .

What is the specific end of line sequence in an HTTP header?

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.


4 Answers

If you are designing your own custom extension field, you may use BASE64 or quoted-printable to escape(and unescape) the value.

like image 126
Dennis C Avatar answered Oct 22 '22 12:10

Dennis C


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 the message/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.

like image 28
jomo Avatar answered Oct 22 '22 12:10

jomo


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).

like image 39
James Mead Avatar answered Oct 22 '22 12:10

James Mead


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.

like image 20
unixman83 Avatar answered Oct 22 '22 10:10

unixman83