libvmod_curl – using cURL from inside Varnish Cache
It’s sometimes necessary to be able to access HTTP resources from inside VCL. Some use cases include authentication or authorization where a service validates a token and then tell Varnish whether to proceed or not.
To do this, we recently implemented libvmod_curl
which is a set of
cURL bindings for VCL so you can fetch remote resource easily. HTTP
would be the usual method, but cURL also supports other protocols such
as LDAP or POP3.
The API is very simple, to use it you would do something like:
require curl;
sub vcl_recv {
curl.fetch("http://authserver/validate?key=" + regsub(req.url, ".*key=([a-z0-9]+), "\1"));
if (curl.status() != 200) {
error 403 "Go away";
}
}
Other methods you can use are curl.header(headername)
to get the
contents of a given header and curl.body()
to get the body of the
response. See the README file in the source for more information.