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.