2

I 'm writing a HTTP web server that can respond to PUT, GET , ... requests when GET request is sent from a browser , the browser should receives the related HTML file I have analyzed the GET request in the Wireshark it sends the whole HTML file of the website and then the browser requests the rest of the files that are used in the HTML file

My problem is that when I use the send() function and send and HTTP respond to the GET requests the body part of the HTTP message that contains the HTML file is not displayed correctly in the browser when it is read from a HTML file but when I type the HTML file into a string variable and give it to the send() function it works.

here is my code:

char send_buffer[1000];
FILE *sendFile = fopen("foo.txt", "r");
while( !feof(sendFile) )
{
int numread = fread(send_buffer, sizeof(unsigned char), 1000, sendFile);
if( numread < 1 ) break; // EOF or error

char *send_buffer_ptr = send_buffer;
do
{
    int numsent = send(connected, send_buffer_ptr, numread, 0);
    if( numsent < 1 ) // 0 if disconnected, otherwise error
    {
        if( numsent < 0 )
        {
            if( WSAGetLastError() == WSAEWOULDBLOCK )
            {
                fd_set wfd;
                FD_ZERO(&wfd);
                FD_SET(connected, &wfd);

                timeval tm;
                tm.tv_sec = 10;
                tm.tv_usec = 0;

                if( select(0, NULL, &wfd, NULL, &tm) > 0 )
                    continue;
            }
        }

        break; // timeout or error
    }

    send_buffer_ptr += numsent;
    numread -= numsent;
}
while( numread > 0 );
}

I have tested the code with the foo file containing the HTTP header and some HTML code as below:

HTTP/1.1 200 OK
Content-length: 60
Content-Type: text/html


<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>

I have also tries sending the header with a separate send function and the HTML file with another send function right after it.

1 Answer 1

1

The Content-Length header in your response is incorrect. The body of your response is over 100 bytes long, not 60 bytes like the header says. (If your file uses CR+LF line endings, it's actually even longer.)

In general, the Content-Length header should be generated by the web server, as it must exactly match the size of the response body. Storing it in the response is far too prone to errors.

Sign up to request clarification or add additional context in comments.

3 Comments

I had no problem sending just part of the HTML code by decreasing the content length ,the main problem is that when reading the same HTML code from the file and sending it to the browser it is just displayed as the HTML code
After sending the file with the exact content-length it worked! Thanks a lot.
Consider using Transfer-Encoding: chunked in the response, then you don't need a Content-Length header at all, you can send the file data as you are reading it from the file, and can signal the client with a 0-length chunk at the end when finished.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.