liveBook · Manning

Ngx_chain_t

As explained in Refactoring to Rust

ngx_chain_t is an NGINX linked-chain structure that connects buffers for processing by HTTP output handlers.

ngx_chain_t connects buffers into a linked chain for NGINX HTTP output processing. A response containing one buffer uses that buffer with next set to a null pointer. section 4.6

Build a response chain incrementally:

  • Configure ngx_buf_t with the response buffer's start and end pointers. section 4.6
  • Initialize ngx_chain_t with the buffer pointer and a null next pointer. section 4.6
  • Pass the chain to ngx_http_output_filter to send the response data. section 4.6

Single-buffer response

A response copies its bytes into NGINX-managed memory, assigns the allocation to buf.pos and computes buf.last, then creates ngx_chain_t { buf, next: null_mut() }. section 4.6

buf.pos = response_buffer as *mut u8;
  buf.last = response_buffer.offset(
    response_bytes.len() as isize) as *mut u8;

  let mut out_chain = ngx_chain_t {
    buf,
    next: std::ptr::null_mut(),
  };

Inspect the Rust construction of ngx_chain_t after buffer pointers are assigned. section 4.6

How Refactoring to Rust covers it

How deep it goes
Defines the linked structure and demonstrates a one-buffer chain, without developing multi-buffer traversal.
Examples
Uses a calculated HTTP response copied into allocated memory and wrapped in a chain with a null next pointer.
What it assumes
Assumes Rust FFI work, NGINX request structures, allocated buffers, and pointer-offset operations.
What you can do after
Build a single-buffer response chain and submit it through ngx_http_output_filter.
How it connects
Connects ngx_buf_t, raw pointers, and ngx_http_output_filter in an NGINX FFI response path.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.