Contents Menu Expand Light mode Dark mode Auto light/dark, in light mode Auto light/dark, in dark mode Skip to content
Vinyl Cache version 9.0.0 documentation
Logo
Vinyl Cache version 9.0.0 documentation
  • Vinyl Cache Installation
    • Prerequisites
    • Installing Vinyl Cache
      • Installing on Debian/Ubuntu
      • Installing on FreeBSD
      • Installing on OpenBSD
      • Installing on RedHat or CentOS
    • Compiling Vinyl Cache from source
      • Compiling Vinyl Cache from source
    • Other pre-built Vinyl Cache packages
    • Getting help
    • Reporting bugs
    • Platform specific notes
  • The Vinyl Cache Tutorial
    • Vinyl Cache: The beef in the sandwich
    • How Vinyl Cache works
    • Caching with Vinyl Cache
    • Content Composition with Vinyl Cache
    • Content Policy with Vinyl Cache
    • Vinyl Cache is general purpose
    • Starting Vinyl Cache
    • Put Vinyl Cache on port 80
    • Restarting vinyld again
    • Backend servers
    • Peculiarities
    • Now what?
  • The Vinyl Cache Users Guide
    • The Big Vinyl Cache Picture
    • Starting and running Vinyl Cache
      • Security first
      • Required command line arguments
      • CLI - bossing Vinyl Cache around
      • Storage backends
      • Transient Storage
      • Parameters
      • Sizing your cache
    • VCL - Vinyl Configuration Language
      • VCL Syntax
      • Built-in VCL
      • Request and response VCL objects
      • Backend servers
      • The “none” backend
      • Multiple backends
      • Backends and virtual hosts in Vinyl Cache
      • Connecting Through a Proxy
      • Directors
      • Health checks
      • Layering
      • Director Resolution
      • Connection Pooling
      • Hashing
      • Grace mode and keep
      • Separate VCL files
      • Using inline C to extend Vinyl Cache
      • VCL Examples
        • Manipulating request headers in VCL
        • Altering the backend response
        • ACLs
        • Adding WebSockets support
      • Device detection
    • Reporting and statistics
      • Logging in Vinyl Cache
      • Statistics
    • Vinyl Cache and Website Performance
      • Achieving a high hitrate
      • The role of HTTP Headers
      • HTTP Vary
      • Cache misses
      • Uncacheable content
      • Purging and banning
      • Compression
    • Content composition with Edge Side Includes
    • Troubleshooting Vinyl Cache
  • The Vinyl Cache Reference Manual
    • VCL - The Vinyl Configuration Language
    • VCL Variables
    • VCL Steps
    • VCL backend configuration
    • VCL backend health probe
    • Vinyl Cache Processing States
    • VMOD blob - Utilities for the VCL blob type, encoding and decoding
    • VMOD cookie - Vinyl Cache Cookie Module
    • VMOD directors - Vinyl Cache Directors Module
    • VMOD h2 - Module to control the built-in HTTP2 transport
    • VMOD math - VMOD wrapping math.h
    • VMOD proxy - Vinyl Cache Module to extract TLV attributes from PROXYv2
    • VMOD purge - Vinyl Cache Purge Module
    • VMOD std - Vinyl Cache Standard Module
    • VMOD unix - Utilities for Unix domain sockets
    • VinylAdm - Control program for Vinyl Cache
    • CLI - The commands Vinyl Cache understands
    • VSL - The log records Vinyl Cache generates
    • VSLQ - Filter/Query expressions for VSL
    • VinylLog - Logging raw VSL
    • VinylNCSA - Logging in NCSA format
    • VinylHist - Realtime response histogram display
    • VinylTop - Realtime activity display
    • VSC - The statistics Vinyl Cache collects
    • VinylStat - Watching and logging statistics
    • VinylD - The program which does the actual work
    • VTC - Language for writing test cases
    • VinylTest - execute test cases
    • VMOD vtc - Utility module for vinyltest
    • Shell tricks
    • VMODS - Extensions to VCL
    • VEXT - Vinyl Cache Extensions
    • VSM - Shared memory use
    • VDIR - Backends & Directors
    • VCLI - CLI protocol API
    • VTLA - Vinyl Three Letter Acronyms
  • VCL Design Patterns
    • Using extra digits in resp.status
    • Ignoring the Vary header for bots
  • What’s new / Upgrading
    • Changes in Vinyl Cache 9.0
    • Upgrading to Vinyl Cache 9.0
    • Changes in Varnish-Cache 8.0
    • Upgrading to Varnish-Cache 8.0
    • Changes in Varnish-Cache 7.7
    • Upgrading to Varnish-Cache 7.7
    • Changes in Varnish 6.0
    • Upgrading to Varnish 6.0
  • The Vinyl cache Developers Guide
    • Vinyl Cache organization and day-to-day operation
    • Bundling VMODs with the Vinyl Cache distribution
    • How our website works
    • How to contribute content to vinyl-cache.org
    • Who is … ?
  • Poul-Hennings random outbursts
    • On the deck-chairs of HTTP/2
    • How Varnish met CHERI 7/N
    • How Varnish met CHERI 6/N
    • How Varnish met CHERI 5/N
    • How Varnish met CHERI 4/N
    • How Varnish met CHERI 3/N
    • How Varnish met CHERI 2/N
    • How Varnish met CHERI 1/N
    • Getting into the routine
    • The 503’s heard around the world
    • Legacy-.*
    • IP Addresses - A long expected problem
    • Varnish Developer Day 2019Q3
    • QUIC visions of OSI
    • Here we are again - VSV00003 in perspective
    • A patently good idea
    • Do you feel lucky ?
    • API spaces
    • Yah! A security issue - finally!
    • Something (funny) happened on the way to 5.1.0^H1^H2
    • Trial&Error - Prototyping - Continuous Integration
    • Far, far away
    • Going fast slowly
    • The first design of Varnish
    • Varnish - 10 going on 50
    • Brinch-Hansens Arrows
    • SSL revisited
    • A persistent message
    • Raking in the dough on Free and Open Source Software
    • Wanton destruction for the greater good
    • What SPDY did to my summer vacation
    • Why HTTP/2.0 does not seem interesting
    • Varnish Does Not Hash
    • The Tools We Work With
    • Thoughts on the eve of Varnish 3.0
    • Why no SSL ?
    • How GZIP, and GZIP+ESI works in Vinyl Cache
    • VCL Expressions
    • IPv6 Suckage
    • What do you mean by ‘backend’ ?
    • Picking platforms
    • Security barriers in Vinyl Cache
    • What were they thinking ?
    • Did you call them autocrap tools ?
    • Why Sphinx and reStructuredText ?
    • Notes from the Architect
  • Vinyl Cache Glossary
Back to top

VMOD cookie - Vinyl Cache Cookie Module¶

SYNOPSIS¶

import cookie [as name] [from "path"]

VOID clean()

VOID delete(STRING cookiename)

VOID filter(STRING filterstring)

VOID filter_re(REGEX expression)

VOID keep(STRING filterstring)

VOID keep_re(REGEX expression)

STRING format_date(TIME now, DURATION timedelta)

STRING get(STRING cookiename)

STRING get_re(REGEX expression)

STRING get_string()

BOOL isset(STRING cookiename)

VOID parse(STRING cookieheader)

VOID set(STRING cookiename, STRING value)

DESCRIPTION¶

Handle HTTP cookies more easily in Vinyl Cache VCL.

Parses a cookie header into an internal data store, where per-cookie get/set/delete functions are available. A keep() function removes all but a set comma-separated list of cookies. A filter() function removes a comma- separated list of cookies.

Regular expressions can be used for either selecting cookies, deleting matching cookies and deleting non-matching cookie names.

A convenience function for formatting the Set-Cookie Expires date field is also included.

The state loaded with cookie.parse() has a lifetime of the current request or backend request context. To pass variables to the backend request, store the contents as fake bereq headers.

Filtering example:

import cookie;

sub vcl_recv {
    if (req.http.cookie) {
        cookie.parse(req.http.cookie);
        # Either delete the ones you want to get rid of:
        cookie.delete("cookie2");
        # or delete all but a few:
        cookie.keep("SESSIONID,PHPSESSID");

        # Store it back into req so it will be passed to the backend.
        set req.http.cookie = cookie.get_string();

        # If empty, unset so the builtin VCL can consider it for caching.
        if (req.http.cookie == "") {
            unset req.http.cookie;
        }
    }
}

VOID clean()¶

Clean up previously parsed cookies. It is not necessary to run clean() in normal operations.

Example:

sub vcl_recv {
    cookie.clean();
}

VOID delete(STRING cookiename)¶

Delete cookiename from internal vmod storage if it exists.

Example:

sub vcl_recv {
    cookie.parse("cookie1=value1; cookie2=value2");
    cookie.delete("cookie2");
    # get_string() will now yield "cookie1=value1"
}

VOID filter(STRING filterstring)¶

Delete all cookies from internal vmod storage that are in the comma-separated argument cookienames.

Example:

sub vcl_recv {
    cookie.parse("cookie1=value1; cookie2=value2; cookie3=value3");
    cookie.filter("cookie1,cookie2");
    # get_string() will now yield "cookie3=value3"
}

VOID filter_re(REGEX expression)¶

Delete all cookies from internal vmod storage that matches the regular expression expression.

Example:

sub vcl_recv {
    cookie.parse("cookie1=value1; cookie2=value2; cookie3=value3");
    cookie.filter_re("^cookie[12]$");
    # get_string() will now yield "cookie3=value3"
}

VOID keep(STRING filterstring)¶

Delete all cookies from internal vmod storage that is not in the comma-separated argument cookienames.

Example:

sub vcl_recv {
    cookie.parse("cookie1=value1; cookie2=value2; cookie3=value3");
    cookie.keep("cookie1,cookie2");
    # get_string() will now yield "cookie1=value1; cookie2=value2"
}

VOID keep_re(REGEX expression)¶

Delete all cookies from internal vmod storage that does not match expression expression.

Example:

sub vcl_recv {
    cookie.parse("cookie1=value1; cookie2=value2; cookie3=value3");
    cookie.keep_re("^cookie[12]$");
    # get_string() will now yield "cookie1=value1; cookie2=value2"
}

STRING format_date(TIME now, DURATION timedelta)¶

Get a RFC1123 formatted date string suitable for inclusion in a Set-Cookie response header.

Care should be taken if the response has multiple Set-Cookie headers. In that case the header vmod should be used.

Example:

sub vcl_deliver {
    # Set a userid cookie on the client that lives for 5 minutes.
    set resp.http.Set-Cookie = "userid=" + req.http.userid +
        "; Expires=" + cookie.format_date(now, 5m) + "; httpOnly";
}

STRING get(STRING cookiename)¶

Get the value of cookiename, as stored in internal vmod storage.

Example:

import std;
sub vcl_recv {
    cookie.parse("cookie1=value1; cookie2=value2");
    std.log("cookie1 value is: " + cookie.get("cookie1"));
}

If cookiename does not exist, the NULL string is returned. Note that a NULL string is converted to an empty string when assigned to a header. This means that the following is correct:

if (req.http.Cookie) {
        cookie.parse(req.http.Cookie);
        set req.http.X-tmp = cookie.get("a_cookie");
} else {
        set req.http.X-tmp = "";
}
if (req.http.X-tmp != "") {
        // do something with the X-tmp header...
} else {
        // fallback case
}

However, using cookie.isset() is often a better way to check if a particular cookie is present, like this:

unset req.http.X-tmp; # unnecessary if no fallback is needed
if (req.http.Cookie) {
        cookie.parse(req.http.Cookie);
        if (cookie.isset("a_cookie")) {
                set req.http.X-tmp = cookie.get("a_cookie");
                # do something with the X-tmp header...
        }
}
# if necessary, do something when a_cookie is not there
if (!req.http.X-tmp) {
        # fallback case
}

STRING get_re(REGEX expression)¶

Get the value of the first cookie in internal vmod storage that matches the regular expression expression. If nothing matches, the NULL string is returned.

Example:

import std;
sub vcl_recv {
    cookie.parse("cookie1=value1; cookie2=value2");
    std.log("cookie1 value is: " + cookie.get_re("^cookie1$"));
}

STRING get_string()¶

Get a Cookie string value with all cookies in internal vmod storage. Does not modify internal storage.

Example:

sub vcl_recv {
    cookie.parse(req.http.cookie);
    cookie.keep("SESSIONID,PHPSESSID");
    set req.http.cookie = cookie.get_string();
}

BOOL isset(STRING cookiename)¶

Check if cookiename is set in the internal vmod storage.

Example:

import std;
sub vcl_recv {
    cookie.parse("cookie1=value1; cookie2=value2");
    if (cookie.isset("cookie2")) {
        std.log("cookie2 is set.");
    }
}

VOID parse(STRING cookieheader)¶

Parse the cookie string in cookieheader. If state already exists, clean() will be run first.

Example:

sub vcl_recv {
    cookie.parse(req.http.Cookie);
}

VOID set(STRING cookiename, STRING value)¶

Set the internal vmod storage for cookiename to value.

Example:

sub vcl_recv {
    cookie.set("cookie1", "value1");
    std.log("cookie1 value is: " + cookie.get("cookie1"));
}

COPYRIGHT¶

This document is licensed under the same conditions as Vinyl Cache itself.
See LICENSE for details.

SPDX-License-Identifier: BSD-2-Clause
Next
VMOD directors - Vinyl Cache Directors Module
Previous
VMOD blob - Utilities for the VCL blob type, encoding and decoding
Copyright © 2016-2026, The Vinyl Cache Project and contributors. Vinyl Cache Logo & Mascot: CC-BY 4.0 Rhubarbe.design
Made with Sphinx and @pradyunsg's Furo