Determining the content length of dynamically generated data.
Mag748
Posts: 266
Hello,
I have a simple working http/html content generator. The way it works is it adds an HTTP header to an empty string buffer. One of the lines in the header is "content Length: XXXX". It looks up snippets of HTML and adds it to a string buffer. When its done, it determines the length of the string buffer, subtracts the length of the HTTP header, and replaces the XXXX with the HTML content length. This works fine for pages that are equal to or less in size than the string buffer that I have allocated.
I have run into a problem where my pages are getting longer and will not fit in the string buffer.
How can I send the response to the HTTP client in multiple parts, without first having to generate the entire page to determine the content length? Is there a way to add some type of message that says "Here is part of the response, I have more, but I'm not sure how much more."
Then I can keep sending data, and when I am done, I can say, "Ok this is the last chuck of data".
Thanks for any input,
Marcus.
I have a simple working http/html content generator. The way it works is it adds an HTTP header to an empty string buffer. One of the lines in the header is "content Length: XXXX". It looks up snippets of HTML and adds it to a string buffer. When its done, it determines the length of the string buffer, subtracts the length of the HTTP header, and replaces the XXXX with the HTML content length. This works fine for pages that are equal to or less in size than the string buffer that I have allocated.
I have run into a problem where my pages are getting longer and will not fit in the string buffer.
How can I send the response to the HTTP client in multiple parts, without first having to generate the entire page to determine the content length? Is there a way to add some type of message that says "Here is part of the response, I have more, but I'm not sure how much more."
Then I can keep sending data, and when I am done, I can say, "Ok this is the last chuck of data".
Thanks for any input,
Marcus.
Comments
-Phil
If for some reason sending the length is needed, try Transfer Codings and Chunked Transfer Coding.
I tried removing the Content Length header, and although the webpage did render successfully, even after I closed the connection to the browser, it remained spinning its little wheel waiting for something to happen. I don't like leaving the browser hanging like that.
I will try your suggestion Mike. This seems like the best option, the only downside being that I will only be able to serve clients that support HTTP/1.1.
Is it bad practice to turn away any HTTP/1.0 client requests?
I can't think of a good reason to not accept HTTP/1.0 requests.
I have correct my socket closing procedure, now all works well.
Thanks,