liveBook · Manning

Ngx_http_request_t

As explained in Refactoring to Rust

ngx_http_request_t is the NGINX C structure containing state and metadata for an HTTP request.

ngx_http_request_t is the NGINX C request structure passed into an HTTP handler. Its request_body field leads through linked buffers to the bytes submitted by the client. section 4.4.2 section 4.0

Reading a request body through ngx_http_request_t involves:

  • Following request.request_body to ngx_http_request_body_t, then bufs to ngx_chain_t, buf to ngx_buf_t, and finally the last pointer. section 4.4.2
  • Checking each pointer for null before dereferencing it, because uninitialized request buffers must produce an error rather than an invalid access. section 4.4.2
  • Creating a read-only Rust slice over NGINX-owned bytes instead of reallocating the request body. section 4.4.2
  • Tying the returned string slice's lifetime to the request reference, preventing the body from outliving ngx_http_request_t. section 4.4.2

Borrowing the request body

An unsafe request_body_as_str function accepts a reference to ngx_http_request_t and returns Result<&str, ...>. The returned slice views NGINX's existing buffers, so it remains valid exactly as long as the request reference and cannot be retained after the request is deallocated. section 4.4.2

struct ngx_http_request_t {
  request_body: *mut ngx_http_request_body_t,
  ...
}

struct ngx_http_request_body_t {
  bufs: *mut ngx_chain_t,
  ...
}

struct ngx_chain_t {
  buf: *mut ngx_buf_t,
  ...
}

struct ngx_buf_t {
  last: *mut u_char,
  ...
}

Browse the abbreviated C-compatible Rust definitions to see how ngx_http_request_t reaches the request body and its buffer end pointer. section 4.4.2

How Refactoring to Rust covers it

How deep it goes
Traces request-body pointers through nested C structures and explains null checks, slices, raw offsets, and Rust lifetimes.
Examples
Uses request_body_as_str, nested request-body definitions, null-pointer checks, and curl requests sent to a calculator endpoint.
What it assumes
Assumes C-style pointer access and basic Rust lifetime understanding while introducing slices as cheap views over existing storage.
What you can do after
Build it from scratch by extracting an NGINX request body into a lifetime-safe Rust string view for an HTTP handler.
How it connects
Connects NGINX modules with FFI, raw pointers, slices, lifetime annotations, HTTP handlers, and request-body buffers.

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.