HTTP 标头换行样式

发布于 2024-11-03 01:40:23 字数 70 浏览 2 评论 0原文

哪种换行符样式更适合在 HTTP 标头中使用:\r\n\n,为什么?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尝蛊 2024-11-10 01:40:23

\r\n,因为它在协议规范中被定义为换行符。 RFC2616 在第 2.2 节“基本规则”开头声明,相当明确地:

CR =
LF =
HTTP/1.1 将序列 CR LF 定义为除实体主体之外的所有协议元素的行尾标记

RFC2616 在技术上已被 RFC7230 废弃,但它没有进行重大更改,并再次调用 CRLF 作为 第 3 节,并且 RFC 引用了 第 3 节。 rfc-editor.org/rfc/rfc5234#appendix-B.1" rel="noreferrer">RFC5234,附录 B.1 将“CRLF”定义为 %x0D %x0A

然而,认识到人们出于任何目的都会违反标准,第 19.3 节(请注意,它重复了正确的序列):

消息头字段的行终止符是序列 CRLF。但是,我们建议应用程序在解析此类标头时将单个 LF 识别为行终止符并忽略前导 CR。

在较新的 RFC7230,§ 3.5

虽然起始行和头字段的行终止符是序列 CRLF,但接收者可以将单个 LF 识别为行终止符并忽略任何前面的 CR。

因此,除非您想作恶或以其他方式违反 RFC 规则,否则请使用 \r\n

\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

RFC2616 was technically obsoleted by RFC7230, but it makes no drastic changes and again calls out CRLF as the delimiter in section 3, and that RFC references RFC5234, Appendix B.1 to define "CRLF" as %x0D %x0A.

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.

In the newer RFC7230, § 3.5

Although the line terminator for the start-line and header fields is the sequence CRLF, a recipient MAY recognize a single LF as a line terminator and ignore any preceding CR.

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

淡淡的优雅 2024-11-10 01:40:23

\r\n 因为 RFC 2616 是这么说的(第 2.2 节“基本规则”):

HTTP/1.1 定义了序列 CR LF
作为所有的行尾标记
协议元素除了
实体主体(参见附录 19.3
宽容的应用程序)。这
行尾标记
实体主体由其定义
关联的媒体类型,如中所述
第 3.7 节。

<前><代码> CRLF = CR LF

\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
我爱人 2024-11-10 01:40:23

CRLF ("\r\n"),因为浏览器遵循 RFC2616

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文