Data is not sent with http response in my http server
Question
I'm working on making an HTTP server for my class. But, the returned file is not being displayed by Firefox. I construct a response header and put in the right content-length
of the file I want to send, then I send the response headers with a call to send()
. Next I use send()
again to send the file, why is Firefox not displaying the file? I'll post the relevant code below.
As you'll see with the code, I am having trouble sending an error_response
, during a 404 error. Using live http headers I can see that Firefox receives the correct response header, and the cout << error_response << endl;
does print the correct response. Why doesn't Firefox display it? Do I need to make only one call to send()
containing the header and the response packet?
// send response headers
response->Print( buffer, 2000 );
if ( send( acc_tcp_sock, buffer, sizeof(buffer), 0 ) < 0 ) {
cerr << "Unable to send response headers to client." << endl;
return 5;
}
// if 200 and GET then send file
if ( response->Get_code() == 200 && method == "GET" ) {
// send file
}
// close file, if it has been opened
if ( response->Get_code() == 200 ) {
fclose( returned_file );
}
else if ( method == "GET" ) {
if ( send( acc_tcp_sock, error_response.c_str(), error_response.length(), 0 ) < 0 ) {
cerr << "Unable to send data to client." << endl;
return 6;
}
cout << error_response << endl;
}
close( acc_tcp_sock ); // close the connection
}
return 0;
}
Solution
Have you tried sending the response header and the file in the same send command? Is there a way you can tell what firefox is receiving. You could try writing, using some HTTPRequester class, a console app that sends a HTTP request to your server and see what its getting in return.