vmod-accept
sanitizes
content negotiations headers,
most notably the Accept-Language
header. As backends can deliver different
objects for the same URL based on this headers, it can be needed to normalize
them to avoid object duplication.
The vmod follows RFC 7231 and evaluates a list of possible candidates and
returns a match to the user’s header, if any, according to the quality
(the q
parameter) specified by the client for each choice.
Note: Varnish already handles the Accept-Encoding
header, so you should NOT
do it in VCL.
import accept;
sub vcl_init {
# our server can only return html, json, and its default, plain text
new format = accept.rule("text/plain");
format.add("text/html");
format.add("application/json");
# also, the content is available in english (the default)
# as well as french
new lang = accept.rule("en");
lang.add("fr");
}
sub vcl_recv {
# only leave one choice for each header
set req.http.accept = format.filter(req.http.accept);
set req.http.accept-language = lang.filter(req.http.accept-language);
}
rule(STRING string) -> RULE
Create a new rule object, using string
as fallback in case no match is
found.
.add(STRING string)
This is a method of the rule object, and adds string
to the list of
acceptable candidates.
.remove(STRING string)
This method removes string
from the list of candidates.
.filter(STRING string) -> STRING
Process string
and compares it with the list of candidates. The one with the
highest quality is returned, or, failing that, the fallback string specified at
the rule object creation is used.