README.rst 17.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
..
.. NB:  This file is machine generated, DO NOT EDIT!
..
.. Edit vmod.vcc and run make instead
..

.. role:: ref(emphasis)

===========
10
VMOD brotli
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
===========

--------------------------------------------------
Varnish Fetch Processor for brotli de-/compression
--------------------------------------------------

:Manual section: 3


SYNOPSIS
========

::

  import brotli;

Geoff Simmons's avatar
Geoff Simmons committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  # The built-in "unbr" filter decompresses brotli-encoded backend
  # responses with default parameters.
  sub vcl_backend_response {
  	if (beresp.http.Content-Encoding == "br") {
		set beresp.filters = "unbr";
	}
  }

  # The built-in "br" filter compresses backend responses with default
  # parameters.
  sub vcl_backend_response {
  	if (bereq.http.Accept-Encoding ~ "\bbr\b") {
		set beresp.filters = "br";
	}
  }

  # Create a brotli compression filter with custom parameters.
  new <obj> = brotli.encoder(STRING name, BYTES buffer, INT quality,
45
                             BOOL large_win, INT lgwin, ENUM mode)
Geoff Simmons's avatar
Geoff Simmons committed
46 47 48
  VOID <obj>.create_stats()

  # Create a brotli decompression filter with custom parameters.
49
  new <obj> = brotli.decoder(STRING name, BYTES buffer, BOOL large_win)
Geoff Simmons's avatar
Geoff Simmons committed
50
  VOID <obj>.create_stats()
51 52 53 54

  # VFP version
  STRING brotli.version()

Geoff Simmons's avatar
Geoff Simmons committed
55 56 57 58 59 60
  # encoder library version
  STRING brotli.encoder_version()

  # decoder library version
  STRING brotli.decoder_version()

61 62 63
DESCRIPTION
===========

Geoff Simmons's avatar
Geoff Simmons committed
64 65 66
VFP brotli is a Varnish Fetch Processor to support the brotli
compression algorithm for responses fetched from backends. The VFP
integrates Varnish with the `Google brotli library`_.
67 68 69 70 71 72 73

The Brotli Compressed Data Format is specified in `RFC 7932`_. Details
of the compression algorithm are beyond the scope of this manual; see
the RFC and library documentation for more information.

A VFP is technically similar to a Varnish Module (VMOD). In
particular, it must be installed against Varnish in the same way a
Geoff Simmons's avatar
Geoff Simmons committed
74 75 76 77 78
VMOD is installed; and as with a VMOD, a VCL source must instruct
Varnish to load the VFP using the ``import`` statement. But unlike a
VMOD, a VFP's primary purpose is not to extend the VCL language;
instead, a VFP creates filters for backend fetches that can be named
as strings in the space-separated list assigned to the VCL variable
79 80 81
``beresp.filters`` (see ``vcl(7)``).

VFP brotli always creates two filters named ``"br"`` and ``"unbr"``,
Geoff Simmons's avatar
Geoff Simmons committed
82
for compression and decompression respectively, with default
83
parameters for the compression algorithm::
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
  import brotli;
  
  # Use the built-in "br" filter for brotli compression with default
  # parameters, if the Accept-Encoding request header indicates that
  # brotli compression is accepted.
  sub vcl_backend_response {
  	if (bereq.http.Accept-Encoding ~ "\bbr\b") {
		set beresp.filters = "br";
	}
  }
  
  # Use the built-in "unbr" filter for brotli decompression with
  # default parameters, if the Content-Encoding response header
  # indicates that the response is compressed using brotli.
  sub vcl_backend_response {
  	if (beresp.http.Content-Encoding == "br") {
		set beresp.filters = "unbr";
	}
  }

Note that the content encoding type ``br`` has been standardized to
indicate brotli compression, for use in headers such as
``Content-Encoding`` and ``Accept-Encoding``.

Note also that ``beresp.filters`` may only be written or read in the
VCL subroutine ``vcl_backend_response``.

When a brotli filter appears in ``beresp.filters``, it is applied to
the incoming backend response body. The resulting response body is
passed to the client, and if the response is cached, then the body is
stored in the cache as it results from the filtering.

Geoff Simmons's avatar
Geoff Simmons committed
117
To use non-default settings for the compression algorithm, create an
118 119
``encoder`` or ``decoder`` object as specified below. The string
passed in the ``name`` parameter of the object constructor can be used
Geoff Simmons's avatar
Geoff Simmons committed
120
in a ``beresp.filters`` list, as with ``"br"`` and ``"unbr"``::
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

  import brotli;
  
  sub vcl_init {
	# Create a compression filter with a reduced quality level,
	# for faster processing, but larger compressed responses
	# (the default quality level is 11).
	new brQ10 = brotli.encoder("brQ10", quality=10);

	# Create a decompression filter that uses a 1MiB buffer
	# (the default buffer size is 32KiB).
	new unbr1M = brotli.decoder("unbr1M", buffer=1m);
  }

  sub vcl_backend_response {
  	# Use the custom compression filter defined above.
  	if (bereq.http.Accept-Encoding ~ "\bbr\b") {
		set beresp.filters = "brQ10";
	}

  	# Use the custom decompression filter.
	if (beresp.http.Content-Encoding == "br") {
		set beresp.filters = "unbr1M";
	}
  }

Parameter settings for the compression algorithm represent various
ways to affect the trade-off between speed of processing and the rate
of compression (how much compression reduces the size of an object).

The VFP always creates counter statistics, observable with a tool like
``varnishstat(1)``, for the standard ``"br"`` and ``"unbr"``
filters. You may also optionally create statistics for custom filters
created with the ``encoder`` and ``decoder`` constructors. See
`STATISTICS`_ below for details.

Compression and HTTP
--------------------

The brotli VFP interacts with the HTTP protocol (headers and response
codes) in much the same way that Varnish does with its built-in gzip
support:

* Compression filters (the built-in ``"br"`` filter, or a custom
  filter created from the ``encoder`` constructor) are not executed
  for a backend response that already has a ``Content-Encoding``
  header.

* Decompression filters (built-in ``"unbr"`` or a custom ``decoder``
  filter) are not executed unless the backend response has the
  ``Content-Encoding`` header set to ``br``.

* When a compression filter is applied, the VFP sets the
  ``Content-Encoding`` response header to ``br``; this header value
  may appear in a client response, and it may be cached with the
  response. The value ``Accept-Encoding`` is also added to the
  ``Vary`` response header.

* When a decompression filter is applied, the ``Content-Encoding``
  response header is removed.

* Any ``Content-Length`` response header fetched from the backend is
Geoff Simmons's avatar
Geoff Simmons committed
183
  removed -- Varnish may add a new ``Content-Length`` header in a
184 185 186 187
  client response, set to the changed size of the response body. When
  streaming is enabled, Varnish sends the client response for a fetch
  with chunked encoding (and hence with no ``Content-Length`` header).

Geoff Simmons's avatar
Geoff Simmons committed
188
* If the backend response has an ``ETag`` header, then the ``ETag``
189 190 191 192 193 194 195 196 197 198 199
  value is "weakened" (prefixed with ``W/``), for weak validation
  according to `RFC 7232`_ section 2.1. This is because the response
  after de-/compression is not byte-identical with the fetched
  response.

* The filters ignore partial responses to range requests (status 206
  for "Partial Content").

Note that (unlike gzip) brotli compression does not work together with
Edge Side Includes (ESI). See `LIMITATIONS`_ below for a discussion of
this issue, and of possible solutions with VCL.
200

Geoff Simmons's avatar
Geoff Simmons committed
201 202
.. _vmod_brotli.encoder:

Geoff Simmons's avatar
Geoff Simmons committed
203 204
new xencoder = brotli.encoder(STRING name, BYTES buffer, INT quality, BOOL large_win, INT lgwin, ENUM mode)
-----------------------------------------------------------------------------------------------------------
Geoff Simmons's avatar
Geoff Simmons committed
205 206 207

::

208 209
   new xencoder = brotli.encoder(
      STRING name,
Geoff Simmons's avatar
Geoff Simmons committed
210
      BYTES buffer=32768,
211 212
      INT quality=11,
      BOOL large_win=0,
213 214
      INT lgwin=22,
      ENUM {GENERIC, TEXT, FONT} mode=GENERIC
215
   )
Geoff Simmons's avatar
Geoff Simmons committed
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
Create a compression filter named ``name`` with custom parameters for
the brotli algorithm. The string given in ``name`` may then be used in
``beresp.filters``.

The default values for the parameters correspond to the defaults used
in the standard compression filter (so if you create a filter with all
defaults, it is functionally identical to the ``"br"`` filter).

The ``name`` MAY NOT be ``"br"`` or ``"unbr"``, or the same as any of
the standard filters built into Varnish: ``"esi"``, ``"esi_gzip"``,
``"gzip"``, ``"gunzip"`` or ``"testgunzip"``. The results are
undefined (and almost certainly not what you want) if you use a name
that is also used by another third-party VFP.

The parameters are:

* ``buffer`` (default 32KiB): Like the ``varnishd`` parameter
  ``gzip_buffer``, this is the size of the temporary internal buffer
  used for compression. As with ``gzip_buffer``, a buffer size that is
  too small results in more overhead, and if it is too large, then it
  probably wastes memory.

* ``quality`` (default 11): sets a compression level, where 0
  represents the fastest execution, and 11 represents the highest
  compression rate (smallest compressed result). ``quality`` MUST be
Geoff Simmons's avatar
Geoff Simmons committed
242
  in the range 0 to 11, inclusive.
243 244 245 246 247 248 249

* ``large_win`` (default ``false``): if ``true``, use Large Window
  Brotli, which may yield better results for response bodies larger
  than 16 MiB.  The Large Window algorithm differs from the standard
  algorithm; under certain circumstances, it may be necessary to use
  the Large Window setting for both compression and decompression.

Geoff Simmons's avatar
Geoff Simmons committed
250 251 252 253
* ``lgwin`` (default 22): set the size of the LZ77 sliding window used
  by the brotli algorithm.  The window size is (2 ^ ``lgwin`` - 16),
  and larger window sizes may result in better compression rates.
  ``lgwin`` MUST be in the range 10 to 24, inclusive.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

* ``mode`` (default ``GENERIC``): provide a hint about the expected
  contents of the response body; the compression may benefit from
  optimizations based on assumptions about the content. Possible
  values are:

  * ``GENERIC``: no assumptions are made about the content

  * ``TEXT``: for UTF-8 formatted text

  * ``FONT``: for the WOFF 2.0 font format (the original application
    for brotli)

For more details about the parameters and their effects, see `RFC
7932`_ and the library documentation.

Example::

  import brotli;
  
  sub vcl_init {
	# Create a compression filter with a 1MiB buffer, optimized
	# for WOFF.
Geoff Simmons's avatar
Geoff Simmons committed
277
	new br_woff = brotli.encoder("br_woff", buffer=1m, mode=FONT);
278 279 280 281 282 283 284 285
  }

  sub vcl_backend_response {
  	# Use the custom filter for the WOFF media type.
  	if (beresp.http.Content-Type ~ "\bfont/woff2?\b") {
		set beresp.filters = "br_woff";
	}
  }
Geoff Simmons's avatar
Geoff Simmons committed
286

287 288 289 290 291
.. _vmod_brotli.encoder.create_stats:

VOID xencoder.create_stats()
----------------------------

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
Create statistics, observable with a tool like ``varnishstat(1)``, for
the custom compression filter. These are the same as the counters
created for the standard ``"br"`` filter. See `STATISTICS`_ below for
details.

The ``.create_stats()`` method MAY NOT be called in any VCL subroutine
besides ``vcl_init``. If it is called in another subroutine, then VCL
failure is invoked -- ordinarily, the request fails with a "503 VCL
failed" response.

Example::

  import brotli;
  
  sub vcl_init {
	# Create a custom compression filter with stats.
	new mybr = brotli.encoder("mybr", buffer=64k);
	mybr.create_stats();
  }
311

312 313
.. _vmod_brotli.decoder:

314 315
new xdecoder = brotli.decoder(STRING name, BYTES buffer, BOOL large_win)
------------------------------------------------------------------------
316 317 318

::

319 320 321 322 323
   new xdecoder = brotli.decoder(
      STRING name,
      BYTES buffer=32768,
      BOOL large_win=0
   )
324

325 326 327 328 329 330 331
Create a decompression filter named ``name`` with custom parameters,
suitable for use in ``beresp.filters``.

As with the ``encoder`` object, the default values for the parameters
correspond to the defaults used in the standard ``"unbr"``
decompression filter.

Geoff Simmons's avatar
Geoff Simmons committed
332
Also as with ``encoder``, the ``name`` MAY NOT be ``"br"`` or
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
``"unbr"``, or the same as any of Varnish's standard filters. It
SHOULD NOT be the same as a name used by another third-party VFP.

The parameters are:

* ``buffer`` (default 32KiB): like the ``buffer`` parameter for the
  ``encoder`` object, this is the size of the temporary internal
  buffer used for decompression.

* ``large_win`` (default ``false``): if ``true``, use Large Window
  Brotli. As noted above, it may be necessary to use the Large Window
  setting for both compression and decompression.

Example::

  import brotli;
  
  sub vcl_init {
	# Create a decompression filter with a small buffer size.
	new unbr_small = brotli.decoder("unbr_small", buffer=1k);
  }

  sub vcl_backend_response {
  	# Use the custom filter when the URL begins with "/tiny"
  	if (bereq.url ~ "/tiny" && beresp.http.Content-Encoding ~ "br") {
		set beresp.filters = "unbr_tiny";
	}
  }
361

362 363 364 365 366
.. _vmod_brotli.decoder.create_stats:

VOID xdecoder.create_stats()
----------------------------

367 368 369 370 371 372
Create statistics for the custom decompression filter, like the
``.create_stats()`` method for ``encoder``. See `STATISTICS`_ below
for details.

As with ``encoder.create_stats()``, this method MAY NOT be called in
any VCL subroutine besides ``vcl_init``, otherwise VCL failure is invoked.
373

374 375 376 377 378
.. _vmod_brotli.encoder_version:

STRING encoder_version()
------------------------

379
Return the version string for the brotli encoder library.
380 381 382 383 384

Example::

  std.log("Using brotli encoder version: " + brotli.encoder_version());

385 386 387 388
.. _vmod_brotli.decoder_version:

STRING decoder_version()
------------------------
389

390
Return the version string for the brotli decoder library.
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

Example::

  std.log("Using brotli decoder version: " + brotli.decoder_version());

.. _vmod_brotli.version:

STRING version()
----------------

Return the version string for this VFP.

Example::

  std.log("Using VFP brotli version: " + brotli.version());

STATISTICS
==========

Geoff Simmons's avatar
Geoff Simmons committed
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
The VFP creates counters in the ``BROTLI.*`` namespace, which can be
read and monitored with a tool like ``varnishstat(1)``, or any other
client of the Varnish statistics API.

Counters for the built-in filters ``"br"`` and ``"unbr"`` are always
created, with names of the form ``BROTLI.br.*`` and
``BROTLI.unbr.*``. If you use the ``encoder`` and/or ``decoder``
objects to create filters with custom parameters, you may optionally
use the ``.create_stats()`` method for either object type to create
counters for the custom filters. If so, then these counters have names
of the form::

  BROTLI.<vcl_name>.<filter_name>.*

... where ``<vcl_name>`` is the name of the VCL configuration, and
``<filter_name>`` is the name of the filter as used in
``beresp.filters``.

The stats are:

* ``ops``: total number of de-/compression operations. Always incremented
  even when the filter does not make any changes, for example when a
  compression filter finds that a backend response already has a
  ``Content-Encoding`` header.

* ``in``: total number of input bytes processed by the filter

* ``out``: total number of ouput bytes produced by the filter

All three stats are counters, meaning that they are always monotonic
increasing, except when the Varnish worker process restarts and they
are reset to 0.

So these stats are always created when the VFP is imported:

* ``BROTLI.br.ops``
* ``BROTLI.br.in``
* ``BROTLI.br.out``
* ``BROTLI.unbr.ops``
* ``BROTLI.unbr.in``
* ``BROTLI.unbr.out``

And stats for custom filters are also created if you so choose.

The work of the VFP in individual request/response transactions can
also be monitored in the Varnish log, recorded with the ``VfpAcct``
tag (see ``vsl(7)``). By default, ``VfpAcct`` is not written to the
log; to see it, set the ``varnishd`` parameter ``vsl_mask`` to
``+VfpAcct``.
459 460 461 462

ERRORS
======

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
If errors are encountered during compression or decompression, they
are reported in the Varnish log with the tag ``FetchError`` (see
``vsl(7)``). The ``FetchError`` message for decompression errors
includes the error message from the brotli decoder library.

If the decoder reaches end-of-stream indicators, signaling a completed
decompression, but there are still additional bytes in the response
body, then the filter fails with the ``FetchError`` message "Junk
after brotli data".

The backend fetch fails when any VFP filter fails, which can lead to a
"503 Backend fetch failed" response from Varnish. When streaming is
enabled (i.e when the VCL variable ``beresp.do_stream`` variable is
``true``, which is the default), the error might not be detected until
after the response code and header have already been sent to the
client; then it is too late to change the response status. In such
cases, the client response body may be empty, and the network
connection to the client is closed.
481 482 483 484

REQUIREMENTS
============

485 486
The VFP currently requires the Varnish master branch since commit
``54af42d``.
487

488 489
The VFP also requires the `Google brotli library`_, see
`INSTALL.rst <INSTALL.rst>`_ in the source directory for details.
490 491 492 493 494

The VFP has been tested with version 1.0.5 of both the encoder and
decoder libraries.

If you are building the VFP from source, you will need development
495
versions of the brotli libraries. See `INSTALL.rst <INSTALL.rst>`_.
496 497 498 499 500 501 502 503 504

INSTALLATION
============

See `INSTALL.rst <INSTALL.rst>`_ in the source repository.

LIMITATIONS
===========

505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
Memory and CPU usage for compression and decompression is largely
driven by the choice of parameters for the brotli algorithm. If you
need to limit resource consumption, consider using custom filters, and
adjusting the parameters as needed.

Memory used by the VFP, such as the temporary internal buffer and
other internal structures, is allocated from the heap, and hence is
limited by available virtual memory.

As noted above, brotli compression does not work together with Edge
Side Includes, or ESI includes, as gzip support does for standard
Varnish. If you are using ESI, then brotli-compressed backend
responses will have to be decompressed on fetch. Without any special
measures taken in VCL, client responses with ESI-included content will
have to be delivered uncompressed.
520 521 522 523 524 525 526

SEE ALSO
========

* varnishd(1)
* vcl(7)
* varnishstat(1)
Geoff Simmons's avatar
Geoff Simmons committed
527
* vsl(7)
528
* VMOD source repository: https://code.uplex.de/uplex-varnish/libvfp-brotli
529 530 531 532
* brotli library repository: https://github.com/google/brotli
* RFC 7932 (brotli specification): https://tools.ietf.org/html/rfc7932
* RFC 7232 (HTTP conditional requests, with the specification for weak
  validation): https://tools.ietf.org/html/rfc7232
533 534 535 536 537 538 539 540 541 542 543 544 545

COPYRIGHT
=========

::

  Copyright (c) 2019 UPLEX Nils Goroll Systemoptimierung
  All rights reserved
 
  Author: Geoffrey Simmons <geoffrey.simmons@uplex.de>
 
  See LICENSE
 
546 547 548 549 550 551

.. _Google brotli library: https://github.com/google/brotli

.. _RFC 7932: https://tools.ietf.org/html/rfc7932

.. _RFC 7232: https://tools.ietf.org/html/rfc7232