Handling whitespaces in http headers

2020-04-10 02:07发布

问题:

Should leading or trailing whitespaces be ignored in an http request/response?

For example, should a HTTP/1.1 compliant client interpret this :

Connection  :   close      \r\n

as this :

Connection: close\r\n

回答1:

According with paragraph 4.2 of RFC2616 (HTTP/1.1), field values might be preceded by whitespace, but not the field name:

Each header field consists of a name followed by a colon (":") and the field value. Field names are case-insensitive. The field value may be preceded by any amount of LWS (linear white space), though a single SP is preferred. Header fields can be extended over multiple lines by preceding each extra line with at least one SP or HT. Applications ought to follow "common form", where one is known or indicated, when generating HTTP constructs, since there might exist some implementations that fail to accept anything.



标签: http