views:

439

answers:

1

For fun I'm trying to write a very simple server in C.

When I send this response to Firefox it prints out the body "hello, world" but with Chromium it gives me a Error 100 (net::ERR_CONNECTION_CLOSED): Unknown error.

This, I believe, is the relevant code:

char *response = "HTTP/1.0 200 OK\r\nVary: Accept-Encoding, Accept-Language\r\nConnection: Close\r\nContent-Type: text/plain\r\nContent-Length:20\r\n\r\nhello, world";
    if(send(new_fd, response, strlen(response), 0) == strlen(response)) {
        printf("sent\n");   
    };

    close(new_fd);

What am I missing?

Thanks!

+2  A: 

Content-Length seems to be 12, not 20.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4:

When a Content-Length is given in a message where a message-body is allowed, its field value MUST exactly match the number of OCTETs in the message-body. HTTP/1.1 user agents MUST notify the user when an invalid length is received and detected

Doesn't that mean FF violates specs? (Well, you are using HTTP/1.0, so maybe not.)

jholster
That was very silly of me. I assumed the Content-Length was unimportant as long it was big enough. Thank you.
Tyler
Just checked with HTTP/1.1 in FF, it does indeed allow for incorrect message length.
Tyler