Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the advantage to use chunked encoding?

Tags:

html

http

In html, I know there is a chunked encoding. Anyone knows its advantage?

like image 325
user496949 Avatar asked Feb 20 '11 09:02

user496949


People also ask

Why is encoding chunked?

Chunked encoding allows the sender to send additional header fields after the message body. This is important in cases where values of a field cannot be known until the content has been produced, such as when the content of the message must be digitally signed.

What is chunked message?

Chunked transfer-coding, also known as chunking, involves transferring the body of a message as a series of chunks, each with its own chunk size header. The end of the message is indicated by a chunk with zero length and an empty line.

What is chunking in Java?

Chunk class in IText represents the smallest possible "chunk" of text. A Chunk can contain as little as a single character, up to several sentences.

What is chunk stream?

A chunked stream is a stream where each event is a chunk of elements. Byte-streams with the type Stream<List<int>> is common of example of this.


2 Answers

It's not part of HTML - it's part of HTTP.

It means that you can start writing the content to the output stream before you know exactly how large the output is going to be. This means you don't have to buffer the whole page (or whatever you're delivering) in memory or on disk before you start transmitting.

like image 91
Jon Skeet Avatar answered Oct 20 '22 16:10

Jon Skeet


You need to be aware of its disadvantages, too. Some firewalls/antiviruses want to download the complete response so they can inspect it, and will therefore block any incomplete response chunks from reaching the client.

A lot of firewalls have chunked encoding set to block by default, especially on corporate networks. If you want people to be able to reach your web service from their work computers, you need to either https the whole website (since https traffic cannot be inspected), or avoid chunked transfers.

The only situation that I can think of where that downside is justified is https streaming. If you don't need streaming, it's not worth it, in my opinion.

Responding to comment below here because I think it's important.

Network problems with chunked responses are very common. In my current project (B2B so every customer is behind a corporate network) I estimate roughly 3/4 customers experience issues.

To prove/disprove, I set up a test that sends 2 identical responses, one regular and one chunked (streaming chunks of html in 1 sec intervals for 3 minutes). Chunked response was consistently blocked (customer observed blank page) or accumulated (customer observed blank page for 3 minutes followed by complete rendering of html). Same chunked response was fine over https (customer observed incremental rendering of html in 1 sec intervals). I ran this on different customers/firewalls.

This is a known issue. You can read up on websockets, part of their appeal is that they help overcome these very common firewall/proxy problems.

like image 36
RADA Avatar answered Oct 20 '22 16:10

RADA