1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
#-
# Copyright (c) 2010-2014 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.
$Module std 3 Varnish Standard Module
DESCRIPTION
===========
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.
$Function STRING toupper(STRING_LIST)
Description
Converts the string *s* to upper case.
Example
set beresp.http.x-scream = std.toupper("yes!");
$Function STRING tolower(STRING_LIST)
Description
Converts the string *s* to lower case.
Example
set beresp.http.x-nice = std.tolower("VerY");
$Function VOID set_ip_tos(INT)
Description
Sets the Type-of-Service flag for the current session. Please
note that the TOS flag is not removed by the end of the
request so probably want to set it on every request should you
utilize it.
Example
| if (req.url ~ ^/slow/) {
| std.set_ip_tos(0x0);
| }
$Function REAL random(REAL, REAL)
Description
Returns a random REAL number between *a* and *b*.
Example
set beresp.http.x-random-number = std.random(1, 100);
$Function VOID log(STRING_LIST)
Description
Logs *string* to the shared memory log, using VSL tag *SLT_VCL_Log*.
Example
std.log("Something fishy is going on with the vhost " + req.host);
$Function VOID syslog(INT, STRING_LIST)
Description
Logs *string* to syslog marked with *priority*. See your
system's syslog.h file for the legal values of *priority*.
Example
std.syslog(8 + 1, "Something is wrong");
$Function STRING fileread(PRIV_CALL, STRING)
Description
Reads a file and returns a string with the content. Please
note that it is not recommended to send variables to this
function the caching in the function doesn't take this into
account. Also, files are not re-read.
Example
set beresp.http.x-served-by = std.fileread("/etc/hostname");
$Function VOID collect(HEADER)
Description
Collapses the header, joining the headers into one.
Example
std.collect(req.http.cookie);
This will collapse several Cookie: headers into one, long
cookie header.
$Function DURATION duration(STRING, DURATION)
Description
Converts the string *s* to seconds. *s* must be quantified
with ms (milliseconds), s (seconds), m (minutes), h (hours),
d (days), w (weeks) or y (years) units. If *s* fails to parse,
*fallback* will be returned.
Example
set beresp.ttl = std.duration("1w", 3600s);
$Function INT integer(STRING, INT)
Description
Converts the string *s* to an integer. If *s* fails to parse,
*fallback* will be returned.
Example
if (std.integer(beresp.http.x-foo, 0) > 5) { ... }
$Function IP ip(STRING, IP)
Description
Converts string *s* to the first IP number returned by
the system library function getaddrinfo(3). If conversion
fails, *fallback* will be returned.
Example
if (std.ip(req.http.X-forwarded-for, "0.0.0.0") ~ my_acl) { ... }
$Function REAL real(STRING, REAL)
Description
Converts the string *s* to a real. If *s* fails to parse,
*fallback* will be returned.
Example
set req.http.x-real = std.real(req.http.x-foo, 0.0);
$Function TIME real2time(REAL)
Description
Converts the real *r* to a time.
Example
set req.http.x-time = std.real2time(1140618699.00);
$Function INT time2integer(TIME)
Description
Converts the time *t* to a integer.
Example
set req.http.x-int = std.time2integer(now);
$Function REAL time2real(TIME)
Description
Converts the time *t* to a real.
Example
set req.http.x-real = std.time2real(now);
$Function BOOL healthy(BACKEND)
Description
Returns true if the backend is healthy.
$Function INT port(IP)
Description
Returns the port number of an IP address.
$Function VOID rollback(HTTP)
Description
Restore *h* HTTP headers to their original state.
Example
std.rollback(bereq);
$Function VOID timestamp(STRING)
Description
Introduces a timestamp in the log with the current time. Uses
the argument as the timespamp label. This is useful to time
the execution of lengthy VCL procedures, and makes the
timestamps inserted automatically by Varnish more accurate.
Example
std.timestamp("curl-request");
$Function STRING querysort(STRING)
Description
Sorts the querystring for cache normalization purposes.
Example
set req.url = std.querysort(req.url);
$Function STRING strstr(STRING, STRING)
Description
Returns the substring if the second string is a substring of the first
string. Note that the comparison is case sensitive. You can
use the tolower function on both strings if you want case
insensitivity.
If there is no match a NULL pointer is returned which would
evaluate to false in an if-test.
Example
if (std.strstr(req.url, req.http.x-restrict))
SEE ALSO
========
* vcl(7)
* varnishd(1)
HISTORY
=======
The Varnish standard module was released along with Varnish Cache 3.0.
This manual page was written by Per Buer with help from Martin Blix
Grydeland.
COPYRIGHT
=========
This document is licensed under the same licence as Varnish
itself. See LICENCE for details.
|