Standard Library
class ChunkedDecoder
Decodes an HTTP body sent with chunked transfer coding.
since 0.1.0-alpha.1linuxwindowswasm
Overview
Create one decoder per body and give it the received text in pieces, in any sizes; a piece may even end in the middle of a chunk's size line. Each call to feed returns the body text that became complete with that piece. When the final zero-size chunk and the lines after it have been read, isDone becomes true. Chunk extensions after a ; and trailer headers are skipped.
HttpResponse.parse and the HTTP client use this internally, so you only need it to decode a body yourself.
Examples
Feeding a body in two pieces
ChunkedDecoder dec = ChunkedDecoder();
string first = dec.feed("5\r\nhel");
string second = dec.feed("lo\r\n0\r\n\r\n");
console.writeln("[${first}][${second}]");
console.writeln(dec.isDone);
[hel][lo]
true
Fields
bad
bool badBecomes true when a chunk size line was not a valid hexadecimal number. Decoding stops and isDone is also true.
isDone
bool isDoneBecomes true once the whole body has been read, or when a malformed chunk made decoding stop.
Methods
feed
feed(string chunk) -> stringGives the decoder the next piece of the received body and returns the body text decoded so far.
The piece may be of any size and may end anywhere. Text that does not yet form a complete size line is kept until the next call. After isDone is true, further text is ignored.
Parameters
- chunk
- The next piece of the chunked body, exactly as received.
Returns
The decoded body text that this piece completed; empty when nothing new is ready.
Examples
A malformed size stops decoding
ChunkedDecoder dec = ChunkedDecoder();
string out = dec.feed("zz\r\nabc");
console.writeln("[${out}]");
console.writeln(dec.bad);
console.writeln(dec.isDone);
[]
true
true
See also
- chunkEncode — Wraps text as one chunk of a chunked HTTP body.
- parse — Fills in the response from a complete response text.