mod_lua in apache trunk

The module formally known as mod_wombat was renamed mod_lua, and has pulled into the Apache HTTP Server trunk, and will be part of the future 2.4 stable release.

For an example of why it is cool, lets look at replacing a common task with mod_rewrite: Blocking Image Theft.

The HTTPD wiki even has an example of how to do this for us:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !=""
RewriteCond %{HTTP_REFERER} !example\.com [NC]
RewriteRule \.(jpe?g|gif|png)$ - [F,NC]

With the new mod_lua, you can do this using real if statements and functions.

For example:

<LuaHookTranslateName imagetheft>
function is_image(path)
    -- You could put complicated regular expressions here.
    if path:match("%a+.png") then
        return true
    end
    return false
end

function imagetheft(r)
    if not is_image(r.uri) then
        return apache2.DECLINED
    end

    referer = r:headers_in("Referer")
    if referer then
        if referer:find('example.com') then
            return apache2.DECLINED
        else
            r:err("Forbidden for Image Theft! uri=".. r.uri)
            return 403
        end
    end
    return apache2.DECLINED
end
</LuaHookTranslateName>

While this example comes out signifigantly longer, I do believe in the long run it will let people write more maintainable configurations, espcially for things like complicated RewriteRules — since basic things that have long be obsecured into RewriteCond can now be easily accessed and evaluated with an If statement in Lua.

Much of the API is still in flux, but hopefully we will come up with some shortcut builtins that make many common taskes easier and shorter.

4 Responses to “mod_lua in apache trunk”

  1. unwesen says:

    You know, the next move would be to dump the current apache config format and move to Lua exclusively… I for one would welcome our new Lua overlords!

    Seriously cool stuff :)

  2. John Yerhot says:

    @unwesen
    I’m more of the thinking that having the option to use Lua in my httpd configs would be better than ditching the current format entirely, at least in the foreseeable future.

    I must admit, I do love me some Lisp due to Emacs though.

    Anyways, looks promising Paul.

  3. Kaveh Shahbazian says:

    @unwesen
    You said it! :D

  4. HyperHacker says:

    While Lua, especially as an Apache module, is quite awesome, I can’t help but chuckle at the concept of “image theft”. The idea of being able to send someone a file (as part of your page) and not let them keep it kinda doesn’t make any sense. RefControl deals with this kind of nonsense nicely.

    If you want to know why Lua as an Apache module is a good idea, learn the basics PHP, then of Lua.

Leave a Reply