vmod_bodyaccess(3)

✨ vmod_bodyaccess ✨

okay bestie, gather round, because we're talking about vmod_bodyaccess β€” the Varnish Module for request body access πŸ’–. yes, that one. the one that lets you actually peek at what the client sent you in the body, instead of just staring at headers like a clown. 🫢


🌸 SYNOPSIS 🌸

import bodyaccess [as name] [from "path"]

INT rematch_req_body(REGEX re)

VOID hash_req_body()

INT len_req_body()

VOID log_req_body(STRING prefix, INT length)

like, that's the whole vibe. four little functions and they carry the entire module. iconic behavior honestly πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

so, vmod_bodyaccess is the Varnish module that lets you access the request body. that's it. that's the whole job. and it slays at it. ✨

here's the VCL example straight from the source, no cap, copy it exactly:

vcl 4.0;
import std;
import bodyaccess;

backend default { .host = "192.0.2.11"; .port = "8080"; }

sub vcl_recv {
        if (req.method == "POST") {
                set req.http.x-method = req.method;
                if (std.cache_req_body(110KB)) {
                        set req.http.x-len = bodyaccess.len_req_body();
                        set req.http.x-re = bodyaccess.rematch_req_body("Regex");
                        bodyaccess.log_req_body("PREFIX:", 3);
                }
        }
return(hash);
}

sub vcl_hash {
        bodyaccess.hash_req_body();
        return (lookup);
}

sub vcl_backend_fetch {
        set bereq.method = bereq.http.x-method;
}

sub vcl_deliver {
        set resp.http.x-len = req.http.x-len;
        set resp.http.x-re = req.http.x-re;
}
N.B. the request body must be retrieved before you do literally any operations on it. like, do not skip this step, bestie. 😭 it can be buffered using the cache_req_body() function from libvmod_std. that's your whole entry ticket.

also important: these functions apply only to standard REST methods. and caching is not allowed on PUT requests. periodt. 🚫


πŸ‘‘ INT rematch_req_body(REGEX re)

Description

  • returns -1 if an error occurrs πŸ’€
  • returns 0 if the request body doesn't contain the string re
  • returns 1 if the request body does contain the string re πŸŽ‰

heads up though, bestie: the comparison is case sensitive, and the request body must be buffered. no buffering, no service. πŸ™ˆ

Example

std.cache_req_body(1KB);

if (bodyaccess.rematch_req_body("FOO") == 1) {
   std.log("is true");
}

🌈 VOID hash_req_body()

Description

adds the available request body bytes to the lookup hash key. so your cache key actually knows about the body. the way that works?? obsessed. πŸ”₯

but wait β€” this function can only be used in vcl_hash, and the request body must be buffered. if you try it anywhere else it's giving "not allowed", okay? πŸ’…

Example

sub vcl_recv {
    std.cache_req_body(1KB);
}

sub vcl_hash{
    bodyaccess.hash_req_body();
}

πŸŽ€ INT len_req_body()

Description

returns the request body length, or -1 if an error occurs. that's the tea. β˜•

note: the request body must be buffered. again. always. it's the rule. 😀

Example

std.cache_req_body(1KB);
set req.http.x-len = bodyaccess.len_req_body();

🧚 VOID log_req_body(STRING prefix="", INT length=200)

Description

logs the request body to the VSL, making it available for other components. so other parts of your stack can see it too β€” generous queen behavior πŸ‘‘

when logging, it takes an optional prefix and a max line length, so the body could be split up across multiple lines, because there is a limit to how large a single line can be. so don't be shocked when your body shows up in chunks, it's not broken, it's just doing its thing. πŸ“

the defaults are right there in the signature: prefix="" and length=200. free of charge. ✨

Example

std.cache_req_body(1KB);
bodyaccess.log_req_body("PREFIX:", 3);

and that's the whole page, bestie!! πŸ’œ go buffer some bodies (respectfully), hash responsibly, and remember: no buffering, no body access. iykyk. πŸ¦‹

this page signs off as vmod_bodyaccess(3) from the Library Functions Manual β€” same name up top, same name at the bottom, symmetry queen. πŸ‘‘βœ¨