0

I have finished the server TODOS and all seem to be working. I realized that probably the least explained part of the TODOs was the most important 3 lines of code (at least for me). It's this part:

each of which is terminated with CRLF (i.e., \r\n), which are followed after a single blank line (i.e., an additional CRLF) by the bytes of the file itself, whereby %i represents the file’s size in bytes and %s represents the file’s MIME type.

If I DO NOT implement that extra CRLF, I cannot see any static content (eg: html) using Chrome, despite the fact that telnet seems to be working just fine. For instance, calling cat.html via telnet would show the following:

GET /cat.html HTTP/1.1

HTTP/1.1 200 OK
Connection: close
Content-Length: 156
Content-type: text/html
<!DOCTYPE html>

<html>
<head>
    <title>Happy Cat</title>
</head>
<body>
    <img alt="Happy Cat" src="cat.jpg"/>
</body>
</html>
Connection closed by foreign host.

But loading cat.html via Chrome shows nothing. As soon as I implement just that additional CRLF in the static content code, then everything works great. I cannot wrap my head around this? Could someone explain that in the context of server???

1

Great question! Google gives about 381,000 results for "the history of crlf", so you're not the first to wonder. This post says, among other things:

This protocol dates back to the days of teletypewriters. CR stands for "carriage return" - the CR control character returned the print head ("carriage") to column 0 without advancing the paper. LF stands for "linefeed" - the LF control character advanced the paper one line without moving the print head. So if you wanted to return the print head to column zero (ready to print the next line) and advance the paper (so it prints on fresh paper), you need both CR and LF.

If you go to the various internet protocol documents, such as RFC 0821 (SMTP), RFC 1939 (POP), RFC 2060 (IMAP), or RFC 2616 (HTTP), you'll see that they all specify CR+LF as the line termination sequence. So the the real question is not "Why do CP/M, MS-DOS, and Win32 use CR+LF as the line terminator?" but rather "Why did other people choose to differ from these standards documents and use some other line terminator?"

Now you know.

2
  • Thanks for the info. I did know that CRLF is \r\n . What I don't know is why just outputting 1 additional \r\n to the socket makes the difference between having the website load correctly and not seeing anything at all? Again, telnet doesn't seem to care. Telnet's output seems to indicate that the site's html (ie. static content) was loaded. But nothing shows up on the browser.
    – buitri84
    Nov 5 '15 at 5:51
  • The specification for HTTP defines the rule to "show output" when it encounters CRLF.. telnet is defined in its spec to "show output" when it encounters LF. They follow different rules and standards. Some cars require you to step on the brake and turn the key to start. Others only need you to turn the key. Nov 5 '15 at 14:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .