Skip to content

Instantly share code, notes, and snippets.

@Luit
Created December 4, 2011 19:40
Show Gist options
  • Save Luit/1431098 to your computer and use it in GitHub Desktop.
Save Luit/1431098 to your computer and use it in GitHub Desktop.
A much better version of a URL shortening service in VCL. It now uses libvmod-redis and works similar to the ugly Inline C VCL.
import redis;
sub vcl_recv {
if (req.url ~ "^/[lL]/[A-Za-z0-9\-_]{4,32}=?=?$") {
set req.http.New-Location = redis.call(regsub(req.url, "^/[lL]/([A-Za-z0-9\-_]{2})([A-Za-z0-9\-_]{2,30}=?=?)$", "HGET short:\1 \2"));
if (req.http.New-Location) {
error 666;
}
error 404 "Not Found";
}
}
sub vcl_error {
if (obj.status == 666) {
set obj.http.Location = req.http.New-Location;
unset req.http.New-Location;
set obj.status = 302;
set obj.response = "Found";
set obj.ttl = 120s;
return (deliver);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment