mod_lua.c revision 8ef30b572f5cee245f1dd27860e9a4c7d5f375cd
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
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * the License. You may obtain a copy of the License at
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd *
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * http://www.apache.org/licenses/LICENSE-2.0
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Unless required by applicable law or agreed to in writing, software
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * distributed under the License is distributed on an "AS IS" BASIS,
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * See the License for the specific language governing permissions and
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * limitations under the License.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "mod_lua.h"
e8f95a682820a599fe41b22977010636be5c2717jim#include <string.h>
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include <stdlib.h>
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include <ctype.h>
e8f95a682820a599fe41b22977010636be5c2717jim
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "lua_apr.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "lua_config.h"
b6055b7832a0e4d0818416252fff5925aaebae4brbb#include "apr_optional.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "mod_ssl.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "mod_auth.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#ifdef APR_HAS_THREADS
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "apr_thread_proc.h"
2d71630471d1c23f0137309e3c3957c633ecbfd6rbb#endif
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
449efc4dc68e42cc4421d15498a689618aab5dc3coarAPR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ap_lua, AP_LUA, int, lua_open,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding (lua_State *L, apr_pool_t *p),
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding (L, p), OK, DECLINED)
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar
291eb44b3adaf8247425286615b4f4b69fbea274minfrinAPR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ap_lua, AP_LUA, int, lua_request,
1b21d7b3d97def358b2e923655edeb16613a1c31gstein (lua_State *L, request_rec *r),
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding (L, r), OK, DECLINED)
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *lua_ssl_val = NULL;
a7ad08f37d876bde1a32f0cf793f4799536ab1a5benstatic APR_OPTIONAL_FN_TYPE(ssl_is_https) *lua_ssl_is_https = NULL;
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingmodule AP_MODULE_DECLARE_DATA lua_module;
ef5650b61a8e35f3cc93ec07e73efc17ea329894jorton
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz#define AP_LUA_HOOK_FIRST (APR_HOOK_FIRST - 1)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#define AP_LUA_HOOK_LAST (APR_HOOK_LAST + 1)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingtypedef struct {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm const char *name;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm const char *file_name;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const char *function_name;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_lua_vm_spec *spec;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_array_header_t *args;
61c688e070e72a669cb2426f85960b9e9a1f9a03minfrin} lua_authz_provider_spec;
61c688e070e72a669cb2426f85960b9e9a1f9a03minfrin
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmapr_hash_t *lua_authz_providers;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrintypedef struct
291eb44b3adaf8247425286615b4f4b69fbea274minfrin{
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_bucket_brigade *tmpBucket;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding lua_State *L;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_lua_vm_spec *spec;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe int broken;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding} lua_filter_ctx;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin/**
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * error reporting if lua has an error.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Extracts the error from lua stack and prints
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic void report_lua_error(lua_State *L, request_rec *r)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
066877f1a045103acfdd376d48cdd473c33f409bdougm const char *lua_response;
066877f1a045103acfdd376d48cdd473c33f409bdougm r->status = HTTP_INTERNAL_SERVER_ERROR;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding r->content_type = "text/html";
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_rputs("<b>Error!</b>\n", r);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_rputs("<p>", r);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm lua_response = lua_tostring(L, -1);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_rputs(lua_response, r);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_rputs("</p>\n", r);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm
066877f1a045103acfdd376d48cdd473c33f409bdougm ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, r->pool, APLOGNO(01471) "Lua error: %s",
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding lua_response);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic void lua_open_callback(lua_State *L, apr_pool_t *p, void *ctx)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_lua_init(L, p);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ap_lua_load_apache2_lmodule(L);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ap_lua_load_request_lmodule(L, p);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ap_lua_load_config_lmodule(L);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
066877f1a045103acfdd376d48cdd473c33f409bdougm
066877f1a045103acfdd376d48cdd473c33f409bdougmstatic int lua_open_hook(lua_State *L, apr_pool_t *p)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding lua_open_callback(L, p, NULL);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return OK;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic const char *scope_to_string(unsigned int scope)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm{
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz switch (scope) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz case AP_LUA_SCOPE_ONCE:
291eb44b3adaf8247425286615b4f4b69fbea274minfrin case AP_LUA_SCOPE_UNSET:
066877f1a045103acfdd376d48cdd473c33f409bdougm return "once";
291eb44b3adaf8247425286615b4f4b69fbea274minfrin case AP_LUA_SCOPE_REQUEST:
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return "request";
291eb44b3adaf8247425286615b4f4b69fbea274minfrin case AP_LUA_SCOPE_CONN:
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return "conn";
291eb44b3adaf8247425286615b4f4b69fbea274minfrin#if APR_HAS_THREADS
291eb44b3adaf8247425286615b4f4b69fbea274minfrin case AP_LUA_SCOPE_THREAD:
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return "thread";
291eb44b3adaf8247425286615b4f4b69fbea274minfrin case AP_LUA_SCOPE_SERVER:
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return "server";
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding default:
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_assert(0);
4d7e28c869788fb00bffda29a67f1b10e19f159dnd }
4d7e28c869788fb00bffda29a67f1b10e19f159dnd}
4d7e28c869788fb00bffda29a67f1b10e19f159dnd
a7ad08f37d876bde1a32f0cf793f4799536ab1a5benstatic void ap_lua_release_state(lua_State* L, ap_lua_vm_spec* spec, request_rec* r) {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf char *hash;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz apr_reslist_t* reslist = NULL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (spec->scope == AP_LUA_SCOPE_SERVER) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_lua_server_spec* sspec = NULL;
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein lua_settop(L, 0);
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein lua_getfield(L, LUA_REGISTRYINDEX, "Apache2.Lua.server_spec");
066877f1a045103acfdd376d48cdd473c33f409bdougm sspec = (ap_lua_server_spec*) lua_touserdata(L, 1);
4d7e28c869788fb00bffda29a67f1b10e19f159dnd hash = apr_psprintf(r->pool, "reslist:%s", spec->file);
4d7e28c869788fb00bffda29a67f1b10e19f159dnd if (apr_pool_userdata_get((void **)&reslist, hash,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding r->server->process->pool) == APR_SUCCESS) {
18413e519b6e5d05ff00d18efd784307442ca2f3igalic AP_DEBUG_ASSERT(sspec != NULL);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (reslist != NULL) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_reslist_release(reslist, sspec);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin}
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic ap_lua_vm_spec *create_vm_spec(apr_pool_t **lifecycle_pool,
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf request_rec *r,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz const ap_lua_dir_cfg *cfg,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz const ap_lua_server_cfg *server_cfg,
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf const char *filename,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const char *bytecode,
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe apr_size_t bytecode_len,
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe const char *function,
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe const char *what)
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe{
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe apr_pool_t *pool;
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe ap_lua_vm_spec *spec = apr_pcalloc(r->pool, sizeof(ap_lua_vm_spec));
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe spec->scope = cfg->vm_scope;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf spec->pool = r->pool;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding spec->package_paths = cfg->package_paths;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding spec->package_cpaths = cfg->package_cpaths;
4d7e28c869788fb00bffda29a67f1b10e19f159dnd spec->cb = &lua_open_callback;
4d7e28c869788fb00bffda29a67f1b10e19f159dnd spec->cb_arg = NULL;
4d7e28c869788fb00bffda29a67f1b10e19f159dnd spec->bytecode = bytecode;
4d7e28c869788fb00bffda29a67f1b10e19f159dnd spec->bytecode_len = bytecode_len;
4d7e28c869788fb00bffda29a67f1b10e19f159dnd spec->codecache = (cfg->codecache == AP_LUA_CACHE_UNSET) ? AP_LUA_CACHE_STAT : cfg->codecache;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf spec->vm_min = cfg->vm_min ? cfg->vm_min : 1;
4d7e28c869788fb00bffda29a67f1b10e19f159dnd spec->vm_max = cfg->vm_max ? cfg->vm_max : 1;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf if (filename) {
185aa71728867671e105178b4c66fbc22b65ae26sf char *file;
1f0ca94141196628ecadf1a91f2b60a33349872fnd apr_filepath_merge(&file, server_cfg->root_path,
1f0ca94141196628ecadf1a91f2b60a33349872fnd filename, APR_FILEPATH_NOTRELATIVE, r->pool);
1f0ca94141196628ecadf1a91f2b60a33349872fnd spec->file = file;
1f0ca94141196628ecadf1a91f2b60a33349872fnd }
1f0ca94141196628ecadf1a91f2b60a33349872fnd else {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf spec->file = r->filename;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf }
4d7e28c869788fb00bffda29a67f1b10e19f159dnd ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r, APLOGNO(02313)
4d7e28c869788fb00bffda29a67f1b10e19f159dnd "%s details: scope: %s, file: %s, func: %s",
4d7e28c869788fb00bffda29a67f1b10e19f159dnd what, scope_to_string(spec->scope), spec->file,
4d7e28c869788fb00bffda29a67f1b10e19f159dnd function ? function : "-");
4d7e28c869788fb00bffda29a67f1b10e19f159dnd
4d7e28c869788fb00bffda29a67f1b10e19f159dnd switch (spec->scope) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding case AP_LUA_SCOPE_ONCE:
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding case AP_LUA_SCOPE_UNSET:
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_pool_create(&pool, r->pool);
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf break;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin case AP_LUA_SCOPE_REQUEST:
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding pool = r->pool;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin break;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin case AP_LUA_SCOPE_CONN:
291eb44b3adaf8247425286615b4f4b69fbea274minfrin pool = r->connection->pool;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin break;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin#if APR_HAS_THREADS
291eb44b3adaf8247425286615b4f4b69fbea274minfrin case AP_LUA_SCOPE_THREAD:
291eb44b3adaf8247425286615b4f4b69fbea274minfrin pool = apr_thread_pool_get(r->connection->current_thread);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin break;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin case AP_LUA_SCOPE_SERVER:
291eb44b3adaf8247425286615b4f4b69fbea274minfrin pool = r->server->process->pool;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin break;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin#endif
291eb44b3adaf8247425286615b4f4b69fbea274minfrin default:
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_assert(0);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin *lifecycle_pool = pool;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return spec;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin}
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrinstatic const char* ap_lua_interpolate_string(apr_pool_t* pool, const char* string, const char** values)
291eb44b3adaf8247425286615b4f4b69fbea274minfrin{
291eb44b3adaf8247425286615b4f4b69fbea274minfrin char *stringBetween;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin const char* ret;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin int srclen,x,y;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin srclen = strlen(string);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = "";
291eb44b3adaf8247425286615b4f4b69fbea274minfrin y = 0;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin for (x=0; x < srclen; x++) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (string[x] == '$' && x != srclen-1 && string[x+1] >= '0' && string[x+1] <= '9') {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin int v = *(string+x+1) - '0';
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (x-y > 0) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin stringBetween = apr_pstrndup(pool, string+y, x-y);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf stringBetween = "";
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = apr_pstrcat(pool, ret, stringBetween, values[v], NULL);
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf y = ++x+1;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (x-y > 0 && y > 0) {
e8f95a682820a599fe41b22977010636be5c2717jim stringBetween = apr_pstrndup(pool, string+y, x-y);
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick ret = apr_pstrcat(pool, ret, stringBetween, NULL);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* If no replacement was made, just return the original string */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else if (y==0) {
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein return string;
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return ret;
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim}
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/**
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim * "main"
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim */
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjimstatic int lua_handler(request_rec *r)
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim{
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim int rc = OK;
afadb90f859c4987505e745edee656850d7f8d49jim if (strcmp(r->handler, "lua-script")) {
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim return DECLINED;
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim }
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim /* Decline the request if the script does not exist (or is a directory),
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim * rather than just returning internal server error */
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim if (
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim (r->finfo.filetype == APR_NOFILE)
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim || (r->finfo.filetype & APR_DIR)
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim ) {
172e83c0f024fe6396dd1f3ca3492fd83c304db5jim return DECLINED;
66a73d4405f9d941672c0343b36f6c494413a6b5rpluem }
66a73d4405f9d941672c0343b36f6c494413a6b5rpluem ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, APLOGNO(01472)
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim "handling [%s] in mod_lua", r->filename);
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim /* XXX: This seems wrong because it may generate wrong headers for HEAD requests */
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim if (!r->header_only) {
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim lua_State *L;
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim apr_pool_t *pool;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin &lua_module);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_lua_vm_spec *spec = create_vm_spec(&pool, r, cfg, NULL, NULL, NULL,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin 0, "handle", "request handler");
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin L = ap_lua_get_lua_state(pool, spec, r);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (!L) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin /* TODO annotate spec with failure reason */
291eb44b3adaf8247425286615b4f4b69fbea274minfrin r->status = HTTP_INTERNAL_SERVER_ERROR;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_rputs("Unable to compile VM, see logs", r);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_lua_release_state(L, spec, r);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return HTTP_INTERNAL_SERVER_ERROR;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r, APLOGNO(01474) "got a vm!");
291eb44b3adaf8247425286615b4f4b69fbea274minfrin lua_getglobal(L, "handle");
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (!lua_isfunction(L, -1)) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01475)
291eb44b3adaf8247425286615b4f4b69fbea274minfrin "lua: Unable to find function %s in %s",
291eb44b3adaf8247425286615b4f4b69fbea274minfrin "handle",
291eb44b3adaf8247425286615b4f4b69fbea274minfrin spec->file);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_lua_release_state(L, spec, r);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return HTTP_INTERNAL_SERVER_ERROR;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_lua_run_lua_request(L, r);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (lua_pcall(L, 1, 1, 0)) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin report_lua_error(L, r);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (lua_isnumber(L, -1)) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin rc = lua_tointeger(L, -1);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_lua_release_state(L, spec, r);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return rc;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin}
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin/* ------------------- Input/output content filters ------------------- */
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrinstatic apr_status_t lua_setup_filter_ctx(ap_filter_t* f, request_rec* r, lua_filter_ctx** c) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_pool_t *pool;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_lua_vm_spec *spec;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin int n, rc;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin lua_State *L;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin lua_filter_ctx *ctx;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin &lua_module);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin &lua_module);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ctx = apr_pcalloc(r->pool, sizeof(lua_filter_ctx));
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ctx->broken = 0;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin *c = ctx;
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim /* Find the filter that was called */
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim for (n = 0; n < cfg->mapped_filters->nelts; n++) {
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim ap_lua_filter_handler_spec *hook_spec =
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim ((ap_lua_filter_handler_spec **) cfg->mapped_filters->elts)[n];
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim if (hook_spec == NULL) {
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim continue;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (!strcasecmp(hook_spec->filter_name, f->frec->name)) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding spec = create_vm_spec(&pool, r, cfg, server_cfg,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding hook_spec->file_name,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding NULL,
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf 0,
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf hook_spec->function_name,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz "filter");
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding L = ap_lua_get_lua_state(pool, spec, r);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (L) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding L = lua_newthread(L);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
c7ab5a433d38d5eae5fc0bb76be80ffab6e4f71dniq if (!L) {
c7ab5a433d38d5eae5fc0bb76be80ffab6e4f71dniq ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02328)
c7ab5a433d38d5eae5fc0bb76be80ffab6e4f71dniq "lua: Failed to obtain lua interpreter for %s %s",
c7ab5a433d38d5eae5fc0bb76be80ffab6e4f71dniq hook_spec->function_name, hook_spec->file_name);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ap_lua_release_state(L, spec, r);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return APR_EGENERAL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (hook_spec->function_name != NULL) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz lua_getglobal(L, hook_spec->function_name);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (!lua_isfunction(L, -1)) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02329)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "lua: Unable to find function %s in %s",
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz hook_spec->function_name,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding hook_spec->file_name);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ap_lua_release_state(L, spec, r);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return APR_EGENERAL;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf ap_lua_run_lua_request(L, r);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding int t;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_lua_run_lua_request(L, r);
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben t = lua_gettop(L);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz lua_setglobal(L, "r");
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding lua_settop(L, t);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ctx->L = L;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ctx->spec = spec;
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz /* If a Lua filter is interested in filtering a request, it must first do a yield,
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben * otherwise we'll assume that it's not interested and pretend we didn't find it.
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben */
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben rc = lua_resume(L, 1);
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben if (rc == LUA_YIELD) {
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben return OK;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz else {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_lua_release_state(L, spec, r);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return APR_ENOENT;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return APR_ENOENT;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin}
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrinstatic apr_status_t lua_output_filter_handle(ap_filter_t *f, apr_bucket_brigade *pbbIn) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_bucket *e;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin request_rec *r = f->r;
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick int rc;
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick lua_State *L;
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick lua_filter_ctx* ctx;
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick conn_rec *c = r->connection;
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick apr_bucket *pbktIn;
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick apr_status_t rv;
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick
e8f95a682820a599fe41b22977010636be5c2717jim /* Set up the initial filter context and acquire the function.
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick * The corresponding Lua function should yield here.
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick */
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick if (!f->ctx) {
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben rc = lua_setup_filter_ctx(f,r,&ctx);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (rc == APR_EGENERAL) {
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick return HTTP_INTERNAL_SERVER_ERROR;
e8f95a682820a599fe41b22977010636be5c2717jim }
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick if (rc == APR_ENOENT) {
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick /* No filter entry found (or the script declined to filter), just pass on the buckets */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_remove_output_filter(f);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return ap_pass_brigade(f->next,pbbIn);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding f->ctx = ctx;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ctx->tmpBucket = apr_brigade_create(r->pool, c->bucket_alloc);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ctx = (lua_filter_ctx*) f->ctx;
418c12fdca30a401f7921cf66d82f7061dc67149brianp L = ctx->L;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz /* While the Lua function is still yielding, pass in buckets to the coroutine */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (!ctx->broken) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz for (pbktIn = APR_BRIGADE_FIRST(pbbIn);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz pbktIn != APR_BRIGADE_SENTINEL(pbbIn);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz pbktIn = APR_BUCKET_NEXT(pbktIn))
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz const char *data;
fd7cb2b590294250e5b219512992cd5747289fbbbrianp apr_size_t len;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz apr_bucket *pbktOut;
fd7cb2b590294250e5b219512992cd5747289fbbbrianp
fd7cb2b590294250e5b219512992cd5747289fbbbrianp /* read the bucket */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz apr_bucket_read(pbktIn,&data,&len,APR_BLOCK_READ);
fd7cb2b590294250e5b219512992cd5747289fbbbrianp
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz /* Push the bucket onto the Lua stack as a global var */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz lua_pushlstring(L, data, len);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz lua_setglobal(L, "bucket");
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz /* If Lua yielded, it means we have something to pass on */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (lua_resume(L, 0) == LUA_YIELD) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding size_t olen;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const char* output = lua_tolstring(L, 1, &olen);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding pbktOut = apr_bucket_heap_create(output, olen, NULL,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding c->bucket_alloc);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktOut);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz rv = ap_pass_brigade(f->next, ctx->tmpBucket);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_brigade_cleanup(ctx->tmpBucket);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (rv != APR_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return rv;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ctx->broken = 1;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_lua_release_state(L, ctx->spec, r);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_remove_output_filter(f);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_brigade_cleanup(pbbIn);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_brigade_cleanup(ctx->tmpBucket);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return HTTP_INTERNAL_SERVER_ERROR;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin /* If we've safely reached the end, do a final call to Lua to allow for any
291eb44b3adaf8247425286615b4f4b69fbea274minfrin finishing moves by the script, such as appending a tail. */
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(pbbIn))) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_bucket *pbktEOS;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin lua_pushnil(L);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin lua_setglobal(L, "bucket");
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (lua_resume(L, 0) == LUA_YIELD) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_bucket *pbktOut;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin size_t olen;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin const char* output = lua_tolstring(L, 1, &olen);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin pbktOut = apr_bucket_heap_create(output, olen, NULL,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin c->bucket_alloc);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktOut);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin pbktEOS = apr_bucket_eos_create(c->bucket_alloc);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktEOS);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_lua_release_state(L, ctx->spec, r);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin rv = ap_pass_brigade(f->next, ctx->tmpBucket);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_brigade_cleanup(ctx->tmpBucket);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (rv != APR_SUCCESS) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return rv;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin /* Clean up */
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_brigade_cleanup(pbbIn);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return APR_SUCCESS;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin}
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrinstatic apr_status_t lua_input_filter_handle(ap_filter_t *f,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_bucket_brigade *pbbOut,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_input_mode_t eMode,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_read_type_e eBlock,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_off_t nBytes)
291eb44b3adaf8247425286615b4f4b69fbea274minfrin{
291eb44b3adaf8247425286615b4f4b69fbea274minfrin request_rec *r = f->r;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin int rc, lastCall = 0;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin lua_State *L;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin lua_filter_ctx* ctx;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin conn_rec *c = r->connection;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_status_t ret;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin /* Set up the initial filter context and acquire the function.
291eb44b3adaf8247425286615b4f4b69fbea274minfrin * The corresponding Lua function should yield here.
291eb44b3adaf8247425286615b4f4b69fbea274minfrin */
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (!f->ctx) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin rc = lua_setup_filter_ctx(f,r,&ctx);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin f->ctx = ctx;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (rc == APR_EGENERAL) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ctx->broken = 1;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_remove_input_filter(f);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return HTTP_INTERNAL_SERVER_ERROR;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (rc == APR_ENOENT ) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_remove_input_filter(f);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ctx->broken = 1;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (rc == APR_SUCCESS) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ctx->tmpBucket = apr_brigade_create(r->pool, c->bucket_alloc);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ctx = (lua_filter_ctx*) f->ctx;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin L = ctx->L;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin /* If the Lua script broke or denied serving the request, just pass the buckets through */
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (ctx->broken) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return ap_get_brigade(f->next, pbbOut, eMode, eBlock, nBytes);
4c138e467a7d384582aa6f8c9809af589e572feesf }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
33510984c759eb3da154ceb0db9b75fa0031d3b4sf if (APR_BRIGADE_EMPTY(ctx->tmpBucket)) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = ap_get_brigade(f->next, ctx->tmpBucket, eMode, eBlock, nBytes);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (eMode == AP_MODE_EATCRLF || ret != APR_SUCCESS)
ef5650b61a8e35f3cc93ec07e73efc17ea329894jorton return ret;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* While the Lua function is still yielding, pass buckets to the coroutine */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (!ctx->broken) {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf lastCall = 0;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz while(!APR_BRIGADE_EMPTY(ctx->tmpBucket)) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz apr_bucket *pbktIn = APR_BRIGADE_FIRST(ctx->tmpBucket);
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf apr_bucket *pbktOut;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf const char *data;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf apr_size_t len;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf
bbe046d7cbb950ab3e372e4119ae001a5fe52ed4striker if(APR_BUCKET_IS_EOS(pbktIn)) {
33510984c759eb3da154ceb0db9b75fa0031d3b4sf APR_BUCKET_REMOVE(pbktIn);
33510984c759eb3da154ceb0db9b75fa0031d3b4sf break;
33510984c759eb3da154ceb0db9b75fa0031d3b4sf }
33510984c759eb3da154ceb0db9b75fa0031d3b4sf
33510984c759eb3da154ceb0db9b75fa0031d3b4sf /* read the bucket */
33510984c759eb3da154ceb0db9b75fa0031d3b4sf ret = apr_bucket_read(pbktIn, &data, &len, eBlock);
33510984c759eb3da154ceb0db9b75fa0031d3b4sf if(ret != APR_SUCCESS) {
33510984c759eb3da154ceb0db9b75fa0031d3b4sf return ret;
33510984c759eb3da154ceb0db9b75fa0031d3b4sf }
33510984c759eb3da154ceb0db9b75fa0031d3b4sf
33510984c759eb3da154ceb0db9b75fa0031d3b4sf /* Push the bucket onto the Lua stack as a global var */
33510984c759eb3da154ceb0db9b75fa0031d3b4sf lastCall++;
33510984c759eb3da154ceb0db9b75fa0031d3b4sf lua_pushlstring(L, data, len);
33510984c759eb3da154ceb0db9b75fa0031d3b4sf lua_setglobal(L, "bucket");
33510984c759eb3da154ceb0db9b75fa0031d3b4sf
33510984c759eb3da154ceb0db9b75fa0031d3b4sf /* If Lua yielded, it means we have something to pass on */
33510984c759eb3da154ceb0db9b75fa0031d3b4sf if (lua_resume(L, 0) == LUA_YIELD) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz size_t olen;
4c138e467a7d384582aa6f8c9809af589e572feesf const char* output = lua_tolstring(L, 1, &olen);
185aa71728867671e105178b4c66fbc22b65ae26sf pbktOut = apr_bucket_heap_create(output, olen, 0, c->bucket_alloc);
4c138e467a7d384582aa6f8c9809af589e572feesf APR_BRIGADE_INSERT_TAIL(pbbOut, pbktOut);
4c138e467a7d384582aa6f8c9809af589e572feesf apr_bucket_delete(pbktIn);
4c138e467a7d384582aa6f8c9809af589e572feesf return APR_SUCCESS;
4c138e467a7d384582aa6f8c9809af589e572feesf }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz else {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ctx->broken = 1;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ap_lua_release_state(L, ctx->spec, r);
1d9e72aaaebdda86c3afc856c35872edf19c69bbsf ap_remove_input_filter(f);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz apr_bucket_delete(pbktIn);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return HTTP_INTERNAL_SERVER_ERROR;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf /* If we've safely reached the end, do a final call to Lua to allow for any
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz finishing moves by the script, such as appending a tail. */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (lastCall == 0) {
33510984c759eb3da154ceb0db9b75fa0031d3b4sf apr_bucket *pbktEOS = apr_bucket_eos_create(c->bucket_alloc);
33510984c759eb3da154ceb0db9b75fa0031d3b4sf lua_pushnil(L);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz lua_setglobal(L, "bucket");
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (lua_resume(L, 0) == LUA_YIELD) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz apr_bucket *pbktOut;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf size_t olen;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz const char* output = lua_tolstring(L, 1, &olen);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz pbktOut = apr_bucket_heap_create(output, olen, 0, c->bucket_alloc);
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf APR_BRIGADE_INSERT_TAIL(pbbOut, pbktOut);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz APR_BRIGADE_INSERT_TAIL(pbbOut,pbktEOS);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ap_lua_release_state(L, ctx->spec, r);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf }
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf return APR_SUCCESS;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz}
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe/* ---------------- Configury stuff --------------- */
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe
32c4bc04f89b16521718145dc731f750144d7b38wrowe/** harnesses for magic hooks **/
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe
33510984c759eb3da154ceb0db9b75fa0031d3b4sfstatic int lua_request_rec_hook_harness(request_rec *r, const char *name, int apr_hook_when)
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe{
32c4bc04f89b16521718145dc731f750144d7b38wrowe int rc;
32c4bc04f89b16521718145dc731f750144d7b38wrowe apr_pool_t *pool;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf lua_State *L;
32c4bc04f89b16521718145dc731f750144d7b38wrowe ap_lua_vm_spec *spec;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz &lua_module);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding &lua_module);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const char *key = apr_psprintf(r->pool, "%s_%d", name, apr_hook_when);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, key,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding APR_HASH_KEY_STRING);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (hook_specs) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding int i;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding for (i = 0; i < hook_specs->nelts; i++) {
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein ap_lua_mapped_handler_spec *hook_spec =
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein ((ap_lua_mapped_handler_spec **) hook_specs->elts)[i];
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (hook_spec == NULL) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding continue;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz spec = create_vm_spec(&pool, r, cfg, server_cfg,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz hook_spec->file_name,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding hook_spec->bytecode,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin hook_spec->bytecode_len,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin hook_spec->function_name,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin "request hook");
4c138e467a7d384582aa6f8c9809af589e572feesf
4c138e467a7d384582aa6f8c9809af589e572feesf L = ap_lua_get_lua_state(pool, spec, r);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
30e3e760b737f13ce800fa02c5930ade7659ba66niq if (!L) {
30e3e760b737f13ce800fa02c5930ade7659ba66niq ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01477)
30e3e760b737f13ce800fa02c5930ade7659ba66niq "lua: Failed to obtain lua interpreter for %s %s",
30e3e760b737f13ce800fa02c5930ade7659ba66niq hook_spec->function_name, hook_spec->file_name);
185aa71728867671e105178b4c66fbc22b65ae26sf return HTTP_INTERNAL_SERVER_ERROR;
30e3e760b737f13ce800fa02c5930ade7659ba66niq }
30e3e760b737f13ce800fa02c5930ade7659ba66niq
30e3e760b737f13ce800fa02c5930ade7659ba66niq if (hook_spec->function_name != NULL) {
30e3e760b737f13ce800fa02c5930ade7659ba66niq lua_getglobal(L, hook_spec->function_name);
30e3e760b737f13ce800fa02c5930ade7659ba66niq if (!lua_isfunction(L, -1)) {
30e3e760b737f13ce800fa02c5930ade7659ba66niq ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01478)
185aa71728867671e105178b4c66fbc22b65ae26sf "lua: Unable to find function %s in %s",
30e3e760b737f13ce800fa02c5930ade7659ba66niq hook_spec->function_name,
30e3e760b737f13ce800fa02c5930ade7659ba66niq hook_spec->file_name);
30e3e760b737f13ce800fa02c5930ade7659ba66niq ap_lua_release_state(L, spec, r);
30e3e760b737f13ce800fa02c5930ade7659ba66niq return HTTP_INTERNAL_SERVER_ERROR;
30e3e760b737f13ce800fa02c5930ade7659ba66niq }
30e3e760b737f13ce800fa02c5930ade7659ba66niq
30e3e760b737f13ce800fa02c5930ade7659ba66niq ap_lua_run_lua_request(L, r);
30e3e760b737f13ce800fa02c5930ade7659ba66niq }
30e3e760b737f13ce800fa02c5930ade7659ba66niq else {
30e3e760b737f13ce800fa02c5930ade7659ba66niq int t;
30e3e760b737f13ce800fa02c5930ade7659ba66niq ap_lua_run_lua_request(L, r);
30e3e760b737f13ce800fa02c5930ade7659ba66niq
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz t = lua_gettop(L);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz lua_setglobal(L, "r");
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz lua_settop(L, t);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (lua_pcall(L, 1, 1, 0)) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin report_lua_error(L, r);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_lua_release_state(L, spec, r);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return HTTP_INTERNAL_SERVER_ERROR;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding rc = DECLINED;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (lua_isnumber(L, -1)) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding rc = lua_tointeger(L, -1);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (rc != DECLINED) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_lua_release_state(L, spec, r);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return rc;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_lua_release_state(L, spec, r);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return DECLINED;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
291eb44b3adaf8247425286615b4f4b69fbea274minfrinstatic int lua_map_handler(request_rec *r)
291eb44b3adaf8247425286615b4f4b69fbea274minfrin{
291eb44b3adaf8247425286615b4f4b69fbea274minfrin int rc, n = 0;
4c138e467a7d384582aa6f8c9809af589e572feesf apr_pool_t *pool;
4c138e467a7d384582aa6f8c9809af589e572feesf lua_State *L;
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar const char *filename, *function_name;
449efc4dc68e42cc4421d15498a689618aab5dc3coar const char *values[10];
449efc4dc68e42cc4421d15498a689618aab5dc3coar ap_lua_vm_spec *spec;
449efc4dc68e42cc4421d15498a689618aab5dc3coar ap_regmatch_t match[10];
449efc4dc68e42cc4421d15498a689618aab5dc3coar ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config,
185aa71728867671e105178b4c66fbc22b65ae26sf &lua_module);
449efc4dc68e42cc4421d15498a689618aab5dc3coar const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
449efc4dc68e42cc4421d15498a689618aab5dc3coar &lua_module);
449efc4dc68e42cc4421d15498a689618aab5dc3coar for (n = 0; n < cfg->mapped_handlers->nelts; n++) {
449efc4dc68e42cc4421d15498a689618aab5dc3coar ap_lua_mapped_handler_spec *hook_spec =
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar ((ap_lua_mapped_handler_spec **) cfg->mapped_handlers->elts)[n];
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar
185aa71728867671e105178b4c66fbc22b65ae26sf if (hook_spec == NULL) {
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar continue;
449efc4dc68e42cc4421d15498a689618aab5dc3coar }
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar if (!ap_regexec(hook_spec->uri_pattern, r->uri, 10, match, 0)) {
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar int i;
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar for (i=0;i<10;i++) {
affb82a2d7fc07c1a862d800ef47af966b898768nd if (match[i].rm_eo >= 0) {
affb82a2d7fc07c1a862d800ef47af966b898768nd values[i] = apr_pstrndup(r->pool, r->uri+match[i].rm_so, match[i].rm_eo - match[i].rm_so);
affb82a2d7fc07c1a862d800ef47af966b898768nd }
affb82a2d7fc07c1a862d800ef47af966b898768nd else values[i] = "";
affb82a2d7fc07c1a862d800ef47af966b898768nd }
affb82a2d7fc07c1a862d800ef47af966b898768nd filename = ap_lua_interpolate_string(r->pool, hook_spec->file_name, values);
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar function_name = ap_lua_interpolate_string(r->pool, hook_spec->function_name, values);
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar spec = create_vm_spec(&pool, r, cfg, server_cfg,
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar filename,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz hook_spec->bytecode,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding hook_spec->bytecode_len,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding function_name,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "mapped handler");
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding L = ap_lua_get_lua_state(pool, spec, r);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
73e8b26287de5c06fa470d36162e103dbac9c7e5wrowe if (!L) {
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02330)
e8c95302287c86cd1f984eeb25cf3bfa9e2d33bbslive "lua: Failed to obtain lua interpreter for %s %s",
e8c95302287c86cd1f984eeb25cf3bfa9e2d33bbslive function_name, filename);
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding ap_lua_release_state(L, spec, r);
1376737cb6afa24d3e12f3a223318fe1bd71bb1fslive return HTTP_INTERNAL_SERVER_ERROR;
b980ad7fdc218b4855cde9f75a747527f50c554dwrowe }
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding if (function_name != NULL) {
36ef8f77bffe75d1aa327882be1b5bdbe2ff567asf lua_getglobal(L, function_name);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (!lua_isfunction(L, -1)) {
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02331)
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz "lua: Unable to find function %s in %s",
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz function_name,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz filename);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ap_lua_release_state(L, spec, r);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return HTTP_INTERNAL_SERVER_ERROR;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
ap_lua_run_lua_request(L, r);
}
else {
int t;
ap_lua_run_lua_request(L, r);
t = lua_gettop(L);
lua_setglobal(L, "r");
lua_settop(L, t);
}
if (lua_pcall(L, 1, 1, 0)) {
report_lua_error(L, r);
ap_lua_release_state(L, spec, r);
return HTTP_INTERNAL_SERVER_ERROR;
}
rc = DECLINED;
if (lua_isnumber(L, -1)) {
rc = lua_tointeger(L, -1);
}
if (rc != DECLINED) {
ap_lua_release_state(L, spec, r);
return rc;
}
ap_lua_release_state(L, spec, r);
}
}
return DECLINED;
}
static apr_size_t config_getstr(ap_configfile_t *cfg, char *buf,
size_t bufsiz)
{
apr_size_t i = 0;
if (cfg->getstr) {
apr_status_t rc = (cfg->getstr) (buf, bufsiz, cfg->param);
if (rc == APR_SUCCESS) {
i = strlen(buf);
if (i && buf[i - 1] == '\n')
++cfg->line_number;
}
else {
buf[0] = '\0';
i = 0;
}
}
else {
while (i < bufsiz) {
char ch;
apr_status_t rc = (cfg->getch) (&ch, cfg->param);
if (rc != APR_SUCCESS)
break;
buf[i++] = ch;
if (ch == '\n') {
++cfg->line_number;
break;
}
}
}
return i;
}
typedef struct cr_ctx
{
cmd_parms *cmd;
ap_configfile_t *cfp;
size_t startline;
const char *endstr;
char buf[HUGE_STRING_LEN];
} cr_ctx;
/* Okay, this deserves a little explaination -- in order for the errors that lua
* generates to be 'accuarate', including line numbers, we basically inject
* N line number new lines into the 'top' of the chunk reader.....
*
* be happy. this is cool.
*
*/
static const char *lf =
"\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";
#define N_LF 32
static const char *direct_chunkreader(lua_State *lvm, void *udata,
size_t *plen)
{
const char *p;
struct cr_ctx *ctx = udata;
if (ctx->startline) {
*plen = ctx->startline > N_LF ? N_LF : ctx->startline;
ctx->startline -= *plen;
return lf;
}
*plen = config_getstr(ctx->cfp, ctx->buf, HUGE_STRING_LEN);
for (p = ctx->buf; isspace(*p); ++p);
if (p[0] == '<' && p[1] == '/') {
apr_size_t i = 0;
while (i < strlen(ctx->endstr)) {
if (tolower(p[i + 2]) != ctx->endstr[i])
return ctx->buf;
++i;
}
*plen = 0;
return NULL;
}
/*fprintf(stderr, "buf read: %s\n", ctx->buf); */
return ctx->buf;
}
static int ldump_writer(lua_State *L, const void *b, size_t size, void *B)
{
(void) L;
luaL_addlstring((luaL_Buffer *) B, (const char *) b, size);
return 0;
}
typedef struct hack_section_baton
{
const char *name;
ap_lua_mapped_handler_spec *spec;
int apr_hook_when;
} hack_section_baton;
/* You can be unhappy now.
*
* This is uncool.
*
* When you create a <Section handler in httpd, the only 'easy' way to create
* a directory context is to parse the section, and convert it into a 'normal'
* Configureation option, and then collapse the entire section, in memory,
* back into the parent section -- from which you can then get the new directive
* invoked.... anyways. evil. Rici taught me how to do this hack :-)
*/
static const char *hack_section_handler(cmd_parms *cmd, void *_cfg,
const char *arg)
{
ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
ap_directive_t *directive = cmd->directive;
hack_section_baton *baton = directive->data;
const char *key = apr_psprintf(cmd->pool, "%s_%d", baton->name, baton->apr_hook_when);
apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, key,
APR_HASH_KEY_STRING);
if (!hook_specs) {
hook_specs = apr_array_make(cmd->pool, 2,
sizeof(ap_lua_mapped_handler_spec *));
apr_hash_set(cfg->hooks, key,
APR_HASH_KEY_STRING, hook_specs);
}
baton->spec->scope = cfg->vm_scope;
*(ap_lua_mapped_handler_spec **) apr_array_push(hook_specs) = baton->spec;
return NULL;
}
static const char *register_named_block_function_hook(const char *name,
cmd_parms *cmd,
void *mconfig,
const char *line)
{
const char *function = NULL;
ap_lua_mapped_handler_spec *spec;
int when = APR_HOOK_MIDDLE;
const char *endp = ap_strrchr_c(line, '>');
if (endp == NULL) {
return apr_pstrcat(cmd->pool, cmd->cmd->name,
"> directive missing closing '>'", NULL);
}
line = apr_pstrndup(cmd->temp_pool, line, endp - line);
if (line[0]) {
const char *word;
word = ap_getword_conf(cmd->temp_pool, &line);
if (word && *word) {
function = apr_pstrdup(cmd->pool, word);
}
word = ap_getword_conf(cmd->temp_pool, &line);
if (word && *word) {
if (!strcasecmp("early", word)) {
when = AP_LUA_HOOK_FIRST;
}
else if (!strcasecmp("late", word)) {
when = AP_LUA_HOOK_LAST;
}
else {
return apr_pstrcat(cmd->pool, cmd->cmd->name,
"> 2nd argument must be 'early' or 'late'", NULL);
}
}
}
spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec));
{
cr_ctx ctx;
lua_State *lvm;
char *tmp;
int rv;
ap_directive_t **current;
hack_section_baton *baton;
spec->file_name = apr_psprintf(cmd->pool, "%s:%u",
cmd->config_file->name,
cmd->config_file->line_number);
if (function) {
spec->function_name = (char *) function;
}
else {
function = NULL;
}
ctx.cmd = cmd;
tmp = apr_pstrdup(cmd->pool, cmd->err_directive->directive + 1);
ap_str_tolower(tmp);
ctx.endstr = tmp;
ctx.cfp = cmd->config_file;
ctx.startline = cmd->config_file->line_number;
/* This lua State is used only to compile the input strings -> bytecode, so we don't need anything extra. */
lvm = luaL_newstate();
lua_settop(lvm, 0);
rv = lua_load(lvm, direct_chunkreader, &ctx, spec->file_name);
if (rv != 0) {
const char *errstr = apr_pstrcat(cmd->pool, "Lua Error:",
lua_tostring(lvm, -1), NULL);
lua_close(lvm);
return errstr;
}
else {
luaL_Buffer b;
luaL_buffinit(lvm, &b);
lua_dump(lvm, ldump_writer, &b);
luaL_pushresult(&b);
spec->bytecode_len = lua_strlen(lvm, -1);
spec->bytecode = apr_pstrmemdup(cmd->pool, lua_tostring(lvm, -1),
spec->bytecode_len);
lua_close(lvm);
}
current = mconfig;
/* Here, we have to replace our current config node for the next pass */
if (!*current) {
*current = apr_pcalloc(cmd->pool, sizeof(**current));
}
baton = apr_pcalloc(cmd->pool, sizeof(hack_section_baton));
baton->name = name;
baton->spec = spec;
baton->apr_hook_when = when;
(*current)->filename = cmd->config_file->name;
(*current)->line_num = cmd->config_file->line_number;
(*current)->directive = apr_pstrdup(cmd->pool, "Lua_____ByteCodeHack");
(*current)->args = NULL;
(*current)->data = baton;
}
return NULL;
}
static const char *register_named_file_function_hook(const char *name,
cmd_parms *cmd,
void *_cfg,
const char *file,
const char *function,
int apr_hook_when)
{
ap_lua_mapped_handler_spec *spec;
ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
const char *key = apr_psprintf(cmd->pool, "%s_%d", name, apr_hook_when);
apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, key,
APR_HASH_KEY_STRING);
if (!hook_specs) {
hook_specs = apr_array_make(cmd->pool, 2,
sizeof(ap_lua_mapped_handler_spec *));
apr_hash_set(cfg->hooks, key, APR_HASH_KEY_STRING, hook_specs);
}
spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec));
spec->file_name = apr_pstrdup(cmd->pool, file);
spec->function_name = apr_pstrdup(cmd->pool, function);
spec->scope = cfg->vm_scope;
*(ap_lua_mapped_handler_spec **) apr_array_push(hook_specs) = spec;
return NULL;
}
static const char *register_mapped_file_function_hook(const char *pattern,
cmd_parms *cmd,
void *_cfg,
const char *file,
const char *function)
{
ap_lua_mapped_handler_spec *spec;
ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
ap_regex_t *regex = apr_pcalloc(cmd->pool, sizeof(ap_regex_t));
if (ap_regcomp(regex, pattern,0)) {
return "Invalid regex pattern!";
}
spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec));
spec->file_name = apr_pstrdup(cmd->pool, file);
spec->function_name = apr_pstrdup(cmd->pool, function);
spec->scope = cfg->vm_scope;
spec->uri_pattern = regex;
*(ap_lua_mapped_handler_spec **) apr_array_push(cfg->mapped_handlers) = spec;
return NULL;
}
static const char *register_filter_function_hook(const char *filter,
cmd_parms *cmd,
void *_cfg,
const char *file,
const char *function,
int direction)
{
ap_lua_filter_handler_spec *spec;
ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_filter_handler_spec));
spec->file_name = apr_pstrdup(cmd->pool, file);
spec->function_name = apr_pstrdup(cmd->pool, function);
spec->filter_name = filter;
*(ap_lua_filter_handler_spec **) apr_array_push(cfg->mapped_filters) = spec;
/* TODO: Make it work on other types than just AP_FTYPE_RESOURCE? */
if (direction == AP_LUA_FILTER_OUTPUT) {
spec->direction = AP_LUA_FILTER_OUTPUT;
ap_register_output_filter(filter, lua_output_filter_handle, NULL, AP_FTYPE_RESOURCE);
}
else {
spec->direction = AP_LUA_FILTER_INPUT;
ap_register_input_filter(filter, lua_input_filter_handle, NULL, AP_FTYPE_RESOURCE);
}
return NULL;
}
static int lua_check_user_id_harness_first(request_rec *r)
{
return lua_request_rec_hook_harness(r, "check_user_id", AP_LUA_HOOK_FIRST);
}
static int lua_check_user_id_harness(request_rec *r)
{
return lua_request_rec_hook_harness(r, "check_user_id", APR_HOOK_MIDDLE);
}
static int lua_check_user_id_harness_last(request_rec *r)
{
return lua_request_rec_hook_harness(r, "check_user_id", AP_LUA_HOOK_LAST);
}
static int lua_translate_name_harness_first(request_rec *r)
{
return lua_request_rec_hook_harness(r, "translate_name", AP_LUA_HOOK_FIRST);
}
static int lua_translate_name_harness(request_rec *r)
{
return lua_request_rec_hook_harness(r, "translate_name", APR_HOOK_MIDDLE);
}
static int lua_translate_name_harness_last(request_rec *r)
{
return lua_request_rec_hook_harness(r, "translate_name", AP_LUA_HOOK_LAST);
}
static int lua_fixup_harness(request_rec *r)
{
return lua_request_rec_hook_harness(r, "fixups", APR_HOOK_MIDDLE);
}
static int lua_map_to_storage_harness(request_rec *r)
{
return lua_request_rec_hook_harness(r, "map_to_storage", APR_HOOK_MIDDLE);
}
static int lua_type_checker_harness(request_rec *r)
{
return lua_request_rec_hook_harness(r, "type_checker", APR_HOOK_MIDDLE);
}
static int lua_access_checker_harness_first(request_rec *r)
{
return lua_request_rec_hook_harness(r, "access_checker", AP_LUA_HOOK_FIRST);
}
static int lua_access_checker_harness(request_rec *r)
{
return lua_request_rec_hook_harness(r, "access_checker", APR_HOOK_MIDDLE);
}
static int lua_access_checker_harness_last(request_rec *r)
{
return lua_request_rec_hook_harness(r, "access_checker", AP_LUA_HOOK_LAST);
}
static int lua_auth_checker_harness_first(request_rec *r)
{
return lua_request_rec_hook_harness(r, "auth_checker", AP_LUA_HOOK_FIRST);
}
static int lua_auth_checker_harness(request_rec *r)
{
return lua_request_rec_hook_harness(r, "auth_checker", APR_HOOK_MIDDLE);
}
static int lua_auth_checker_harness_last(request_rec *r)
{
return lua_request_rec_hook_harness(r, "auth_checker", AP_LUA_HOOK_LAST);
}
static void lua_insert_filter_harness(request_rec *r)
{
/* ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "LuaHookInsertFilter not yet implemented"); */
}
static int lua_quick_harness(request_rec *r, int lookup)
{
if (lookup) {
return DECLINED;
}
return lua_request_rec_hook_harness(r, "quick", APR_HOOK_MIDDLE);
}
static const char *register_translate_name_hook(cmd_parms *cmd, void *_cfg,
const char *file,
const char *function,
const char *when)
{
const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
NOT_IN_HTACCESS);
int apr_hook_when = APR_HOOK_MIDDLE;
if (err) {
return err;
}
if (when) {
if (!strcasecmp(when, "early")) {
apr_hook_when = AP_LUA_HOOK_FIRST;
}
else if (!strcasecmp(when, "late")) {
apr_hook_when = AP_LUA_HOOK_LAST;
}
else {
return "Third argument must be 'early' or 'late'";
}
}
return register_named_file_function_hook("translate_name", cmd, _cfg,
file, function, apr_hook_when);
}
static const char *register_translate_name_block(cmd_parms *cmd, void *_cfg,
const char *line)
{
return register_named_block_function_hook("translate_name", cmd, _cfg,
line);
}
static const char *register_fixups_hook(cmd_parms *cmd, void *_cfg,
const char *file,
const char *function)
{
return register_named_file_function_hook("fixups", cmd, _cfg, file,
function, APR_HOOK_MIDDLE);
}
static const char *register_fixups_block(cmd_parms *cmd, void *_cfg,
const char *line)
{
return register_named_block_function_hook("fixups", cmd, _cfg, line);
}
static const char *register_map_to_storage_hook(cmd_parms *cmd, void *_cfg,
const char *file,
const char *function)
{
return register_named_file_function_hook("map_to_storage", cmd, _cfg,
file, function, APR_HOOK_MIDDLE);
}
static const char *register_map_to_storage_block(cmd_parms *cmd, void *_cfg,
const char *line)
{
return register_named_block_function_hook("map_to_storage", cmd, _cfg,
line);
}
static const char *register_check_user_id_hook(cmd_parms *cmd, void *_cfg,
const char *file,
const char *function,
const char *when)
{
int apr_hook_when = APR_HOOK_MIDDLE;
if (when) {
if (!strcasecmp(when, "early")) {
apr_hook_when = AP_LUA_HOOK_FIRST;
}
else if (!strcasecmp(when, "late")) {
apr_hook_when = AP_LUA_HOOK_LAST;
}
else {
return "Third argument must be 'early' or 'late'";
}
}
return register_named_file_function_hook("check_user_id", cmd, _cfg, file,
function, apr_hook_when);
}
static const char *register_check_user_id_block(cmd_parms *cmd, void *_cfg,
const char *line)
{
return register_named_block_function_hook("check_user_id", cmd, _cfg,
line);
}
static const char *register_type_checker_hook(cmd_parms *cmd, void *_cfg,
const char *file,
const char *function)
{
return register_named_file_function_hook("type_checker", cmd, _cfg, file,
function, APR_HOOK_MIDDLE);
}
static const char *register_type_checker_block(cmd_parms *cmd, void *_cfg,
const char *line)
{
return register_named_block_function_hook("type_checker", cmd, _cfg,
line);
}
static const char *register_access_checker_hook(cmd_parms *cmd, void *_cfg,
const char *file,
const char *function,
const char *when)
{
int apr_hook_when = APR_HOOK_MIDDLE;
if (when) {
if (!strcasecmp(when, "early")) {
apr_hook_when = AP_LUA_HOOK_FIRST;
}
else if (!strcasecmp(when, "late")) {
apr_hook_when = AP_LUA_HOOK_LAST;
}
else {
return "Third argument must be 'early' or 'late'";
}
}
return register_named_file_function_hook("access_checker", cmd, _cfg,
file, function, apr_hook_when);
}
static const char *register_access_checker_block(cmd_parms *cmd, void *_cfg,
const char *line)
{
return register_named_block_function_hook("access_checker", cmd, _cfg,
line);
}
static const char *register_auth_checker_hook(cmd_parms *cmd, void *_cfg,
const char *file,
const char *function,
const char *when)
{
int apr_hook_when = APR_HOOK_MIDDLE;
if (when) {
if (!strcasecmp(when, "early")) {
apr_hook_when = AP_LUA_HOOK_FIRST;
}
else if (!strcasecmp(when, "late")) {
apr_hook_when = AP_LUA_HOOK_LAST;
}
else {
return "Third argument must be 'early' or 'late'";
}
}
return register_named_file_function_hook("auth_checker", cmd, _cfg, file,
function, apr_hook_when);
}
static const char *register_auth_checker_block(cmd_parms *cmd, void *_cfg,
const char *line)
{
return register_named_block_function_hook("auth_checker", cmd, _cfg,
line);
}
static const char *register_insert_filter_hook(cmd_parms *cmd, void *_cfg,
const char *file,
const char *function)
{
return "LuaHookInsertFilter not yet implemented";
}
static const char *register_quick_hook(cmd_parms *cmd, void *_cfg,
const char *file, const char *function)
{
const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
NOT_IN_HTACCESS);
if (err) {
return err;
}
return register_named_file_function_hook("quick", cmd, _cfg, file,
function, APR_HOOK_MIDDLE);
}
static const char *register_map_handler(cmd_parms *cmd, void *_cfg,
const char* match, const char *file, const char *function)
{
const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
NOT_IN_HTACCESS);
if (err) {
return err;
}
if (!function) function = "handle";
return register_mapped_file_function_hook(match, cmd, _cfg, file,
function);
}
static const char *register_output_filter(cmd_parms *cmd, void *_cfg,
const char* filter, const char *file, const char *function)
{
const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
NOT_IN_HTACCESS);
if (err) {
return err;
}
if (!function) function = "handle";
return register_filter_function_hook(filter, cmd, _cfg, file,
function, AP_LUA_FILTER_OUTPUT);
}
static const char *register_input_filter(cmd_parms *cmd, void *_cfg,
const char* filter, const char *file, const char *function)
{
const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
NOT_IN_HTACCESS);
if (err) {
return err;
}
if (!function) function = "handle";
return register_filter_function_hook(filter, cmd, _cfg, file,
function, AP_LUA_FILTER_INPUT);
}
static const char *register_quick_block(cmd_parms *cmd, void *_cfg,
const char *line)
{
return register_named_block_function_hook("quick", cmd, _cfg,
line);
}
static const char *register_package_helper(cmd_parms *cmd,
const char *arg,
apr_array_header_t *dir_array)
{
apr_status_t rv;
ap_lua_server_cfg *server_cfg =
ap_get_module_config(cmd->server->module_config, &lua_module);
char *fixed_filename;
rv = apr_filepath_merge(&fixed_filename,
server_cfg->root_path,
arg,
APR_FILEPATH_NOTRELATIVE,
cmd->pool);
if (rv != APR_SUCCESS) {
return apr_psprintf(cmd->pool,
"Unable to build full path to file, %s", arg);
}
*(const char **) apr_array_push(dir_array) = fixed_filename;
return NULL;
}
/**
* Called for config directive which looks like
* LuaPackagePath /lua/package/path/mapped/thing/like/this/?.lua
*/
static const char *register_package_dir(cmd_parms *cmd, void *_cfg,
const char *arg)
{
ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
return register_package_helper(cmd, arg, cfg->package_paths);
}
/**
* Called for config directive which looks like
* LuaPackageCPath /lua/package/path/mapped/thing/like/this/?.so
*/
static const char *register_package_cdir(cmd_parms *cmd,
void *_cfg,
const char *arg)
{
ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
return register_package_helper(cmd, arg, cfg->package_cpaths);
}
static const char *register_lua_inherit(cmd_parms *cmd,
void *_cfg,
const char *arg)
{
ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
if (strcasecmp("none", arg) == 0) {
cfg->inherit = AP_LUA_INHERIT_NONE;
}
else if (strcasecmp("parent-first", arg) == 0) {
cfg->inherit = AP_LUA_INHERIT_PARENT_FIRST;
}
else if (strcasecmp("parent-last", arg) == 0) {
cfg->inherit = AP_LUA_INHERIT_PARENT_LAST;
}
else {
return apr_psprintf(cmd->pool,
"LuaInherit type of '%s' not recognized, valid "
"options are 'none', 'parent-first', and 'parent-last'",
arg);
}
return NULL;
}
static const char *register_lua_codecache(cmd_parms *cmd,
void *_cfg,
const char *arg)
{
ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
if (strcasecmp("never", arg) == 0) {
cfg->codecache = AP_LUA_CACHE_NEVER;
}
else if (strcasecmp("stat", arg) == 0) {
cfg->codecache = AP_LUA_CACHE_STAT;
}
else if (strcasecmp("forever", arg) == 0) {
cfg->codecache = AP_LUA_CACHE_FOREVER;
}
else {
return apr_psprintf(cmd->pool,
"LuaCodeCache type of '%s' not recognized, valid "
"options are 'never', 'stat', and 'forever'",
arg);
}
return NULL;
}
static const char *register_lua_scope(cmd_parms *cmd,
void *_cfg,
const char *scope,
const char *min,
const char *max)
{
ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
if (strcmp("once", scope) == 0) {
cfg->vm_scope = AP_LUA_SCOPE_ONCE;
}
else if (strcmp("request", scope) == 0) {
cfg->vm_scope = AP_LUA_SCOPE_REQUEST;
}
else if (strcmp("conn", scope) == 0) {
cfg->vm_scope = AP_LUA_SCOPE_CONN;
}
else if (strcmp("thread", scope) == 0) {
#if !APR_HAS_THREADS
return apr_psprintf(cmd->pool,
"Scope type of '%s' cannot be used because this "
"server does not have threading support "
"(APR_HAS_THREADS)"
scope);
#endif
cfg->vm_scope = AP_LUA_SCOPE_THREAD;
}
else if (strcmp("server", scope) == 0) {
unsigned int vmin, vmax;
#if !APR_HAS_THREADS
return apr_psprintf(cmd->pool,
"Scope type of '%s' cannot be used because this "
"server does not have threading support "
"(APR_HAS_THREADS)"
scope);
#endif
cfg->vm_scope = AP_LUA_SCOPE_SERVER;
vmin = min ? atoi(min) : 1;
vmax = max ? atoi(max) : 1;
if (vmin == 0) {
vmin = 1;
}
if (vmax < vmin) {
vmax = vmin;
}
cfg->vm_min = vmin;
cfg->vm_max = vmax;
}
else {
return apr_psprintf(cmd->pool,
"Invalid value for LuaScope, '%s', acceptable "
"values are: 'once', 'request', 'conn'"
#if APR_HAS_THREADS
", 'thread', 'server'"
#endif
,scope);
}
return NULL;
}
static const char *register_lua_root(cmd_parms *cmd, void *_cfg,
const char *root)
{
/* ap_lua_dir_cfg* cfg = (ap_lua_dir_cfg*)_cfg; */
ap_lua_server_cfg *cfg = ap_get_module_config(cmd->server->module_config,
&lua_module);
cfg->root_path = root;
return NULL;
}
AP_LUA_DECLARE(const char *) ap_lua_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c, request_rec *r, const char *var)
{
if (lua_ssl_val) {
return (const char *)lua_ssl_val(p, s, c, r, (char *)var);
}
return NULL;
}
AP_LUA_DECLARE(int) ap_lua_ssl_is_https(conn_rec *c)
{
return lua_ssl_is_https ? lua_ssl_is_https(c) : 0;
}
/*******************************/
static const char *lua_authz_parse(cmd_parms *cmd, const char *require_line,
const void **parsed_require_line)
{
const char *provider_name;
lua_authz_provider_spec *spec;
apr_pool_userdata_get((void**)&provider_name, AUTHZ_PROVIDER_NAME_NOTE,
cmd->temp_pool);
ap_assert(provider_name != NULL);
spec = apr_hash_get(lua_authz_providers, provider_name, APR_HASH_KEY_STRING);
ap_assert(spec != NULL);
if (require_line && *require_line) {
const char *arg;
spec->args = apr_array_make(cmd->pool, 2, sizeof(const char *));
while ((arg = ap_getword_conf(cmd->pool, &require_line)) && *arg) {
APR_ARRAY_PUSH(spec->args, const char *) = arg;
}
}
*parsed_require_line = spec;
return NULL;
}
static authz_status lua_authz_check(request_rec *r, const char *require_line,
const void *parsed_require_line)
{
apr_pool_t *pool;
ap_lua_vm_spec *spec;
lua_State *L;
ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config,
&lua_module);
const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
&lua_module);
const lua_authz_provider_spec *prov_spec = parsed_require_line;
int result;
int nargs = 0;
spec = create_vm_spec(&pool, r, cfg, server_cfg, prov_spec->file_name,
NULL, 0, prov_spec->function_name, "authz provider");
L = ap_lua_get_lua_state(pool, spec, r);
if (L == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02314)
"Unable to compile VM for authz provider %s", prov_spec->name);
return AUTHZ_GENERAL_ERROR;
}
lua_getglobal(L, prov_spec->function_name);
if (!lua_isfunction(L, -1)) {
ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02319)
"Unable to find function %s in %s",
prov_spec->function_name, prov_spec->file_name);
ap_lua_release_state(L, spec, r);
return AUTHZ_GENERAL_ERROR;
}
ap_lua_run_lua_request(L, r);
if (prov_spec->args) {
int i;
if (!lua_checkstack(L, prov_spec->args->nelts)) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02315)
"Error: authz provider %s: too many arguments", prov_spec->name);
ap_lua_release_state(L, spec, r);
return AUTHZ_GENERAL_ERROR;
}
for (i = 0; i < prov_spec->args->nelts; i++) {
const char *arg = APR_ARRAY_IDX(prov_spec->args, i, const char *);
lua_pushstring(L, arg);
}
nargs = prov_spec->args->nelts;
}
if (lua_pcall(L, 1 + nargs, 1, 0)) {
const char *err = lua_tostring(L, -1);
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02316)
"Error executing authz provider %s: %s", prov_spec->name, err);
ap_lua_release_state(L, spec, r);
return AUTHZ_GENERAL_ERROR;
}
if (!lua_isnumber(L, -1)) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02317)
"Error: authz provider %s did not return integer", prov_spec->name);
ap_lua_release_state(L, spec, r);
return AUTHZ_GENERAL_ERROR;
}
result = lua_tointeger(L, -1);
ap_lua_release_state(L, spec, r);
switch (result) {
case AUTHZ_DENIED:
case AUTHZ_GRANTED:
case AUTHZ_NEUTRAL:
case AUTHZ_GENERAL_ERROR:
case AUTHZ_DENIED_NO_USER:
return result;
default:
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02318)
"Error: authz provider %s: invalid return value %d",
prov_spec->name, result);
}
return AUTHZ_GENERAL_ERROR;
}
static const authz_provider lua_authz_provider =
{
&lua_authz_check,
&lua_authz_parse,
};
static const char *register_authz_provider(cmd_parms *cmd, void *_cfg,
const char *name, const char *file,
const char *function)
{
lua_authz_provider_spec *spec;
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err)
return err;
spec = apr_pcalloc(cmd->pool, sizeof(*spec));
spec->name = name;
spec->file_name = file;
spec->function_name = function;
apr_hash_set(lua_authz_providers, name, APR_HASH_KEY_STRING, spec);
ap_register_auth_provider(cmd->pool, AUTHZ_PROVIDER_GROUP, name,
AUTHZ_PROVIDER_VERSION,
&lua_authz_provider,
AP_AUTH_INTERNAL_PER_CONF);
return NULL;
}
command_rec lua_commands[] = {
AP_INIT_TAKE1("LuaRoot", register_lua_root, NULL, OR_ALL,
"Specify the base path for resolving relative paths for mod_lua directives"),
AP_INIT_TAKE1("LuaPackagePath", register_package_dir, NULL, OR_ALL,
"Add a directory to lua's package.path"),
AP_INIT_TAKE1("LuaPackageCPath", register_package_cdir, NULL, OR_ALL,
"Add a directory to lua's package.cpath"),
AP_INIT_TAKE3("LuaAuthzProvider", register_authz_provider, NULL, RSRC_CONF|EXEC_ON_READ,
"Provide an authorization provider"),
AP_INIT_TAKE23("LuaHookTranslateName", register_translate_name_hook, NULL,
OR_ALL,
"Provide a hook for the translate name phase of request processing"),
AP_INIT_RAW_ARGS("<LuaHookTranslateName", register_translate_name_block,
NULL,
EXEC_ON_READ | OR_ALL,
"Provide a hook for the translate name phase of request processing"),
AP_INIT_TAKE2("LuaHookFixups", register_fixups_hook, NULL, OR_ALL,
"Provide a hook for the fixups phase of request processing"),
AP_INIT_RAW_ARGS("<LuaHookFixups", register_fixups_block, NULL,
EXEC_ON_READ | OR_ALL,
"Provide a inline hook for the fixups phase of request processing"),
/* todo: test */
AP_INIT_TAKE2("LuaHookMapToStorage", register_map_to_storage_hook, NULL,
OR_ALL,
"Provide a hook for the map_to_storage phase of request processing"),
AP_INIT_RAW_ARGS("<LuaHookMapToStorage", register_map_to_storage_block,
NULL,
EXEC_ON_READ | OR_ALL,
"Provide a hook for the map_to_storage phase of request processing"),
/* todo: test */
AP_INIT_TAKE23("LuaHookCheckUserID", register_check_user_id_hook, NULL,
OR_ALL,
"Provide a hook for the check_user_id phase of request processing"),
AP_INIT_RAW_ARGS("<LuaHookCheckUserID", register_check_user_id_block,
NULL,
EXEC_ON_READ | OR_ALL,
"Provide a hook for the check_user_id phase of request processing"),
/* todo: test */
AP_INIT_TAKE2("LuaHookTypeChecker", register_type_checker_hook, NULL,
OR_ALL,
"Provide a hook for the type_checker phase of request processing"),
AP_INIT_RAW_ARGS("<LuaHookTypeChecker", register_type_checker_block, NULL,
EXEC_ON_READ | OR_ALL,
"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_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_post_config(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *s)
{
lua_ssl_val = APR_RETRIEVE_OPTIONAL_FN(ssl_var_lookup);
lua_ssl_is_https = APR_RETRIEVE_OPTIONAL_FN(ssl_is_https);
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);
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);
}
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 */
};