接受的答案在这页上说,我们应该检查\r\n\r\n和\n\n的HTTP响应,作为将头和内容分开的顺序。
比如:
HTTP/1.1 200 Ok\r\n
Server: AAA\r\n
Cache-Control: no-cache\r\n
Date: Fri, 07 Nov 2014 23:20:27 GMT\r\n
Content-Type: text/html\r\n
Connection: close\r\n\r\n <--------------或者:
HTTP/1.1 200 Ok\r\n
Server: AAA\r\n
Cache-Control: no-cache\r\n
Date: Fri, 07 Nov 2014 23:20:27 GMT\r\n
Content-Type: text/html\r\n
Connection: close\n\n <--------------在我在Wireshark中看到的所有响应中,服务器使用\r\n\r\n。
真的有必要同时检查一下吗?哪些服务器/协议版本将使用\n\n
发布于 2021-03-06 13:20:07
我从\r\n\r\n开始,但很快就找到了一些使用\n\n的网站。看看一些专业图书馆,比如curl,他们也可以处理\n\n,即使它不符合标准。
我不太了解curl代码,但在这里可以看到:https://github.com/curl/curl/blob/7a33c4dff985313f60f39fcde2f89d5aa43381c8/lib/http.c#L1406-L1413
/* find the end of the header line */
end = strchr(start, '\r'); /* lines end with CRLF */
if(!end) {
/* in case there's a non-standard compliant line here */
end = strchr(start, '\n');
if(!end)
/* hm, there's no line ending here, use the zero byte! */
end = strchr(start, '\0');
}考虑到这一点,我认为即使是\0\0也会被处理。
所以:
发布于 2014-11-08 16:00:31
HTTP规范说:
消息头字段的行终止符是序列CRLF.但是,我们建议应用程序在解析此类标头时,将单个LF识别为行结束符,而忽略前面的CR。
在实践中,我从未见过带有CR行分隔符的web服务器。
https://stackoverflow.com/questions/26811822
复制相似问题