0

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
mko
  • 21,334
  • 49
  • 130
  • 191
  • 1
    See http://stackoverflow.com/questions/3091524/what-are-carriage-return-linefeed-and-form-feed – Zaki Mar 07 '12 at 15:57

1 Answers1

1

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.

tcovo
  • 7,550
  • 2
  • 20
  • 13