Blame modules/lua/mod_lua.c

Packit 90a5c9
/**
Packit 90a5c9
 * Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 * http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "mod_lua.h"
Packit 90a5c9
#include <string.h>
Packit 90a5c9
#include <stdlib.h>
Packit 90a5c9
#include <ctype.h>
Packit 90a5c9
#include <apr_thread_mutex.h>
Packit 90a5c9
#include <apr_pools.h>
Packit 90a5c9
#include "lua_apr.h"
Packit 90a5c9
#include "lua_config.h"
Packit 90a5c9
#include "apr_optional.h"
Packit 90a5c9
#include "mod_ssl.h"
Packit 90a5c9
#include "mod_auth.h"
Packit 90a5c9
#include "util_mutex.h"
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
#ifdef APR_HAS_THREADS
Packit 90a5c9
#include "apr_thread_proc.h"
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
/* getpid for *NIX */
Packit 90a5c9
#if APR_HAVE_SYS_TYPES_H
Packit 90a5c9
#include <sys/types.h>
Packit 90a5c9
#endif
Packit 90a5c9
#if APR_HAVE_UNISTD_H
Packit 90a5c9
#include <unistd.h>
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
/* getpid for Windows */
Packit 90a5c9
#if APR_HAVE_PROCESS_H
Packit 90a5c9
#include <process.h>
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ap_lua, AP_LUA, int, lua_open,
Packit 90a5c9
                                    (lua_State *L, apr_pool_t *p),
Packit 90a5c9
                                    (L, p), OK, DECLINED)
Packit 90a5c9
Packit 90a5c9
APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ap_lua, AP_LUA, int, lua_request,
Packit 90a5c9
                                    (lua_State *L, request_rec *r),
Packit 90a5c9
                                    (L, r), OK, DECLINED)
Packit 90a5c9
static APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *lua_ssl_val = NULL;
Packit 90a5c9
static APR_OPTIONAL_FN_TYPE(ssl_is_https) *lua_ssl_is_https = NULL;
Packit 90a5c9
Packit 90a5c9
module AP_MODULE_DECLARE_DATA lua_module;
Packit 90a5c9
Packit 90a5c9
#define AP_LUA_HOOK_FIRST (APR_HOOK_FIRST - 1)
Packit 90a5c9
#define AP_LUA_HOOK_LAST  (APR_HOOK_LAST  + 1)
Packit 90a5c9
Packit 90a5c9
typedef struct {
Packit 90a5c9
    const char *name;
Packit 90a5c9
    const char *file_name;
Packit 90a5c9
    const char *function_name;
Packit 90a5c9
    ap_lua_vm_spec *spec;
Packit 90a5c9
} lua_authz_provider_spec;
Packit 90a5c9
Packit 90a5c9
typedef struct {
Packit 90a5c9
    lua_authz_provider_spec *spec;
Packit 90a5c9
    apr_array_header_t *args;
Packit 90a5c9
} lua_authz_provider_func;
Packit 90a5c9
Packit 90a5c9
apr_hash_t *lua_authz_providers;
Packit 90a5c9
Packit 90a5c9
typedef struct
Packit 90a5c9
{
Packit 90a5c9
    apr_bucket_brigade *tmpBucket;
Packit 90a5c9
    lua_State *L;
Packit 90a5c9
    ap_lua_vm_spec *spec;
Packit 90a5c9
    int broken;
Packit 90a5c9
} lua_filter_ctx;
Packit 90a5c9
Packit 90a5c9
#define DEFAULT_LUA_SHMFILE "lua_ivm_shm"
Packit 90a5c9
Packit 90a5c9
apr_global_mutex_t *lua_ivm_mutex;
Packit 90a5c9
apr_shm_t *lua_ivm_shm;
Packit 90a5c9
char *lua_ivm_shmfile;
Packit 90a5c9
Packit 90a5c9
static apr_status_t shm_cleanup_wrapper(void *unused)
Packit 90a5c9
{
Packit 90a5c9
    if (lua_ivm_shm) {
Packit 90a5c9
        return apr_shm_destroy(lua_ivm_shm);
Packit 90a5c9
    }
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * error reporting if lua has an error.
Packit 90a5c9
 * Extracts the error from lua stack and prints
Packit 90a5c9
 */
Packit 90a5c9
static void report_lua_error(lua_State *L, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    const char *lua_response;
Packit 90a5c9
    r->status = HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
    r->content_type = "text/html";
Packit 90a5c9
    ap_rputs("

Error!

\n", r);
Packit 90a5c9
    ap_rputs("
", r);
Packit 90a5c9
    lua_response = lua_tostring(L, -1);
Packit 90a5c9
    ap_rputs(ap_escape_html(r->pool, lua_response), r);
Packit 90a5c9
    ap_rputs("\n", r);
Packit 90a5c9
Packit 90a5c9
    ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, r->pool, APLOGNO(01471) "Lua error: %s",
Packit 90a5c9
                  lua_response);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void lua_open_callback(lua_State *L, apr_pool_t *p, void *ctx)
Packit 90a5c9
{
Packit 90a5c9
    ap_lua_init(L, p);
Packit 90a5c9
    ap_lua_load_apache2_lmodule(L);
Packit 90a5c9
    ap_lua_load_request_lmodule(L, p);
Packit 90a5c9
    ap_lua_load_config_lmodule(L);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int lua_open_hook(lua_State *L, apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    lua_open_callback(L, p, NULL);
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *scope_to_string(unsigned int scope)
Packit 90a5c9
{
Packit 90a5c9
    switch (scope) {
Packit 90a5c9
    case AP_LUA_SCOPE_ONCE:
Packit 90a5c9
    case AP_LUA_SCOPE_UNSET:
Packit 90a5c9
        return "once";
Packit 90a5c9
    case AP_LUA_SCOPE_REQUEST:
Packit 90a5c9
        return "request";
Packit 90a5c9
    case AP_LUA_SCOPE_CONN:
Packit 90a5c9
        return "conn";
Packit 90a5c9
#if APR_HAS_THREADS
Packit 90a5c9
    case AP_LUA_SCOPE_THREAD:
Packit 90a5c9
        return "thread";
Packit 90a5c9
    case AP_LUA_SCOPE_SERVER:
Packit 90a5c9
        return "server";
Packit 90a5c9
#endif
Packit 90a5c9
    default:
Packit 90a5c9
        ap_assert(0);
Packit 90a5c9
        return 0;
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void ap_lua_release_state(lua_State* L, ap_lua_vm_spec* spec, request_rec* r)
Packit 90a5c9
{
Packit 90a5c9
    char *hash;
Packit 90a5c9
    apr_reslist_t* reslist = NULL;
Packit 90a5c9
Packit 90a5c9
    if (spec->scope == AP_LUA_SCOPE_SERVER) {
Packit 90a5c9
        ap_lua_server_spec* sspec = NULL;
Packit 90a5c9
        lua_settop(L, 0);
Packit 90a5c9
        lua_getfield(L, LUA_REGISTRYINDEX, "Apache2.Lua.server_spec");
Packit 90a5c9
        sspec = (ap_lua_server_spec*) lua_touserdata(L, 1);
Packit 90a5c9
        hash = apr_psprintf(r->pool, "reslist:%s", spec->file);
Packit 90a5c9
        if (apr_pool_userdata_get((void **)&reslist, hash,
Packit 90a5c9
                                r->server->process->pool) == APR_SUCCESS) {
Packit 90a5c9
            AP_DEBUG_ASSERT(sspec != NULL);
Packit 90a5c9
            if (reslist != NULL) {
Packit 90a5c9
                apr_reslist_release(reslist, sspec);
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static ap_lua_vm_spec *create_vm_spec(apr_pool_t **lifecycle_pool,
Packit 90a5c9
                                      request_rec *r,
Packit 90a5c9
                                      const ap_lua_dir_cfg *cfg,
Packit 90a5c9
                                      const ap_lua_server_cfg *server_cfg,
Packit 90a5c9
                                      const char *filename,
Packit 90a5c9
                                      const char *bytecode,
Packit 90a5c9
                                      apr_size_t bytecode_len,
Packit 90a5c9
                                      const char *function,
Packit 90a5c9
                                      const char *what)
Packit 90a5c9
{
Packit 90a5c9
    apr_pool_t *pool;
Packit 90a5c9
    ap_lua_vm_spec *spec = apr_pcalloc(r->pool, sizeof(ap_lua_vm_spec));
Packit 90a5c9
Packit 90a5c9
    spec->scope = cfg->vm_scope;
Packit 90a5c9
    spec->pool = r->pool;
Packit 90a5c9
    spec->package_paths = cfg->package_paths;
Packit 90a5c9
    spec->package_cpaths = cfg->package_cpaths;
Packit 90a5c9
    spec->cb = &lua_open_callback;
Packit 90a5c9
    spec->cb_arg = NULL;
Packit 90a5c9
    spec->bytecode = bytecode;
Packit 90a5c9
    spec->bytecode_len = bytecode_len;
Packit 90a5c9
    spec->codecache = (cfg->codecache == AP_LUA_CACHE_UNSET) ? AP_LUA_CACHE_STAT : cfg->codecache;
Packit 90a5c9
    spec->vm_min = cfg->vm_min ? cfg->vm_min : 1;
Packit 90a5c9
    spec->vm_max = cfg->vm_max ? cfg->vm_max : 1;
Packit 90a5c9
    
Packit 90a5c9
    if (filename) {
Packit 90a5c9
        char *file;
Packit 90a5c9
        apr_filepath_merge(&file, server_cfg->root_path,
Packit 90a5c9
                           filename, APR_FILEPATH_NOTRELATIVE, r->pool);
Packit 90a5c9
        spec->file = file;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        spec->file = r->filename;
Packit 90a5c9
    }
Packit 90a5c9
    ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r, APLOGNO(02313)
Packit 90a5c9
                  "%s details: scope: %s, file: %s, func: %s",
Packit 90a5c9
                  what, scope_to_string(spec->scope), spec->file,
Packit 90a5c9
                  function ? function : "-");
Packit 90a5c9
Packit 90a5c9
    switch (spec->scope) {
Packit 90a5c9
    case AP_LUA_SCOPE_ONCE:
Packit 90a5c9
    case AP_LUA_SCOPE_UNSET:
Packit 90a5c9
        apr_pool_create(&pool, r->pool);
Packit 90a5c9
        break;
Packit 90a5c9
    case AP_LUA_SCOPE_REQUEST:
Packit 90a5c9
        pool = r->pool;
Packit 90a5c9
        break;
Packit 90a5c9
    case AP_LUA_SCOPE_CONN:
Packit 90a5c9
        pool = r->connection->pool;
Packit 90a5c9
        break;
Packit 90a5c9
#if APR_HAS_THREADS
Packit 90a5c9
    case AP_LUA_SCOPE_THREAD:
Packit 90a5c9
        pool = apr_thread_pool_get(r->connection->current_thread);
Packit 90a5c9
        break;
Packit 90a5c9
    case AP_LUA_SCOPE_SERVER:
Packit 90a5c9
        pool = r->server->process->pool;
Packit 90a5c9
        break;
Packit 90a5c9
#endif
Packit 90a5c9
    default:
Packit 90a5c9
        ap_assert(0);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    *lifecycle_pool = pool;
Packit 90a5c9
    return spec;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char* ap_lua_interpolate_string(apr_pool_t* pool, const char* string, const char** values)
Packit 90a5c9
{
Packit 90a5c9
    char *stringBetween;
Packit 90a5c9
    const char* ret;
Packit 90a5c9
    int srclen,x,y;
Packit 90a5c9
    srclen = strlen(string);
Packit 90a5c9
    ret = "";
Packit 90a5c9
    y = 0;
Packit 90a5c9
    for (x=0; x < srclen; x++) {
Packit 90a5c9
        if (string[x] == '$' && x != srclen-1 && string[x+1] >= '0' && string[x+1] <= '9') {
Packit 90a5c9
            int v = *(string+x+1) - '0';
Packit 90a5c9
            if (x-y > 0) {
Packit 90a5c9
                stringBetween = apr_pstrndup(pool, string+y, x-y);
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                stringBetween = "";
Packit 90a5c9
            }
Packit 90a5c9
            ret = apr_pstrcat(pool, ret, stringBetween, values[v], NULL);
Packit 90a5c9
            y = ++x+1;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    
Packit 90a5c9
    if (x-y > 0 && y > 0) {
Packit 90a5c9
        stringBetween = apr_pstrndup(pool, string+y, x-y);
Packit 90a5c9
        ret = apr_pstrcat(pool, ret, stringBetween, NULL);
Packit 90a5c9
    }
Packit 90a5c9
    /* If no replacement was made, just return the original string */
Packit 90a5c9
    else if (y == 0) {
Packit 90a5c9
        return string;
Packit 90a5c9
    }
Packit 90a5c9
    return ret;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * "main"
Packit 90a5c9
 */
Packit 90a5c9
static int lua_handler(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    int rc = OK;
Packit 90a5c9
    if (strcmp(r->handler, "lua-script")) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
    /* Decline the request if the script does not exist (or is a directory),
Packit 90a5c9
     * rather than just returning internal server error */
Packit 90a5c9
    if (
Packit 90a5c9
            (r->finfo.filetype == APR_NOFILE)
Packit 90a5c9
            || (r->finfo.filetype & APR_DIR)
Packit 90a5c9
        ) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
    ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, APLOGNO(01472)
Packit 90a5c9
                  "handling [%s] in mod_lua", r->filename);
Packit 90a5c9
Packit 90a5c9
    /* XXX: This seems wrong because it may generate wrong headers for HEAD requests */
Packit 90a5c9
    if (!r->header_only) {
Packit 90a5c9
        lua_State *L;
Packit 90a5c9
        apr_pool_t *pool;
Packit 90a5c9
        const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
Packit 90a5c9
                                                         &lua_module);
Packit 90a5c9
        ap_lua_vm_spec *spec = create_vm_spec(&pool, r, cfg, NULL, NULL, NULL,
Packit 90a5c9
                                              0, "handle", "request handler");
Packit 90a5c9
Packit 90a5c9
        L = ap_lua_get_lua_state(pool, spec, r);
Packit 90a5c9
        if (!L) {
Packit 90a5c9
            /* TODO annotate spec with failure reason */
Packit 90a5c9
            r->status = HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
            ap_rputs("Unable to compile VM, see logs", r);
Packit 90a5c9
            ap_lua_release_state(L, spec, r);
Packit 90a5c9
            return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
        }
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r, APLOGNO(01474) "got a vm!");
Packit 90a5c9
        lua_getglobal(L, "handle");
Packit 90a5c9
        if (!lua_isfunction(L, -1)) {
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01475)
Packit 90a5c9
                          "lua: Unable to find entry function '%s' in %s (not a valid function)",
Packit 90a5c9
                          "handle",
Packit 90a5c9
                          spec->file);
Packit 90a5c9
            ap_lua_release_state(L, spec, r);
Packit 90a5c9
            return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
        }
Packit 90a5c9
        ap_lua_run_lua_request(L, r);
Packit 90a5c9
        if (lua_pcall(L, 1, 1, 0)) {
Packit 90a5c9
            report_lua_error(L, r);
Packit 90a5c9
        }
Packit 90a5c9
        if (lua_isnumber(L, -1)) {
Packit 90a5c9
            rc = lua_tointeger(L, -1);
Packit 90a5c9
        }
Packit 90a5c9
        ap_lua_release_state(L, spec, r);
Packit 90a5c9
    }
Packit 90a5c9
    return rc;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/* ------------------- Input/output content filters ------------------- */
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static apr_status_t lua_setup_filter_ctx(ap_filter_t* f, request_rec* r, lua_filter_ctx** c)
Packit 90a5c9
{
Packit 90a5c9
    apr_pool_t *pool;
Packit 90a5c9
    ap_lua_vm_spec *spec;
Packit 90a5c9
    int n, rc;
Packit 90a5c9
    lua_State *L;
Packit 90a5c9
    lua_filter_ctx *ctx;    
Packit 90a5c9
    ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config,
Packit 90a5c9
                                                        &lua_module);
Packit 90a5c9
    const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
Packit 90a5c9
                                                    &lua_module);
Packit 90a5c9
    
Packit 90a5c9
    ctx = apr_pcalloc(r->pool, sizeof(lua_filter_ctx));
Packit 90a5c9
    ctx->broken = 0;
Packit 90a5c9
    *c = ctx;
Packit 90a5c9
    /* Find the filter that was called.
Packit 90a5c9
     * XXX: If we were wired with mod_filter, the filter (mod_filters name)
Packit 90a5c9
     *      and the provider (our underlying filters name) need to have matched.
Packit 90a5c9
     */
Packit 90a5c9
    for (n = 0; n < cfg->mapped_filters->nelts; n++) {
Packit 90a5c9
        ap_lua_filter_handler_spec *hook_spec =
Packit 90a5c9
            ((ap_lua_filter_handler_spec **) cfg->mapped_filters->elts)[n];
Packit 90a5c9
Packit 90a5c9
        if (hook_spec == NULL) {
Packit 90a5c9
            continue;
Packit 90a5c9
        }
Packit 90a5c9
        if (!strcasecmp(hook_spec->filter_name, f->frec->name)) {
Packit 90a5c9
            spec = create_vm_spec(&pool, r, cfg, server_cfg,
Packit 90a5c9
                                    hook_spec->file_name,
Packit 90a5c9
                                    NULL,
Packit 90a5c9
                                    0,
Packit 90a5c9
                                    hook_spec->function_name,
Packit 90a5c9
                                    "filter");
Packit 90a5c9
            L = ap_lua_get_lua_state(pool, spec, r);
Packit 90a5c9
            if (L) {
Packit 90a5c9
                L = lua_newthread(L);
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (!L) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02328)
Packit 90a5c9
                                "lua: Failed to obtain lua interpreter for %s %s",
Packit 90a5c9
                                hook_spec->function_name, hook_spec->file_name);
Packit 90a5c9
                ap_lua_release_state(L, spec, r);
Packit 90a5c9
                return APR_EGENERAL;
Packit 90a5c9
            }
Packit 90a5c9
            if (hook_spec->function_name != NULL) {
Packit 90a5c9
                lua_getglobal(L, hook_spec->function_name);
Packit 90a5c9
                if (!lua_isfunction(L, -1)) {
Packit 90a5c9
                    ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02329)
Packit 90a5c9
                                "lua: Unable to find entry function '%s' in %s (not a valid function)",
Packit 90a5c9
                                    hook_spec->function_name,
Packit 90a5c9
                                    hook_spec->file_name);
Packit 90a5c9
                    ap_lua_release_state(L, spec, r);
Packit 90a5c9
                    return APR_EGENERAL;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                ap_lua_run_lua_request(L, r);
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                int t;
Packit 90a5c9
                ap_lua_run_lua_request(L, r);
Packit 90a5c9
Packit 90a5c9
                t = lua_gettop(L);
Packit 90a5c9
                lua_setglobal(L, "r");
Packit 90a5c9
                lua_settop(L, t);
Packit 90a5c9
            }
Packit 90a5c9
            ctx->L = L;
Packit 90a5c9
            ctx->spec = spec;
Packit 90a5c9
            
Packit 90a5c9
            /* If a Lua filter is interested in filtering a request, it must first do a yield, 
Packit 90a5c9
             * otherwise we'll assume that it's not interested and pretend we didn't find it.
Packit 90a5c9
             */
Packit 90a5c9
            rc = lua_resume(L, 1);
Packit 90a5c9
            if (rc == LUA_YIELD) {
Packit 90a5c9
                if (f->frec->providers == NULL) { 
Packit 90a5c9
                    /* Not wired by mod_filter */
Packit 90a5c9
                    apr_table_unset(r->headers_out, "Content-Length");
Packit 90a5c9
                    apr_table_unset(r->headers_out, "Content-MD5");
Packit 90a5c9
                    apr_table_unset(r->headers_out, "ETAG");
Packit 90a5c9
                }
Packit 90a5c9
                return OK;
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                ap_lua_release_state(L, spec, r);
Packit 90a5c9
                return APR_ENOENT;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    return APR_ENOENT;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t lua_output_filter_handle(ap_filter_t *f, apr_bucket_brigade *pbbIn)
Packit 90a5c9
{
Packit 90a5c9
    request_rec *r = f->r;
Packit 90a5c9
    int rc;
Packit 90a5c9
    lua_State *L;
Packit 90a5c9
    lua_filter_ctx* ctx;
Packit 90a5c9
    conn_rec *c = r->connection;
Packit 90a5c9
    apr_bucket *pbktIn;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    
Packit 90a5c9
    /* Set up the initial filter context and acquire the function.
Packit 90a5c9
     * The corresponding Lua function should yield here.
Packit 90a5c9
     */
Packit 90a5c9
    if (!f->ctx) {
Packit 90a5c9
        rc = lua_setup_filter_ctx(f,r,&ctx;;
Packit 90a5c9
        if (rc == APR_EGENERAL) {
Packit 90a5c9
            return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
        }
Packit 90a5c9
        if (rc == APR_ENOENT) {
Packit 90a5c9
            /* No filter entry found (or the script declined to filter), just pass on the buckets */
Packit 90a5c9
            ap_remove_output_filter(f);
Packit 90a5c9
            return ap_pass_brigade(f->next,pbbIn);
Packit 90a5c9
        }
Packit 90a5c9
        else { 
Packit 90a5c9
            /* We've got a willing lua filter, setup and check for a prefix */
Packit 90a5c9
            size_t olen;
Packit 90a5c9
            apr_bucket *pbktOut;
Packit 90a5c9
            const char* output = lua_tolstring(ctx->L, 1, &olen);
Packit 90a5c9
Packit 90a5c9
            f->ctx = ctx;
Packit 90a5c9
            ctx->tmpBucket = apr_brigade_create(r->pool, c->bucket_alloc);
Packit 90a5c9
Packit 90a5c9
            if (olen > 0) { 
Packit 90a5c9
                pbktOut = apr_bucket_heap_create(output, olen, NULL, c->bucket_alloc);
Packit 90a5c9
                APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktOut);
Packit 90a5c9
                rv = ap_pass_brigade(f->next, ctx->tmpBucket);
Packit 90a5c9
                apr_brigade_cleanup(ctx->tmpBucket);
Packit 90a5c9
                if (rv != APR_SUCCESS) {
Packit 90a5c9
                    return rv;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    ctx = (lua_filter_ctx*) f->ctx;
Packit 90a5c9
    L = ctx->L;
Packit 90a5c9
    /* While the Lua function is still yielding, pass in buckets to the coroutine */
Packit 90a5c9
    if (!ctx->broken) {
Packit 90a5c9
        for (pbktIn = APR_BRIGADE_FIRST(pbbIn);
Packit 90a5c9
            pbktIn != APR_BRIGADE_SENTINEL(pbbIn);
Packit 90a5c9
            pbktIn = APR_BUCKET_NEXT(pbktIn))
Packit 90a5c9
            {
Packit 90a5c9
            const char *data;
Packit 90a5c9
            apr_size_t len;
Packit 90a5c9
            apr_bucket *pbktOut;
Packit 90a5c9
Packit 90a5c9
            /* read the bucket */
Packit 90a5c9
            apr_bucket_read(pbktIn,&data,&len,APR_BLOCK_READ);
Packit 90a5c9
Packit 90a5c9
            /* Push the bucket onto the Lua stack as a global var */
Packit 90a5c9
            lua_pushlstring(L, data, len);
Packit 90a5c9
            lua_setglobal(L, "bucket");
Packit 90a5c9
            
Packit 90a5c9
            /* If Lua yielded, it means we have something to pass on */
Packit 90a5c9
            if (lua_resume(L, 0) == LUA_YIELD) {
Packit 90a5c9
                size_t olen;
Packit 90a5c9
                const char* output = lua_tolstring(L, 1, &olen);
Packit 90a5c9
                if (olen > 0) { 
Packit 90a5c9
                    pbktOut = apr_bucket_heap_create(output, olen, NULL,
Packit 90a5c9
                                            c->bucket_alloc);
Packit 90a5c9
                    APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktOut);
Packit 90a5c9
                    rv = ap_pass_brigade(f->next, ctx->tmpBucket);
Packit 90a5c9
                    apr_brigade_cleanup(ctx->tmpBucket);
Packit 90a5c9
                    if (rv != APR_SUCCESS) {
Packit 90a5c9
                        return rv;
Packit 90a5c9
                    }
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                ctx->broken = 1;
Packit 90a5c9
                ap_lua_release_state(L, ctx->spec, r);
Packit 90a5c9
                ap_remove_output_filter(f);
Packit 90a5c9
                apr_brigade_cleanup(pbbIn);
Packit 90a5c9
                apr_brigade_cleanup(ctx->tmpBucket);
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02663)
Packit 90a5c9
                              "lua: Error while executing filter: %s",
Packit 90a5c9
                              lua_tostring(L, -1));
Packit 90a5c9
                return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        /* If we've safely reached the end, do a final call to Lua to allow for any 
Packit 90a5c9
        finishing moves by the script, such as appending a tail. */
Packit 90a5c9
        if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(pbbIn))) {
Packit 90a5c9
            apr_bucket *pbktEOS;
Packit 90a5c9
            lua_pushnil(L);
Packit 90a5c9
            lua_setglobal(L, "bucket");
Packit 90a5c9
            if (lua_resume(L, 0) == LUA_YIELD) {
Packit 90a5c9
                apr_bucket *pbktOut;
Packit 90a5c9
                size_t olen;
Packit 90a5c9
                const char* output = lua_tolstring(L, 1, &olen);
Packit 90a5c9
                if (olen > 0) { 
Packit 90a5c9
                    pbktOut = apr_bucket_heap_create(output, olen, NULL,
Packit 90a5c9
                            c->bucket_alloc);
Packit 90a5c9
                    APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktOut);
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
            pbktEOS = apr_bucket_eos_create(c->bucket_alloc);
Packit 90a5c9
            APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktEOS);
Packit 90a5c9
            ap_lua_release_state(L, ctx->spec, r);
Packit 90a5c9
            rv = ap_pass_brigade(f->next, ctx->tmpBucket);
Packit 90a5c9
            apr_brigade_cleanup(ctx->tmpBucket);
Packit 90a5c9
            if (rv != APR_SUCCESS) {
Packit 90a5c9
                return rv;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    /* Clean up */
Packit 90a5c9
    apr_brigade_cleanup(pbbIn);
Packit 90a5c9
    return APR_SUCCESS;    
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static apr_status_t lua_input_filter_handle(ap_filter_t *f,
Packit 90a5c9
                                       apr_bucket_brigade *pbbOut,
Packit 90a5c9
                                       ap_input_mode_t eMode,
Packit 90a5c9
                                       apr_read_type_e eBlock,
Packit 90a5c9
                                       apr_off_t nBytes) 
Packit 90a5c9
{
Packit 90a5c9
    request_rec *r = f->r;
Packit 90a5c9
    int rc, lastCall = 0;
Packit 90a5c9
    lua_State *L;
Packit 90a5c9
    lua_filter_ctx* ctx;
Packit 90a5c9
    conn_rec *c = r->connection;
Packit 90a5c9
    apr_status_t ret;
Packit 90a5c9
    
Packit 90a5c9
    /* Set up the initial filter context and acquire the function.
Packit 90a5c9
     * The corresponding Lua function should yield here.
Packit 90a5c9
     */
Packit 90a5c9
    if (!f->ctx) {
Packit 90a5c9
        rc = lua_setup_filter_ctx(f,r,&ctx;;
Packit 90a5c9
        f->ctx = ctx;
Packit 90a5c9
        if (rc == APR_EGENERAL) {
Packit 90a5c9
            ctx->broken = 1;
Packit 90a5c9
            ap_remove_input_filter(f); 
Packit 90a5c9
            return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
        }
Packit 90a5c9
        if (rc == APR_ENOENT ) {
Packit 90a5c9
            ap_remove_input_filter(f);
Packit 90a5c9
            ctx->broken = 1;
Packit 90a5c9
        }
Packit 90a5c9
        if (rc == APR_SUCCESS) {
Packit 90a5c9
            ctx->tmpBucket = apr_brigade_create(r->pool, c->bucket_alloc);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    ctx = (lua_filter_ctx*) f->ctx;
Packit 90a5c9
    L = ctx->L;
Packit 90a5c9
    /* If the Lua script broke or denied serving the request, just pass the buckets through */
Packit 90a5c9
    if (ctx->broken) {
Packit 90a5c9
        return ap_get_brigade(f->next, pbbOut, eMode, eBlock, nBytes);
Packit 90a5c9
    }
Packit 90a5c9
    
Packit 90a5c9
    if (APR_BRIGADE_EMPTY(ctx->tmpBucket)) {
Packit 90a5c9
        ret = ap_get_brigade(f->next, ctx->tmpBucket, eMode, eBlock, nBytes);
Packit 90a5c9
        if (eMode == AP_MODE_EATCRLF || ret != APR_SUCCESS)
Packit 90a5c9
            return ret;
Packit 90a5c9
    }
Packit 90a5c9
    
Packit 90a5c9
    /* While the Lua function is still yielding, pass buckets to the coroutine */
Packit 90a5c9
    if (!ctx->broken) {
Packit 90a5c9
        lastCall = 0;
Packit 90a5c9
        while (!APR_BRIGADE_EMPTY(ctx->tmpBucket)) {
Packit 90a5c9
            apr_bucket *pbktIn = APR_BRIGADE_FIRST(ctx->tmpBucket);
Packit 90a5c9
            apr_bucket *pbktOut;
Packit 90a5c9
            const char *data;
Packit 90a5c9
            apr_size_t len;
Packit 90a5c9
            
Packit 90a5c9
            if (APR_BUCKET_IS_EOS(pbktIn)) {
Packit 90a5c9
                APR_BUCKET_REMOVE(pbktIn);
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* read the bucket */
Packit 90a5c9
            ret = apr_bucket_read(pbktIn, &data, &len, eBlock);
Packit 90a5c9
            if (ret != APR_SUCCESS)
Packit 90a5c9
                return ret;
Packit 90a5c9
Packit 90a5c9
            /* Push the bucket onto the Lua stack as a global var */
Packit 90a5c9
            lastCall++;
Packit 90a5c9
            lua_pushlstring(L, data, len);
Packit 90a5c9
            lua_setglobal(L, "bucket");
Packit 90a5c9
            
Packit 90a5c9
            /* If Lua yielded, it means we have something to pass on */
Packit 90a5c9
            if (lua_resume(L, 0) == LUA_YIELD) {
Packit 90a5c9
                size_t olen;
Packit 90a5c9
                const char* output = lua_tolstring(L, 1, &olen);
Packit 90a5c9
                pbktOut = apr_bucket_heap_create(output, olen, 0, c->bucket_alloc);
Packit 90a5c9
                APR_BRIGADE_INSERT_TAIL(pbbOut, pbktOut);
Packit 90a5c9
                apr_bucket_delete(pbktIn);
Packit 90a5c9
                return APR_SUCCESS;
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                ctx->broken = 1;
Packit 90a5c9
                ap_lua_release_state(L, ctx->spec, r);
Packit 90a5c9
                ap_remove_input_filter(f); 
Packit 90a5c9
                apr_bucket_delete(pbktIn);
Packit 90a5c9
                return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        /* If we've safely reached the end, do a final call to Lua to allow for any 
Packit 90a5c9
        finishing moves by the script, such as appending a tail. */
Packit 90a5c9
        if (lastCall == 0) {
Packit 90a5c9
            apr_bucket *pbktEOS = apr_bucket_eos_create(c->bucket_alloc);
Packit 90a5c9
            lua_pushnil(L);
Packit 90a5c9
            lua_setglobal(L, "bucket");
Packit 90a5c9
            if (lua_resume(L, 0) == LUA_YIELD) {
Packit 90a5c9
                apr_bucket *pbktOut;
Packit 90a5c9
                size_t olen;
Packit 90a5c9
                const char* output = lua_tolstring(L, 1, &olen);
Packit 90a5c9
                pbktOut = apr_bucket_heap_create(output, olen, 0, c->bucket_alloc);
Packit 90a5c9
                APR_BRIGADE_INSERT_TAIL(pbbOut, pbktOut);
Packit 90a5c9
            }
Packit 90a5c9
            APR_BRIGADE_INSERT_TAIL(pbbOut,pbktEOS);
Packit 90a5c9
            ap_lua_release_state(L, ctx->spec, r);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/* ---------------- Configury stuff --------------- */
Packit 90a5c9
Packit 90a5c9
/** harnesses for magic hooks **/
Packit 90a5c9
Packit 90a5c9
static int lua_request_rec_hook_harness(request_rec *r, const char *name, int apr_hook_when)
Packit 90a5c9
{
Packit 90a5c9
    int rc;
Packit 90a5c9
    apr_pool_t *pool;
Packit 90a5c9
    lua_State *L;
Packit 90a5c9
    ap_lua_vm_spec *spec;
Packit 90a5c9
    ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config,
Packit 90a5c9
                                                         &lua_module);
Packit 90a5c9
    const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
Packit 90a5c9
                                                     &lua_module);
Packit 90a5c9
    const char *key = apr_psprintf(r->pool, "%s_%d", name, apr_hook_when);
Packit 90a5c9
    apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, key,
Packit 90a5c9
                                                  APR_HASH_KEY_STRING);
Packit 90a5c9
    if (hook_specs) {
Packit 90a5c9
        int i;
Packit 90a5c9
        for (i = 0; i < hook_specs->nelts; i++) {
Packit 90a5c9
            ap_lua_mapped_handler_spec *hook_spec =
Packit 90a5c9
                ((ap_lua_mapped_handler_spec **) hook_specs->elts)[i];
Packit 90a5c9
Packit 90a5c9
            if (hook_spec == NULL) {
Packit 90a5c9
                continue;
Packit 90a5c9
            }
Packit 90a5c9
            spec = create_vm_spec(&pool, r, cfg, server_cfg,
Packit 90a5c9
                                  hook_spec->file_name,
Packit 90a5c9
                                  hook_spec->bytecode,
Packit 90a5c9
                                  hook_spec->bytecode_len,
Packit 90a5c9
                                  hook_spec->function_name,
Packit 90a5c9
                                  "request hook");
Packit 90a5c9
Packit 90a5c9
            L = ap_lua_get_lua_state(pool, spec, r);
Packit 90a5c9
Packit 90a5c9
            if (!L) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01477)
Packit 90a5c9
                    "lua: Failed to obtain lua interpreter for entry function '%s' in %s",
Packit 90a5c9
                              hook_spec->function_name, hook_spec->file_name);
Packit 90a5c9
                return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (hook_spec->function_name != NULL) {
Packit 90a5c9
                lua_getglobal(L, hook_spec->function_name);
Packit 90a5c9
                if (!lua_isfunction(L, -1)) {
Packit 90a5c9
                    ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01478)
Packit 90a5c9
                               "lua: Unable to find entry function '%s' in %s (not a valid function)",
Packit 90a5c9
                                  hook_spec->function_name,
Packit 90a5c9
                                  hook_spec->file_name);
Packit 90a5c9
                    ap_lua_release_state(L, spec, r);
Packit 90a5c9
                    return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                ap_lua_run_lua_request(L, r);
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                int t;
Packit 90a5c9
                ap_lua_run_lua_request(L, r);
Packit 90a5c9
Packit 90a5c9
                t = lua_gettop(L);
Packit 90a5c9
                lua_setglobal(L, "r");
Packit 90a5c9
                lua_settop(L, t);
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (lua_pcall(L, 1, 1, 0)) {
Packit 90a5c9
                report_lua_error(L, r);
Packit 90a5c9
                ap_lua_release_state(L, spec, r);
Packit 90a5c9
                return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
            }
Packit 90a5c9
            rc = DECLINED;
Packit 90a5c9
            if (lua_isnumber(L, -1)) {
Packit 90a5c9
                rc = lua_tointeger(L, -1);
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_TRACE4, 0, r, "Lua hook %s:%s for phase %s returned %d", 
Packit 90a5c9
                              hook_spec->file_name, hook_spec->function_name, name, rc);
Packit 90a5c9
            }
Packit 90a5c9
            else { 
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(03017)
Packit 90a5c9
                              "Lua hook %s:%s for phase %s did not return a numeric value",
Packit 90a5c9
                              hook_spec->file_name, hook_spec->function_name, name);
Packit 90a5c9
                return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
            }
Packit 90a5c9
            if (rc != DECLINED) {
Packit 90a5c9
                ap_lua_release_state(L, spec, r);
Packit 90a5c9
                return rc;
Packit 90a5c9
            }
Packit 90a5c9
            ap_lua_release_state(L, spec, r);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    return DECLINED;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/* Fix for making sure that LuaMapHandler works when FallbackResource is set */
Packit 90a5c9
static int lua_map_handler_fixups(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    /* If there is no handler set yet, this might be a LuaMapHandler request */
Packit 90a5c9
    if (r->handler == NULL) {
Packit 90a5c9
        int n = 0;
Packit 90a5c9
        ap_regmatch_t match[10];
Packit 90a5c9
        const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
Packit 90a5c9
                                                     &lua_module);
Packit 90a5c9
        for (n = 0; n < cfg->mapped_handlers->nelts; n++) {
Packit 90a5c9
            ap_lua_mapped_handler_spec *hook_spec =
Packit 90a5c9
            ((ap_lua_mapped_handler_spec **) cfg->mapped_handlers->elts)[n];
Packit 90a5c9
Packit 90a5c9
            if (hook_spec == NULL) {
Packit 90a5c9
                continue;
Packit 90a5c9
            }
Packit 90a5c9
            if (!ap_regexec(hook_spec->uri_pattern, r->uri, 10, match, 0)) {
Packit 90a5c9
                r->handler = apr_pstrdup(r->pool, "lua-map-handler");
Packit 90a5c9
                return OK;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    return DECLINED;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static int lua_map_handler(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    int rc, n = 0;
Packit 90a5c9
    apr_pool_t *pool;
Packit 90a5c9
    lua_State *L;
Packit 90a5c9
    const char *filename, *function_name;
Packit 90a5c9
    const char *values[10];
Packit 90a5c9
    ap_lua_vm_spec *spec;
Packit 90a5c9
    ap_regmatch_t match[10];
Packit 90a5c9
    ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config,
Packit 90a5c9
                                                         &lua_module);
Packit 90a5c9
    const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
Packit 90a5c9
                                                     &lua_module);
Packit 90a5c9
    for (n = 0; n < cfg->mapped_handlers->nelts; n++) {
Packit 90a5c9
        ap_lua_mapped_handler_spec *hook_spec =
Packit 90a5c9
            ((ap_lua_mapped_handler_spec **) cfg->mapped_handlers->elts)[n];
Packit 90a5c9
Packit 90a5c9
        if (hook_spec == NULL) {
Packit 90a5c9
            continue;
Packit 90a5c9
        }
Packit 90a5c9
        if (!ap_regexec(hook_spec->uri_pattern, r->uri, 10, match, 0)) {
Packit 90a5c9
            int i;
Packit 90a5c9
            for (i=0 ; i < 10; i++) {
Packit 90a5c9
                if (match[i].rm_eo >= 0) {
Packit 90a5c9
                    values[i] = apr_pstrndup(r->pool, r->uri+match[i].rm_so, match[i].rm_eo - match[i].rm_so);
Packit 90a5c9
                }
Packit 90a5c9
                else values[i] = "";
Packit 90a5c9
            }
Packit 90a5c9
            filename = ap_lua_interpolate_string(r->pool, hook_spec->file_name, values);
Packit 90a5c9
            function_name = ap_lua_interpolate_string(r->pool, hook_spec->function_name, values);
Packit 90a5c9
            spec = create_vm_spec(&pool, r, cfg, server_cfg,
Packit 90a5c9
                                    filename,
Packit 90a5c9
                                    hook_spec->bytecode,
Packit 90a5c9
                                    hook_spec->bytecode_len,
Packit 90a5c9
                                    function_name,
Packit 90a5c9
                                    "mapped handler");
Packit 90a5c9
            L = ap_lua_get_lua_state(pool, spec, r);
Packit 90a5c9
Packit 90a5c9
            if (!L) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02330)
Packit 90a5c9
                                "lua: Failed to obtain Lua interpreter for entry function '%s' in %s",
Packit 90a5c9
                                function_name, filename);
Packit 90a5c9
                ap_lua_release_state(L, spec, r);
Packit 90a5c9
                return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (function_name != NULL) {
Packit 90a5c9
                lua_getglobal(L, function_name);
Packit 90a5c9
                if (!lua_isfunction(L, -1)) {
Packit 90a5c9
                    ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02331)
Packit 90a5c9
                                    "lua: Unable to find entry function '%s' in %s (not a valid function)",
Packit 90a5c9
                                    function_name,
Packit 90a5c9
                                    filename);
Packit 90a5c9
                    ap_lua_release_state(L, spec, r);
Packit 90a5c9
                    return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                ap_lua_run_lua_request(L, r);
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                int t;
Packit 90a5c9
                ap_lua_run_lua_request(L, r);
Packit 90a5c9
Packit 90a5c9
                t = lua_gettop(L);
Packit 90a5c9
                lua_setglobal(L, "r");
Packit 90a5c9
                lua_settop(L, t);
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (lua_pcall(L, 1, 1, 0)) {
Packit 90a5c9
                report_lua_error(L, r);
Packit 90a5c9
                ap_lua_release_state(L, spec, r);
Packit 90a5c9
                return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
            }
Packit 90a5c9
            rc = DECLINED;
Packit 90a5c9
            if (lua_isnumber(L, -1)) {
Packit 90a5c9
                rc = lua_tointeger(L, -1);
Packit 90a5c9
            }
Packit 90a5c9
            else { 
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02483)
Packit 90a5c9
                              "lua: Lua handler %s in %s did not return a value, assuming apache2.OK",
Packit 90a5c9
                              function_name,
Packit 90a5c9
                              filename);
Packit 90a5c9
                rc = OK;
Packit 90a5c9
            }
Packit 90a5c9
            ap_lua_release_state(L, spec, r);
Packit 90a5c9
            if (rc != DECLINED) {
Packit 90a5c9
                return rc;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    return DECLINED;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static apr_size_t config_getstr(ap_configfile_t *cfg, char *buf,
Packit 90a5c9
                                size_t bufsiz)
Packit 90a5c9
{
Packit 90a5c9
    apr_size_t i = 0;
Packit 90a5c9
Packit 90a5c9
    if (cfg->getstr) {
Packit 90a5c9
        apr_status_t rc = (cfg->getstr) (buf, bufsiz, cfg->param);
Packit 90a5c9
        if (rc == APR_SUCCESS) {
Packit 90a5c9
            i = strlen(buf);
Packit 90a5c9
            if (i && buf[i - 1] == '\n')
Packit 90a5c9
                ++cfg->line_number;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            buf[0] = '\0';
Packit 90a5c9
            i = 0;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        while (i < bufsiz) {
Packit 90a5c9
            char ch;
Packit 90a5c9
            apr_status_t rc = (cfg->getch) (&ch, cfg->param);
Packit 90a5c9
            if (rc != APR_SUCCESS)
Packit 90a5c9
                break;
Packit 90a5c9
            buf[i++] = ch;
Packit 90a5c9
            if (ch == '\n') {
Packit 90a5c9
                ++cfg->line_number;
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    return i;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
typedef struct cr_ctx
Packit 90a5c9
{
Packit 90a5c9
    cmd_parms *cmd;
Packit 90a5c9
    ap_configfile_t *cfp;
Packit 90a5c9
    size_t startline;
Packit 90a5c9
    const char *endstr;
Packit 90a5c9
    char buf[HUGE_STRING_LEN];
Packit 90a5c9
} cr_ctx;
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/* Okay, this deserves a little explanation -- in order for the errors that lua
Packit 90a5c9
 * generates to be 'accuarate', including line numbers, we basically inject
Packit 90a5c9
 * N line number new lines into the 'top' of the chunk reader.....
Packit 90a5c9
 *
Packit 90a5c9
 * be happy. this is cool.
Packit 90a5c9
 *
Packit 90a5c9
 */
Packit 90a5c9
static const char *lf =
Packit 90a5c9
    "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
Packit 90a5c9
#define N_LF 32
Packit 90a5c9
Packit 90a5c9
static const char *direct_chunkreader(lua_State *lvm, void *udata,
Packit 90a5c9
                                      size_t *plen)
Packit 90a5c9
{
Packit 90a5c9
    const char *p;
Packit 90a5c9
    struct cr_ctx *ctx = udata;
Packit 90a5c9
Packit 90a5c9
    if (ctx->startline) {
Packit 90a5c9
        *plen = ctx->startline > N_LF ? N_LF : ctx->startline;
Packit 90a5c9
        ctx->startline -= *plen;
Packit 90a5c9
        return lf;
Packit 90a5c9
    }
Packit 90a5c9
    *plen = config_getstr(ctx->cfp, ctx->buf, HUGE_STRING_LEN);
Packit 90a5c9
Packit 90a5c9
    for (p = ctx->buf; isspace(*p); ++p);
Packit 90a5c9
    if (p[0] == '<' && p[1] == '/') {
Packit 90a5c9
        apr_size_t i = 0;
Packit 90a5c9
        while (i < strlen(ctx->endstr)) {
Packit 90a5c9
            if (tolower(p[i + 2]) != ctx->endstr[i])
Packit 90a5c9
                return ctx->buf;
Packit 90a5c9
            ++i;
Packit 90a5c9
        }
Packit 90a5c9
        *plen = 0;
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
    /*fprintf(stderr, "buf read: %s\n", ctx->buf); */
Packit 90a5c9
    return ctx->buf;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int ldump_writer(lua_State *L, const void *b, size_t size, void *B)
Packit 90a5c9
{
Packit 90a5c9
    (void) L;
Packit 90a5c9
    luaL_addlstring((luaL_Buffer *) B, (const char *) b, size);
Packit 90a5c9
    return 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
typedef struct hack_section_baton
Packit 90a5c9
{
Packit 90a5c9
    const char *name;
Packit 90a5c9
    ap_lua_mapped_handler_spec *spec;
Packit 90a5c9
    int apr_hook_when;
Packit 90a5c9
} hack_section_baton;
Packit 90a5c9
Packit 90a5c9
/* You can be unhappy now.
Packit 90a5c9
 *
Packit 90a5c9
 * This is uncool.
Packit 90a5c9
 *
Packit 90a5c9
 * When you create a 
Packit 90a5c9
 * a directory context is to parse the section, and convert it into a 'normal'
Packit 90a5c9
 * Configureation option, and then collapse the entire section, in memory,
Packit 90a5c9
 * back into the parent section -- from which you can then get the new directive
Packit 90a5c9
 * invoked.... anyways. evil. Rici taught me how to do this hack :-)
Packit 90a5c9
 */
Packit 90a5c9
static const char *hack_section_handler(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                        const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
Packit 90a5c9
    ap_directive_t *directive = cmd->directive;
Packit 90a5c9
    hack_section_baton *baton = directive->data;
Packit 90a5c9
    const char *key = apr_psprintf(cmd->pool, "%s_%d", baton->name, baton->apr_hook_when);
Packit 90a5c9
Packit 90a5c9
    apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, key,
Packit 90a5c9
                                                  APR_HASH_KEY_STRING);
Packit 90a5c9
    if (!hook_specs) {
Packit 90a5c9
        hook_specs = apr_array_make(cmd->pool, 2,
Packit 90a5c9
                                    sizeof(ap_lua_mapped_handler_spec *));
Packit 90a5c9
        apr_hash_set(cfg->hooks, key,
Packit 90a5c9
                     APR_HASH_KEY_STRING, hook_specs);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    baton->spec->scope = cfg->vm_scope;
Packit 90a5c9
Packit 90a5c9
    *(ap_lua_mapped_handler_spec **) apr_array_push(hook_specs) = baton->spec;
Packit 90a5c9
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *register_named_block_function_hook(const char *name,
Packit 90a5c9
                                                      cmd_parms *cmd,
Packit 90a5c9
                                                      void *mconfig,
Packit 90a5c9
                                                      const char *line)
Packit 90a5c9
{
Packit 90a5c9
    const char *function = NULL;
Packit 90a5c9
    ap_lua_mapped_handler_spec *spec;
Packit 90a5c9
    int when = APR_HOOK_MIDDLE;
Packit 90a5c9
    const char *endp = ap_strrchr_c(line, '>');
Packit 90a5c9
Packit 90a5c9
    if (endp == NULL) {
Packit 90a5c9
        return apr_pstrcat(cmd->pool, cmd->cmd->name,
Packit 90a5c9
                           "> directive missing closing '>'", NULL);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    line = apr_pstrndup(cmd->temp_pool, line, endp - line);
Packit 90a5c9
Packit 90a5c9
    if (line[0]) { 
Packit 90a5c9
        const char *word;
Packit 90a5c9
        word = ap_getword_conf(cmd->temp_pool, &line);
Packit 90a5c9
        if (*word) {
Packit 90a5c9
            function = apr_pstrdup(cmd->pool, word);
Packit 90a5c9
        }
Packit 90a5c9
        word = ap_getword_conf(cmd->temp_pool, &line);
Packit 90a5c9
        if (*word) {
Packit 90a5c9
            if (!strcasecmp("early", word)) { 
Packit 90a5c9
                when = AP_LUA_HOOK_FIRST;
Packit 90a5c9
            }
Packit 90a5c9
            else if (!strcasecmp("late", word)) {
Packit 90a5c9
                when = AP_LUA_HOOK_LAST;
Packit 90a5c9
            }
Packit 90a5c9
            else { 
Packit 90a5c9
                return apr_pstrcat(cmd->pool, cmd->cmd->name,
Packit 90a5c9
                                   "> 2nd argument must be 'early' or 'late'", NULL);
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec));
Packit 90a5c9
Packit 90a5c9
    {
Packit 90a5c9
        cr_ctx ctx;
Packit 90a5c9
        lua_State *lvm;
Packit 90a5c9
        char *tmp;
Packit 90a5c9
        int rv;
Packit 90a5c9
        ap_directive_t **current;
Packit 90a5c9
        hack_section_baton *baton;
Packit 90a5c9
Packit 90a5c9
        spec->file_name = apr_psprintf(cmd->pool, "%s:%u",
Packit 90a5c9
                                       cmd->config_file->name,
Packit 90a5c9
                                       cmd->config_file->line_number);
Packit 90a5c9
        if (function) {
Packit 90a5c9
            spec->function_name = (char *) function;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            function = NULL;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        ctx.cmd = cmd;
Packit 90a5c9
        tmp = apr_pstrdup(cmd->pool, cmd->err_directive->directive + 1);
Packit 90a5c9
        ap_str_tolower(tmp);
Packit 90a5c9
        ctx.endstr = tmp;
Packit 90a5c9
        ctx.cfp = cmd->config_file;
Packit 90a5c9
        ctx.startline = cmd->config_file->line_number;
Packit 90a5c9
Packit 90a5c9
        /* This lua State is used only to compile the input strings -> bytecode, so we don't need anything extra. */
Packit 90a5c9
        lvm = luaL_newstate();
Packit 90a5c9
Packit 90a5c9
        lua_settop(lvm, 0);
Packit 90a5c9
Packit 90a5c9
        rv = lua_load(lvm, direct_chunkreader, &ctx, spec->file_name);
Packit 90a5c9
Packit 90a5c9
        if (rv != 0) {
Packit 90a5c9
            const char *errstr = apr_pstrcat(cmd->pool, "Lua Error:",
Packit 90a5c9
                                             lua_tostring(lvm, -1), NULL);
Packit 90a5c9
            lua_close(lvm);
Packit 90a5c9
            return errstr;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            luaL_Buffer b;
Packit 90a5c9
            luaL_buffinit(lvm, &b);
Packit 90a5c9
            lua_dump(lvm, ldump_writer, &b);
Packit 90a5c9
            luaL_pushresult(&b);
Packit 90a5c9
            spec->bytecode_len = lua_rawlen(lvm, -1);
Packit 90a5c9
            spec->bytecode = apr_pstrmemdup(cmd->pool, lua_tostring(lvm, -1),
Packit 90a5c9
                                            spec->bytecode_len);
Packit 90a5c9
            lua_close(lvm);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        current = mconfig;
Packit 90a5c9
Packit 90a5c9
        /* Here, we have to replace our current config node for the next pass */
Packit 90a5c9
        if (!*current) {
Packit 90a5c9
            *current = apr_pcalloc(cmd->pool, sizeof(**current));
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        baton = apr_pcalloc(cmd->pool, sizeof(hack_section_baton));
Packit 90a5c9
        baton->name = name;
Packit 90a5c9
        baton->spec = spec;
Packit 90a5c9
        baton->apr_hook_when = when;
Packit 90a5c9
Packit 90a5c9
        (*current)->filename = cmd->config_file->name;
Packit 90a5c9
        (*current)->line_num = cmd->config_file->line_number;
Packit 90a5c9
        (*current)->directive = apr_pstrdup(cmd->pool, "Lua_____ByteCodeHack");
Packit 90a5c9
        (*current)->args = NULL;
Packit 90a5c9
        (*current)->data = baton;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *register_named_file_function_hook(const char *name,
Packit 90a5c9
                                                     cmd_parms *cmd,
Packit 90a5c9
                                                     void *_cfg,
Packit 90a5c9
                                                     const char *file,
Packit 90a5c9
                                                     const char *function,
Packit 90a5c9
                                                     int apr_hook_when)
Packit 90a5c9
{
Packit 90a5c9
    ap_lua_mapped_handler_spec *spec;
Packit 90a5c9
    ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
Packit 90a5c9
    const char *key = apr_psprintf(cmd->pool, "%s_%d", name, apr_hook_when);
Packit 90a5c9
    apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, key,
Packit 90a5c9
                                                  APR_HASH_KEY_STRING);
Packit 90a5c9
Packit 90a5c9
    if (!hook_specs) {
Packit 90a5c9
        hook_specs = apr_array_make(cmd->pool, 2,
Packit 90a5c9
                                    sizeof(ap_lua_mapped_handler_spec *));
Packit 90a5c9
        apr_hash_set(cfg->hooks, key, APR_HASH_KEY_STRING, hook_specs);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec));
Packit 90a5c9
    spec->file_name = apr_pstrdup(cmd->pool, file);
Packit 90a5c9
    spec->function_name = apr_pstrdup(cmd->pool, function);
Packit 90a5c9
    spec->scope = cfg->vm_scope;
Packit 90a5c9
Packit 90a5c9
    *(ap_lua_mapped_handler_spec **) apr_array_push(hook_specs) = spec;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
static const char *register_mapped_file_function_hook(const char *pattern,
Packit 90a5c9
                                                     cmd_parms *cmd,
Packit 90a5c9
                                                     void *_cfg,
Packit 90a5c9
                                                     const char *file,
Packit 90a5c9
                                                     const char *function)
Packit 90a5c9
{
Packit 90a5c9
    ap_lua_mapped_handler_spec *spec;
Packit 90a5c9
    ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
Packit 90a5c9
    ap_regex_t *regex = apr_pcalloc(cmd->pool, sizeof(ap_regex_t));
Packit 90a5c9
    if (ap_regcomp(regex, pattern,0)) {
Packit 90a5c9
        return "Invalid regex pattern!";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec));
Packit 90a5c9
    spec->file_name = apr_pstrdup(cmd->pool, file);
Packit 90a5c9
    spec->function_name = apr_pstrdup(cmd->pool, function);
Packit 90a5c9
    spec->scope = cfg->vm_scope;
Packit 90a5c9
    spec->uri_pattern = regex;
Packit 90a5c9
Packit 90a5c9
    *(ap_lua_mapped_handler_spec **) apr_array_push(cfg->mapped_handlers) = spec;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
static const char *register_filter_function_hook(const char *filter,
Packit 90a5c9
                                                     cmd_parms *cmd,
Packit 90a5c9
                                                     void *_cfg,
Packit 90a5c9
                                                     const char *file,
Packit 90a5c9
                                                     const char *function,
Packit 90a5c9
                                                     int direction)
Packit 90a5c9
{
Packit 90a5c9
    ap_lua_filter_handler_spec *spec;
Packit 90a5c9
    ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
Packit 90a5c9
   
Packit 90a5c9
    spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_filter_handler_spec));
Packit 90a5c9
    spec->file_name = apr_pstrdup(cmd->pool, file);
Packit 90a5c9
    spec->function_name = apr_pstrdup(cmd->pool, function);
Packit 90a5c9
    spec->filter_name = filter;
Packit 90a5c9
Packit 90a5c9
    *(ap_lua_filter_handler_spec **) apr_array_push(cfg->mapped_filters) = spec;
Packit 90a5c9
    /* TODO: Make it work on other types than just AP_FTYPE_RESOURCE? */
Packit 90a5c9
    if (direction == AP_LUA_FILTER_OUTPUT) {
Packit 90a5c9
        spec->direction = AP_LUA_FILTER_OUTPUT;
Packit 90a5c9
        ap_register_output_filter_protocol(filter, lua_output_filter_handle, NULL, AP_FTYPE_RESOURCE,
Packit 90a5c9
                                            AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH);
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        spec->direction = AP_LUA_FILTER_INPUT;
Packit 90a5c9
        ap_register_input_filter(filter, lua_input_filter_handle, NULL, AP_FTYPE_RESOURCE);
Packit 90a5c9
    }
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
/* disabled (see reference below)
Packit 90a5c9
static int lua_check_user_id_harness_first(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "check_user_id", AP_LUA_HOOK_FIRST);
Packit 90a5c9
}
Packit 90a5c9
*/
Packit 90a5c9
static int lua_check_user_id_harness(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "check_user_id", APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
/* disabled (see reference below)
Packit 90a5c9
static int lua_check_user_id_harness_last(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "check_user_id", AP_LUA_HOOK_LAST);
Packit 90a5c9
}
Packit 90a5c9
*/
Packit 90a5c9
Packit 90a5c9
static int lua_translate_name_harness_first(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "translate_name", AP_LUA_HOOK_FIRST);
Packit 90a5c9
}
Packit 90a5c9
static int lua_translate_name_harness(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "translate_name", APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
static int lua_translate_name_harness_last(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "translate_name", AP_LUA_HOOK_LAST);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int lua_fixup_harness(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "fixups", APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int lua_map_to_storage_harness(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "map_to_storage", APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int lua_type_checker_harness(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "type_checker", APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int lua_access_checker_harness_first(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "access_checker", AP_LUA_HOOK_FIRST);
Packit 90a5c9
}
Packit 90a5c9
static int lua_access_checker_harness(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "access_checker", APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
static int lua_access_checker_harness_last(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "access_checker", AP_LUA_HOOK_LAST);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int lua_auth_checker_harness_first(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "auth_checker", AP_LUA_HOOK_FIRST);
Packit 90a5c9
}
Packit 90a5c9
static int lua_auth_checker_harness(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "auth_checker", APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
static int lua_auth_checker_harness_last(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "auth_checker", AP_LUA_HOOK_LAST);
Packit 90a5c9
}
Packit 90a5c9
static void lua_insert_filter_harness(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    /* ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(03223)
Packit 90a5c9
     *               "LuaHookInsertFilter not yet implemented"); */
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int lua_log_transaction_harness(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "log_transaction", APR_HOOK_FIRST);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int lua_quick_harness(request_rec *r, int lookup)
Packit 90a5c9
{
Packit 90a5c9
    if (lookup) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
    return lua_request_rec_hook_harness(r, "quick", APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *register_translate_name_hook(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                                const char *file,
Packit 90a5c9
                                                const char *function,
Packit 90a5c9
                                                const char *when)
Packit 90a5c9
{
Packit 90a5c9
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
Packit 90a5c9
                                           NOT_IN_HTACCESS);
Packit 90a5c9
    int apr_hook_when = APR_HOOK_MIDDLE;
Packit 90a5c9
    if (err) {
Packit 90a5c9
        return err;
Packit 90a5c9
    }
Packit 90a5c9
    
Packit 90a5c9
    if (when) { 
Packit 90a5c9
        if (!strcasecmp(when, "early")) { 
Packit 90a5c9
            apr_hook_when = AP_LUA_HOOK_FIRST;
Packit 90a5c9
        } 
Packit 90a5c9
        else if (!strcasecmp(when, "late")) { 
Packit 90a5c9
            apr_hook_when = AP_LUA_HOOK_LAST;
Packit 90a5c9
        } 
Packit 90a5c9
        else { 
Packit 90a5c9
            return "Third argument must be 'early' or 'late'";
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return register_named_file_function_hook("translate_name", cmd, _cfg,
Packit 90a5c9
                                             file, function, apr_hook_when);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *register_translate_name_block(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                                 const char *line)
Packit 90a5c9
{
Packit 90a5c9
    return register_named_block_function_hook("translate_name", cmd, _cfg,
Packit 90a5c9
                                              line);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static const char *register_fixups_hook(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                        const char *file,
Packit 90a5c9
                                        const char *function)
Packit 90a5c9
{
Packit 90a5c9
    return register_named_file_function_hook("fixups", cmd, _cfg, file,
Packit 90a5c9
                                             function, APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
static const char *register_fixups_block(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                         const char *line)
Packit 90a5c9
{
Packit 90a5c9
    return register_named_block_function_hook("fixups", cmd, _cfg, line);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *register_map_to_storage_hook(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                                const char *file,
Packit 90a5c9
                                                const char *function)
Packit 90a5c9
{
Packit 90a5c9
    return register_named_file_function_hook("map_to_storage", cmd, _cfg,
Packit 90a5c9
                                             file, function, APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *register_log_transaction_hook(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                                const char *file,
Packit 90a5c9
                                                const char *function)
Packit 90a5c9
{
Packit 90a5c9
    return register_named_file_function_hook("log_transaction", cmd, _cfg,
Packit 90a5c9
                                             file, function, APR_HOOK_FIRST);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *register_map_to_storage_block(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                                 const char *line)
Packit 90a5c9
{
Packit 90a5c9
    return register_named_block_function_hook("map_to_storage", cmd, _cfg,
Packit 90a5c9
                                              line);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static const char *register_check_user_id_hook(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                               const char *file,
Packit 90a5c9
                                               const char *function,
Packit 90a5c9
                                               const char *when)
Packit 90a5c9
{
Packit 90a5c9
    int apr_hook_when = APR_HOOK_MIDDLE;
Packit 90a5c9
/* XXX: This does not currently work!!
Packit 90a5c9
    if (when) {
Packit 90a5c9
        if (!strcasecmp(when, "early")) {
Packit 90a5c9
            apr_hook_when = AP_LUA_HOOK_FIRST;
Packit 90a5c9
        }
Packit 90a5c9
        else if (!strcasecmp(when, "late")) {
Packit 90a5c9
            apr_hook_when = AP_LUA_HOOK_LAST;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            return "Third argument must be 'early' or 'late'";
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
*/
Packit 90a5c9
    return register_named_file_function_hook("check_user_id", cmd, _cfg, file,
Packit 90a5c9
                                             function, apr_hook_when);
Packit 90a5c9
}
Packit 90a5c9
static const char *register_check_user_id_block(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                                const char *line)
Packit 90a5c9
{
Packit 90a5c9
    return register_named_block_function_hook("check_user_id", cmd, _cfg,
Packit 90a5c9
                                              line);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *register_type_checker_hook(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                              const char *file,
Packit 90a5c9
                                              const char *function)
Packit 90a5c9
{
Packit 90a5c9
    return register_named_file_function_hook("type_checker", cmd, _cfg, file,
Packit 90a5c9
                                             function, APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
static const char *register_type_checker_block(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                               const char *line)
Packit 90a5c9
{
Packit 90a5c9
    return register_named_block_function_hook("type_checker", cmd, _cfg,
Packit 90a5c9
                                              line);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *register_access_checker_hook(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                                const char *file,
Packit 90a5c9
                                                const char *function,
Packit 90a5c9
                                                const char *when)
Packit 90a5c9
{
Packit 90a5c9
    int apr_hook_when = APR_HOOK_MIDDLE;
Packit 90a5c9
Packit 90a5c9
    if (when) {
Packit 90a5c9
        if (!strcasecmp(when, "early")) {
Packit 90a5c9
            apr_hook_when = AP_LUA_HOOK_FIRST;
Packit 90a5c9
        }
Packit 90a5c9
        else if (!strcasecmp(when, "late")) {
Packit 90a5c9
            apr_hook_when = AP_LUA_HOOK_LAST;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            return "Third argument must be 'early' or 'late'";
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return register_named_file_function_hook("access_checker", cmd, _cfg,
Packit 90a5c9
                                             file, function, apr_hook_when);
Packit 90a5c9
}
Packit 90a5c9
static const char *register_access_checker_block(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                                 const char *line)
Packit 90a5c9
{
Packit 90a5c9
Packit 90a5c9
    return register_named_block_function_hook("access_checker", cmd, _cfg,
Packit 90a5c9
                                              line);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *register_auth_checker_hook(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                              const char *file,
Packit 90a5c9
                                              const char *function,
Packit 90a5c9
                                              const char *when)
Packit 90a5c9
{
Packit 90a5c9
    int apr_hook_when = APR_HOOK_MIDDLE;
Packit 90a5c9
Packit 90a5c9
    if (when) {
Packit 90a5c9
        if (!strcasecmp(when, "early")) {
Packit 90a5c9
            apr_hook_when = AP_LUA_HOOK_FIRST;
Packit 90a5c9
        }
Packit 90a5c9
        else if (!strcasecmp(when, "late")) {
Packit 90a5c9
            apr_hook_when = AP_LUA_HOOK_LAST;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            return "Third argument must be 'early' or 'late'";
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return register_named_file_function_hook("auth_checker", cmd, _cfg, file,
Packit 90a5c9
                                             function, apr_hook_when);
Packit 90a5c9
}
Packit 90a5c9
static const char *register_auth_checker_block(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                               const char *line)
Packit 90a5c9
{
Packit 90a5c9
    return register_named_block_function_hook("auth_checker", cmd, _cfg,
Packit 90a5c9
                                              line);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *register_insert_filter_hook(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                               const char *file,
Packit 90a5c9
                                               const char *function)
Packit 90a5c9
{
Packit 90a5c9
    return "LuaHookInsertFilter not yet implemented";
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *register_quick_hook(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                       const char *file, const char *function)
Packit 90a5c9
{
Packit 90a5c9
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
Packit 90a5c9
                                                NOT_IN_HTACCESS);
Packit 90a5c9
    if (err) {
Packit 90a5c9
        return err;
Packit 90a5c9
    }
Packit 90a5c9
    return register_named_file_function_hook("quick", cmd, _cfg, file,
Packit 90a5c9
                                             function, APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
static const char *register_map_handler(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                       const char* match, const char *file, const char *function)
Packit 90a5c9
{
Packit 90a5c9
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
Packit 90a5c9
                                                NOT_IN_HTACCESS);
Packit 90a5c9
    if (err) {
Packit 90a5c9
        return err;
Packit 90a5c9
    }
Packit 90a5c9
    if (!function) function = "handle";
Packit 90a5c9
    return register_mapped_file_function_hook(match, cmd, _cfg, file,
Packit 90a5c9
                                             function);
Packit 90a5c9
}
Packit 90a5c9
static const char *register_output_filter(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                       const char* filter, const char *file, const char *function)
Packit 90a5c9
{
Packit 90a5c9
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
Packit 90a5c9
                                                NOT_IN_HTACCESS);
Packit 90a5c9
    if (err) {
Packit 90a5c9
        return err;
Packit 90a5c9
    }
Packit 90a5c9
    if (!function) function = "handle";
Packit 90a5c9
    return register_filter_function_hook(filter, cmd, _cfg, file,
Packit 90a5c9
                                             function, AP_LUA_FILTER_OUTPUT);
Packit 90a5c9
}
Packit 90a5c9
static const char *register_input_filter(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                       const char* filter, const char *file, const char *function)
Packit 90a5c9
{
Packit 90a5c9
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
Packit 90a5c9
                                                NOT_IN_HTACCESS);
Packit 90a5c9
    if (err) {
Packit 90a5c9
        return err;
Packit 90a5c9
    }
Packit 90a5c9
    if (!function) function = "handle";
Packit 90a5c9
    return register_filter_function_hook(filter, cmd, _cfg, file,
Packit 90a5c9
                                             function, AP_LUA_FILTER_INPUT);
Packit 90a5c9
}
Packit 90a5c9
static const char *register_quick_block(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                        const char *line)
Packit 90a5c9
{
Packit 90a5c9
    return register_named_block_function_hook("quick", cmd, _cfg,
Packit 90a5c9
                                              line);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static const char *register_package_helper(cmd_parms *cmd, 
Packit 90a5c9
                                           const char *arg,
Packit 90a5c9
                                           apr_array_header_t *dir_array)
Packit 90a5c9
{
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    ap_lua_server_cfg *server_cfg =
Packit 90a5c9
        ap_get_module_config(cmd->server->module_config, &lua_module);
Packit 90a5c9
Packit 90a5c9
    char *fixed_filename;
Packit 90a5c9
    rv = apr_filepath_merge(&fixed_filename, 
Packit 90a5c9
                            server_cfg->root_path, 
Packit 90a5c9
                            arg,
Packit 90a5c9
                            APR_FILEPATH_NOTRELATIVE, 
Packit 90a5c9
                            cmd->pool);
Packit 90a5c9
Packit 90a5c9
    if (rv != APR_SUCCESS) {
Packit 90a5c9
        return apr_psprintf(cmd->pool,
Packit 90a5c9
                            "Unable to build full path to file, %s", arg);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    *(const char **) apr_array_push(dir_array) = fixed_filename;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Called for config directive which looks like
Packit 90a5c9
 * LuaPackagePath /lua/package/path/mapped/thing/like/this/?.lua
Packit 90a5c9
 */
Packit 90a5c9
static const char *register_package_dir(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                        const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
Packit 90a5c9
Packit 90a5c9
    return register_package_helper(cmd, arg, cfg->package_paths);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Called for config directive which looks like
Packit 90a5c9
 * LuaPackageCPath /lua/package/path/mapped/thing/like/this/?.so
Packit 90a5c9
 */
Packit 90a5c9
static const char *register_package_cdir(cmd_parms *cmd, 
Packit 90a5c9
                                         void *_cfg,
Packit 90a5c9
                                         const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
Packit 90a5c9
Packit 90a5c9
    return register_package_helper(cmd, arg, cfg->package_cpaths);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *register_lua_inherit(cmd_parms *cmd, 
Packit 90a5c9
                                      void *_cfg,
Packit 90a5c9
                                      const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
Packit 90a5c9
    
Packit 90a5c9
    if (strcasecmp("none", arg) == 0) {
Packit 90a5c9
        cfg->inherit = AP_LUA_INHERIT_NONE;
Packit 90a5c9
    }
Packit 90a5c9
    else if (strcasecmp("parent-first", arg) == 0) {
Packit 90a5c9
        cfg->inherit = AP_LUA_INHERIT_PARENT_FIRST;
Packit 90a5c9
    }
Packit 90a5c9
    else if (strcasecmp("parent-last", arg) == 0) {
Packit 90a5c9
        cfg->inherit = AP_LUA_INHERIT_PARENT_LAST;
Packit 90a5c9
    }
Packit 90a5c9
    else { 
Packit 90a5c9
        return apr_psprintf(cmd->pool,
Packit 90a5c9
                            "LuaInherit type of '%s' not recognized, valid "
Packit 90a5c9
                            "options are 'none', 'parent-first', and 'parent-last'", 
Packit 90a5c9
                            arg);
Packit 90a5c9
    }
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
static const char *register_lua_codecache(cmd_parms *cmd, 
Packit 90a5c9
                                      void *_cfg,
Packit 90a5c9
                                      const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
Packit 90a5c9
    
Packit 90a5c9
    if (strcasecmp("never", arg) == 0) {
Packit 90a5c9
        cfg->codecache = AP_LUA_CACHE_NEVER;
Packit 90a5c9
    }
Packit 90a5c9
    else if (strcasecmp("stat", arg) == 0) {
Packit 90a5c9
        cfg->codecache = AP_LUA_CACHE_STAT;
Packit 90a5c9
    }
Packit 90a5c9
    else if (strcasecmp("forever", arg) == 0) {
Packit 90a5c9
        cfg->codecache = AP_LUA_CACHE_FOREVER;
Packit 90a5c9
    }
Packit 90a5c9
    else { 
Packit 90a5c9
        return apr_psprintf(cmd->pool,
Packit 90a5c9
                            "LuaCodeCache type of '%s' not recognized, valid "
Packit 90a5c9
                            "options are 'never', 'stat', and 'forever'", 
Packit 90a5c9
                            arg);
Packit 90a5c9
    }
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
static const char *register_lua_scope(cmd_parms *cmd, 
Packit 90a5c9
                                      void *_cfg,
Packit 90a5c9
                                      const char *scope, 
Packit 90a5c9
                                      const char *min,
Packit 90a5c9
                                      const char *max)
Packit 90a5c9
{
Packit 90a5c9
    ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
Packit 90a5c9
    if (strcmp("once", scope) == 0) {
Packit 90a5c9
        cfg->vm_scope = AP_LUA_SCOPE_ONCE;
Packit 90a5c9
    }
Packit 90a5c9
    else if (strcmp("request", scope) == 0) {
Packit 90a5c9
        cfg->vm_scope = AP_LUA_SCOPE_REQUEST;
Packit 90a5c9
    }
Packit 90a5c9
    else if (strcmp("conn", scope) == 0) {
Packit 90a5c9
        cfg->vm_scope = AP_LUA_SCOPE_CONN;
Packit 90a5c9
    }
Packit 90a5c9
    else if (strcmp("thread", scope) == 0) {
Packit 90a5c9
#if !APR_HAS_THREADS
Packit 90a5c9
        return apr_psprintf(cmd->pool,
Packit 90a5c9
                            "Scope type of '%s' cannot be used because this "
Packit 90a5c9
                            "server does not have threading support "
Packit 90a5c9
                            "(APR_HAS_THREADS)" 
Packit 90a5c9
                            scope);
Packit 90a5c9
#endif
Packit 90a5c9
        cfg->vm_scope = AP_LUA_SCOPE_THREAD;
Packit 90a5c9
    }
Packit 90a5c9
    else if (strcmp("server", scope) == 0) {
Packit 90a5c9
        unsigned int vmin, vmax;
Packit 90a5c9
#if !APR_HAS_THREADS
Packit 90a5c9
        return apr_psprintf(cmd->pool,
Packit 90a5c9
                            "Scope type of '%s' cannot be used because this "
Packit 90a5c9
                            "server does not have threading support "
Packit 90a5c9
                            "(APR_HAS_THREADS)" 
Packit 90a5c9
                            scope);
Packit 90a5c9
#endif
Packit 90a5c9
        cfg->vm_scope = AP_LUA_SCOPE_SERVER;
Packit 90a5c9
        vmin = min ? atoi(min) : 1;
Packit 90a5c9
        vmax = max ? atoi(max) : 1;
Packit 90a5c9
        if (vmin == 0) {
Packit 90a5c9
            vmin = 1;
Packit 90a5c9
        }
Packit 90a5c9
        if (vmax < vmin) {
Packit 90a5c9
            vmax = vmin;
Packit 90a5c9
        }
Packit 90a5c9
        cfg->vm_min = vmin;
Packit 90a5c9
        cfg->vm_max = vmax;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        return apr_psprintf(cmd->pool,
Packit 90a5c9
                            "Invalid value for LuaScope, '%s', acceptable "
Packit 90a5c9
                            "values are: 'once', 'request', 'conn'"
Packit 90a5c9
#if APR_HAS_THREADS
Packit 90a5c9
                            ", 'thread', 'server'"
Packit 90a5c9
#endif
Packit 90a5c9
                            ,scope);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static const char *register_lua_root(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                     const char *root)
Packit 90a5c9
{
Packit 90a5c9
    /* ap_lua_dir_cfg* cfg = (ap_lua_dir_cfg*)_cfg; */
Packit 90a5c9
    ap_lua_server_cfg *cfg = ap_get_module_config(cmd->server->module_config,
Packit 90a5c9
                                                  &lua_module);
Packit 90a5c9
Packit 90a5c9
    cfg->root_path = root;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
const char *ap_lua_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c,
Packit 90a5c9
                           request_rec *r, const char *var)
Packit 90a5c9
{
Packit 90a5c9
    if (lua_ssl_val) { 
Packit 90a5c9
        return (const char *)lua_ssl_val(p, s, c, r, (char *)var);
Packit 90a5c9
    }
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
int ap_lua_ssl_is_https(conn_rec *c)
Packit 90a5c9
{
Packit 90a5c9
    return lua_ssl_is_https ? lua_ssl_is_https(c) : 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*******************************/
Packit 90a5c9
Packit 90a5c9
static const char *lua_authz_parse(cmd_parms *cmd, const char *require_line,
Packit 90a5c9
                                   const void **parsed_require_line)
Packit 90a5c9
{
Packit 90a5c9
    const char *provider_name;
Packit 90a5c9
    lua_authz_provider_spec *spec;
Packit 90a5c9
    lua_authz_provider_func *func = apr_pcalloc(cmd->pool, sizeof(lua_authz_provider_func));
Packit 90a5c9
Packit 90a5c9
    apr_pool_userdata_get((void**)&provider_name, AUTHZ_PROVIDER_NAME_NOTE,
Packit 90a5c9
                          cmd->temp_pool);
Packit 90a5c9
    ap_assert(provider_name != NULL);
Packit 90a5c9
Packit 90a5c9
    spec = apr_hash_get(lua_authz_providers, provider_name, APR_HASH_KEY_STRING);
Packit 90a5c9
    ap_assert(spec != NULL);
Packit 90a5c9
    func->spec = spec;
Packit 90a5c9
Packit 90a5c9
    if (require_line && *require_line) {
Packit 90a5c9
        const char *arg;
Packit 90a5c9
        func->args = apr_array_make(cmd->pool, 2, sizeof(const char *));
Packit 90a5c9
        while ((arg = ap_getword_conf(cmd->pool, &require_line)) && *arg) {
Packit 90a5c9
            APR_ARRAY_PUSH(func->args, const char *) = arg;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    *parsed_require_line = func;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static authz_status lua_authz_check(request_rec *r, const char *require_line,
Packit 90a5c9
                                    const void *parsed_require_line)
Packit 90a5c9
{
Packit 90a5c9
    apr_pool_t *pool;
Packit 90a5c9
    ap_lua_vm_spec *spec;
Packit 90a5c9
    lua_State *L;
Packit 90a5c9
    ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config,
Packit 90a5c9
                                                         &lua_module);
Packit 90a5c9
    const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
Packit 90a5c9
                                                     &lua_module);
Packit 90a5c9
    const lua_authz_provider_func *prov_func = parsed_require_line;
Packit 90a5c9
    const lua_authz_provider_spec *prov_spec = prov_func->spec;
Packit 90a5c9
    int result;
Packit 90a5c9
    int nargs = 0;
Packit 90a5c9
Packit 90a5c9
    spec = create_vm_spec(&pool, r, cfg, server_cfg, prov_spec->file_name,
Packit 90a5c9
                          NULL, 0, prov_spec->function_name, "authz provider");
Packit 90a5c9
Packit 90a5c9
    L = ap_lua_get_lua_state(pool, spec, r);
Packit 90a5c9
    if (L == NULL) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02314)
Packit 90a5c9
                      "Unable to compile VM for authz provider %s", prov_spec->name);
Packit 90a5c9
        return AUTHZ_GENERAL_ERROR;
Packit 90a5c9
    }
Packit 90a5c9
    lua_getglobal(L, prov_spec->function_name);
Packit 90a5c9
    if (!lua_isfunction(L, -1)) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02319)
Packit 90a5c9
                      "Unable to find entry function '%s' in %s (not a valid function)",
Packit 90a5c9
                      prov_spec->function_name, prov_spec->file_name);
Packit 90a5c9
        ap_lua_release_state(L, spec, r);
Packit 90a5c9
        return AUTHZ_GENERAL_ERROR;
Packit 90a5c9
    }
Packit 90a5c9
    ap_lua_run_lua_request(L, r);
Packit 90a5c9
    if (prov_func->args) {
Packit 90a5c9
        int i;
Packit 90a5c9
        if (!lua_checkstack(L, prov_func->args->nelts)) {
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02315)
Packit 90a5c9
                          "Error: authz provider %s: too many arguments", prov_spec->name);
Packit 90a5c9
            ap_lua_release_state(L, spec, r);
Packit 90a5c9
            return AUTHZ_GENERAL_ERROR;
Packit 90a5c9
        }
Packit 90a5c9
        for (i = 0; i < prov_func->args->nelts; i++) {
Packit 90a5c9
            const char *arg = APR_ARRAY_IDX(prov_func->args, i, const char *);
Packit 90a5c9
            lua_pushstring(L, arg);
Packit 90a5c9
        }
Packit 90a5c9
        nargs = prov_func->args->nelts;
Packit 90a5c9
    }
Packit 90a5c9
    if (lua_pcall(L, 1 + nargs, 1, 0)) {
Packit 90a5c9
        const char *err = lua_tostring(L, -1);
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02316)
Packit 90a5c9
                      "Error executing authz provider %s: %s", prov_spec->name, err);
Packit 90a5c9
        ap_lua_release_state(L, spec, r);
Packit 90a5c9
        return AUTHZ_GENERAL_ERROR;
Packit 90a5c9
    }
Packit 90a5c9
    if (!lua_isnumber(L, -1)) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02317)
Packit 90a5c9
                      "Error: authz provider %s did not return integer", prov_spec->name);
Packit 90a5c9
        ap_lua_release_state(L, spec, r);
Packit 90a5c9
        return AUTHZ_GENERAL_ERROR;
Packit 90a5c9
    }
Packit 90a5c9
    result = lua_tointeger(L, -1);
Packit 90a5c9
    ap_lua_release_state(L, spec, r);
Packit 90a5c9
    switch (result) {
Packit 90a5c9
        case AUTHZ_DENIED:
Packit 90a5c9
        case AUTHZ_GRANTED:
Packit 90a5c9
        case AUTHZ_NEUTRAL:
Packit 90a5c9
        case AUTHZ_GENERAL_ERROR:
Packit 90a5c9
        case AUTHZ_DENIED_NO_USER:
Packit 90a5c9
            return result;
Packit 90a5c9
        default:
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02318)
Packit 90a5c9
                          "Error: authz provider %s: invalid return value %d",
Packit 90a5c9
                          prov_spec->name, result);
Packit 90a5c9
    }
Packit 90a5c9
    return AUTHZ_GENERAL_ERROR;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const authz_provider lua_authz_provider =
Packit 90a5c9
{
Packit 90a5c9
    &lua_authz_check,
Packit 90a5c9
    &lua_authz_parse,
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
static const char *register_authz_provider(cmd_parms *cmd, void *_cfg,
Packit 90a5c9
                                           const char *name, const char *file,
Packit 90a5c9
                                           const char *function)
Packit 90a5c9
{
Packit 90a5c9
    lua_authz_provider_spec *spec;
Packit 90a5c9
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
Packit 90a5c9
    if (err)
Packit 90a5c9
        return err;
Packit 90a5c9
Packit 90a5c9
    spec = apr_pcalloc(cmd->pool, sizeof(*spec));
Packit 90a5c9
    spec->name = name;
Packit 90a5c9
    spec->file_name = file;
Packit 90a5c9
    spec->function_name = function;
Packit 90a5c9
Packit 90a5c9
    apr_hash_set(lua_authz_providers, name, APR_HASH_KEY_STRING, spec);
Packit 90a5c9
    ap_register_auth_provider(cmd->pool, AUTHZ_PROVIDER_GROUP, name,
Packit 90a5c9
                              AUTHZ_PROVIDER_VERSION,
Packit 90a5c9
                              &lua_authz_provider,
Packit 90a5c9
                              AP_AUTH_INTERNAL_PER_CONF);
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
command_rec lua_commands[] = {
Packit 90a5c9
Packit 90a5c9
    AP_INIT_TAKE1("LuaRoot", register_lua_root, NULL, OR_ALL,
Packit 90a5c9
                  "Specify the base path for resolving relative paths for mod_lua directives"),
Packit 90a5c9
Packit 90a5c9
    AP_INIT_TAKE1("LuaPackagePath", register_package_dir, NULL, OR_ALL,
Packit 90a5c9
                  "Add a directory to lua's package.path"),
Packit 90a5c9
Packit 90a5c9
    AP_INIT_TAKE1("LuaPackageCPath", register_package_cdir, NULL, OR_ALL,
Packit 90a5c9
                  "Add a directory to lua's package.cpath"),
Packit 90a5c9
Packit 90a5c9
    AP_INIT_TAKE3("LuaAuthzProvider", register_authz_provider, NULL, RSRC_CONF|EXEC_ON_READ,
Packit 90a5c9
                  "Provide an authorization provider"),
Packit 90a5c9
Packit 90a5c9
    AP_INIT_TAKE23("LuaHookTranslateName", register_translate_name_hook, NULL,
Packit 90a5c9
                  OR_ALL,
Packit 90a5c9
                  "Provide a hook for the translate name phase of request processing"),
Packit 90a5c9
Packit 90a5c9
    AP_INIT_RAW_ARGS("
Packit 90a5c9
                     NULL,
Packit 90a5c9
                     EXEC_ON_READ | OR_ALL,
Packit 90a5c9
                     "Provide a hook for the translate name phase of request processing"),
Packit 90a5c9
Packit 90a5c9
    AP_INIT_TAKE2("LuaHookFixups", register_fixups_hook, NULL, OR_ALL,
Packit 90a5c9
                  "Provide a hook for the fixups phase of request processing"),
Packit 90a5c9
    AP_INIT_RAW_ARGS("
Packit 90a5c9
                     EXEC_ON_READ | OR_ALL,
Packit 90a5c9
                     "Provide a inline hook for the fixups phase of request processing"),
Packit 90a5c9
Packit 90a5c9
    /* todo: test */
Packit 90a5c9
    AP_INIT_TAKE2("LuaHookMapToStorage", register_map_to_storage_hook, NULL,
Packit 90a5c9
                  OR_ALL,
Packit 90a5c9
                  "Provide a hook for the map_to_storage phase of request processing"),
Packit 90a5c9
    AP_INIT_RAW_ARGS("
Packit 90a5c9
                     NULL,
Packit 90a5c9
                     EXEC_ON_READ | OR_ALL,
Packit 90a5c9
                     "Provide a hook for the map_to_storage phase of request processing"),
Packit 90a5c9
Packit 90a5c9
    /* todo: test */
Packit 90a5c9
    AP_INIT_TAKE23("LuaHookCheckUserID", register_check_user_id_hook, NULL,
Packit 90a5c9
                  OR_ALL,
Packit 90a5c9
                  "Provide a hook for the check_user_id phase of request processing"),
Packit 90a5c9
    AP_INIT_RAW_ARGS("
Packit 90a5c9
                     NULL,
Packit 90a5c9
                     EXEC_ON_READ | OR_ALL,
Packit 90a5c9
                     "Provide a hook for the check_user_id phase of request processing"),
Packit 90a5c9
Packit 90a5c9
    /* todo: test */
Packit 90a5c9
    AP_INIT_TAKE2("LuaHookTypeChecker", register_type_checker_hook, NULL,
Packit 90a5c9
                  OR_ALL,
Packit 90a5c9
                  "Provide a hook for the type_checker phase of request processing"),
Packit 90a5c9
    AP_INIT_RAW_ARGS("
Packit 90a5c9
                     EXEC_ON_READ | OR_ALL,
Packit 90a5c9
                     "Provide a hook for the type_checker phase of request processing"),
Packit 90a5c9
Packit 90a5c9
    /* todo: test */
Packit 90a5c9
    AP_INIT_TAKE23("LuaHookAccessChecker", register_access_checker_hook, NULL,
Packit 90a5c9
                  OR_ALL,
Packit 90a5c9
                  "Provide a hook for the access_checker phase of request processing"),
Packit 90a5c9
    AP_INIT_RAW_ARGS("
Packit 90a5c9
                     NULL,
Packit 90a5c9
                     EXEC_ON_READ | OR_ALL,
Packit 90a5c9
                     "Provide a hook for the access_checker phase of request processing"),
Packit 90a5c9
Packit 90a5c9
    /* todo: test */
Packit 90a5c9
    AP_INIT_TAKE23("LuaHookAuthChecker", register_auth_checker_hook, NULL,
Packit 90a5c9
                  OR_ALL,
Packit 90a5c9
                  "Provide a hook for the auth_checker phase of request processing"),
Packit 90a5c9
    AP_INIT_RAW_ARGS("
Packit 90a5c9
                     EXEC_ON_READ | OR_ALL,
Packit 90a5c9
                     "Provide a hook for the auth_checker phase of request processing"),
Packit 90a5c9
Packit 90a5c9
    /* todo: test */
Packit 90a5c9
    AP_INIT_TAKE2("LuaHookInsertFilter", register_insert_filter_hook, NULL,
Packit 90a5c9
                  OR_ALL,
Packit 90a5c9
                  "Provide a hook for the insert_filter phase of request processing"),
Packit 90a5c9
    
Packit 90a5c9
    AP_INIT_TAKE2("LuaHookLog", register_log_transaction_hook, NULL,
Packit 90a5c9
                  OR_ALL,
Packit 90a5c9
                  "Provide a hook for the logging phase of request processing"),
Packit 90a5c9
Packit 90a5c9
    AP_INIT_TAKE123("LuaScope", register_lua_scope, NULL, OR_ALL,
Packit 90a5c9
                    "One of once, request, conn, server -- default is once"),
Packit 90a5c9
Packit 90a5c9
    AP_INIT_TAKE1("LuaInherit", register_lua_inherit, NULL, OR_ALL,
Packit 90a5c9
     "Controls how Lua scripts in parent contexts are merged with the current " 
Packit 90a5c9
     " context: none|parent-last|parent-first (default: parent-first) "),
Packit 90a5c9
    
Packit 90a5c9
    AP_INIT_TAKE1("LuaCodeCache", register_lua_codecache, NULL, OR_ALL,
Packit 90a5c9
     "Controls the behavior of the in-memory code cache " 
Packit 90a5c9
     " context: stat|forever|never (default: stat) "),
Packit 90a5c9
Packit 90a5c9
    AP_INIT_TAKE2("LuaQuickHandler", register_quick_hook, NULL, OR_ALL,
Packit 90a5c9
                  "Provide a hook for the quick handler of request processing"),
Packit 90a5c9
    AP_INIT_RAW_ARGS("
Packit 90a5c9
                     EXEC_ON_READ | OR_ALL,
Packit 90a5c9
                     "Provide a hook for the quick handler of request processing"),
Packit 90a5c9
    AP_INIT_RAW_ARGS("Lua_____ByteCodeHack", hack_section_handler, NULL,
Packit 90a5c9
                     OR_ALL,
Packit 90a5c9
                     "(internal) Byte code handler"),
Packit 90a5c9
    AP_INIT_TAKE23("LuaMapHandler", register_map_handler, NULL, OR_ALL,
Packit 90a5c9
                  "Maps a path to a lua handler"),
Packit 90a5c9
    AP_INIT_TAKE3("LuaOutputFilter", register_output_filter, NULL, OR_ALL,
Packit 90a5c9
                  "Registers a Lua function as an output filter"),
Packit 90a5c9
    AP_INIT_TAKE3("LuaInputFilter", register_input_filter, NULL, OR_ALL,
Packit 90a5c9
                  "Registers a Lua function as an input filter"),
Packit 90a5c9
    {NULL}
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static void *create_dir_config(apr_pool_t *p, char *dir)
Packit 90a5c9
{
Packit 90a5c9
    ap_lua_dir_cfg *cfg = apr_pcalloc(p, sizeof(ap_lua_dir_cfg));
Packit 90a5c9
    cfg->package_paths = apr_array_make(p, 2, sizeof(char *));
Packit 90a5c9
    cfg->package_cpaths = apr_array_make(p, 2, sizeof(char *));
Packit 90a5c9
    cfg->mapped_handlers =
Packit 90a5c9
        apr_array_make(p, 1, sizeof(ap_lua_mapped_handler_spec *));
Packit 90a5c9
    cfg->mapped_filters =
Packit 90a5c9
        apr_array_make(p, 1, sizeof(ap_lua_filter_handler_spec *));
Packit 90a5c9
    cfg->pool = p;
Packit 90a5c9
    cfg->hooks = apr_hash_make(p);
Packit 90a5c9
    cfg->dir = apr_pstrdup(p, dir);
Packit 90a5c9
    cfg->vm_scope = AP_LUA_SCOPE_UNSET;
Packit 90a5c9
    cfg->codecache = AP_LUA_CACHE_UNSET;
Packit 90a5c9
    cfg->vm_min = 0;
Packit 90a5c9
    cfg->vm_max = 0;
Packit 90a5c9
    cfg->inherit = AP_LUA_INHERIT_UNSET;
Packit 90a5c9
Packit 90a5c9
    return cfg;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int create_request_config(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    ap_lua_request_cfg *cfg = apr_palloc(r->pool, sizeof(ap_lua_request_cfg));
Packit 90a5c9
    cfg->mapped_request_details = NULL;
Packit 90a5c9
    cfg->request_scoped_vms = apr_hash_make(r->pool);
Packit 90a5c9
    ap_set_module_config(r->request_config, &lua_module, cfg);
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void *create_server_config(apr_pool_t *p, server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
Packit 90a5c9
    ap_lua_server_cfg *cfg = apr_pcalloc(p, sizeof(ap_lua_server_cfg));
Packit 90a5c9
    cfg->root_path = NULL;
Packit 90a5c9
Packit 90a5c9
    return cfg;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int lua_request_hook(lua_State *L, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    ap_lua_push_request(L, r);
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int lua_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
Packit 90a5c9
                            apr_pool_t *ptemp)
Packit 90a5c9
{
Packit 90a5c9
    ap_mutex_register(pconf, "lua-ivm-shm", NULL, APR_LOCK_DEFAULT, 0);
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int lua_post_config(apr_pool_t *pconf, apr_pool_t *plog,
Packit 90a5c9
                             apr_pool_t *ptemp, server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
    apr_pool_t **pool;
Packit 90a5c9
    apr_status_t rs;
Packit 90a5c9
Packit 90a5c9
    lua_ssl_val = APR_RETRIEVE_OPTIONAL_FN(ssl_var_lookup);
Packit 90a5c9
    lua_ssl_is_https = APR_RETRIEVE_OPTIONAL_FN(ssl_is_https);
Packit 90a5c9
    
Packit 90a5c9
    if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG)
Packit 90a5c9
        return OK;
Packit 90a5c9
Packit 90a5c9
    /* Create ivm mutex */
Packit 90a5c9
    rs = ap_global_mutex_create(&lua_ivm_mutex, NULL, "lua-ivm-shm", NULL,
Packit 90a5c9
                            s, pconf, 0);
Packit 90a5c9
    if (APR_SUCCESS != rs) {
Packit 90a5c9
        return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Create shared memory space, anonymous first if possible. */
Packit 90a5c9
    rs = apr_shm_create(&lua_ivm_shm, sizeof pool, NULL, pconf);
Packit 90a5c9
    if (APR_STATUS_IS_ENOTIMPL(rs)) {
Packit 90a5c9
        /* Fall back to filename-based; nuke any left-over first. */
Packit 90a5c9
        lua_ivm_shmfile = ap_runtime_dir_relative(pconf, DEFAULT_LUA_SHMFILE);
Packit 90a5c9
Packit 90a5c9
        apr_shm_remove(lua_ivm_shmfile, pconf);
Packit 90a5c9
        
Packit 90a5c9
        rs = apr_shm_create(&lua_ivm_shm, sizeof pool, lua_ivm_shmfile, pconf);
Packit 90a5c9
    }
Packit 90a5c9
    if (rs != APR_SUCCESS) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_ERR, rs, s, APLOGNO(02665)
Packit 90a5c9
            "mod_lua: Failed to create shared memory segment on file %s",
Packit 90a5c9
                     lua_ivm_shmfile ? lua_ivm_shmfile : "(anonymous)");
Packit 90a5c9
        return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
    }
Packit 90a5c9
    pool = (apr_pool_t **)apr_shm_baseaddr_get(lua_ivm_shm);
Packit 90a5c9
    apr_pool_create(pool, pconf);
Packit 90a5c9
    apr_pool_cleanup_register(pconf, NULL, shm_cleanup_wrapper,
Packit 90a5c9
                          apr_pool_cleanup_null);
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
static void *overlay_hook_specs(apr_pool_t *p,
Packit 90a5c9
                                        const void *key,
Packit 90a5c9
                                        apr_ssize_t klen,
Packit 90a5c9
                                        const void *overlay_val,
Packit 90a5c9
                                        const void *base_val,
Packit 90a5c9
                                        const void *data)
Packit 90a5c9
{
Packit 90a5c9
    const apr_array_header_t *overlay_info = (const apr_array_header_t*)overlay_val;
Packit 90a5c9
    const apr_array_header_t *base_info = (const apr_array_header_t*)base_val;
Packit 90a5c9
    return apr_array_append(p, base_info, overlay_info);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void *merge_dir_config(apr_pool_t *p, void *basev, void *overridesv)
Packit 90a5c9
{
Packit 90a5c9
    ap_lua_dir_cfg *a, *base, *overrides;
Packit 90a5c9
Packit 90a5c9
    a         = (ap_lua_dir_cfg *)apr_pcalloc(p, sizeof(ap_lua_dir_cfg));
Packit 90a5c9
    base      = (ap_lua_dir_cfg*)basev;
Packit 90a5c9
    overrides = (ap_lua_dir_cfg*)overridesv;
Packit 90a5c9
Packit 90a5c9
    a->pool = overrides->pool;
Packit 90a5c9
    a->dir = apr_pstrdup(p, overrides->dir);
Packit 90a5c9
Packit 90a5c9
    a->vm_scope = (overrides->vm_scope == AP_LUA_SCOPE_UNSET) ? base->vm_scope: overrides->vm_scope;
Packit 90a5c9
    a->inherit = (overrides->inherit == AP_LUA_INHERIT_UNSET) ? base->inherit : overrides->inherit;
Packit 90a5c9
    a->codecache = (overrides->codecache == AP_LUA_CACHE_UNSET) ? base->codecache : overrides->codecache;
Packit 90a5c9
    
Packit 90a5c9
    a->vm_min = (overrides->vm_min == 0) ? base->vm_min : overrides->vm_min;
Packit 90a5c9
    a->vm_max = (overrides->vm_max == 0) ? base->vm_max : overrides->vm_max;
Packit 90a5c9
Packit 90a5c9
    if (a->inherit == AP_LUA_INHERIT_UNSET || a->inherit == AP_LUA_INHERIT_PARENT_FIRST) { 
Packit 90a5c9
        a->package_paths = apr_array_append(p, base->package_paths, overrides->package_paths);
Packit 90a5c9
        a->package_cpaths = apr_array_append(p, base->package_cpaths, overrides->package_cpaths);
Packit 90a5c9
        a->mapped_handlers = apr_array_append(p, base->mapped_handlers, overrides->mapped_handlers);
Packit 90a5c9
        a->mapped_filters = apr_array_append(p, base->mapped_filters, overrides->mapped_filters);
Packit 90a5c9
        a->hooks = apr_hash_merge(p, overrides->hooks, base->hooks, overlay_hook_specs, NULL);
Packit 90a5c9
    }
Packit 90a5c9
    else if (a->inherit == AP_LUA_INHERIT_PARENT_LAST) { 
Packit 90a5c9
        a->package_paths = apr_array_append(p, overrides->package_paths, base->package_paths);
Packit 90a5c9
        a->package_cpaths = apr_array_append(p, overrides->package_cpaths, base->package_cpaths);
Packit 90a5c9
        a->mapped_handlers = apr_array_append(p, overrides->mapped_handlers, base->mapped_handlers);
Packit 90a5c9
        a->mapped_filters = apr_array_append(p, overrides->mapped_filters, base->mapped_filters);
Packit 90a5c9
        a->hooks = apr_hash_merge(p, base->hooks, overrides->hooks, overlay_hook_specs, NULL);
Packit 90a5c9
    }
Packit 90a5c9
    else { 
Packit 90a5c9
        a->package_paths = overrides->package_paths;
Packit 90a5c9
        a->package_cpaths = overrides->package_cpaths;
Packit 90a5c9
        a->mapped_handlers= overrides->mapped_handlers;
Packit 90a5c9
        a->mapped_filters= overrides->mapped_filters;
Packit 90a5c9
        a->hooks= overrides->hooks;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return a;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void lua_register_hooks(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    /* ap_register_output_filter("luahood", luahood, NULL, AP_FTYPE_RESOURCE); */
Packit 90a5c9
    ap_hook_handler(lua_handler, NULL, NULL, APR_HOOK_MIDDLE);
Packit 90a5c9
    ap_hook_create_request(create_request_config, NULL, NULL,
Packit 90a5c9
                           APR_HOOK_MIDDLE);
Packit 90a5c9
Packit 90a5c9
    /* http_request.h hooks */
Packit 90a5c9
    ap_hook_translate_name(lua_translate_name_harness_first, NULL, NULL,
Packit 90a5c9
                           AP_LUA_HOOK_FIRST);
Packit 90a5c9
    ap_hook_translate_name(lua_translate_name_harness, NULL, NULL,
Packit 90a5c9
                           APR_HOOK_MIDDLE);
Packit 90a5c9
    ap_hook_translate_name(lua_translate_name_harness_last, NULL, NULL,
Packit 90a5c9
                           AP_LUA_HOOK_LAST);
Packit 90a5c9
Packit 90a5c9
    ap_hook_fixups(lua_fixup_harness, NULL, NULL, APR_HOOK_MIDDLE);
Packit 90a5c9
    ap_hook_map_to_storage(lua_map_to_storage_harness, NULL, NULL,
Packit 90a5c9
                           APR_HOOK_MIDDLE);
Packit 90a5c9
Packit 90a5c9
/*  XXX: Does not work :(  
Packit 90a5c9
 *  ap_hook_check_user_id(lua_check_user_id_harness_first, NULL, NULL,
Packit 90a5c9
                          AP_LUA_HOOK_FIRST);
Packit 90a5c9
 */
Packit 90a5c9
    ap_hook_check_user_id(lua_check_user_id_harness, NULL, NULL,
Packit 90a5c9
                           APR_HOOK_MIDDLE);
Packit 90a5c9
/*  XXX: Does not work :(
Packit 90a5c9
 * ap_hook_check_user_id(lua_check_user_id_harness_last, NULL, NULL,
Packit 90a5c9
                          AP_LUA_HOOK_LAST);
Packit 90a5c9
*/
Packit 90a5c9
    ap_hook_type_checker(lua_type_checker_harness, NULL, NULL,
Packit 90a5c9
                         APR_HOOK_MIDDLE);
Packit 90a5c9
Packit 90a5c9
    ap_hook_access_checker(lua_access_checker_harness_first, NULL, NULL,
Packit 90a5c9
                           AP_LUA_HOOK_FIRST);
Packit 90a5c9
    ap_hook_access_checker(lua_access_checker_harness, NULL, NULL,
Packit 90a5c9
                           APR_HOOK_MIDDLE);
Packit 90a5c9
    ap_hook_access_checker(lua_access_checker_harness_last, NULL, NULL,
Packit 90a5c9
                           AP_LUA_HOOK_LAST);
Packit 90a5c9
    ap_hook_auth_checker(lua_auth_checker_harness_first, NULL, NULL,
Packit 90a5c9
                         AP_LUA_HOOK_FIRST);
Packit 90a5c9
    ap_hook_auth_checker(lua_auth_checker_harness, NULL, NULL,
Packit 90a5c9
                         APR_HOOK_MIDDLE);
Packit 90a5c9
    ap_hook_auth_checker(lua_auth_checker_harness_last, NULL, NULL,
Packit 90a5c9
                         AP_LUA_HOOK_LAST);
Packit 90a5c9
Packit 90a5c9
    ap_hook_insert_filter(lua_insert_filter_harness, NULL, NULL,
Packit 90a5c9
                          APR_HOOK_MIDDLE);
Packit 90a5c9
    ap_hook_quick_handler(lua_quick_harness, NULL, NULL, APR_HOOK_FIRST);
Packit 90a5c9
Packit 90a5c9
    ap_hook_post_config(lua_post_config, NULL, NULL, APR_HOOK_MIDDLE);
Packit 90a5c9
    ap_hook_pre_config(lua_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
Packit 90a5c9
Packit 90a5c9
    APR_OPTIONAL_HOOK(ap_lua, lua_open, lua_open_hook, NULL, NULL,
Packit 90a5c9
                      APR_HOOK_REALLY_FIRST);
Packit 90a5c9
Packit 90a5c9
    APR_OPTIONAL_HOOK(ap_lua, lua_request, lua_request_hook, NULL, NULL,
Packit 90a5c9
                      APR_HOOK_REALLY_FIRST);
Packit 90a5c9
    ap_hook_handler(lua_map_handler, NULL, NULL, AP_LUA_HOOK_FIRST);
Packit 90a5c9
    
Packit 90a5c9
    /* Hook this right before FallbackResource kicks in */
Packit 90a5c9
    ap_hook_fixups(lua_map_handler_fixups, NULL, NULL, AP_LUA_HOOK_LAST-2);
Packit 90a5c9
#if APR_HAS_THREADS
Packit 90a5c9
    ap_hook_child_init(ap_lua_init_mutex, NULL, NULL, APR_HOOK_MIDDLE);
Packit 90a5c9
#endif
Packit 90a5c9
    /* providers */
Packit 90a5c9
    lua_authz_providers = apr_hash_make(p);
Packit 90a5c9
    
Packit 90a5c9
    /* Logging catcher */
Packit 90a5c9
    ap_hook_log_transaction(lua_log_transaction_harness,NULL,NULL,
Packit 90a5c9
                            APR_HOOK_FIRST);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_MODULE(lua) = {
Packit 90a5c9
    STANDARD20_MODULE_STUFF,
Packit 90a5c9
    create_dir_config,          /* create per-dir    config structures */
Packit 90a5c9
    merge_dir_config,           /* merge  per-dir    config structures */
Packit 90a5c9
    create_server_config,       /* create per-server config structures */
Packit 90a5c9
    NULL,                       /* merge  per-server config structures */
Packit 90a5c9
    lua_commands,               /* table of config file commands       */
Packit 90a5c9
    lua_register_hooks          /* register hooks                      */
Packit 90a5c9
};