https://github.com/refinitiv/libvmod-utils
VMOD Varnish utility module. Return hostname, test if a file exists, and so on.
https://github.com/refinitiv/libvmod-utils
Last synced: about 1 year ago
JSON representation
VMOD Varnish utility module. Return hostname, test if a file exists, and so on.
- Host: GitHub
- URL: https://github.com/refinitiv/libvmod-utils
- Owner: Refinitiv
- License: bsd-2-clause
- Created: 2014-05-05T10:43:55.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2019-11-04T14:34:52.000Z (over 6 years ago)
- Last Synced: 2025-04-05T01:32:24.690Z (about 1 year ago)
- Language: C
- Homepage:
- Size: 9.77 KB
- Stars: 3
- Watchers: 102
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: License
Awesome Lists containing this project
README
libvmod-utils
=============
VMOD Varnish module utility
```
Function STRING hostname(PRIV_VCL) # Return hostname
Function STRING timestamp() # Return timestamp (%.9f)
Function REAL real(STRING, REAL) # Convert a string into a REAL (double)
Function IP ip(STRING, IP) # Convert a string into an IP
Function BOOL exists(STRING) # Test presence of a file
```
Examples
=============
Add varnish hostname to HTTP headers sent to backend :
```
import utils;
sub vcl_pass {
set bereq.http.X-Varnish-Server = utils.hostname();
}
sub vcl_miss {
set bereq.http.X-Varnish-Server = utils.hostname();
}
```
Measure performance between VCL code or VMOD calls :
```
import utils;
sub vcl_recv {
std.log("t_prerecv:" + utils.timestamp());
(..) a lot of VCL or VMOD (..)
std.log("t_postrecv:" + utils.timestamp());
}
```
Do an action based on the presence of a file :
```
import utils;
sub vcl_recv {
if (std.tolower(req.url) == "/health_check") {
if (utils.exists("/etc/varnish/healthcheck_" + server.identity)) {
error 443 "Service not available";
}
else {
error 440 "OK";
}
}
}
sub vcl_error {
if (obj.status == 443) {
set obj.status = 503;
synthetic {"0"};
return (deliver);
}
else if (obj.status == 440) {
set obj.status = 200;
synthetic {"1"};
return (deliver);
}
}
```
Compatibility
=============
This module is compatible and useful for Varnish 3.x only.
Most of these functions have been integrated to Varnish 4. See the migration table below:
| Varnish 3.x libvmod-utils | Varnish 4.x|
| --------------------------------- |------------|
| `utils.hostname()` | `server.hostname` |
| `utils.timestamp("foobar")` | `std.timestamp("foobar")` |
| `utils.real("42.1234", 0)` | `std.real("42.1234", 0)` |
| `utils.ip(req.http.REMOTEADDRESS, client.ip)` | `std.ip(req.http.REMOTEADDRESS, client.ip)` |
| `utils.exists("/etc/return_503")` | `std.file_exists("/etc/return_503")` |
Copyright
=============
This document is licensed under BSD-2-Clause license. See LICENSE for details.
The code was opened by (c) Refinitiv (previously Thomson Reuters).