November 07, 2006

Extensible mod_rewrite with Lua

I was running an experiment with apache + mod_rewrite that required rules that cannot be expressed with regular expressions. mod_rewrite has the notion of rewrite maps, which let you choose from a couple of built in functions (int:tolower, int:toupper, etc.) or a key-value pair map backed by a text or dbm file.

Unfortunately, there's no way to plug in arbitrary mapping functions*, so my first thought was to extend mod_rewrite by adding a new built in function. This lets me write:


LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine on
RewriteMap foo int:foo

RewriteRule (.*) ${foo:$1} [L]


It works great, but this approach requires forking mod_rewrite.c, which is obviously not a good thing.

Inspired by Brian's mod_wombat, which allows you write http handlers in Lua, I thought "wouldn't it be nice to be able to extend mod_rewrite with custom Lua scripts?". Say, something like:



LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine on
RewriteMap foo lua:/usr/local/apache2/rewrite-rules.lua

RewriteRule (.*) ${foo:$1} [L]



30 lines of code later, my lua-enabled mod_rewrite prototype is able to do exactly that. Mind you, this is a very crude implementation and doesn't check for errors, cache compiled scripts, etc. But all those details can be improved.

Anyway, this is all becomes somewhat irrelevant with the latest version of mod_wombat, which allows you to define transform_name hooks in Lua, as well.

It was a interesting experiment, nonetheless :)



* To be fair, there is actually a way to do this, but I consider it to be too clunky and non-scalable to be of any value in a real production system. The mechanism consists on mod_rewrite talking to an external process that does translations via its stdin/stdout streams. As a result, a global mutex is required to guarantee correctness in the face of multiple simultaneous requests.

4 Comments:

Blogger pimpinkicks said...

There is obviously a lot more than this. Would you mind telling me how long it took you to gather your content? Please come visit my site New Jersey when you got time.

July 29, 2009 1:01 AM  
Blogger pimpinkicks said...

This article was extremely interesting, especially since I was searching for thoughts on this subject last Thursday. Please come visit my site New Mexico, when you got time.

July 29, 2009 1:04 AM  
Blogger pimpinkicks said...

I usually don’t leave comments!!! Trust me! But I liked your blog…especially this post! Would you mind terribly if I put up a backlink from my site to your site? Please come visit my site Pennsylvania when you got time.

August 14, 2009 3:58 AM  
Blogger pimpinkicks said...

I enjoyed reading your work! GREAT post! I looked around for this… but I found you! Anyway, would you mind if I threw up a backlink from my site? Please come visit my site Vermont when you got time.

August 14, 2009 4:00 AM  

Post a Comment

<< Home