Loading ...
Sorry, an error occurred while loading the content.
 

Content-Length or chunked encoding

Expand Messages
  • Keyur Shah
    This may be a vague question bordering on absurd but I better ask it. When the content length is not known, chunked encoding is the obvious thing to do. But
    Message 1 of 5 , Aug 18, 2009
      This may be a vague question bordering on absurd but I better ask it.

      When the content length is not known, chunked encoding is the obvious thing to do.

      But say I am streaming files so I know the content length before hand. Is not setting a content-length header still a good idea even in this case?

      Both approaches allow for persistent connections. But by setting the content-length header do I lose the efficient server memory utilization provided by chunked transfers? Will certain web servers on seeing a content-length header not stream the content until it has been fully loaded in memory.

      I hope the answer is no so I can happily set content-length for static content and used chunked encoding for dynamic.

      Thanks,
      Keyur
    • Subbu Allamaraju
      If you are using chunked transfer encoding, there is no need to set the Content-Length. AFAIK, most HTTP 1.1 aware libraries will ignore the Content-Length
      Message 2 of 5 , Aug 18, 2009
        If you are using chunked transfer encoding, there is no need to set
        the Content-Length. AFAIK, most HTTP 1.1 aware libraries will ignore
        the Content-Length header when they see chunked encoding. But adding a
        correct Content-Length header should not hurt. The key is to set the
        correct value for Content-Length, as I saw at least one instance of a
        client choking on incorrect Content-Length even when the response was
        using chunked encoding.

        Subbu
        ---
        http://subbu.org
        http://www.restful-webservices-cookbook.org

        On Aug 18, 2009, at 11:49 AM, Keyur Shah wrote:

        > This may be a vague question bordering on absurd but I better ask it.
        >
        > When the content length is not known, chunked encoding is the
        > obvious thing
        > to do.
        >
        > But say I am streaming files so I know the content length before
        > hand. Is
        > not setting a content-length header still a good idea even in this
        > case?
        >
        > Both approaches allow for persistent connections. But by setting the
        > content-length header do I lose the efficient server memory
        > utilization
        > provided by chunked transfers? Will certain web servers on seeing a
        > content-length header not stream the content until it has been fully
        > loaded
        > in memory.
        >
        > I hope the answer is no so I can happily set content-length for static
        > content and used chunked encoding for dynamic.
        >
        > Thanks,
        > Keyur
      • Julian Reschke
        ... This is an HTTP question, so the answer should be in the spec: If a Content-Length header field (Section 14.13) is present, its decimal value in OCTETs
        Message 3 of 5 , Aug 19, 2009
          Subbu Allamaraju wrote:
          >
          >
          > If you are using chunked transfer encoding, there is no need to set
          > the Content-Length. AFAIK, most HTTP 1.1 aware libraries will ignore
          > the Content-Length header when they see chunked encoding. But adding a
          > correct Content-Length header should not hurt. The key is to set the
          > correct value for Content-Length, as I saw at least one instance of a
          > client choking on incorrect Content-Length even when the response was
          > using chunked encoding.
          > ...

          This is an HTTP question, so the answer should be in the spec:

          "If a Content-Length header field (Section 14.13) is present, its
          decimal value in OCTETs represents both the entity-length and the
          transfer-length. The Content-Length header field MUST NOT be sent if
          these two lengths are different (i.e., if a Transfer-Encoding header
          field is present). If a message is received with both a
          Transfer-Encoding header field and a Content-Length header field, the
          latter MUST be ignored." --
          <http://greenbytes.de/tech/webdav/rfc2616.html#rfc.section.4.4>


          BR, Julian
        • Keyur Shah
          Interesting... So (unless a Content-Disposition header is set) is it always a better idea then to never set Content-Length?
          Message 4 of 5 , Aug 19, 2009
            Interesting... So (unless a Content-Disposition header is set) is it always a better idea then to never set Content-Length?

            On Wed, Aug 19, 2009 at 12:10 AM, Julian Reschke <julian.reschke@...> wrote:
            Subbu Allamaraju wrote:
             
            If you are using chunked transfer encoding, there is no need to set
            the Content-Length. AFAIK, most HTTP 1.1 aware libraries will ignore
            the Content-Length header when they see chunked encoding. But adding a
            correct Content-Length header should not hurt. The key is to set the
            correct value for Content-Length, as I saw at least one instance of a
            client choking on incorrect Content-Length even when the response was
            using chunked encoding.
            ...

            This is an HTTP question, so the answer should be in the spec:

            "If a Content-Length header field (Section 14.13) is present, its decimal value in OCTETs represents both the entity-length and the transfer-length. The Content-Length header field MUST NOT be sent if these two lengths are different (i.e., if a Transfer-Encoding header field is present). If a message is received with both a Transfer-Encoding header field and a Content-Length header field, the latter MUST be ignored." -- <http://greenbytes.de/tech/webdav/rfc2616.html#rfc.section.4.4>


            BR, Julian

          • Julian Reschke
            ... What does this have to do with Content-Disposition? BR, Julian
            Message 5 of 5 , Aug 19, 2009
              Keyur Shah wrote:
              > Interesting... So (unless a Content-Disposition header is set) is it
              > always a better idea then to never set Content-Length?
              > ...

              What does this have to do with Content-Disposition?

              BR, Julian
            Your message has been successfully submitted and would be delivered to recipients shortly.