HTTP.Stream (http_fetch v0.21.19)

Copy Markdown View Source

Process-backed readable stream used for Fetch-style response and request bodies.

Streamed responses expose this PID in HTTP.Response.body. Streaming uploads can pass a stream PID as body when HTTP.fetch/2 is called with duplex: "half". Producers write chunks with chunk/3 and finish with finish/1; consumers read by sending {:read_chunk, pid} or {:read_chunk, pid, :ack}.

Summary

Functions

Finishes a stream with ordered trailer fields (a list or HTTP.Headers).

Creates a stream from an enumerable.

Sends response trailers to the reader before the terminal stream event.

Functions

chunk(pid, chunk, timeout \\ HTTP.Config.streaming_timeout())

@spec chunk(pid(), binary(), timeout()) :: :ok | {:error, term()}

error(pid, reason)

@spec error(pid(), term()) :: :ok

finish(pid)

@spec finish(pid()) :: :ok

finish(pid, trailers)

@spec finish(pid(), HTTP.Headers.t() | HTTP.Headers.headers_list()) ::
  :ok | {:error, term()}

Finishes a stream with ordered trailer fields (a list or HTTP.Headers).

Validation limits trailers to 128 fields and 65,536 serialized bytes. Invalid trailers return an error without finishing the stream. For HTTP/1 uploads, every field must be declared in the request's Trailer header and the body must use chunk framing (no Content-Length). HTTP/2 uploads send trailing HEADERS after acknowledged DATA; fixed content length counts DATA bytes only. HTTP/3 uploads send ordered trailing HEADERS with FIN after acknowledged DATA; valid Trailer declarations are optional. Completion is signalled asynchronously, like finish/1.

from_enumerable(enumerable, opts \\ [])

@spec from_enumerable(Enumerable.t(), keyword()) :: {:ok, pid()} | {:error, term()}

Creates a stream from an enumerable.

Pass telemetry: false in the optional second argument to silence producer streaming events. The default is true.

Each enumerable item is converted to binary and emitted with backpressure. The returned PID can be passed as a streaming request body:

{:ok, stream} = HTTP.Stream.from_enumerable(["hello", " ", "world"])
HTTP.fetch(url, method: :post, body: stream, duplex: "half")

start_link(content_length)

@spec start_link(non_neg_integer()) :: {:ok, pid()}

trailers(pid, headers)

Sends response trailers to the reader before the terminal stream event.