Add or remove cookies

Read individual cookies, set new cookies in response.

VCL

Use this solution in your VCL service (click RUN below to test this solution or clone it to make changes):

Compute@Edge

Use this solution in your Compute@Edge service:

  1. Rust
if let Some(mut req_cookie_jar) = req
.get_header(COOKIE)
.and_then(|h| parse_cookies_to_jar(h).ok())
{
// Read and log a specific cookie
log::info!(
"The value of myCookie is {}",
req_cookie_jar
.get("myCookie")
.map(|c| c.value())
.unwrap_or_default()
);
// Remove a cookie from the request
req_cookie_jar.remove(Cookie::named("myCookie"));
// rebuild `cookie` header
let new_hdr = req_cookie_jar
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join("; ");
req.set_header(COOKIE, new_hdr);
} else {
log::warn!("Header cookie absent or could not be parsed");
}
log::info!(
"Header cookie = {}",
req.get_header(COOKIE)
.and_then(|h| h.to_str().ok())
.unwrap_or_default(),
);
// Remove all cookies from the request
req.remove_header(COOKIE);
// Set a cookie in response
beresp.set_header(SET_COOKIE, "myCookie=foo; path=/; max-age=60");
// You can set multiple cookies in one response
beresp.append_header(SET_COOKIE, "mySecondCookie=bar; httpOnly");
// It is usually a good idea to prevent downstream caching of
// responses that set cookies
beresp.set_header(CACHE_CONTROL, "no-store, private");
fn parse_cookies_to_jar(value: &HeaderValue) -> Result<CookieJar, Error> {
let mut jar = CookieJar::new();
for cookie in value.to_str()?.split(';').map(Cookie::parse_encoded) {
jar.add_original(cookie?.into_owned());
}
Ok(jar)
}