What's the difference between \r\n and \n
-
09-12-2019 - |
题
In Terminal it seems like no difference between the two
echo -en 'first\r\nsecond'
and echo -en 'first\n\second'
but in the code without \r it doesn't work
echo -en 'GET /test HTTP/1.1\r\nHost: localhost\r\n\r\n' | nc localhost 9292
works, but
echo -en 'GET /test HTTP/1.1\r\nHost: localhost\n\n' | nc localhost 9292
doesn't
anyone can explain why it is?
解决方案
Some applications can handle both \r\n (a.k.a. CRLF, carriage return line feed) and \n (a.k.a. LF, line feed) equivalently as newline sequences. Your terminal is an example.
The HTTP/1.1 Specification dictates that HTTP header lines always end with CRLF. So, an HTTP server which adheres to the specification (such as the one you're running on localhost:9292) will not interpret LF by itself as a valid HTTP header line termination sequence.
不隶属于 StackOverflow