Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
edgestash through link headers & a synthesized JSON backend
vcl 4.1;
import cookieplus;
import edgestash;
import std;
import ykey;
import redis;
import synthbackend;
backend default {
.host = "origin";
.port = "80";
}
sub vcl_init {
new db = redis.db(
location="redis:6379",
type=master,
connection_timeout=500,
shared_connections=false,
max_connections=1);
}
sub vcl_recv {
set req.http.X-Login = "false";
set req.http.Surrogate-Capability="key=ESI/1.0, edgestash=EDGESTASH/2.1";
set req.http.x-session = cookieplus.get("PHPSESSID","guest");
if(req.http.x-session != "guest") {
db.command("EXISTS");
db.push("sf_s"+cookieplus.get("PHPSESSID"));
db.execute();
if(db.get_integer_reply() == 1) {
set req.http.X-Login = "true";
}
}
if(req.method == "POST") {
ykey.purge(req.http.x-session);
}
if ((req.method != "GET" && req.method != "HEAD") || req.http.Authorization) {
return (pass);
}
return(hash);
}
sub vcl_hash {
if(req.http.host ~ "^edgestash\.") {
hash_data(req.http.x-session);
}
}
sub vcl_backend_fetch
{
if(bereq.url == "/api/session") {
db.command("EVAL");
db.push({"
local session = redis.call('GET', KEYS[1])
if session == nil then
return '{"username":"","liked":[],"somethingCool": false}'
end
local result = string.gsub(session, '[%c]', '')
local username = string.gsub(result,'.+Userusername\";s:[0-9]+:\"([^\"]+)\";.+','%1')
return '{"username":"'.. username ..'","liked":[],"somethingCool": true}'
"});
db.push(1);
db.push("sf_s"+cookieplus.get("PHPSESSID"));
db.execute();
set bereq.backend = synthbackend.from_string(db.get_string_reply());
} else {
set bereq.backend = default;
}
if (bereq.http.Host ~ "^edgestash\.") {
set bereq.http.Host = regsub(bereq.http.Host, "^edgestash\.", "");
}
}
sub vcl_backend_response {
std.collect(beresp.http.Link);
set beresp.http.x-edgestash-json-urls = regsuball(beresp.http.Link,"<([^>]+)>; rel=edgestash","\1");
if(beresp.http.Surrogate-Control~"ESI/1.0") {
set beresp.do_esi=true;
}
if(beresp.http.Surrogate-Control~"EDGESTASH/2\.[0-9]+") {
edgestash.parse_response();
}
if(beresp.http.Surrogate-Control) {
unset beresp.http.Surrogate-Control;
}
}
sub vcl_deliver {
if(edgestash.is_edgestash()) {
edgestash.add_json_url_csv(resp.http.x-edgestash-json-urls, json_host="edgestash." + req.http.host);
edgestash.execute();
}
unset resp.http.Link;
unset resp.http.x-edgestash-json-urls;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.