HTTP header line break style

2019-01-02 20:15发布

Which line break style is preferable for use in HTTP headers: \r\n or \n, and why?

3条回答
回忆,回不去的记忆
2楼-- · 2019-01-02 20:52

CRLF ("\r\n"), because browsers follow RFC2616.

查看更多
泛滥B
3楼-- · 2019-01-02 21:04

\r\n, because it's defined as the line break in the protocol specification. RFC2616 states at the beginning of Section 2.2 (Basic Rules (!)), quite unambiguously:

CR = <US-ASCII CR, carriage return (13)>
LF = <US-ASCII LF, linefeed (10)>
HTTP/1.1 defines the sequence CR LF as the end-of-line marker for all protocol elements except the entity-body

However, recognizing that people will break the standard for whatever purposes, there is a "tolerance provision" in Section 19.3 (note that it re-iterates the correct sequence):

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.

Therefore, unless you want to be Evil or otherwise break the RFC's rules, use \r\n.

查看更多
高级女魔头
4楼-- · 2019-01-02 21:07

\r\n because RFC 2616 says so (Section 2.2, "Basic Rules"):

HTTP/1.1 defines the sequence CR LF as the end-of-line marker for all
protocol elements except the entity-body (see appendix 19.3 for
tolerant applications). The end-of-line marker within an entity-body is defined by its associated media type, as described in section 3.7.

   CRLF           = CR LF
查看更多
登录 后发表回答