60

I was trying to understand more on Transfer-Encoding:chunked. referred some articles: http://zoompf.com/blog/2012/05/too-chunky and "Transfer-Encoding: chunked" header in PHP.

I still didn't get very clear picture. I understand setting this encoding allows server to set content in chunk to the browser and cause partial rendering of content at a time that makes web site responsive.

If I've a web application that serves dynamic content (ex: JSF based web app) hosted on IBM WAS, most of the web pages are designed to server rich static content with lots of CSS and JS files + dynamic content. How can I set transfer-encoding 'chunked' for my pages? Or in other words:

  • How do you decide which page will have 'Transfer-Encoding: chunked' and how do you set it for that page?

Your personal experience will certainly be valuable for my understanding.

Community
  • 1
  • 1
Vicky
  • 5,380
  • 18
  • 60
  • 83

2 Answers2

52

Transfer-Encoding: chunked isn't needed for progressive rendering. However, it is needed when the total content length is unknown before the first bytes are sent.

Liam
  • 27,717
  • 28
  • 128
  • 190
Julian Reschke
  • 40,156
  • 8
  • 95
  • 98
12

When the server needs to send large amount of data, chunked encoding is used by the server because it did not exactly know how big (length) the data is going to be. In HTTP terms, when server sends response Content-Length header is omitted by the server. Instead server writes the length of current chunk in hexadecimal format followed by \r\n and then chunk, followed by \r\n (Content begins with chunk size in hex followed by chunk)

This feature can be used for progressive rendering; however the server needs to flush the data as much as possible so that client can render content progressively (in case of html,css etc)

This feature is often used when server pushes data to the client in large amounts - usually in large size (mega/giga)

Mozilla Documentation

webjockey
  • 1,647
  • 2
  • 20
  • 28
  • 1
    "usually in giga" - should be mega, if anything. ;) But it't not even necessarily about size: it's as much about *time*, so the client can proceed with the received portions, even before knowing the exact size of the whole response. – Sz. Mar 27 '18 at 15:04
  • @webjockey, I am wondering if the client needs to send data to the server in chunks. It seems like chunking is only implemented by the server. – hipeople321 Sep 01 '20 at 22:24
  • web browsers has chunking decoding algorithm in-built as specified in https://tools.ietf.org/html/rfc7230#section-3.3.1. I am not aware whether web servers implement such algorithm. Alternatively you can use mutipart/form-data with a custom boundary header name but you will have to do your own implementation on how to save the bigfile that is received in chunks delimited by custom boundary header name – webjockey Sep 02 '20 at 03:50