import std [from "path"] ;
vmod_std contains basic functions which are part and parcel of Varnish, but which for reasons of architecture fit better in a VMOD.
One particular class of functions in vmod_std is the conversions functions which all have the form:
TYPE type(STRING, TYPE)
These functions attempt to convert STRING to the TYPE, and if that fails, they return the second argument, which must have the given TYPE.
STRING toupper(STRING s)
STRING tolower(STRING s)
VOID set_ip_tos(INT tos)
if (req.url ~ "^/slow/") {
std.set_ip_tos(0);
}
REAL random(REAL lo, REAL hi)
VOID log(STRING s)
VOID syslog(INT priority, STRING s)
This will send a message to syslog using LOG_USER | LOG_ALERT.
STRING fileread(PRIV_CALL, STRING)
BOOL file_exists(STRING path)
if (std.file_exists("/etc/return_503")) {
return (synth(503, "Varnish is in maintenance"));
}
VOID collect(HEADER hdr, STRING sep=", ")
Care should be taken when collapsing headers. In particular collapsing Set-Cookie will lead to unexpected results on the browser side.
std.collect(req.http.accept); std.collect(req.http.cookie, "; ");
DURATION duration(STRING s, DURATION fallback)
INT integer(STRING s, INT fallback)
if (std.integer(req.http.foo, 0) > 5) {
...
}
IP ip(STRING s, IP fallback)
if (std.ip(req.http.X-forwarded-for, "0.0.0.0") ~ my_acl) {
...
}
REAL real(STRING s, REAL fallback)
if (std.real(req.http.foo, 0.0) > 5.5) {
...
}
INT real2integer(REAL r, INT fallback)
TIME real2time(REAL r, TIME fallback)
INT time2integer(TIME t, INT fallback)
REAL time2real(TIME t, REAL fallback)
BOOL healthy(BACKEND be)
INT port(IP ip)
VOID rollback(HTTP h)
VOID timestamp(STRING s)
STRING querysort(STRING)
BOOL cache_req_body(BYTES size)
Normally the request body is not available after sending it to the backend. By caching it is possible to retry pass operations, e.g. POST and PUT.
if (std.cache_req_body(1KB)) {
...
}
STRING strstr(STRING s1, STRING s2)
Note that the comparison is case sensitive.
if (std.strstr(req.url, req.http.restrict)) {
...
}
This will check if the content of req.http.restrict occurs anywhere in req.url.
TIME time(STRING s, TIME fallback)
Supported formats:
"Sun, 06 Nov 1994 08:49:37 GMT" "Sunday, 06-Nov-94 08:49:37 GMT" "Sun Nov 6 08:49:37 1994" "1994-11-06T08:49:37" "784111777.00" "784111777"
if (std.time(resp.http.last-modified, now) < now - 1w) {
...
}
STRING getenv(STRING name)
See getenv(3)
set req.http.My-Env = std.getenv("MY_ENV");
VOID late_100_continue(BOOL late)
Varnish always generates a 100 Continue response if requested by the client trough the Expect: 100-continue header when waiting for request body data.
But, by default, the 100 Continue response is already generated immediately after vcl_recv returns to reduce latencies under the assumption that the request body will be read eventually.
Calling std.late_100_continue(true) in vcl_recv will cause the 100 Continue response to only be sent when needed. This may cause additional latencies for processing request bodies, but is the correct behavior by strict interpretation of RFC7231.
This function has no effect outside vcl_recv and after calling std.cache_req_body() or any other function consuming the request body.
vcl_recv {
std.late_100_continue(true);
if (req.method == "POST") {
std.late_100_continue(false);
return (pass);
}
...
}
Copyright (c) 2010-2017 Varnish Software AS All rights reserved. Author: Poul-Henning Kamp <phk@FreeBSD.org> Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.