Skip to content

Chunked Transfer Coding (std/chunked) ​

milo
from "std/chunked" import { ChunkScan, decodeChunked }

HTTP/1.1 chunked transfer coding (RFC 9112 §7.1): framing and decoding, with no IO. std/fetch reads chunked responses with it and std/http reads chunked request bodies. It is a separate module so a plain HTTP server can decode chunked bodies without linking OpenSSL, which std/fetch pulls in.

Most programs never call it: fetch and std/http handlers see the decoded body. Reach for it only when you read HTTP framing off a socket yourself. ChunkScan checks the framing incrementally as bytes arrive; decodeChunked extracts the data once the body is complete.

API reference ​

ChunkScan ​

milo
pub struct ChunkScan

An incremental walk over a chunked body that is still arriving. Each step resumes after the last complete chunk instead of rescanning from the start, so reading a body in many segments stays linear.

ChunkScan.declared ​

milo
fn ChunkScan.declared(self: &ChunkScan): i64

Data bytes the body declares so far: every complete chunk plus the size the chunk still arriving announced. A server compares this with its body limit before the bytes arrive, so a chunk declaring a gigabyte is refused at its size line.

ChunkScan.new ​

milo
fn ChunkScan.new(start: i64): ChunkScan

A scan of a chunked body that starts at offset start (just past the headers).

ChunkScan.step ​

milo
fn ChunkScan.step(self: &mut ChunkScan, raw: &string): i64

Index just past the body (last chunk and trailers included), chunkIncomplete while more bytes are needed, or chunkMalformed.

Functions ​

decodeChunked ​

milo
pub fn decodeChunked(raw: &string, from: i64): string

The data of the chunked body in raw from offset from, chunk extensions and trailers dropped. Lenient: it decodes as far as the framing goes and stops at the last chunk or the end of input, so a caller that must refuse bad framing checks it with ChunkScan first.