Skip to content

Instantly share code, notes, and snippets.

View Luit's full-sized avatar

Luit van Drongelen Luit

View GitHub Profile
@Luit
Luit / default.vcl
Created December 4, 2011 22:14
A variation on the libvmod-redis based URL shortener, now doing a curveball to make the redirect cacheable.
import redis;
backend default {
.host = "127.0.0.1";
.port = "8080";
}
backend self {
.host = "127.0.0.1";
.port = "80";
@Luit
Luit / default.vcl
Created December 4, 2011 19:40
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";
}
@Luit
Luit / reconnect.patch
Created December 4, 2011 14:33
A simple patch to fix libvmod-redis from completely halting to function after redis-server closes the connection
diff --git a/src/vmod_redis.c b/src/vmod_redis.c
index b59652b..7c0544e 100644
--- a/src/vmod_redis.c
+++ b/src/vmod_redis.c
@@ -81,6 +81,18 @@ redis_common(struct sess *sp, struct vmod_priv *priv, const char *command)
}
reply = redisCommand(c, command);
+ if (c->err == REDIS_ERR_EOF) {
+ // It seems Redis has disconnected the client, let's try to
@Luit
Luit / shortener.vcl
Created December 1, 2011 11:03
A very ugly first version of a URL shortening service written in VCL inline C. There's a whole lot still to be done, but this thing works quite well already.
C{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hiredis/hiredis.h>
}C
sub vcl_recv {
if (req.http.host ~ "(?i)^a.luit.it$") {
if (req.url ~ "(?i)^/l/.*[A-Za-z0-9\-_]{4,}\+?$") {