HTTP头中空白符的定义是什么?对RFC 7230相关描述存疑
Great question—this is a super common point of confusion because HTTP's definition of whitespace is intentionally stricter than the broader set of ASCII whitespace characters you might see referenced elsewhere.
Let's cut straight to the exact definition from RFC 7230 Section 3 (the paragraph you're asking about):
Whitespace is defined as either a space (SP, U+0020) or horizontal tab (HTAB, U+0009).
That's it—only two characters qualify. Here's why this matters, and why your confusion makes sense:
- CRLF is excluded by design: As you noted, CRLF (
\r\n, U+000D followed by U+000A) is the line terminator for HTTP header fields and the marker that ends the header section entirely. It's a structural character, not whitespace used for formatting within fields or values. - Other ASCII whitespace doesn't count: While Wikipedia lists characters like non-breaking space (U+00A0), line feed (LF, U+000A), or carriage return (CR, U+000D) as ASCII whitespace, HTTP explicitly rejects these in its whitespace definition. Using any of these in place of SP or HTAB will either be treated as part of a header field/value (leading to unexpected behavior) or cause the message to be parsed as invalid.
The reason for this strictness is HTTP's need for consistent, unambiguous parsing across all clients and servers. By limiting whitespace to only SP and HTAB, the standard eliminates edge cases that could break message handling.
For example, if you tried to use a non-breaking space (U+00A0) in a header like Content-Type: text/html, most servers would interpret that non-breaking space as part of the media type value, leading to a misinterpretation of the content format.
内容的提问来源于stack exchange,提问作者RedRidingHood

