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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
|
<!DOCTYPE html>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>vmod.vcc</title>
<meta name="generator" content="KF5::SyntaxHighlighting - Definition (Varnish module spec file) - Theme (Breeze Dark)"/>
</head><body style="background-color:#232629;color:#cfcfc2"><pre>
<span style="color:#7a7c7d">#-</span>
<span style="color:#7a7c7d"># Copyright (c) 2010-2017 Varnish Software AS</span>
<span style="color:#7a7c7d"># All rights reserved.</span>
<span style="color:#7a7c7d">#</span>
<span style="color:#7a7c7d"># Author: Poul-Henning Kamp <phk@FreeBSD.org></span>
<span style="color:#7a7c7d">#</span>
<span style="color:#7a7c7d"># </span><span style="color:#3f8058;font-style:italic">SPDX-License-Identifier:</span><span style="color:#3f8058;font-style:italic"> </span><span style="color:#3f8058;font-style:italic">BSD-2-Clause</span>
<span style="color:#7a7c7d">#</span>
<span style="color:#7a7c7d"># Redistribution and use in source and binary forms, with or without</span>
<span style="color:#7a7c7d"># modification, are permitted provided that the following conditions</span>
<span style="color:#7a7c7d"># are met:</span>
<span style="color:#7a7c7d"># 1. Redistributions of source code must retain the above copyright</span>
<span style="color:#7a7c7d"># notice, this list of conditions and the following disclaimer.</span>
<span style="color:#7a7c7d"># 2. Redistributions in binary form must reproduce the above copyright</span>
<span style="color:#7a7c7d"># notice, this list of conditions and the following disclaimer in the</span>
<span style="color:#7a7c7d"># documentation and/or other materials provided with the distribution.</span>
<span style="color:#7a7c7d">#</span>
<span style="color:#7a7c7d"># THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND</span>
<span style="color:#7a7c7d"># ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span>
<span style="color:#7a7c7d"># IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span>
<span style="color:#7a7c7d"># ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE</span>
<span style="color:#7a7c7d"># FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL</span>
<span style="color:#7a7c7d"># DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS</span>
<span style="color:#7a7c7d"># OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)</span>
<span style="color:#7a7c7d"># HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT</span>
<span style="color:#7a7c7d"># LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY</span>
<span style="color:#7a7c7d"># OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF</span>
<span style="color:#7a7c7d"># SUCH DAMAGE.</span>
$ABI strict
<span style="font-weight:bold">$Module</span> <span style="color:#2980b9">std</span> <span style="color:#f67400">3</span><span style="color:#f44f4f"> "Varnish Standard Module"</span>
DESCRIPTION
===========
<span style="color:#7a7c7d">.. note: not using :ref:`vmod_std(3)` because it expands to "VMOD</span>
<span style="color:#2980b9"> </span><span style="color:#7a7c7d">std - Varnish Standard Module" and here just the plan vmod name</span>
<span style="color:#2980b9"> </span><span style="color:#7a7c7d">makes more sense.</span>
<span style="font-style:italic">*vmod_std*</span> contains basic functions which are part and parcel of
Varnish, but which for reasons of architecture fit better in a VMOD.
Numeric functions
=================
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">REAL</span> <span style="color:#8e44ad">random</span>(<span style="color:#2980b9">REAL</span> lo, <span style="color:#2980b9">REAL</span> hi)
Returns a random real number between <span style="font-style:italic">*lo*</span> and <span style="font-style:italic">*hi*</span>.
This function uses the "testable" random generator in varnishd which
enables determinstic tests to be run (See <span style="color:#2980b9">``m00002.vtc``</span>). This
function should not be used for cryptographic applications.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> set beresp.http.random-number = std.random(1, 100);</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">REAL</span> <span style="color:#8e44ad">round</span>(<span style="color:#2980b9">REAL</span> r)
Rounds the real <span style="font-style:italic">*r*</span> to the nearest integer, but round halfway cases
away from zero (see <span style="color:#f67400">`round(3)`</span>).
String functions
================
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">VOID</span> <span style="color:#8e44ad">collect</span>(<span style="color:#2980b9">HEADER</span> hdr, <span style="color:#2980b9">STRING</span> sep=", ")
Collapses multiple <span style="font-style:italic">*hdr*</span> headers into one long header. The default
separator <span style="font-style:italic">*sep*</span> is the standard comma separator to use when collapsing
headers, with an additional whitespace for pretty printing.
Care should be taken when collapsing headers. In particular collapsing
<span style="color:#2980b9">``Set-Cookie``</span> will lead to unexpected results on the browser side.
Examples<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> std.collect(req.http.accept);</span>
<span style="color:#2980b9"> std.collect(req.http.cookie, "; ");</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">STRING</span> <span style="color:#8e44ad">querysort</span>(<span style="color:#2980b9">STRING</span>)
Sorts the query string for cache normalization purposes.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> set req.url = std.querysort(req.url);</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">STRING</span> <span style="color:#8e44ad">toupper</span>(STRANDS s)
Converts the string <span style="font-style:italic">*s*</span> to uppercase.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> set beresp.http.scream = std.toupper("yes!");</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">STRING</span> <span style="color:#8e44ad">tolower</span>(STRANDS s)
Converts the string <span style="font-style:italic">*s*</span> to lowercase.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> set beresp.http.nice = std.tolower("VerY");</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">STRING</span> <span style="color:#8e44ad">strstr</span>(<span style="color:#2980b9">STRING</span> s1, <span style="color:#2980b9">STRING</span> s2)
Returns a string beginning at the first occurrence of the string <span style="font-style:italic">*s2*</span>
in the string <span style="font-style:italic">*s1*</span>, or an empty string if <span style="font-style:italic">*s2*</span> is not found.
Note that the comparison is case sensitive.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> if (std.strstr(req.url, req.http.restrict)) {</span>
<span style="color:#2980b9"> ...</span>
<span style="color:#2980b9"> }</span>
This will check if the content of <span style="color:#2980b9">``req.http.restrict``</span> occurs
anywhere in <span style="color:#2980b9">``req.url``</span>.
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">BOOL</span> <span style="color:#8e44ad">fnmatch</span>(<span style="color:#2980b9">STRING</span> pattern, <span style="color:#2980b9">STRING</span> subject, <span style="color:#2980b9">BOOL</span> pathname=1,
BOOL noescape=0, BOOL period=0)
Shell-style pattern matching; returns <span style="color:#2980b9">``true``</span> if <span style="font-style:italic">*subject*</span> matches
<span style="font-style:italic">*pattern*</span>, where <span style="font-style:italic">*pattern*</span> may contain wildcard characters such as <span style="color:#2980b9">``*``</span>
or <span style="color:#2980b9">``?``</span>.
The match is executed by the implementation of <span style="color:#f67400">`fnmatch(3)`</span> on your
system. The rules for pattern matching on most systems include the
following:
* <span style="color:#2980b9">``*``</span> matches any sequence of characters
* <span style="color:#2980b9">``?``</span> matches a single character
* a bracket expression such as <span style="color:#2980b9">``[abc]``</span> or <span style="color:#2980b9">``[!0-9]``</span> is interpreted
as a character class according to the rules of basic regular
expressions (<span style="font-style:italic">*not*</span> <span style="color:#f67400">`pcre(3)`</span> regexen), except that <span style="color:#2980b9">``!``</span> is used for
character class negation instead of <span style="color:#2980b9">``^``</span>.
If <span style="font-style:italic">*pathname*</span> is <span style="color:#2980b9">``true``</span>, then the forward slash character <span style="color:#2980b9">``/``</span> is
only matched literally, and never matches <span style="color:#2980b9">``*``</span>, <span style="color:#2980b9">``?``</span> or a bracket
expression. Otherwise, <span style="color:#2980b9">``/``</span> may match one of those patterns. By
default, <span style="font-style:italic">*pathname*</span> is <span style="color:#2980b9">``true``</span>.
If <span style="font-style:italic">*noescape*</span> is <span style="color:#2980b9">``true``</span>, then the backslash character <span style="color:#2980b9">``\``</span> is
matched as an ordinary character. Otherwise, <span style="color:#2980b9">``\``</span> is an escape
character, and matches the character that follows it in the
<span style="font-style:italic">*pattern*</span>. For example, <span style="color:#2980b9">``\\``</span> matches <span style="color:#2980b9">``\``</span> when <span style="font-style:italic">*noescape*</span> is
<span style="color:#2980b9">``true``</span>, and <span style="color:#2980b9">``\\``</span> when <span style="color:#2980b9">``false``</span>. By default, <span style="font-style:italic">*noescape*</span> is
<span style="color:#2980b9">``false``</span>.
If <span style="font-style:italic">*period*</span> is <span style="color:#2980b9">``true``</span>, then a leading period character <span style="color:#2980b9">``.``</span> only
matches literally, and never matches <span style="color:#2980b9">``*``</span>, <span style="color:#2980b9">``?``</span> or a bracket
expression. A period is leading if it is the first character in
<span style="font-style:italic">*subject*</span>; if <span style="font-style:italic">*pathname*</span> is also <span style="color:#2980b9">``true``</span>, then a period that
immediately follows a <span style="color:#2980b9">``/``</span> is also leading (as in <span style="color:#2980b9">``/.``</span>). By
default, <span style="font-style:italic">*period*</span> is <span style="color:#2980b9">``false``</span>.
<span style="color:#27ae60">`std.fnmatch()`_</span> invokes VCL failure and returns <span style="color:#2980b9">``false``</span> if
either of <span style="font-style:italic">*pattern*</span> or <span style="font-style:italic">*subject*</span> is <span style="color:#2980b9">``NULL``</span> -- for example, if an
unset header is specified.
Examples<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> # Matches URLs such as /foo/bar and /foo/baz</span>
<span style="color:#2980b9"> if (std.fnmatch("/foo/\*", req.url)) { ... }</span>
<span style="color:#2980b9"> # Matches URLs such as /foo/bar/baz and /foo/baz/quux</span>
<span style="color:#2980b9"> if (std.fnmatch("/foo/\*/\*", bereq.url)) { ... }</span>
<span style="color:#2980b9"> # Matches /foo/bar/quux, but not /foo/bar/baz/quux</span>
<span style="color:#2980b9"> if (std.fnmatch("/foo/\*/quux", req.url)) { ... }</span>
<span style="color:#2980b9"> # Matches /foo/bar/quux and /foo/bar/baz/quux</span>
<span style="color:#2980b9"> if (std.fnmatch("/foo/\*/quux", req.url, pathname=false)) { ... }</span>
<span style="color:#2980b9"> # Matches /foo/bar, /foo/car and /foo/far</span>
<span style="color:#2980b9"> if (std.fnmatch("/foo/?ar", req.url)) { ... }</span>
<span style="color:#2980b9"> # Matches /foo/ followed by a non-digit</span>
<span style="color:#2980b9"> if (std.fnmatch("/foo/[!0-9]", req.url)) { ... }</span>
File(system) functions
======================
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">STRING</span> <span style="color:#8e44ad">fileread</span>(<span style="color:#2980b9">PRIV_CALL</span>, <span style="color:#2980b9">STRING</span>)
Reads a file and returns a string with the content. The result is
cached indefinitely per filename.
This function should not be used for reading binary files.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> synthetic("Response was served by " + std.fileread("/etc/hostname"));</span>
Consider that the entire contents of the file appear in the string
that is returned, including newlines that may result in invalid
headers if <span style="color:#27ae60">`std.fileread()`_</span> is used to form a header. In that
case, you may need to modify the string, for example with
<span style="color:#2980b9">``regsub()``</span> (see <span style="font-weight:bold">:ref:</span><span style="color:#f67400">`vcl(7)`</span>)<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> set beresp.http.served-by = regsub(std.fileread("/etc/hostname"), "\R$", "");</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">BOOL</span> <span style="color:#8e44ad">file_exists</span>(<span style="color:#2980b9">STRING</span> path)
Returns <span style="color:#2980b9">``true``</span> if path or the file pointed to by path exists,
<span style="color:#2980b9">``false``</span> otherwise.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> if (std.file_exists("/etc/return_503")) {</span>
<span style="color:#2980b9"> return (synth(503, "Varnish is in maintenance"));</span>
<span style="color:#2980b9"> }</span>
Type Inspection functions
=========================
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">BOOL</span> <span style="color:#8e44ad">healthy</span>(<span style="color:#2980b9">BACKEND</span> be)
Returns <span style="color:#2980b9">``true``</span> if the backend <span style="font-style:italic">*be*</span> is healthy.
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">INT</span> <span style="color:#8e44ad">port</span>(<span style="color:#2980b9">IP</span> ip)
Returns the port number of the IP address <span style="font-style:italic">*ip*</span>. Always returns <span style="color:#2980b9">``0``</span>
for a <span style="color:#2980b9">``*.ip``</span> variable when the address is a Unix domain socket.
Type Conversion functions
=========================
These functions all have the same form<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> TYPE type([arguments], [fallback TYPE])</span>
Precisely one of the <span style="font-style:italic">*arguments*</span> must be provided (besides the
optional <span style="font-style:italic">*fallback*</span>), and it will be converted to <span style="font-style:italic">*TYPE*</span>.
If conversion fails, <span style="font-style:italic">*fallback*</span> will be returned and if no
fallback was specified, the VCL will be failed.
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">DURATION</span> <span style="color:#8e44ad">duration</span>([<span style="color:#2980b9">STRING</span> s], [<span style="color:#2980b9">DURATION</span> fallback],
[REAL real], [INT integer])
Returns a DURATION from a STRING, REAL or INT argument.
For a STRING <span style="font-style:italic">*s*</span> argument, <span style="font-style:italic">*s*</span> must be quantified by <span style="color:#2980b9">``ms``</span>
(milliseconds), <span style="color:#2980b9">``s``</span> (seconds), <span style="color:#2980b9">``m``</span> (minutes), <span style="color:#2980b9">``h``</span> (hours),``d``
(days), <span style="color:#2980b9">``w``</span> (weeks) or <span style="color:#2980b9">``y``</span> (years) units.
<span style="font-style:italic">*real*</span> and <span style="font-style:italic">*integer*</span> arguments are taken as seconds.
If the conversion of an <span style="font-style:italic">*s*</span> argument fails, <span style="font-style:italic">*fallback*</span> will be
returned if provided, or a VCL failure will be triggered.
Conversions from <span style="font-style:italic">*real*</span> and <span style="font-style:italic">*integer*</span> arguments never fail.
Only one of the <span style="font-style:italic">*s*</span>, <span style="font-style:italic">*real*</span> or <span style="font-style:italic">*integer*</span> arguments may be given or a VCL
failure will be triggered.
Examples<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> set beresp.ttl = std.duration("1w", 3600s);</span>
<span style="color:#2980b9"> set beresp.ttl = std.duration(real=1.5);</span>
<span style="color:#2980b9"> set beresp.ttl = std.duration(integer=10);</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">BYTES</span> <span style="color:#8e44ad">bytes</span>([<span style="color:#2980b9">STRING</span> s], [<span style="color:#2980b9">BYTES</span> fallback], [<span style="color:#2980b9">REAL</span> real], [<span style="color:#2980b9">INT</span> integer])
Returns BYTES from a STRING, REAL or INT argument.
A STRING <span style="font-style:italic">*s*</span> argument can be quantified with a multiplier (<span style="color:#2980b9">``k``</span>
(kilo), <span style="color:#2980b9">``m``</span> (mega), <span style="color:#2980b9">``g``</span> (giga), <span style="color:#2980b9">``t``</span> (tera) or <span style="color:#2980b9">``p``</span> (peta)).
<span style="font-style:italic">*real*</span> and <span style="font-style:italic">*integer*</span> arguments are taken as bytes.
If the conversion of an <span style="font-style:italic">*s*</span> argument fails, <span style="font-style:italic">*fallback*</span> will be
returned if provided, or a VCL failure will be triggered.
Other conversions may fail if the argument can not be represented,
because it is negative, too small or too large. Again, <span style="font-style:italic">*fallback*</span> will
be returned if provided, or a VCL failure will be triggered.
<span style="font-style:italic">*real*</span> arguments will be rounded down.
Only one of the <span style="font-style:italic">*s*</span>, <span style="font-style:italic">*real*</span> or <span style="font-style:italic">*integer*</span> arguments may be given or a VCL
failure will be triggered.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> std.cache_req_body(std.bytes(something.somewhere, 10K));</span>
<span style="color:#2980b9"> std.cache_req_body(std.bytes(integer=10*1024));</span>
<span style="color:#2980b9"> std.cache_req_body(std.bytes(real=10.0*1024));</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">INT</span> <span style="color:#8e44ad">integer</span>([<span style="color:#2980b9">STRING</span> s], [<span style="color:#2980b9">INT</span> fallback],
[BOOL bool], [BYTES bytes], [DURATION duration], [REAL real],
[TIME time])
Returns an INT from a STRING, BOOL or other quantity.
If the conversion of an <span style="font-style:italic">*s*</span> argument fails, <span style="font-style:italic">*fallback*</span> will be
returned if provided, or a VCL failure will be triggered.
A <span style="font-style:italic">*bool*</span> argument will be returned as 0 for <span style="color:#2980b9">``false``</span> and 1 for
<span style="color:#2980b9">``true``</span>. This conversion will never fail.
For a <span style="font-style:italic">*bytes*</span> argument, the number of bytes will be returned. This
conversion will never fail.
A <span style="font-style:italic">*duration*</span> argument will be rounded down to the number of seconds
and returned.
A <span style="font-style:italic">*real*</span> argument will be rounded down and returned.
For a <span style="font-style:italic">*time*</span> argument, the number of seconds since the UNIX epoch
(1970-01-01 00:00:00 UTC) will be returned.
<span style="font-style:italic">*duration*</span>, <span style="font-style:italic">*real*</span> and <span style="font-style:italic">*time*</span> conversions may fail if the argument can
not be represented because it is too small or too large. If so,
<span style="font-style:italic">*fallback*</span> will be returned if provided, or a VCL failure will be
triggered.
Only one of the <span style="font-style:italic">*s*</span>, <span style="font-style:italic">*bool*</span>, <span style="font-style:italic">*bytes*</span>, <span style="font-style:italic">*duration*</span>, <span style="font-style:italic">*real*</span> or <span style="font-style:italic">*time*</span>
arguments may be given or a VCL failure will be triggered.
Examples<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> if (std.integer(req.http.foo, 0) > 5) {</span>
<span style="color:#2980b9"> ...</span>
<span style="color:#2980b9"> }</span>
<span style="color:#2980b9"> set resp.http.answer = std.integer(real=126.42/3);</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">IP</span> <span style="color:#8e44ad">ip</span>(<span style="color:#2980b9">STRING</span> s, [<span style="color:#2980b9">IP</span> fallback], <span style="color:#2980b9">BOOL</span> resolve = 1, [<span style="color:#2980b9">STRING</span> p])
Converts the string <span style="font-style:italic">*s*</span> to the first IP number returned by the system
library function <span style="color:#f67400">`getaddrinfo(3)`</span>. If conversion fails, <span style="font-style:italic">*fallback*</span> will
be returned or VCL failure will happen.
The IP address includes a port number that can be found with <span style="color:#2980b9">``std.port()``</span>
that defaults to 80. The default port can be set to a different value with
the <span style="font-style:italic">*p*</span> argument. It will be overriden if <span style="font-style:italic">*s*</span> contains both an IP address
and a port number or service name.
When <span style="font-style:italic">*s*</span> contains both, the syntax is either <span style="color:#2980b9">``address:port``</span> or
<span style="color:#2980b9">``address port``</span>. If the address is a numerical IPv6 address it must be
enclosed between brackets, for example <span style="color:#2980b9">``[::1] 80``</span> or <span style="color:#2980b9">``[::1]:http``</span>.
The <span style="font-style:italic">*fallback*</span> may also contain both an address and a port, but its default
port is always 80.
If <span style="font-style:italic">*resolve*</span> is false, <span style="color:#f67400">`getaddrinfo(3)`</span> is called using <span style="color:#2980b9">``AI_NUMERICHOST``</span>
and <span style="color:#2980b9">``AI_NUMERICSERV``</span> to avoid network lookups depending on the system's
<span style="color:#f67400">`getaddrinfo(3)`</span> or nsswitch configuration. This makes "numerical" IP
strings and services cheaper to convert.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> if (std.ip(req.http.X-forwarded-for, "0.0.0.0") ~ my_acl) {</span>
<span style="color:#2980b9"> ...</span>
<span style="color:#2980b9"> }</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">REAL</span> <span style="color:#8e44ad">real</span>([<span style="color:#2980b9">STRING</span> s], [<span style="color:#2980b9">REAL</span> fallback], [<span style="color:#2980b9">INT</span> integer],
[BOOL bool], [BYTES bytes], [DURATION duration],
[TIME time])
Returns a REAL from a STRING, BOOL or other quantity.
If the conversion of an <span style="font-style:italic">*s*</span> argument fails, <span style="font-style:italic">*fallback*</span> will be
returned if provided, or a VCL failure will be triggered.
A <span style="font-style:italic">*bool*</span> argument will be returned as 0.0 for <span style="color:#2980b9">``false``</span> and 1.0 for
<span style="color:#2980b9">``true``</span>.
For a <span style="font-style:italic">*bytes*</span> argument, the number of bytes will be returned.
For a <span style="font-style:italic">*duration*</span> argument, the number of seconds will be returned.
An <span style="font-style:italic">*integer*</span> argument will be returned as a REAL.
For a <span style="font-style:italic">*time*</span> argument, the number of seconds since the UNIX epoch
(1970-01-01 00:00:00 UTC) will be returned.
None of these conversions other than <span style="font-style:italic">*s*</span> will fail.
Only one of the <span style="font-style:italic">*s*</span>, <span style="font-style:italic">*integer*</span>, <span style="font-style:italic">*bool*</span>, <span style="font-style:italic">*bytes*</span>, <span style="font-style:italic">*duration*</span> or <span style="font-style:italic">*time*</span>
arguments may be given or a VCL failure will be triggered.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> if (std.real(req.http.foo, 0.0) > 5.5) {</span>
<span style="color:#2980b9"> ...</span>
<span style="color:#2980b9"> }</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">TIME</span> <span style="color:#8e44ad">time</span>([<span style="color:#2980b9">STRING</span> s], [<span style="color:#2980b9">TIME</span> fallback], [<span style="color:#2980b9">REAL</span> real], [<span style="color:#2980b9">INT</span> integer])
Returns a TIME from a STRING, REAL or INT argument.
For a STRING <span style="font-style:italic">*s*</span> argument, the following formats are supported<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> "Sun, 06 Nov 1994 08:49:37 GMT"</span>
<span style="color:#2980b9"> "Sunday, 06-Nov-94 08:49:37 GMT"</span>
<span style="color:#2980b9"> "Sun Nov 6 08:49:37 1994"</span>
<span style="color:#2980b9"> "1994-11-06T08:49:37"</span>
<span style="color:#2980b9"> "784111777.00"</span>
<span style="color:#2980b9"> "784111777"</span>
<span style="font-style:italic">*real*</span> and <span style="font-style:italic">*integer*</span> arguments are taken as seconds since the epoch.
If the conversion of an <span style="font-style:italic">*s*</span> argument fails or a negative <span style="font-style:italic">*real*</span> or
<span style="font-style:italic">*integer*</span> argument is given, <span style="font-style:italic">*fallback*</span> will be returned if provided,
or a VCL failure will be triggered.
Examples<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> if (std.time(resp.http.last-modified, now) < now - 1w) {</span>
<span style="color:#2980b9"> ...</span>
<span style="color:#2980b9"> }</span>
<span style="color:#2980b9"> if (std.time(int=2147483647) < now - 1w) {</span>
<span style="color:#2980b9"> ...</span>
<span style="color:#2980b9"> }</span>
LOGGING functions
=================
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">VOID</span> <span style="color:#8e44ad">log</span>(STRANDS s)
Logs the string <span style="font-style:italic">*s*</span> to the shared memory log, using <span style="font-weight:bold">:ref:</span><span style="color:#f67400">`vsl(7)`</span> tag
<span style="color:#2980b9">``SLT_VCL_Log``</span>.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> std.log("Something fishy is going on with the vhost " + req.http.host);</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">VOID</span> <span style="color:#8e44ad">syslog</span>(<span style="color:#2980b9">INT</span> priority, STRANDS s)
Logs the string <span style="font-style:italic">*s*</span> to syslog tagged with <span style="font-style:italic">*priority*</span>. <span style="font-style:italic">*priority*</span> is
formed by ORing the facility and level values. See your system's
<span style="color:#2980b9">``syslog.h``</span> file for possible values.
Notice: Unlike VCL and other functions in the std vmod, this function
will not fail VCL processing for workspace overflows: For an out of
workspace condition, the <span style="color:#27ae60">`std.syslog()`_</span> function has no effect.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> std.syslog(9, "Something is wrong");</span>
This will send a message to syslog using <span style="color:#2980b9">``LOG_USER | LOG_ALERT``</span>.
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">VOID</span> <span style="color:#8e44ad">timestamp</span>(<span style="color:#2980b9">STRING</span> s)
Introduces a timestamp in the log with the current time, using the
string <span style="font-style:italic">*s*</span> as the label. This is useful to time the execution of lengthy
VCL subroutines, and makes the timestamps inserted automatically by
Varnish more accurate.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> std.timestamp("curl-request");</span>
CONTROL and INFORMATION functions
=================================
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">BOOL</span> <span style="color:#8e44ad">syntax</span>(<span style="color:#2980b9">REAL</span>)
Returns <span style="color:#2980b9">``true``</span> if VCL version is at least <span style="font-style:italic">*REAL*</span>.
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">STRING</span> <span style="color:#8e44ad">getenv</span>(<span style="color:#2980b9">STRING</span> name)
Return environment variable <span style="font-style:italic">*name*</span> or the empty string. See <span style="color:#f67400">`getenv(3)`</span>.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> set req.http.My-Env = std.getenv("MY_ENV");</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">BOOL</span> <span style="color:#8e44ad">cache_req_body</span>(<span style="color:#2980b9">BYTES</span> size)
Caches the request body if it is smaller than <span style="font-style:italic">*size*</span>. Returns
<span style="color:#2980b9">``true``</span> if the body was cached, <span style="color:#2980b9">``false``</span> otherwise.
Normally the request body can only be sent once. Caching it enables
retrying backend requests with a request body, as usually the case
with <span style="color:#2980b9">``POST``</span> and <span style="color:#2980b9">``PUT``</span>.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> if (std.cache_req_body(1KB)) {</span>
<span style="color:#2980b9"> ...</span>
<span style="color:#2980b9"> }</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">VOID</span> <span style="color:#8e44ad">late_100_continue</span>(<span style="color:#2980b9">BOOL</span> late)
Controls when varnish reacts to an <span style="color:#2980b9">``Expect: 100-continue``</span> client
request header.
Varnish always generates a <span style="color:#2980b9">``100 Continue``</span> response if requested by
the client trough the <span style="color:#2980b9">``Expect: 100-continue``</span> header when waiting for
request body data.
But, by default, the <span style="color:#2980b9">``100 Continue``</span> response is already generated
immediately after <span style="color:#2980b9">``vcl_recv``</span> returns to reduce latencies under the
assumption that the request body will be read eventually.
Calling <span style="color:#2980b9">``std.late_100_continue(true)``</span> in <span style="color:#2980b9">``vcl_recv``</span> will cause the
<span style="color:#2980b9">``100 Continue``</span> 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 <span style="color:#2980b9">``vcl_recv``</span> and after calling
<span style="color:#2980b9">``std.cache_req_body()``</span> or any other function consuming the request
body.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> vcl_recv {</span>
<span style="color:#2980b9"> std.late_100_continue(true);</span>
<span style="color:#2980b9"> if (req.method == "POST") {</span>
<span style="color:#2980b9"> std.late_100_continue(false);</span>
<span style="color:#2980b9"> return (pass);</span>
<span style="color:#2980b9"> }</span>
<span style="color:#2980b9"> ...</span>
<span style="color:#2980b9"> }</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">VOID</span> <span style="color:#8e44ad">set_ip_tos</span>(<span style="color:#2980b9">INT</span> tos)
Sets the IP type-of-service (TOS) field for the current session to
<span style="font-style:italic">*tos*</span>. Silently ignored if the listen address is a Unix domain socket.
Please note that the TOS field is not removed by the end of the
request so probably want to set it on every request should you utilize
it.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> if (req.url ~ "^/slow/") {</span>
<span style="color:#2980b9"> std.set_ip_tos(0);</span>
<span style="color:#2980b9"> }</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">VOID</span> <span style="color:#8e44ad">rollback</span>(<span style="color:#2980b9">HTTP</span> h)
Restores the <span style="font-style:italic">*h*</span> HTTP headers to their original state.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> std.rollback(bereq);</span>
DEPRECATED functions
====================
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">INT</span> <span style="color:#8e44ad">real2integer</span>(<span style="color:#2980b9">REAL</span> r, <span style="color:#2980b9">INT</span> fallback)
<span style="font-weight:bold">**DEPRECATED**</span>: This function will be removed in a future version of
varnish, use <span style="color:#27ae60">`std.integer()`_</span> with a <span style="font-style:italic">*real*</span> argument and the
<span style="color:#27ae60">`std.round()`_</span> function instead, for example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> std.integer(real=std.round(...), fallback=...)</span>
Rounds the real <span style="font-style:italic">*r*</span> to the nearest integer, but round halfway cases
away from zero (see <span style="color:#f67400">`round(3)`</span>). If conversion fails, <span style="font-style:italic">*fallback*</span> will
be returned.
Examples<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> set req.http.integer = std.real2integer(1140618699.00, 0);</span>
<span style="color:#2980b9"> set req.http.posone = real2integer( 0.5, 0); # = 1.0</span>
<span style="color:#2980b9"> set req.http.negone = real2integer(-0.5, 0); # = -1.0</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">TIME</span> <span style="color:#8e44ad">real2time</span>(<span style="color:#2980b9">REAL</span> r, <span style="color:#2980b9">TIME</span> fallback)
<span style="font-weight:bold">**DEPRECATED**</span>: This function will be removed in a future version of
varnish, use <span style="color:#27ae60">`std.time()`_</span> with a <span style="font-style:italic">*real*</span> argument and the
<span style="color:#27ae60">`std.round()`_</span> function instead, for example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> std.time(real=std.round(...), fallback=...)</span>
Rounds the real <span style="font-style:italic">*r*</span> to the nearest integer (see
<span style="color:#27ae60">`std.real2integer()`_</span>) and returns the corresponding time when
interpreted as a unix epoch. If conversion fails, <span style="font-style:italic">*fallback*</span> will be
returned.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> set req.http.time = std.real2time(1140618699.00, now);</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">INT</span> <span style="color:#8e44ad">time2integer</span>(<span style="color:#2980b9">TIME</span> t, <span style="color:#2980b9">INT</span> fallback)
<span style="font-weight:bold">**DEPRECATED**</span>: This function will be removed in a future version of
varnish, use <span style="color:#27ae60">`std.integer()`_</span> with a <span style="font-style:italic">*time*</span> argument instead, for
example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> std.integer(time=..., fallback=...)</span>
Converts the time <span style="font-style:italic">*t*</span> to a integer. If conversion fails,
<span style="font-style:italic">*fallback*</span> will be returned.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> set req.http.int = std.time2integer(now, 0);</span>
<span style="font-weight:bold">$Function</span> <span style="color:#2980b9">REAL</span> <span style="color:#8e44ad">time2real</span>(<span style="color:#2980b9">TIME</span> t, <span style="color:#2980b9">REAL</span> fallback)
<span style="font-weight:bold">**DEPRECATED**</span>: This function will be removed in a future version of
varnish, use <span style="color:#27ae60">`std.real()`_</span> with a <span style="font-style:italic">*time*</span> argument instead, for
example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> std.real(time=..., fallback=...)</span>
Converts the time <span style="font-style:italic">*t*</span> to a real. If conversion fails, <span style="font-style:italic">*fallback*</span> will
be returned.
Example<span style="color:#2980b9">::</span>
<span style="color:#2980b9"> set req.http.real = std.time2real(now, 1.0);</span>
SEE ALSO
========
* <span style="font-weight:bold">:ref:</span><span style="color:#f67400">`varnishd(1)`</span>
* <span style="font-weight:bold">:ref:</span><span style="color:#f67400">`vsl(7)`</span>
* <span style="color:#f67400">`fnmatch(3)`</span>
</pre></body></html>
|