Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to download a file via a basic socket connection, using a HTTP GET request. So, I have to specify how many bytes of data coming in I have to read from the socket. However, I am having trouble deciding the amount of data (say, in bytes) the server would reply back with. I know that there is a "Content-Length" field in the reply sent by server, but that gives me the size of the actual data (without the http headers).
Is there a way to get the exact size of HTTP headers sent by the server or an estimation is required?
(I am doing this for downloading on a mobile network, where every bit of data matters in terms of time and money, so I don't wish to make an unnecessary larger estimate of the header size.)

share|improve this question
up vote 2 down vote accepted

You don't read a fixed number of bytes. You read as many bytes as there are (or until you abort) until you hit the END OF HEADER mark (for HTTP that's \n\r\n to my knowledge). Although, for security reasons, the actual header length is limited by the HTTP server:

There is no limits to size of each header field name or value, or number of headers in standard itself. However most servers, clients and proxy software impose some limits for practical and security reasons. For example Apache 2.3 server by default limits each header size to 8190 bytes, and there can be at most 100 headers in single request.

Relying on fixed size buffers in this communication would be a big mistake. Your buffer needs to grow dynamically. You shouldn't even rely on the correctness of the Content-Length field when receiving the body of the message.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .