mod_lua.c revision 001a44c352f89c9ec332ffd3e0a6927dcd19432c
842ae4bd224140319ae7feec1872b93dfd491143fielding/**
842ae4bd224140319ae7feec1872b93dfd491143fielding * Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * the License. You may obtain a copy of the License at
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz *
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * http://www.apache.org/licenses/LICENSE-2.0
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz *
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * Unless required by applicable law or agreed to in writing, software
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * distributed under the License is distributed on an "AS IS" BASIS,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * See the License for the specific language governing permissions and
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * limitations under the License.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "mod_lua.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include <string.h>
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include <stdlib.h>
2d728410bcf9e6e53698bb035f516e18efd76d8bnd#include <ctype.h>
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include <apr_thread_mutex.h>
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include <apr_pools.h>
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "lua_apr.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "lua_config.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "apr_optional.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "mod_ssl.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "mod_auth.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "util_mutex.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#ifdef APR_HAS_THREADS
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "apr_thread_proc.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#endif
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/* getpid for *NIX */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#if APR_HAVE_SYS_TYPES_H
806b50b4d37ade77f1c767a1593fac5a2d7786c8minfrin#include <sys/types.h>
c07866e582be4daac7f6a0a8754fa12fca044663fuankg#endif
07ed2f9227b98d2cac21a7121ef7ea5328fa3f98fuankg#if APR_HAVE_UNISTD_H
15ff8c621815e8337abc10638f2b2853ee6fd076minfrin#include <unistd.h>
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#endif
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/* getpid for Windows */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#if APR_HAVE_PROCESS_H
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include <process.h>
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#endif
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzAPR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ap_lua, AP_LUA, int, lua_open,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz (lua_State *L, apr_pool_t *p),
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz (L, p), OK, DECLINED)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzAPR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ap_lua, AP_LUA, int, lua_request,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz (lua_State *L, request_rec *r),
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz (L, r), OK, DECLINED)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *lua_ssl_val = NULL;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic APR_OPTIONAL_FN_TYPE(ssl_is_https) *lua_ssl_is_https = NULL;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzmodule AP_MODULE_DECLARE_DATA lua_module;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#define AP_LUA_HOOK_FIRST (APR_HOOK_FIRST - 1)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#define AP_LUA_HOOK_LAST (APR_HOOK_LAST + 1)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantztypedef struct {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *name;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *file_name;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *function_name;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_lua_vm_spec *spec;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_array_header_t *args;
15351ab1a34130c1f880daef4e5dc15407a75969colm} lua_authz_provider_spec;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzapr_hash_t *lua_authz_providers;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndtypedef struct
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_bucket_brigade *tmpBucket;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd lua_State *L;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_lua_vm_spec *spec;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd int broken;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd} lua_filter_ctx;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzapr_global_mutex_t *lua_ivm_mutex;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzapr_shm_t *lua_ivm_shm;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndchar *lua_ivm_shmfile;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic apr_status_t shm_cleanup_wrapper(void *unused) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (lua_ivm_shm) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return apr_shm_destroy(lua_ivm_shm);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return OK;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd}
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/**
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * error reporting if lua has an error.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * Extracts the error from lua stack and prints
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic void report_lua_error(lua_State *L, request_rec *r)
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd{
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd const char *lua_response;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd r->status = HTTP_INTERNAL_SERVER_ERROR;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd r->content_type = "text/html";
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_rputs("<b>Error!</b>\n", r);
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz ap_rputs("<p>", r);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin lua_response = lua_tostring(L, -1);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ap_rputs(lua_response, r);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_rputs("</p>\n", r);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, r->pool, APLOGNO(01471) "Lua error: %s",
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd lua_response);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin}
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic void lua_open_callback(lua_State *L, apr_pool_t *p, void *ctx)
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd{
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_lua_init(L, p);
2d728410bcf9e6e53698bb035f516e18efd76d8bnd ap_lua_load_apache2_lmodule(L);
2d728410bcf9e6e53698bb035f516e18efd76d8bnd ap_lua_load_request_lmodule(L, p);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_lua_load_config_lmodule(L);
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin}
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrinstatic int lua_open_hook(lua_State *L, apr_pool_t *p)
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin{
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin lua_open_callback(L, p, NULL);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return OK;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin}
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrinstatic const char *scope_to_string(unsigned int scope)
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin{
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin switch (scope) {
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin case AP_LUA_SCOPE_ONCE:
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin case AP_LUA_SCOPE_UNSET:
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin return "once";
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin case AP_LUA_SCOPE_REQUEST:
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin return "request";
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd case AP_LUA_SCOPE_CONN:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return "conn";
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#if APR_HAS_THREADS
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case AP_LUA_SCOPE_THREAD:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return "thread";
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case AP_LUA_SCOPE_SERVER:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return "server";
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#endif
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz default:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_assert(0);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
a51acf58d9d82f52e0ee56106cd9282030f3f3bend}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic void ap_lua_release_state(lua_State* L, ap_lua_vm_spec* spec, request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz char *hash;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_reslist_t* reslist = NULL;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (spec->scope == AP_LUA_SCOPE_SERVER) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_lua_server_spec* sspec = NULL;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_settop(L, 0);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_getfield(L, LUA_REGISTRYINDEX, "Apache2.Lua.server_spec");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz sspec = (ap_lua_server_spec*) lua_touserdata(L, 1);
a51acf58d9d82f52e0ee56106cd9282030f3f3bend hash = apr_psprintf(r->pool, "reslist:%s", spec->file);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (apr_pool_userdata_get((void **)&reslist, hash,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz r->server->process->pool) == APR_SUCCESS) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz AP_DEBUG_ASSERT(sspec != NULL);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (reslist != NULL) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_reslist_release(reslist, sspec);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic ap_lua_vm_spec *create_vm_spec(apr_pool_t **lifecycle_pool,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz request_rec *r,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const ap_lua_dir_cfg *cfg,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const ap_lua_server_cfg *server_cfg,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *filename,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *bytecode,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_size_t bytecode_len,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *function,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *what)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_pool_t *pool;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_lua_vm_spec *spec = apr_pcalloc(r->pool, sizeof(ap_lua_vm_spec));
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz spec->scope = cfg->vm_scope;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd spec->pool = r->pool;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd spec->package_paths = cfg->package_paths;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz spec->package_cpaths = cfg->package_cpaths;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz spec->cb = &lua_open_callback;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz spec->cb_arg = NULL;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz spec->bytecode = bytecode;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz spec->bytecode_len = bytecode_len;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz spec->codecache = (cfg->codecache == AP_LUA_CACHE_UNSET) ? AP_LUA_CACHE_STAT : cfg->codecache;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin spec->vm_min = cfg->vm_min ? cfg->vm_min : 1;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin spec->vm_max = cfg->vm_max ? cfg->vm_max : 1;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (filename) {
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin char *file;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin apr_filepath_merge(&file, server_cfg->root_path,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz filename, APR_FILEPATH_NOTRELATIVE, r->pool);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz spec->file = file;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin else {
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin spec->file = r->filename;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r, APLOGNO(02313)
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin "%s details: scope: %s, file: %s, func: %s",
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin what, scope_to_string(spec->scope), spec->file,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz function ? function : "-");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz switch (spec->scope) {
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin case AP_LUA_SCOPE_ONCE:
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin case AP_LUA_SCOPE_UNSET:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_pool_create(&pool, r->pool);
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case AP_LUA_SCOPE_REQUEST:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz pool = r->pool;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case AP_LUA_SCOPE_CONN:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz pool = r->connection->pool;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#if APR_HAS_THREADS
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case AP_LUA_SCOPE_THREAD:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz pool = apr_thread_pool_get(r->connection->current_thread);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case AP_LUA_SCOPE_SERVER:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz pool = r->server->process->pool;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#endif
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz default:
a51acf58d9d82f52e0ee56106cd9282030f3f3bend ap_assert(0);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin *lifecycle_pool = pool;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return spec;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin}
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrinstatic const char* ap_lua_interpolate_string(apr_pool_t* pool, const char* string, const char** values)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin{
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin char *stringBetween;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin const char* ret;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin int srclen,x,y;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin srclen = strlen(string);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ret = "";
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin y = 0;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin for (x=0; x < srclen; x++) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (string[x] == '$' && x != srclen-1 && string[x+1] >= '0' && string[x+1] <= '9') {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin int v = *(string+x+1) - '0';
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (x-y > 0) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin stringBetween = apr_pstrndup(pool, string+y, x-y);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin stringBetween = "";
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ret = apr_pstrcat(pool, ret, stringBetween, values[v], NULL);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz y = ++x+1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin }
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin if (x-y > 0 && y > 0) {
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin stringBetween = apr_pstrndup(pool, string+y, x-y);
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin ret = apr_pstrcat(pool, ret, stringBetween, NULL);
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin }
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin /* If no replacement was made, just return the original string */
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin else if (y == 0) {
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin return string;
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return ret;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/**
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin * "main"
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int lua_handler(request_rec *r)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin{
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin int rc = OK;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (strcmp(r->handler, "lua-script")) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return DECLINED;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin /* Decline the request if the script does not exist (or is a directory),
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin * rather than just returning internal server error */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin (r->finfo.filetype == APR_NOFILE)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin || (r->finfo.filetype & APR_DIR)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return DECLINED;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, APLOGNO(01472)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin "handling [%s] in mod_lua", r->filename);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin /* XXX: This seems wrong because it may generate wrong headers for HEAD requests */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (!r->header_only) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin lua_State *L;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_pool_t *pool;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin &lua_module);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_lua_vm_spec *spec = create_vm_spec(&pool, r, cfg, NULL, NULL, NULL,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin 0, "handle", "request handler");
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin L = ap_lua_get_lua_state(pool, spec, r);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (!L) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin /* TODO annotate spec with failure reason */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin r->status = HTTP_INTERNAL_SERVER_ERROR;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_rputs("Unable to compile VM, see logs", r);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_lua_release_state(L, spec, r);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return HTTP_INTERNAL_SERVER_ERROR;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r, APLOGNO(01474) "got a vm!");
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd lua_getglobal(L, "handle");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (!lua_isfunction(L, -1)) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01475)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin "lua: Unable to find function %s in %s",
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin "handle",
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin spec->file);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_lua_release_state(L, spec, r);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return HTTP_INTERNAL_SERVER_ERROR;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_lua_run_lua_request(L, r);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (lua_pcall(L, 1, 1, 0)) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin report_lua_error(L, r);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (lua_isnumber(L, -1)) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin rc = lua_tointeger(L, -1);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_lua_release_state(L, spec, r);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return rc;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin}
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd/* ------------------- Input/output content filters ------------------- */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrinstatic apr_status_t lua_setup_filter_ctx(ap_filter_t* f, request_rec* r, lua_filter_ctx** c) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_pool_t *pool;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_lua_vm_spec *spec;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin int n, rc;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin lua_State *L;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin lua_filter_ctx *ctx;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin &lua_module);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin &lua_module);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ctx = apr_pcalloc(r->pool, sizeof(lua_filter_ctx));
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ctx->broken = 0;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd *c = ctx;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd /* Find the filter that was called.
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * XXX: If we were wired with mod_filter, the filter (mod_filters name)
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * and the provider (our underlying filters name) need to have matched.
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd for (n = 0; n < cfg->mapped_filters->nelts; n++) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_lua_filter_handler_spec *hook_spec =
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ((ap_lua_filter_handler_spec **) cfg->mapped_filters->elts)[n];
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (hook_spec == NULL) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz continue;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (!strcasecmp(hook_spec->filter_name, f->frec->name)) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz spec = create_vm_spec(&pool, r, cfg, server_cfg,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin hook_spec->file_name,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin NULL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz 0,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd hook_spec->function_name,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd "filter");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz L = ap_lua_get_lua_state(pool, spec, r);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (L) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd L = lua_newthread(L);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (!L) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02328)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin "lua: Failed to obtain lua interpreter for %s %s",
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin hook_spec->function_name, hook_spec->file_name);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_lua_release_state(L, spec, r);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return APR_EGENERAL;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (hook_spec->function_name != NULL) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin lua_getglobal(L, hook_spec->function_name);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (!lua_isfunction(L, -1)) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02329)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin "lua: Unable to find function %s in %s",
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin hook_spec->function_name,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin hook_spec->file_name);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_lua_release_state(L, spec, r);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return APR_EGENERAL;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_lua_run_lua_request(L, r);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz else {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd int t;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_lua_run_lua_request(L, r);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd t = lua_gettop(L);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd lua_setglobal(L, "r");
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd lua_settop(L, t);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ctx->L = L;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ctx->spec = spec;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin /* If a Lua filter is interested in filtering a request, it must first do a yield,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin * otherwise we'll assume that it's not interested and pretend we didn't find it.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz rc = lua_resume(L, 1);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (rc == LUA_YIELD) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (f->frec->providers == NULL) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* Not wired by mod_filter */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_table_unset(r->headers_out, "Content-Length");
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_table_unset(r->headers_out, "Content-MD5");
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_table_unset(r->headers_out, "ETAG");
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return OK;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin else {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ap_lua_release_state(L, spec, r);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return APR_ENOENT;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return APR_ENOENT;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin}
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrinstatic apr_status_t lua_output_filter_handle(ap_filter_t *f, apr_bucket_brigade *pbbIn) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin request_rec *r = f->r;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin int rc;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin lua_State *L;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin lua_filter_ctx* ctx;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin conn_rec *c = r->connection;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_bucket *pbktIn;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_status_t rv;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* Set up the initial filter context and acquire the function.
5876f43a746f688a32b7201bced8591ddf19bd43minfrin * The corresponding Lua function should yield here.
5876f43a746f688a32b7201bced8591ddf19bd43minfrin */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (!f->ctx) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin rc = lua_setup_filter_ctx(f,r,&ctx);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (rc == APR_EGENERAL) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return HTTP_INTERNAL_SERVER_ERROR;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (rc == APR_ENOENT) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* No filter entry found (or the script declined to filter), just pass on the buckets */
c840604b10dc1d368a2da321ec51362373d502a3minfrin ap_remove_output_filter(f);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return ap_pass_brigade(f->next,pbbIn);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin else {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* We've got a willing lua filter, setup and check for a prefix */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin size_t olen;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_bucket *pbktOut;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin const char* output = lua_tolstring(ctx->L, 1, &olen);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin f->ctx = ctx;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ctx->tmpBucket = apr_brigade_create(r->pool, c->bucket_alloc);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (olen > 0) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin pbktOut = apr_bucket_heap_create(output, olen, NULL, c->bucket_alloc);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktOut);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin rv = ap_pass_brigade(f->next, ctx->tmpBucket);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_brigade_cleanup(ctx->tmpBucket);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (rv != APR_SUCCESS) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return rv;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ctx = (lua_filter_ctx*) f->ctx;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin L = ctx->L;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* While the Lua function is still yielding, pass in buckets to the coroutine */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (!ctx->broken) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin for (pbktIn = APR_BRIGADE_FIRST(pbbIn);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin pbktIn != APR_BRIGADE_SENTINEL(pbbIn);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin pbktIn = APR_BUCKET_NEXT(pbktIn))
5876f43a746f688a32b7201bced8591ddf19bd43minfrin {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin const char *data;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_size_t len;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_bucket *pbktOut;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* read the bucket */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_bucket_read(pbktIn,&data,&len,APR_BLOCK_READ);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* Push the bucket onto the Lua stack as a global var */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin lua_pushlstring(L, data, len);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin lua_setglobal(L, "bucket");
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* If Lua yielded, it means we have something to pass on */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (lua_resume(L, 0) == LUA_YIELD) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin size_t olen;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin const char* output = lua_tolstring(L, 1, &olen);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (olen > 0) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin pbktOut = apr_bucket_heap_create(output, olen, NULL,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin c->bucket_alloc);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktOut);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin rv = ap_pass_brigade(f->next, ctx->tmpBucket);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_brigade_cleanup(ctx->tmpBucket);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (rv != APR_SUCCESS) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return rv;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin else {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ctx->broken = 1;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ap_lua_release_state(L, ctx->spec, r);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ap_remove_output_filter(f);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_brigade_cleanup(pbbIn);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_brigade_cleanup(ctx->tmpBucket);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return HTTP_INTERNAL_SERVER_ERROR;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* If we've safely reached the end, do a final call to Lua to allow for any
5876f43a746f688a32b7201bced8591ddf19bd43minfrin finishing moves by the script, such as appending a tail. */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(pbbIn))) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_bucket *pbktEOS;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin lua_pushnil(L);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin lua_setglobal(L, "bucket");
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (lua_resume(L, 0) == LUA_YIELD) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_bucket *pbktOut;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin size_t olen;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin const char* output = lua_tolstring(L, 1, &olen);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (olen > 0) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin pbktOut = apr_bucket_heap_create(output, olen, NULL,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin c->bucket_alloc);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktOut);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin pbktEOS = apr_bucket_eos_create(c->bucket_alloc);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktEOS);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ap_lua_release_state(L, ctx->spec, r);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin rv = ap_pass_brigade(f->next, ctx->tmpBucket);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_brigade_cleanup(ctx->tmpBucket);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (rv != APR_SUCCESS) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return rv;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* Clean up */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_brigade_cleanup(pbbIn);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return APR_SUCCESS;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin}
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrinstatic apr_status_t lua_input_filter_handle(ap_filter_t *f,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_bucket_brigade *pbbOut,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ap_input_mode_t eMode,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_read_type_e eBlock,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_off_t nBytes)
5876f43a746f688a32b7201bced8591ddf19bd43minfrin{
5876f43a746f688a32b7201bced8591ddf19bd43minfrin request_rec *r = f->r;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin int rc, lastCall = 0;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin lua_State *L;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin lua_filter_ctx* ctx;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin conn_rec *c = r->connection;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_status_t ret;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* Set up the initial filter context and acquire the function.
5876f43a746f688a32b7201bced8591ddf19bd43minfrin * The corresponding Lua function should yield here.
5876f43a746f688a32b7201bced8591ddf19bd43minfrin */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (!f->ctx) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin rc = lua_setup_filter_ctx(f,r,&ctx);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin f->ctx = ctx;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (rc == APR_EGENERAL) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ctx->broken = 1;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ap_remove_input_filter(f);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return HTTP_INTERNAL_SERVER_ERROR;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (rc == APR_ENOENT ) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ap_remove_input_filter(f);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ctx->broken = 1;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (rc == APR_SUCCESS) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ctx->tmpBucket = apr_brigade_create(r->pool, c->bucket_alloc);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ctx = (lua_filter_ctx*) f->ctx;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin L = ctx->L;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* If the Lua script broke or denied serving the request, just pass the buckets through */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (ctx->broken) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return ap_get_brigade(f->next, pbbOut, eMode, eBlock, nBytes);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (APR_BRIGADE_EMPTY(ctx->tmpBucket)) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ret = ap_get_brigade(f->next, ctx->tmpBucket, eMode, eBlock, nBytes);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (eMode == AP_MODE_EATCRLF || ret != APR_SUCCESS)
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return ret;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* While the Lua function is still yielding, pass buckets to the coroutine */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (!ctx->broken) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin lastCall = 0;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin while(!APR_BRIGADE_EMPTY(ctx->tmpBucket)) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_bucket *pbktIn = APR_BRIGADE_FIRST(ctx->tmpBucket);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_bucket *pbktOut;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin const char *data;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_size_t len;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (APR_BUCKET_IS_EOS(pbktIn)) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz APR_BUCKET_REMOVE(pbktIn);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd /* read the bucket */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ret = apr_bucket_read(pbktIn, &data, &len, eBlock);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (ret != APR_SUCCESS)
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return ret;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd /* Push the bucket onto the Lua stack as a global var */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd lastCall++;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd lua_pushlstring(L, data, len);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin lua_setglobal(L, "bucket");
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd /* If Lua yielded, it means we have something to pass on */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (lua_resume(L, 0) == LUA_YIELD) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd size_t olen;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd const char* output = lua_tolstring(L, 1, &olen);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz pbktOut = apr_bucket_heap_create(output, olen, 0, c->bucket_alloc);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd APR_BRIGADE_INSERT_TAIL(pbbOut, pbktOut);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_bucket_delete(pbktIn);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return APR_SUCCESS;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd else {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ctx->broken = 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_lua_release_state(L, ctx->spec, r);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_remove_input_filter(f);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_bucket_delete(pbktIn);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return HTTP_INTERNAL_SERVER_ERROR;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
0ecc05d02d9ee3d110cb9e263080aaa2b77a12f9jim /* If we've safely reached the end, do a final call to Lua to allow for any
07d000c26f08bb7d590da25057748b28b8b59741jim finishing moves by the script, such as appending a tail. */
07d000c26f08bb7d590da25057748b28b8b59741jim if (lastCall == 0) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_bucket *pbktEOS = apr_bucket_eos_create(c->bucket_alloc);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd lua_pushnil(L);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd lua_setglobal(L, "bucket");
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (lua_resume(L, 0) == LUA_YIELD) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_bucket *pbktOut;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz size_t olen;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd const char* output = lua_tolstring(L, 1, &olen);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd pbktOut = apr_bucket_heap_create(output, olen, 0, c->bucket_alloc);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd APR_BRIGADE_INSERT_TAIL(pbbOut, pbktOut);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd APR_BRIGADE_INSERT_TAIL(pbbOut,pbktEOS);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_lua_release_state(L, ctx->spec, r);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return APR_SUCCESS;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd/* ---------------- Configury stuff --------------- */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd/** harnesses for magic hooks **/
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic int lua_request_rec_hook_harness(request_rec *r, const char *name, int apr_hook_when)
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd{
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd int rc;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_pool_t *pool;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd lua_State *L;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_lua_vm_spec *spec;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd &lua_module);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd &lua_module);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd const char *key = apr_psprintf(r->pool, "%s_%d", name, apr_hook_when);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, key,
6850c836b8c7c688ef8a8ec280e798e1d0bfbf01trawick APR_HASH_KEY_STRING);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (hook_specs) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd int i;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd for (i = 0; i < hook_specs->nelts; i++) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_lua_mapped_handler_spec *hook_spec =
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ((ap_lua_mapped_handler_spec **) hook_specs->elts)[i];
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (hook_spec == NULL) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd continue;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd spec = create_vm_spec(&pool, r, cfg, server_cfg,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd hook_spec->file_name,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd hook_spec->bytecode,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd hook_spec->bytecode_len,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd hook_spec->function_name,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd "request hook");
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd L = ap_lua_get_lua_state(pool, spec, r);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (!L) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01477)
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd "lua: Failed to obtain lua interpreter for %s %s",
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd hook_spec->function_name, hook_spec->file_name);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return HTTP_INTERNAL_SERVER_ERROR;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (hook_spec->function_name != NULL) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd lua_getglobal(L, hook_spec->function_name);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (!lua_isfunction(L, -1)) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01478)
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd "lua: Unable to find function %s in %s",
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd hook_spec->function_name,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd hook_spec->file_name);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_lua_release_state(L, spec, r);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return HTTP_INTERNAL_SERVER_ERROR;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_lua_run_lua_request(L, r);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd else {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd int t;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_lua_run_lua_request(L, r);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd t = lua_gettop(L);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd lua_setglobal(L, "r");
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd lua_settop(L, t);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (lua_pcall(L, 1, 1, 0)) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd report_lua_error(L, r);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_lua_release_state(L, spec, r);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return HTTP_INTERNAL_SERVER_ERROR;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd rc = DECLINED;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (lua_isnumber(L, -1)) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd rc = lua_tointeger(L, -1);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_log_rerror(APLOG_MARK, APLOG_TRACE4, 0, r, "Lua hook %s:%s for phase %s returned %d",
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd hook_spec->file_name, hook_spec->function_name, name, rc);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd else {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, "Lua hook %s:%s for phase %s did not return a numeric value",
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd hook_spec->file_name, hook_spec->function_name, name);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return HTTP_INTERNAL_SERVER_ERROR;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (rc != DECLINED) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_lua_release_state(L, spec, r);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return rc;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_lua_release_state(L, spec, r);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return DECLINED;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd}
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic int lua_map_handler(request_rec *r)
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd{
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd int rc, n = 0;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_pool_t *pool;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd lua_State *L;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd const char *filename, *function_name;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd const char *values[10];
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_lua_vm_spec *spec;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_regmatch_t match[10];
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz &lua_module);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz &lua_module);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz for (n = 0; n < cfg->mapped_handlers->nelts; n++) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_lua_mapped_handler_spec *hook_spec =
7585f3892ce3057554a5bef740302dd479b223b2nd ((ap_lua_mapped_handler_spec **) cfg->mapped_handlers->elts)[n];
13be69a729c912bea7d0aa22596b92717df92551pquerna
7585f3892ce3057554a5bef740302dd479b223b2nd if (hook_spec == NULL) {
7585f3892ce3057554a5bef740302dd479b223b2nd continue;
7585f3892ce3057554a5bef740302dd479b223b2nd }
7585f3892ce3057554a5bef740302dd479b223b2nd if (!ap_regexec(hook_spec->uri_pattern, r->uri, 10, match, 0)) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz int i;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz for (i=0 ; i < 10; i++) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (match[i].rm_eo >= 0) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd values[i] = apr_pstrndup(r->pool, r->uri+match[i].rm_so, match[i].rm_eo - match[i].rm_so);
e8f95a682820a599fe41b22977010636be5c2717jim }
85fa7868b2734d79c4e27b74d54b6846a1f9176fcolm else values[i] = "";
13be69a729c912bea7d0aa22596b92717df92551pquerna }
e8f95a682820a599fe41b22977010636be5c2717jim filename = ap_lua_interpolate_string(r->pool, hook_spec->file_name, values);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd function_name = ap_lua_interpolate_string(r->pool, hook_spec->function_name, values);
13be69a729c912bea7d0aa22596b92717df92551pquerna spec = create_vm_spec(&pool, r, cfg, server_cfg,
13be69a729c912bea7d0aa22596b92717df92551pquerna filename,
13be69a729c912bea7d0aa22596b92717df92551pquerna hook_spec->bytecode,
13be69a729c912bea7d0aa22596b92717df92551pquerna hook_spec->bytecode_len,
13be69a729c912bea7d0aa22596b92717df92551pquerna function_name,
13be69a729c912bea7d0aa22596b92717df92551pquerna "mapped handler");
13be69a729c912bea7d0aa22596b92717df92551pquerna L = ap_lua_get_lua_state(pool, spec, r);
13be69a729c912bea7d0aa22596b92717df92551pquerna
13be69a729c912bea7d0aa22596b92717df92551pquerna if (!L) {
13be69a729c912bea7d0aa22596b92717df92551pquerna ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02330)
13be69a729c912bea7d0aa22596b92717df92551pquerna "lua: Failed to obtain lua interpreter for %s %s",
13be69a729c912bea7d0aa22596b92717df92551pquerna function_name, filename);
13be69a729c912bea7d0aa22596b92717df92551pquerna ap_lua_release_state(L, spec, r);
13be69a729c912bea7d0aa22596b92717df92551pquerna return HTTP_INTERNAL_SERVER_ERROR;
13be69a729c912bea7d0aa22596b92717df92551pquerna }
13be69a729c912bea7d0aa22596b92717df92551pquerna
13be69a729c912bea7d0aa22596b92717df92551pquerna if (function_name != NULL) {
13be69a729c912bea7d0aa22596b92717df92551pquerna lua_getglobal(L, function_name);
0696197a54f186a65abacba1037f6fbe0cb975a1niq if (!lua_isfunction(L, -1)) {
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02331)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin "lua: Unable to find function %s in %s",
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin function_name,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin filename);
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin ap_lua_release_state(L, spec, r);
13be69a729c912bea7d0aa22596b92717df92551pquerna return HTTP_INTERNAL_SERVER_ERROR;
13be69a729c912bea7d0aa22596b92717df92551pquerna }
13be69a729c912bea7d0aa22596b92717df92551pquerna
13be69a729c912bea7d0aa22596b92717df92551pquerna ap_lua_run_lua_request(L, r);
13be69a729c912bea7d0aa22596b92717df92551pquerna }
13be69a729c912bea7d0aa22596b92717df92551pquerna else {
13be69a729c912bea7d0aa22596b92717df92551pquerna int t;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_lua_run_lua_request(L, r);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
e8f95a682820a599fe41b22977010636be5c2717jim t = lua_gettop(L);
13be69a729c912bea7d0aa22596b92717df92551pquerna lua_setglobal(L, "r");
13be69a729c912bea7d0aa22596b92717df92551pquerna lua_settop(L, t);
13be69a729c912bea7d0aa22596b92717df92551pquerna }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (lua_pcall(L, 1, 1, 0)) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin report_lua_error(L, r);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_lua_release_state(L, spec, r);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return HTTP_INTERNAL_SERVER_ERROR;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin rc = DECLINED;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (lua_isnumber(L, -1)) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin rc = lua_tointeger(L, -1);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else {
9de73d09e50d2daa2b0ba263d01cf6bf972a27fdrpluem ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02483)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "lua: Lua handler %s in %s did not return a value, assuming apache2.OK",
af91703d7657e52921bb36c44ff093b2d2077d0cminfrin function_name,
af91703d7657e52921bb36c44ff093b2d2077d0cminfrin filename);
af91703d7657e52921bb36c44ff093b2d2077d0cminfrin rc = OK;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
af91703d7657e52921bb36c44ff093b2d2077d0cminfrin ap_lua_release_state(L, spec, r);
af91703d7657e52921bb36c44ff093b2d2077d0cminfrin if (rc != DECLINED) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return rc;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
13be69a729c912bea7d0aa22596b92717df92551pquerna return DECLINED;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic apr_size_t config_getstr(ap_configfile_t *cfg, char *buf,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz size_t bufsiz)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_size_t i = 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (cfg->getstr) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_status_t rc = (cfg->getstr) (buf, bufsiz, cfg->param);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (rc == APR_SUCCESS) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin i = strlen(buf);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (i && buf[i - 1] == '\n')
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ++cfg->line_number;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz else {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz buf[0] = '\0';
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz i = 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz else {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz while (i < bufsiz) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz char ch;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_status_t rc = (cfg->getch) (&ch, cfg->param);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (rc != APR_SUCCESS)
13be69a729c912bea7d0aa22596b92717df92551pquerna break;
13be69a729c912bea7d0aa22596b92717df92551pquerna buf[i++] = ch;
85fa7868b2734d79c4e27b74d54b6846a1f9176fcolm if (ch == '\n') {
85fa7868b2734d79c4e27b74d54b6846a1f9176fcolm ++cfg->line_number;
13be69a729c912bea7d0aa22596b92717df92551pquerna break;
e8f95a682820a599fe41b22977010636be5c2717jim }
13be69a729c912bea7d0aa22596b92717df92551pquerna }
13be69a729c912bea7d0aa22596b92717df92551pquerna }
13be69a729c912bea7d0aa22596b92717df92551pquerna return i;
13be69a729c912bea7d0aa22596b92717df92551pquerna}
13be69a729c912bea7d0aa22596b92717df92551pquerna
13be69a729c912bea7d0aa22596b92717df92551pquernatypedef struct cr_ctx
a059fab04e8584e8bfdcaf1103e2ec6f53f97a14pquerna{
a059fab04e8584e8bfdcaf1103e2ec6f53f97a14pquerna cmd_parms *cmd;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_configfile_t *cfp;
13be69a729c912bea7d0aa22596b92717df92551pquerna size_t startline;
be60e23cacdadb070a83d40086a6d689410acc38rpluem const char *endstr;
13be69a729c912bea7d0aa22596b92717df92551pquerna char buf[HUGE_STRING_LEN];
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin} cr_ctx;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin/* Okay, this deserves a little explaination -- in order for the errors that lua
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin * generates to be 'accuarate', including line numbers, we basically inject
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin * N line number new lines into the 'top' of the chunk reader.....
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin *
be60e23cacdadb070a83d40086a6d689410acc38rpluem * be happy. this is cool.
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin *
be60e23cacdadb070a83d40086a6d689410acc38rpluem */
13be69a729c912bea7d0aa22596b92717df92551pquernastatic const char *lf =
13be69a729c912bea7d0aa22596b92717df92551pquerna "\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";
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin#define N_LF 32
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrinstatic const char *direct_chunkreader(lua_State *lvm, void *udata,
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin size_t *plen)
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin{
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin const char *p;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin struct cr_ctx *ctx = udata;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin if (ctx->startline) {
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin *plen = ctx->startline > N_LF ? N_LF : ctx->startline;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin ctx->startline -= *plen;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin return lf;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin }
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin *plen = config_getstr(ctx->cfp, ctx->buf, HUGE_STRING_LEN);
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin for (p = ctx->buf; isspace(*p); ++p);
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin if (p[0] == '<' && p[1] == '/') {
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin apr_size_t i = 0;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin while (i < strlen(ctx->endstr)) {
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin if (tolower(p[i + 2]) != ctx->endstr[i])
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin return ctx->buf;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin ++i;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin }
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin *plen = 0;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin return NULL;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin }
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin /*fprintf(stderr, "buf read: %s\n", ctx->buf); */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return ctx->buf;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin}
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrinstatic int ldump_writer(lua_State *L, const void *b, size_t size, void *B)
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin{
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin (void) L;
13be69a729c912bea7d0aa22596b92717df92551pquerna luaL_addlstring((luaL_Buffer *) B, (const char *) b, size);
13be69a729c912bea7d0aa22596b92717df92551pquerna return 0;
13be69a729c912bea7d0aa22596b92717df92551pquerna}
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndtypedef struct hack_section_baton
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin{
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd const char *name;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_lua_mapped_handler_spec *spec;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd int apr_hook_when;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz} hack_section_baton;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/* You can be unhappy now.
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd *
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * This is uncool.
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin *
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * When you create a <Section handler in httpd, the only 'easy' way to create
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * a directory context is to parse the section, and convert it into a 'normal'
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * Configureation option, and then collapse the entire section, in memory,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * back into the parent section -- from which you can then get the new directive
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * invoked.... anyways. evil. Rici taught me how to do this hack :-)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char *hack_section_handler(cmd_parms *cmd, void *_cfg,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *arg)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin{
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_directive_t *directive = cmd->directive;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz hack_section_baton *baton = directive->data;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *key = apr_psprintf(cmd->pool, "%s_%d", baton->name, baton->apr_hook_when);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, key,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz APR_HASH_KEY_STRING);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (!hook_specs) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz hook_specs = apr_array_make(cmd->pool, 2,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz sizeof(ap_lua_mapped_handler_spec *));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(cfg->hooks, key,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz APR_HASH_KEY_STRING, hook_specs);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz baton->spec->scope = cfg->vm_scope;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd *(ap_lua_mapped_handler_spec **) apr_array_push(hook_specs) = baton->spec;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return NULL;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char *register_named_block_function_hook(const char *name,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz cmd_parms *cmd,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz void *mconfig,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *line)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin{
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin const char *function = NULL;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_lua_mapped_handler_spec *spec;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd int when = APR_HOOK_MIDDLE;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *endp = ap_strrchr_c(line, '>');
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin if (endp == NULL) {
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin return apr_pstrcat(cmd->pool, cmd->cmd->name,
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin "> directive missing closing '>'", NULL);
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin }
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin line = apr_pstrndup(cmd->temp_pool, line, endp - line);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (line[0]) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin const char *word;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz word = ap_getword_conf(cmd->temp_pool, &line);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (word && *word) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz function = apr_pstrdup(cmd->pool, word);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin word = ap_getword_conf(cmd->temp_pool, &line);
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin if (word && *word) {
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin if (!strcasecmp("early", word)) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz when = AP_LUA_HOOK_FIRST;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin else if (!strcasecmp("late", word)) {
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin when = AP_LUA_HOOK_LAST;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else {
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin return apr_pstrcat(cmd->pool, cmd->cmd->name,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "> 2nd argument must be 'early' or 'late'", NULL);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz cr_ctx ctx;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_State *lvm;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz char *tmp;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin int rv;
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin ap_directive_t **current;
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin hack_section_baton *baton;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin spec->file_name = apr_psprintf(cmd->pool, "%s:%u",
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz cmd->config_file->name,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin cmd->config_file->line_number);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (function) {
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin spec->function_name = (char *) function;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz else {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz function = NULL;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ctx.cmd = cmd;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz tmp = apr_pstrdup(cmd->pool, cmd->err_directive->directive + 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_str_tolower(tmp);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ctx.endstr = tmp;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ctx.cfp = cmd->config_file;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ctx.startline = cmd->config_file->line_number;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* This lua State is used only to compile the input strings -> bytecode, so we don't need anything extra. */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd lvm = luaL_newstate();
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_settop(lvm, 0);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin rv = lua_load(lvm, direct_chunkreader, &ctx, spec->file_name);
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin if (rv != 0) {
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin const char *errstr = apr_pstrcat(cmd->pool, "Lua Error:",
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_tostring(lvm, -1), NULL);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin lua_close(lvm);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return errstr;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd else {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_Buffer b;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_buffinit(lvm, &b);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_dump(lvm, ldump_writer, &b);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_pushresult(&b);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz spec->bytecode_len = lua_strlen(lvm, -1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz spec->bytecode = apr_pstrmemdup(cmd->pool, lua_tostring(lvm, -1),
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz spec->bytecode_len);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_close(lvm);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz current = mconfig;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* Here, we have to replace our current config node for the next pass */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (!*current) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz *current = apr_pcalloc(cmd->pool, sizeof(**current));
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz baton = apr_pcalloc(cmd->pool, sizeof(hack_section_baton));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz baton->name = name;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd baton->spec = spec;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd baton->apr_hook_when = when;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz (*current)->filename = cmd->config_file->name;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz (*current)->line_num = cmd->config_file->line_number;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz (*current)->directive = apr_pstrdup(cmd->pool, "Lua_____ByteCodeHack");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz (*current)->args = NULL;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz (*current)->data = baton;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin return NULL;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin}
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char *register_named_file_function_hook(const char *name,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz cmd_parms *cmd,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz void *_cfg,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *file,
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin const char *function,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz int apr_hook_when)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_lua_mapped_handler_spec *spec;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *key = apr_psprintf(cmd->pool, "%s_%d", name, apr_hook_when);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, key,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin APR_HASH_KEY_STRING);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (!hook_specs) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin hook_specs = apr_array_make(cmd->pool, 2,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin sizeof(ap_lua_mapped_handler_spec *));
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_hash_set(cfg->hooks, key, APR_HASH_KEY_STRING, hook_specs);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec));
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin spec->file_name = apr_pstrdup(cmd->pool, file);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin spec->function_name = apr_pstrdup(cmd->pool, function);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin spec->scope = cfg->vm_scope;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin *(ap_lua_mapped_handler_spec **) apr_array_push(hook_specs) = spec;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return NULL;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic const char *register_mapped_file_function_hook(const char *pattern,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin cmd_parms *cmd,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin void *_cfg,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *file,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *function)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin ap_lua_mapped_handler_spec *spec;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin ap_regex_t *regex = apr_pcalloc(cmd->pool, sizeof(ap_regex_t));
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (ap_regcomp(regex, pattern,0)) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return "Invalid regex pattern!";
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec));
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin spec->file_name = apr_pstrdup(cmd->pool, file);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin spec->function_name = apr_pstrdup(cmd->pool, function);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin spec->scope = cfg->vm_scope;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin spec->uri_pattern = regex;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin *(ap_lua_mapped_handler_spec **) apr_array_push(cfg->mapped_handlers) = spec;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return NULL;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic const char *register_filter_function_hook(const char *filter,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin cmd_parms *cmd,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin void *_cfg,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *file,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *function,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin int direction)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin ap_lua_filter_handler_spec *spec;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_filter_handler_spec));
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin spec->file_name = apr_pstrdup(cmd->pool, file);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin spec->function_name = apr_pstrdup(cmd->pool, function);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin spec->filter_name = filter;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin *(ap_lua_filter_handler_spec **) apr_array_push(cfg->mapped_filters) = spec;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin /* TODO: Make it work on other types than just AP_FTYPE_RESOURCE? */
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (direction == AP_LUA_FILTER_OUTPUT) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin spec->direction = AP_LUA_FILTER_OUTPUT;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin ap_register_output_filter_protocol(filter, lua_output_filter_handle, NULL, AP_FTYPE_RESOURCE,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin else {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin spec->direction = AP_LUA_FILTER_INPUT;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin ap_register_input_filter(filter, lua_input_filter_handle, NULL, AP_FTYPE_RESOURCE);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return NULL;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_check_user_id_harness_first(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "check_user_id", AP_LUA_HOOK_FIRST);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_check_user_id_harness(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "check_user_id", APR_HOOK_MIDDLE);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_check_user_id_harness_last(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "check_user_id", AP_LUA_HOOK_LAST);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_translate_name_harness_first(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "translate_name", AP_LUA_HOOK_FIRST);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_translate_name_harness(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "translate_name", APR_HOOK_MIDDLE);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_translate_name_harness_last(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "translate_name", AP_LUA_HOOK_LAST);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_fixup_harness(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "fixups", APR_HOOK_MIDDLE);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_map_to_storage_harness(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "map_to_storage", APR_HOOK_MIDDLE);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_type_checker_harness(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "type_checker", APR_HOOK_MIDDLE);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_access_checker_harness_first(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "access_checker", AP_LUA_HOOK_FIRST);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_access_checker_harness(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "access_checker", APR_HOOK_MIDDLE);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_access_checker_harness_last(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "access_checker", AP_LUA_HOOK_LAST);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_auth_checker_harness_first(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "auth_checker", AP_LUA_HOOK_FIRST);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_auth_checker_harness(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "auth_checker", APR_HOOK_MIDDLE);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_auth_checker_harness_last(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "auth_checker", AP_LUA_HOOK_LAST);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic void lua_insert_filter_harness(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin /* ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "LuaHookInsertFilter not yet implemented"); */
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_log_transaction_harness(request_rec *r)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "log_transaction", APR_HOOK_FIRST);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic int lua_quick_harness(request_rec *r, int lookup)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (lookup) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return DECLINED;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_request_rec_hook_harness(r, "quick", APR_HOOK_MIDDLE);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic const char *register_translate_name_hook(cmd_parms *cmd, void *_cfg,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *file,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *function,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *when)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin NOT_IN_HTACCESS);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin int apr_hook_when = APR_HOOK_MIDDLE;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (err) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return err;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (when) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (!strcasecmp(when, "early")) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_hook_when = AP_LUA_HOOK_FIRST;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin else if (!strcasecmp(when, "late")) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_hook_when = AP_LUA_HOOK_LAST;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin else {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return "Third argument must be 'early' or 'late'";
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return register_named_file_function_hook("translate_name", cmd, _cfg,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin file, function, apr_hook_when);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic const char *register_translate_name_block(cmd_parms *cmd, void *_cfg,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *line)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return register_named_block_function_hook("translate_name", cmd, _cfg,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin line);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic const char *register_fixups_hook(cmd_parms *cmd, void *_cfg,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *file,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *function)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return register_named_file_function_hook("fixups", cmd, _cfg, file,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz function, APR_HOOK_MIDDLE);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
2d728410bcf9e6e53698bb035f516e18efd76d8bndstatic const char *register_fixups_block(cmd_parms *cmd, void *_cfg,
1018201f5223624476334c6e23aead02db7c4040minfrin const char *line)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
1018201f5223624476334c6e23aead02db7c4040minfrin return register_named_block_function_hook("fixups", cmd, _cfg, line);
9333012aa2af9e669db651f0ade3e044ccc3ec84rpluem}
1018201f5223624476334c6e23aead02db7c4040minfrin
9333012aa2af9e669db651f0ade3e044ccc3ec84rpluemstatic const char *register_map_to_storage_hook(cmd_parms *cmd, void *_cfg,
2d728410bcf9e6e53698bb035f516e18efd76d8bnd const char *file,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin const char *function)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return register_named_file_function_hook("map_to_storage", cmd, _cfg,
487d8b4e063696dd9698792a13c8e426635f1072nd file, function, APR_HOOK_MIDDLE);
2d728410bcf9e6e53698bb035f516e18efd76d8bnd}
2d728410bcf9e6e53698bb035f516e18efd76d8bnd
2d728410bcf9e6e53698bb035f516e18efd76d8bndstatic const char *register_log_transaction_hook(cmd_parms *cmd, void *_cfg,
2d728410bcf9e6e53698bb035f516e18efd76d8bnd const char *file,
487d8b4e063696dd9698792a13c8e426635f1072nd const char *function)
2d728410bcf9e6e53698bb035f516e18efd76d8bnd{
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return register_named_file_function_hook("log_transaction", cmd, _cfg,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin file, function, APR_HOOK_FIRST);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin}
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
487d8b4e063696dd9698792a13c8e426635f1072ndstatic const char *register_map_to_storage_block(cmd_parms *cmd, void *_cfg,
2d728410bcf9e6e53698bb035f516e18efd76d8bnd const char *line)
2d728410bcf9e6e53698bb035f516e18efd76d8bnd{
487d8b4e063696dd9698792a13c8e426635f1072nd return register_named_block_function_hook("map_to_storage", cmd, _cfg,
2d728410bcf9e6e53698bb035f516e18efd76d8bnd line);
2d728410bcf9e6e53698bb035f516e18efd76d8bnd}
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz
487d8b4e063696dd9698792a13c8e426635f1072nd
2d728410bcf9e6e53698bb035f516e18efd76d8bndstatic const char *register_check_user_id_hook(cmd_parms *cmd, void *_cfg,
2d728410bcf9e6e53698bb035f516e18efd76d8bnd const char *file,
487d8b4e063696dd9698792a13c8e426635f1072nd const char *function,
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz const char *when)
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz{
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz int apr_hook_when = APR_HOOK_MIDDLE;
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz
2d728410bcf9e6e53698bb035f516e18efd76d8bnd if (when) {
487d8b4e063696dd9698792a13c8e426635f1072nd if (!strcasecmp(when, "early")) {
4dd97ffc9c529494e0ec820fe9de80b39b589d42minfrin apr_hook_when = AP_LUA_HOOK_FIRST;
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin }
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin else if (!strcasecmp(when, "late")) {
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin apr_hook_when = AP_LUA_HOOK_LAST;
487d8b4e063696dd9698792a13c8e426635f1072nd }
487d8b4e063696dd9698792a13c8e426635f1072nd else {
487d8b4e063696dd9698792a13c8e426635f1072nd return "Third argument must be 'early' or 'late'";
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
2d728410bcf9e6e53698bb035f516e18efd76d8bnd
2d728410bcf9e6e53698bb035f516e18efd76d8bnd return register_named_file_function_hook("check_user_id", cmd, _cfg, file,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin function, apr_hook_when);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
5876f43a746f688a32b7201bced8591ddf19bd43minfrinstatic const char *register_check_user_id_block(cmd_parms *cmd, void *_cfg,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin const char *line)
5876f43a746f688a32b7201bced8591ddf19bd43minfrin{
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return register_named_block_function_hook("check_user_id", cmd, _cfg,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin line);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin}
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrinstatic const char *register_type_checker_hook(cmd_parms *cmd, void *_cfg,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *file,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *function)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return register_named_file_function_hook("type_checker", cmd, _cfg, file,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin function, APR_HOOK_MIDDLE);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic const char *register_type_checker_block(cmd_parms *cmd, void *_cfg,
2d728410bcf9e6e53698bb035f516e18efd76d8bnd const char *line)
2d728410bcf9e6e53698bb035f516e18efd76d8bnd{
2d728410bcf9e6e53698bb035f516e18efd76d8bnd return register_named_block_function_hook("type_checker", cmd, _cfg,
2d728410bcf9e6e53698bb035f516e18efd76d8bnd line);
2d728410bcf9e6e53698bb035f516e18efd76d8bnd}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char *register_access_checker_hook(cmd_parms *cmd, void *_cfg,
2d728410bcf9e6e53698bb035f516e18efd76d8bnd const char *file,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *function,
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick const char *when)
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick{
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick int apr_hook_when = APR_HOOK_MIDDLE;
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick if (when) {
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick if (!strcasecmp(when, "early")) {
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin apr_hook_when = AP_LUA_HOOK_FIRST;
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin }
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin else if (!strcasecmp(when, "late")) {
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin apr_hook_when = AP_LUA_HOOK_LAST;
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin }
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin else {
5c345baea74f2eb2d74548bed271370b972499e5minfrin return "Third argument must be 'early' or 'late'";
5c345baea74f2eb2d74548bed271370b972499e5minfrin }
5c345baea74f2eb2d74548bed271370b972499e5minfrin }
5c345baea74f2eb2d74548bed271370b972499e5minfrin
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin return register_named_file_function_hook("access_checker", cmd, _cfg,
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin file, function, apr_hook_when);
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin}
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrinstatic const char *register_access_checker_block(cmd_parms *cmd, void *_cfg,
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin const char *line)
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin{
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin return register_named_block_function_hook("access_checker", cmd, _cfg,
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin line);
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin}
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrinstatic const char *register_auth_checker_hook(cmd_parms *cmd, void *_cfg,
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin const char *file,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *function,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *when)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz int apr_hook_when = APR_HOOK_MIDDLE;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (when) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (!strcasecmp(when, "early")) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hook_when = AP_LUA_HOOK_FIRST;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz else if (!strcasecmp(when, "late")) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hook_when = AP_LUA_HOOK_LAST;
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return "Third argument must be 'early' or 'late'";
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin }
ef20451e98932c1245225b211c7198cb6c0a8624minfrin
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return register_named_file_function_hook("auth_checker", cmd, _cfg, file,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz function, apr_hook_when);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char *register_auth_checker_block(cmd_parms *cmd, void *_cfg,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *line)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return register_named_block_function_hook("auth_checker", cmd, _cfg,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz line);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin}
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char *register_insert_filter_hook(cmd_parms *cmd, void *_cfg,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *file,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *function)
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz{
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return "LuaHookInsertFilter not yet implemented";
f81f966693feefb53f11dc3363b864779873a608nd}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrinstatic const char *register_quick_hook(cmd_parms *cmd, void *_cfg,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *file, const char *function)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz NOT_IN_HTACCESS);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (err) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return err;
2d728410bcf9e6e53698bb035f516e18efd76d8bnd }
2d728410bcf9e6e53698bb035f516e18efd76d8bnd return register_named_file_function_hook("quick", cmd, _cfg, file,
2d728410bcf9e6e53698bb035f516e18efd76d8bnd function, APR_HOOK_MIDDLE);
2d728410bcf9e6e53698bb035f516e18efd76d8bnd}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char *register_map_handler(cmd_parms *cmd, void *_cfg,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char* match, const char *file, const char *function)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz NOT_IN_HTACCESS);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (err) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return err;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (!function) function = "handle";
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return register_mapped_file_function_hook(match, cmd, _cfg, file,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz function);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrinstatic const char *register_output_filter(cmd_parms *cmd, void *_cfg,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd const char* filter, const char *file, const char *function)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd NOT_IN_HTACCESS);
1018201f5223624476334c6e23aead02db7c4040minfrin if (err) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return err;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin if (!function) function = "handle";
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin return register_filter_function_hook(filter, cmd, _cfg, file,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd function, AP_LUA_FILTER_OUTPUT);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic const char *register_input_filter(cmd_parms *cmd, void *_cfg,
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick const char* filter, const char *file, const char *function)
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd{
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz NOT_IN_HTACCESS);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (err) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return err;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick if (!function) function = "handle";
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return register_filter_function_hook(filter, cmd, _cfg, file,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd function, AP_LUA_FILTER_INPUT);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd}
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic const char *register_quick_block(cmd_parms *cmd, void *_cfg,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *line)
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd{
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick return register_named_block_function_hook("quick", cmd, _cfg,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd line);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
e8f95a682820a599fe41b22977010636be5c2717jim
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantzstatic const char *register_package_helper(cmd_parms *cmd,
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick const char *arg,
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz apr_array_header_t *dir_array)
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz{
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz apr_status_t rv;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_lua_server_cfg *server_cfg =
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_get_module_config(cmd->server->module_config, &lua_module);
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd char *fixed_filename;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz rv = apr_filepath_merge(&fixed_filename,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd server_cfg->root_path,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd arg,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd APR_FILEPATH_NOTRELATIVE,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd cmd->pool);
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (rv != APR_SUCCESS) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return apr_psprintf(cmd->pool,
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz "Unable to build full path to file, %s", arg);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz *(const char **) apr_array_push(dir_array) = fixed_filename;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return NULL;
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick}
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/**
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * Called for config directive which looks like
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * LuaPackagePath /lua/package/path/mapped/thing/like/this/?.lua
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic const char *register_package_dir(cmd_parms *cmd, void *_cfg,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *arg)
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd{
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return register_package_helper(cmd, arg, cfg->package_paths);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd}
487d8b4e063696dd9698792a13c8e426635f1072nd
487d8b4e063696dd9698792a13c8e426635f1072nd/**
487d8b4e063696dd9698792a13c8e426635f1072nd * Called for config directive which looks like
487d8b4e063696dd9698792a13c8e426635f1072nd * LuaPackageCPath /lua/package/path/mapped/thing/like/this/?.so
487d8b4e063696dd9698792a13c8e426635f1072nd */
487d8b4e063696dd9698792a13c8e426635f1072ndstatic const char *register_package_cdir(cmd_parms *cmd,
487d8b4e063696dd9698792a13c8e426635f1072nd void *_cfg,
487d8b4e063696dd9698792a13c8e426635f1072nd const char *arg)
487d8b4e063696dd9698792a13c8e426635f1072nd{
15351ab1a34130c1f880daef4e5dc15407a75969colm ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
15351ab1a34130c1f880daef4e5dc15407a75969colm
15351ab1a34130c1f880daef4e5dc15407a75969colm return register_package_helper(cmd, arg, cfg->package_cpaths);
487d8b4e063696dd9698792a13c8e426635f1072nd}
487d8b4e063696dd9698792a13c8e426635f1072nd
487d8b4e063696dd9698792a13c8e426635f1072ndstatic const char *register_lua_inherit(cmd_parms *cmd,
487d8b4e063696dd9698792a13c8e426635f1072nd void *_cfg,
487d8b4e063696dd9698792a13c8e426635f1072nd const char *arg)
487d8b4e063696dd9698792a13c8e426635f1072nd{
1018201f5223624476334c6e23aead02db7c4040minfrin ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
1018201f5223624476334c6e23aead02db7c4040minfrin
487d8b4e063696dd9698792a13c8e426635f1072nd if (strcasecmp("none", arg) == 0) {
487d8b4e063696dd9698792a13c8e426635f1072nd cfg->inherit = AP_LUA_INHERIT_NONE;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd else if (strcasecmp("parent-first", arg) == 0) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin cfg->inherit = AP_LUA_INHERIT_PARENT_FIRST;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else if (strcasecmp("parent-last", arg) == 0) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin cfg->inherit = AP_LUA_INHERIT_PARENT_LAST;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return apr_psprintf(cmd->pool,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin "LuaInherit type of '%s' not recognized, valid "
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin "options are 'none', 'parent-first', and 'parent-last'",
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin arg);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return NULL;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin}
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrinstatic const char *register_lua_codecache(cmd_parms *cmd,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin void *_cfg,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin const char *arg)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin{
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (strcasecmp("never", arg) == 0) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin cfg->codecache = AP_LUA_CACHE_NEVER;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else if (strcasecmp("stat", arg) == 0) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin cfg->codecache = AP_LUA_CACHE_STAT;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else if (strcasecmp("forever", arg) == 0) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin cfg->codecache = AP_LUA_CACHE_FOREVER;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return apr_psprintf(cmd->pool,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin "LuaCodeCache type of '%s' not recognized, valid "
5876f43a746f688a32b7201bced8591ddf19bd43minfrin "options are 'never', 'stat', and 'forever'",
5876f43a746f688a32b7201bced8591ddf19bd43minfrin arg);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return NULL;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin}
5876f43a746f688a32b7201bced8591ddf19bd43minfrinstatic const char *register_lua_scope(cmd_parms *cmd,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin void *_cfg,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin const char *scope,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin const char *min,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin const char *max)
5876f43a746f688a32b7201bced8591ddf19bd43minfrin{
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (strcmp("once", scope) == 0) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin cfg->vm_scope = AP_LUA_SCOPE_ONCE;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd else if (strcmp("request", scope) == 0) {
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick cfg->vm_scope = AP_LUA_SCOPE_REQUEST;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd else if (strcmp("conn", scope) == 0) {
1018201f5223624476334c6e23aead02db7c4040minfrin cfg->vm_scope = AP_LUA_SCOPE_CONN;
1018201f5223624476334c6e23aead02db7c4040minfrin }
c42b94a839cae93073d33b0d21f0ff1118d958e8poirier else if (strcmp("thread", scope) == 0) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd#if !APR_HAS_THREADS
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return apr_psprintf(cmd->pool,
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin "Scope type of '%s' cannot be used because this "
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin "server does not have threading support "
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin "(APR_HAS_THREADS)"
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin scope);
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin#endif
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin cfg->vm_scope = AP_LUA_SCOPE_THREAD;
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin }
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin else if (strcmp("server", scope) == 0) {
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin unsigned int vmin, vmax;
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin#if !APR_HAS_THREADS
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin return apr_psprintf(cmd->pool,
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin "Scope type of '%s' cannot be used because this "
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin "server does not have threading support "
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin "(APR_HAS_THREADS)"
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin scope);
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin#endif
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin cfg->vm_scope = AP_LUA_SCOPE_SERVER;
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin vmin = min ? atoi(min) : 1;
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin vmax = max ? atoi(max) : 1;
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin if (vmin == 0) {
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin vmin = 1;
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin }
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin if (vmax < vmin) {
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin vmax = vmin;
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin }
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin cfg->vm_min = vmin;
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin cfg->vm_max = vmax;
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin }
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin else {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return apr_psprintf(cmd->pool,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd "Invalid value for LuaScope, '%s', acceptable "
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd "values are: 'once', 'request', 'conn'"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#if APR_HAS_THREADS
1018201f5223624476334c6e23aead02db7c4040minfrin ", 'thread', 'server'"
c42b94a839cae93073d33b0d21f0ff1118d958e8poirier#endif
1018201f5223624476334c6e23aead02db7c4040minfrin ,scope);
1018201f5223624476334c6e23aead02db7c4040minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return NULL;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic const char *register_lua_root(cmd_parms *cmd, void *_cfg,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *root)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin /* ap_lua_dir_cfg* cfg = (ap_lua_dir_cfg*)_cfg; */
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin ap_lua_server_cfg *cfg = ap_get_module_config(cmd->server->module_config,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin &lua_module);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin cfg->root_path = root;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return NULL;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinconst char *ap_lua_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin request_rec *r, const char *var)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (lua_ssl_val) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return (const char *)lua_ssl_val(p, s, c, r, (char *)var);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return NULL;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinint ap_lua_ssl_is_https(conn_rec *c)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return lua_ssl_is_https ? lua_ssl_is_https(c) : 0;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin/*******************************/
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic const char *lua_authz_parse(cmd_parms *cmd, const char *require_line,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const void **parsed_require_line)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *provider_name;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_authz_provider_spec *spec;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
1018201f5223624476334c6e23aead02db7c4040minfrin apr_pool_userdata_get((void**)&provider_name, AUTHZ_PROVIDER_NAME_NOTE,
1018201f5223624476334c6e23aead02db7c4040minfrin cmd->temp_pool);
1018201f5223624476334c6e23aead02db7c4040minfrin ap_assert(provider_name != NULL);
1018201f5223624476334c6e23aead02db7c4040minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin spec = apr_hash_get(lua_authz_providers, provider_name, APR_HASH_KEY_STRING);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ap_assert(spec != NULL);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (require_line && *require_line) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *arg;
1018201f5223624476334c6e23aead02db7c4040minfrin spec->args = apr_array_make(cmd->pool, 2, sizeof(const char *));
1018201f5223624476334c6e23aead02db7c4040minfrin while ((arg = ap_getword_conf(cmd->pool, &require_line)) && *arg) {
1018201f5223624476334c6e23aead02db7c4040minfrin APR_ARRAY_PUSH(spec->args, const char *) = arg;
1018201f5223624476334c6e23aead02db7c4040minfrin }
1018201f5223624476334c6e23aead02db7c4040minfrin }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz *parsed_require_line = spec;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return NULL;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic authz_status lua_authz_check(request_rec *r, const char *require_line,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const void *parsed_require_line)
1018201f5223624476334c6e23aead02db7c4040minfrin{
c42b94a839cae93073d33b0d21f0ff1118d958e8poirier apr_pool_t *pool;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_lua_vm_spec *spec;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_State *L;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config,
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin &lua_module);
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin &lua_module);
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin const lua_authz_provider_spec *prov_spec = parsed_require_line;
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin int result;
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin int nargs = 0;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
03ee592beedc99301c3c79838af79a333af47361minfrin spec = create_vm_spec(&pool, r, cfg, server_cfg, prov_spec->file_name,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin NULL, 0, prov_spec->function_name, "authz provider");
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin L = ap_lua_get_lua_state(pool, spec, r);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (L == NULL) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02314)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Unable to compile VM for authz provider %s", prov_spec->name);
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin return AUTHZ_GENERAL_ERROR;
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin }
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin lua_getglobal(L, prov_spec->function_name);
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin if (!lua_isfunction(L, -1)) {
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02319)
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin "Unable to find function %s in %s",
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin prov_spec->function_name, prov_spec->file_name);
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin ap_lua_release_state(L, spec, r);
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin return AUTHZ_GENERAL_ERROR;
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin }
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin ap_lua_run_lua_request(L, r);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (prov_spec->args) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz int i;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (!lua_checkstack(L, prov_spec->args->nelts)) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02315)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Error: authz provider %s: too many arguments", prov_spec->name);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_lua_release_state(L, spec, r);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return AUTHZ_GENERAL_ERROR;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz for (i = 0; i < prov_spec->args->nelts; i++) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *arg = APR_ARRAY_IDX(prov_spec->args, i, const char *);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushstring(L, arg);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz nargs = prov_spec->args->nelts;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (lua_pcall(L, 1 + nargs, 1, 0)) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *err = lua_tostring(L, -1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02316)
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd "Error executing authz provider %s: %s", prov_spec->name, err);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_lua_release_state(L, spec, r);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return AUTHZ_GENERAL_ERROR;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (!lua_isnumber(L, -1)) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02317)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Error: authz provider %s did not return integer", prov_spec->name);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_lua_release_state(L, spec, r);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return AUTHZ_GENERAL_ERROR;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz result = lua_tointeger(L, -1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_lua_release_state(L, spec, r);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz switch (result) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case AUTHZ_DENIED:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case AUTHZ_GRANTED:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case AUTHZ_NEUTRAL:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case AUTHZ_GENERAL_ERROR:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case AUTHZ_DENIED_NO_USER:
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return result;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd default:
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02318)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Error: authz provider %s: invalid return value %d",
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd prov_spec->name, result);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return AUTHZ_GENERAL_ERROR;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic const authz_provider lua_authz_provider =
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd{
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd &lua_authz_check,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd &lua_authz_parse,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz};
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char *register_authz_provider(cmd_parms *cmd, void *_cfg,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *name, const char *file,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *function)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_authz_provider_spec *spec;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (err)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return err;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd spec = apr_pcalloc(cmd->pool, sizeof(*spec));
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd spec->name = name;
a51acf58d9d82f52e0ee56106cd9282030f3f3bend spec->file_name = file;
a51acf58d9d82f52e0ee56106cd9282030f3f3bend spec->function_name = function;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_hash_set(lua_authz_providers, name, APR_HASH_KEY_STRING, spec);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_register_auth_provider(cmd->pool, AUTHZ_PROVIDER_GROUP, name,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd AUTHZ_PROVIDER_VERSION,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz &lua_authz_provider,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz AP_AUTH_INTERNAL_PER_CONF);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return NULL;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzcommand_rec lua_commands[] = {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd AP_INIT_TAKE1("LuaRoot", register_lua_root, NULL, OR_ALL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Specify the base path for resolving relative paths for mod_lua directives"),
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz AP_INIT_TAKE1("LuaPackagePath", register_package_dir, NULL, OR_ALL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Add a directory to lua's package.path"),
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz AP_INIT_TAKE1("LuaPackageCPath", register_package_cdir, NULL, OR_ALL,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd "Add a directory to lua's package.cpath"),
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz AP_INIT_TAKE3("LuaAuthzProvider", register_authz_provider, NULL, RSRC_CONF|EXEC_ON_READ,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Provide an authorization provider"),
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz AP_INIT_TAKE23("LuaHookTranslateName", register_translate_name_hook, NULL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz OR_ALL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Provide a hook for the translate name phase of request processing"),
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd AP_INIT_RAW_ARGS("<LuaHookTranslateName", register_translate_name_block,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd NULL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz EXEC_ON_READ | OR_ALL,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd "Provide a hook for the translate name phase of request processing"),
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz AP_INIT_TAKE2("LuaHookFixups", register_fixups_hook, NULL, OR_ALL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Provide a hook for the fixups phase of request processing"),
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz AP_INIT_RAW_ARGS("<LuaHookFixups", register_fixups_block, NULL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz EXEC_ON_READ | OR_ALL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Provide a inline hook for the fixups phase of request processing"),
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd /* todo: test */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd AP_INIT_TAKE2("LuaHookMapToStorage", register_map_to_storage_hook, NULL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz OR_ALL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Provide a hook for the map_to_storage phase of request processing"),
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz AP_INIT_RAW_ARGS("<LuaHookMapToStorage", register_map_to_storage_block,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz NULL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz EXEC_ON_READ | OR_ALL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Provide a hook for the map_to_storage phase of request processing"),
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd /* todo: test */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz AP_INIT_TAKE23("LuaHookCheckUserID", register_check_user_id_hook, NULL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz OR_ALL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Provide a hook for the check_user_id phase of request processing"),
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz AP_INIT_RAW_ARGS("<LuaHookCheckUserID", register_check_user_id_block,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz NULL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz EXEC_ON_READ | OR_ALL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Provide a hook for the check_user_id phase of request processing"),
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
a51acf58d9d82f52e0ee56106cd9282030f3f3bend /* todo: test */
a51acf58d9d82f52e0ee56106cd9282030f3f3bend AP_INIT_TAKE2("LuaHookTypeChecker", register_type_checker_hook, NULL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz OR_ALL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Provide a hook for the type_checker phase of request processing"),
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz AP_INIT_RAW_ARGS("<LuaHookTypeChecker", register_type_checker_block, NULL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz EXEC_ON_READ | OR_ALL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Provide a hook for the type_checker phase of request processing"),
/* todo: test */
AP_INIT_TAKE23("LuaHookAccessChecker", register_access_checker_hook, NULL,
OR_ALL,
"Provide a hook for the access_checker phase of request processing"),
AP_INIT_RAW_ARGS("<LuaHookAccessChecker", register_access_checker_block,
NULL,
EXEC_ON_READ | OR_ALL,
"Provide a hook for the access_checker phase of request processing"),
/* todo: test */
AP_INIT_TAKE23("LuaHookAuthChecker", register_auth_checker_hook, NULL,
OR_ALL,
"Provide a hook for the auth_checker phase of request processing"),
AP_INIT_RAW_ARGS("<LuaHookAuthChecker", register_auth_checker_block, NULL,
EXEC_ON_READ | OR_ALL,
"Provide a hook for the auth_checker phase of request processing"),
/* todo: test */
AP_INIT_TAKE2("LuaHookInsertFilter", register_insert_filter_hook, NULL,
OR_ALL,
"Provide a hook for the insert_filter phase of request processing"),
AP_INIT_TAKE2("LuaHookLog", register_log_transaction_hook, NULL,
OR_ALL,
"Provide a hook for the logging phase of request processing"),
AP_INIT_TAKE123("LuaScope", register_lua_scope, NULL, OR_ALL,
"One of once, request, conn, server -- default is once"),
AP_INIT_TAKE1("LuaInherit", register_lua_inherit, NULL, OR_ALL,
"Controls how Lua scripts in parent contexts are merged with the current "
" context: none|parent-last|parent-first (default: parent-first) "),
AP_INIT_TAKE1("LuaCodeCache", register_lua_codecache, NULL, OR_ALL,
"Controls the behavior of the in-memory code cache "
" context: stat|forever|never (default: stat) "),
AP_INIT_TAKE2("LuaQuickHandler", register_quick_hook, NULL, OR_ALL,
"Provide a hook for the quick handler of request processing"),
AP_INIT_RAW_ARGS("<LuaQuickHandler", register_quick_block, NULL,
EXEC_ON_READ | OR_ALL,
"Provide a hook for the quick handler of request processing"),
AP_INIT_RAW_ARGS("Lua_____ByteCodeHack", hack_section_handler, NULL,
OR_ALL,
"(internal) Byte code handler"),
AP_INIT_TAKE23("LuaMapHandler", register_map_handler, NULL, OR_ALL,
"Maps a path to a lua handler"),
AP_INIT_TAKE3("LuaOutputFilter", register_output_filter, NULL, OR_ALL,
"Registers a Lua function as an output filter"),
AP_INIT_TAKE3("LuaInputFilter", register_input_filter, NULL, OR_ALL,
"Registers a Lua function as an input filter"),
{NULL}
};
static void *create_dir_config(apr_pool_t *p, char *dir)
{
ap_lua_dir_cfg *cfg = apr_pcalloc(p, sizeof(ap_lua_dir_cfg));
cfg->package_paths = apr_array_make(p, 2, sizeof(char *));
cfg->package_cpaths = apr_array_make(p, 2, sizeof(char *));
cfg->mapped_handlers =
apr_array_make(p, 1, sizeof(ap_lua_mapped_handler_spec *));
cfg->mapped_filters =
apr_array_make(p, 1, sizeof(ap_lua_filter_handler_spec *));
cfg->pool = p;
cfg->hooks = apr_hash_make(p);
cfg->dir = apr_pstrdup(p, dir);
cfg->vm_scope = AP_LUA_SCOPE_UNSET;
cfg->codecache = AP_LUA_CACHE_UNSET;
cfg->vm_min = 0;
cfg->vm_max = 0;
return cfg;
}
static int create_request_config(request_rec *r)
{
ap_lua_request_cfg *cfg = apr_palloc(r->pool, sizeof(ap_lua_request_cfg));
cfg->mapped_request_details = NULL;
cfg->request_scoped_vms = apr_hash_make(r->pool);
ap_set_module_config(r->request_config, &lua_module, cfg);
return OK;
}
static void *create_server_config(apr_pool_t *p, server_rec *s)
{
ap_lua_server_cfg *cfg = apr_pcalloc(p, sizeof(ap_lua_server_cfg));
cfg->vm_reslists = apr_hash_make(p);
apr_thread_rwlock_create(&cfg->vm_reslists_lock, p);
cfg->root_path = NULL;
return cfg;
}
static int lua_request_hook(lua_State *L, request_rec *r)
{
ap_lua_push_request(L, r);
return OK;
}
static int lua_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp)
{
ap_mutex_register(pconf, "lua-ivm-shm", NULL, APR_LOCK_DEFAULT, 0);
return OK;
}
static int lua_post_config(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *s)
{
apr_pool_t **pool;
lua_ssl_val = APR_RETRIEVE_OPTIONAL_FN(ssl_var_lookup);
lua_ssl_is_https = APR_RETRIEVE_OPTIONAL_FN(ssl_is_https);
const char *tempdir;
apr_status_t rs;
if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG)
return OK;
/* Create ivm mutex */
rs = ap_global_mutex_create(&lua_ivm_mutex, NULL, "lua-ivm-shm", NULL,
s, pconf, 0);
if (APR_SUCCESS != rs) {
return HTTP_INTERNAL_SERVER_ERROR;
}
/* Create shared memory space */
rs = apr_temp_dir_get(&tempdir, pconf);
if (rs != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rs, s,
"mod_lua IVM: Failed to find temporary directory");
return HTTP_INTERNAL_SERVER_ERROR;
}
lua_ivm_shmfile = apr_psprintf(pconf, "%s/httpd_lua_shm.%ld", tempdir,
(long int)getpid());
rs = apr_shm_create(&lua_ivm_shm, sizeof(apr_pool_t**),
(const char *) lua_ivm_shmfile, pconf);
if (rs != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rs, s,
"mod_lua: Failed to create shared memory segment on file %s",
lua_ivm_shmfile);
return HTTP_INTERNAL_SERVER_ERROR;
}
pool = (apr_pool_t **)apr_shm_baseaddr_get(lua_ivm_shm);
apr_pool_create(pool, pconf);
apr_pool_cleanup_register(pconf, NULL, shm_cleanup_wrapper,
apr_pool_cleanup_null);
return OK;
}
static void *overlay_hook_specs(apr_pool_t *p,
const void *key,
apr_ssize_t klen,
const void *overlay_val,
const void *base_val,
const void *data)
{
const apr_array_header_t *overlay_info = (const apr_array_header_t*)overlay_val;
const apr_array_header_t *base_info = (const apr_array_header_t*)base_val;
return apr_array_append(p, base_info, overlay_info);
}
static void *merge_dir_config(apr_pool_t *p, void *basev, void *overridesv)
{
ap_lua_dir_cfg *a, *base, *overrides;
a = (ap_lua_dir_cfg *)apr_pcalloc(p, sizeof(ap_lua_dir_cfg));
base = (ap_lua_dir_cfg*)basev;
overrides = (ap_lua_dir_cfg*)overridesv;
a->pool = overrides->pool;
a->dir = apr_pstrdup(p, overrides->dir);
a->vm_scope = (overrides->vm_scope == AP_LUA_SCOPE_UNSET) ? base->vm_scope: overrides->vm_scope;
a->inherit = (overrides->inherit == AP_LUA_INHERIT_UNSET) ? base->inherit : overrides->inherit;
a->codecache = (overrides->codecache == AP_LUA_CACHE_UNSET) ? base->codecache : overrides->codecache;
a->vm_min = (overrides->vm_min == 0) ? base->vm_min : overrides->vm_min;
a->vm_max = (overrides->vm_max == 0) ? base->vm_max : overrides->vm_max;
if (a->inherit == AP_LUA_INHERIT_UNSET || a->inherit == AP_LUA_INHERIT_PARENT_FIRST) {
a->package_paths = apr_array_append(p, base->package_paths, overrides->package_paths);
a->package_cpaths = apr_array_append(p, base->package_cpaths, overrides->package_cpaths);
a->mapped_handlers = apr_array_append(p, base->mapped_handlers, overrides->mapped_handlers);
a->mapped_filters = apr_array_append(p, base->mapped_filters, overrides->mapped_filters);
a->hooks = apr_hash_merge(p, overrides->hooks, base->hooks, overlay_hook_specs, NULL);
}
else if (a->inherit == AP_LUA_INHERIT_PARENT_LAST) {
a->package_paths = apr_array_append(p, overrides->package_paths, base->package_paths);
a->package_cpaths = apr_array_append(p, overrides->package_cpaths, base->package_cpaths);
a->mapped_handlers = apr_array_append(p, overrides->mapped_handlers, base->mapped_handlers);
a->mapped_filters = apr_array_append(p, overrides->mapped_filters, base->mapped_filters);
a->hooks = apr_hash_merge(p, base->hooks, overrides->hooks, overlay_hook_specs, NULL);
}
else {
a->package_paths = overrides->package_paths;
a->package_cpaths = overrides->package_cpaths;
a->mapped_handlers= overrides->mapped_handlers;
a->mapped_filters= overrides->mapped_filters;
a->hooks= overrides->hooks;
}
return a;
}
static void lua_register_hooks(apr_pool_t *p)
{
/* ap_register_output_filter("luahood", luahood, NULL, AP_FTYPE_RESOURCE); */
ap_hook_handler(lua_handler, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_create_request(create_request_config, NULL, NULL,
APR_HOOK_MIDDLE);
/* http_request.h hooks */
ap_hook_translate_name(lua_translate_name_harness_first, NULL, NULL,
AP_LUA_HOOK_FIRST);
ap_hook_translate_name(lua_translate_name_harness, NULL, NULL,
APR_HOOK_MIDDLE);
ap_hook_translate_name(lua_translate_name_harness_last, NULL, NULL,
AP_LUA_HOOK_LAST);
ap_hook_fixups(lua_fixup_harness, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_map_to_storage(lua_map_to_storage_harness, NULL, NULL,
APR_HOOK_MIDDLE);
ap_hook_check_user_id(lua_check_user_id_harness_first, NULL, NULL,
AP_LUA_HOOK_FIRST);
ap_hook_check_user_id(lua_check_user_id_harness, NULL, NULL,
APR_HOOK_MIDDLE);
ap_hook_check_user_id(lua_check_user_id_harness_last, NULL, NULL,
AP_LUA_HOOK_LAST);
ap_hook_type_checker(lua_type_checker_harness, NULL, NULL,
APR_HOOK_MIDDLE);
ap_hook_access_checker(lua_access_checker_harness_first, NULL, NULL,
AP_LUA_HOOK_FIRST);
ap_hook_access_checker(lua_access_checker_harness, NULL, NULL,
APR_HOOK_MIDDLE);
ap_hook_access_checker(lua_access_checker_harness_last, NULL, NULL,
AP_LUA_HOOK_LAST);
ap_hook_auth_checker(lua_auth_checker_harness_first, NULL, NULL,
AP_LUA_HOOK_FIRST);
ap_hook_auth_checker(lua_auth_checker_harness, NULL, NULL,
APR_HOOK_MIDDLE);
ap_hook_auth_checker(lua_auth_checker_harness_last, NULL, NULL,
AP_LUA_HOOK_LAST);
ap_hook_insert_filter(lua_insert_filter_harness, NULL, NULL,
APR_HOOK_MIDDLE);
ap_hook_quick_handler(lua_quick_harness, NULL, NULL, APR_HOOK_FIRST);
ap_hook_post_config(lua_post_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_pre_config(lua_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
APR_OPTIONAL_HOOK(ap_lua, lua_open, lua_open_hook, NULL, NULL,
APR_HOOK_REALLY_FIRST);
APR_OPTIONAL_HOOK(ap_lua, lua_request, lua_request_hook, NULL, NULL,
APR_HOOK_REALLY_FIRST);
ap_hook_handler(lua_map_handler, NULL, NULL, AP_LUA_HOOK_FIRST);
#if APR_HAS_THREADS
ap_hook_child_init(ap_lua_init_mutex, NULL, NULL, APR_HOOK_MIDDLE);
#endif
/* providers */
lua_authz_providers = apr_hash_make(p);
/* Logging catcher */
ap_hook_log_transaction(lua_log_transaction_harness,NULL,NULL,
APR_HOOK_FIRST);
}
AP_DECLARE_MODULE(lua) = {
STANDARD20_MODULE_STUFF,
create_dir_config, /* create per-dir config structures */
merge_dir_config, /* merge per-dir config structures */
create_server_config, /* create per-server config structures */
NULL, /* merge per-server config structures */
lua_commands, /* table of config file commands */
lua_register_hooks /* register hooks */
};