vendredi 26 octobre 2018

Why web server should clear away socket receive buffer before sending response?

I am studying a tiny web server, it can receive get request from browser and return a html file. The serve_file function sends response messages to browser, and the get_linefunction gets a line from socket buffer. I don't know why must read & discard request headers. I try to comment the two lines and the browser show a connection reset page. I guess the server socket receive buffer is full, but I don't know the specific reason. Could anyone explain it?
source code

/* Send a regular file to the client.  Use headers, and report*/

void serve_file(int client, const char *filename){
    FILE *resource = NULL;
    int numchars = 1;
    char buf[1024];

    buf[0] = 'A'; buf[1] = '\0';

    //why?
    while ((numchars > 0) && strcmp("\n", buf))  /* read & discard request headers */
        numchars = get_line(client, buf, sizeof(buf));

    resource = fopen(filename, "r");
    if (resource == NULL)
        not_found(client);
    else {
        headers(client, filename);//send headers to tcp buffer
        cat(client, resource); //send index.html to tcp buffer
    }
    fclose(resource);
}




Aucun commentaire:

Enregistrer un commentaire