0

Server

I have a Boost::Asio based http WebService, which asynchronously processes client connections and replies with a JSON content preceded by HTTP headers:

std::string response += "HTTP/1.0 200 OK\r\n";
response += "Content-Length: 2000\r\n";        
response += "Content-Type: application/json\r\n";
response += "Connection: close\r\n";
response += "\r\n";
response += meassageBody;

where messageBody is a JSON structure of size not bigger than 500.

Each time - after the response is sent back to client - I call the sequence below to have (I believe) socket closed:

m_oSocket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
m_oSocket.close();

Client

To test the server I wrote a small client application in Qt that asks the server for content and shows if any error has been returned.

If the server replies with the "Content-Length" value in http header:

  • hard-coded (to 2000 as in example above), then the Qt client shows Error code 2, which stands for "Connection closed";

  • set to a proper JSON data size (usually about 400-500), then the Qt client shows Error code 0, which stands for "Unknown error";

Questions:

  1. Does sending improper "Content-Length" value in http header have such influence on the way how the connection is terminated?

  2. I thought that closing the socket on server side could make the connection be seen as closed also on the client side. How to accomplish both at once: having the proper "Content-Lenght" value sent and a "Connection closed" status on client side?

Thanks

schedar
  • 419
  • 6
  • 21

1 Answers1

1

The Content-Length header has to be exactly the number of bytes of the content, no more, no less. If it's not, the results are unpredictable from the client point of view.

Since you are using HTTP 1.0, you should not close the connection at the server (and you should also not send the Connection: close header. The client will close the connection once it has read the data.

Francis Upton IV
  • 19,322
  • 3
  • 53
  • 57
  • Thank you for your reply. After reading in addition about HTTP 1.0 and 1.1 all is much more clear now. – schedar Aug 31 '12 at 13:42