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
