Isolating header values without regular expressions
- English
- 日本語
Fastly supports the ability to extract header subfield values without regular expressions in a human-readable way. Headers subfields are headers with a body syntax style similar to value1=123value123; testValue=asdf_true; staff_user=true;
or max-age=0, surrogate-control=3600
These headers include Cookie, Set-Cookie, Cache-Control, or a custom header. Fastly allows you to isolate these key values with the following syntax:
req.http.Header-Name:key-name
For example, if a Cookie
request from a client was value1=123value123; testValue=asdf_true; staff_user=true;
, you could isolate the staff_user
value using this logic:
set req.http.Staff-User = req.http.Cookie:staff_user;
The same can be accomplished by using the subfield function:
set req.http.Staff-User = subfield(req.http.Cookie, "staff_user", ";");
You can add this logic using VCL Snippets or using a custom header.
:
accessor cannot be used for Set-Cookie
headers.Using VCL Snippets
To execute this logic based on the value of staff_user
within req.http.Cookie
using a VCL Snippet, you would:
Log in to the Fastly control panel.
From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain.
Click Edit configuration and then select the option to clone the active version.
Click VCL Snippets.
Click Create Snippet.
In the Name field, enter an appropriate name (e.g.,
Staff User Cookie
).From the Type controls, select within subroutine.
From the Select subroutine menu, select recv (
vcl_recv
).In the VCL field, add the following condition:
# in vcl_recvif (req.http.Cookie:staff_user ~ "true") {# some logic goes herereturn(pass);}Click Create to create the snippet.
Click Activate to deploy your configuration changes.
Using a custom header
You can isolate the value of staff_user
from Cookie
to the header req.http.staff_user
by creating a custom header with the following settings:
Fill out the Create a header fields as follows:
- In the Name field, enter
Staff User Header
. - From the Type menu, select Request, and from the Action menu select Set.
- In the Destination field, enter
http.staff_user
. - In the Source field, enter
req.http.Cookie:staff_user
. - From the Ignore if set menu, select No.
- In the Priority field, enter
10
.
This will send the staff_user
header in every inbound request.
Attach a condition
link to only create this header when it's needed. See our Using Conditions docs for more information.