Shop OBEX P1 Docs P2 Docs Learn Events
Determining the content length of dynamically generated data. — Parallax Forums

Determining the content length of dynamically generated data.

Mag748Mag748 Posts: 266
edited 2012-10-23 08:49 in Accessories
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.

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-10-22 12:38
    I believe the Content Length header line is optional. Try leaving it off entirely, and see what happens.

    -Phil
  • Mike GMike G Posts: 2,702
    edited 2012-10-23 05:07
    If you are returning from an HTTP GET then the Content-Length is not required - Message Length rules. The browser can figure out where the body ends when the socket closes.

    If for some reason sending the length is needed, try Transfer Codings and Chunked Transfer Coding.
  • Mag748Mag748 Posts: 266
    edited 2012-10-23 07:31
    I believe the Content Length header line is optional. Try leaving it off entirely, and see what happens.

    -Phil

    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.
    Mike G wrote:
    If for some reason sending the length is needed, try Transfer Codings and Chunked Transfer Coding.

    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?
  • Mike GMike G Posts: 2,702
    edited 2012-10-23 07:42
    Mag748 wrote:
    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.
    Make sure the socket is terminated properly.
    Mag748 wrote:
    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.
  • Mag748Mag748 Posts: 266
    edited 2012-10-23 08:49
    Mike G wrote: »
    Make sure the socket is terminated properly.
    .

    I have correct my socket closing procedure, now all works well.

    Thanks,
Sign In or Register to comment.